A complex number is a number in the form a + bi, where a and b are real numbers and i is 2-1. The numbers a and b are known as the real part and imaginary part of the complex number, respectively. You can perform addition, subtraction, multiplication, and division for complex numbers using the following formulas:
You can also obtain the absolute value for a complex number using the following formula:
(A complex number can be interpreted as a point on a plane by identifying the (a,b) values as the coordinates of the point. The absolute value of the complex number corresponds to the distance of the point to the origin, as shown in Figure 13.10.)
Design a class named Complex for representing complex numbers and the methods add, subtract, multiply, divide, and abs for performing complexnumber operations, and override toString method for returning a string representation for a complex number. The toString method returns (a + bi) as astring. If b is 0, it simply returns a. Your Complex class should also implement Cloneable and Comparable. Compare two complex numbers using their absolute values.
Provide three constructors Complex(a, b), Complex(a), and Complex(). Complex() creates a Complex object for number 0, and Complex(a) creates a Complex object with 0 for b. Also provide the getRealPart() and getImaginaryPart() methods for returning the real part and the imaginary part of the complex number, respectively.
Draw the UML class diagram and implement the class. Use the code at https:// liveexample.pearsoncmg.com/test/Exercise13_17.txt to test your implementation. Here is a sample run:
A school has 100 lockers and 100 students. All lockers are closed on the first day of school. As the students enter, the first student, denoted as S1, opens every locker. Then the second student, S2, begins with the second locker, denoted as L2, and closes every other locker. Student S3 begins with the third locker and changes every third locker (closes it if it was open and opens it if it was closed). Student S4 begins with locker L4 and changes every fourth locker. Student S5 starts with L5 and changes every fifth locker, and so on, until student S100 changes L100.
After all the students have passed through the building and changed the lockers, which lockers are open? Write a program to find your answer and display all open locker numbers separated by exactly one space. (Hint: Use an array of 100 Boolean elements, each of which indicates whether a locker is open (true) or closed (false). Initially, all lockers are closed.)
Write the following method that returns true if the list is already sorted in nondecreasing order:
public static boolean isSorted(int list)
Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the program first prompts the user to enter the size of the list.
Enter the size of the list: 8
Enter the contents of the list: 10 1 5 16 61 9 11 1
The list has 8 integers 10 1 5 16 61 9 11 1
The list is not sorted
Enter the size of the list: 10
Enter the contents of the list: 1 1 3 4 4 5 7 9 11 21
The list has 10 integers 1 1 3 4 4 5 7 9 11 21
The list is already sorted
Implement the following two methods:
/** Return true if the sum of every two sides is
* greater than the third side. */
public static boolean isValid(
double side1, double side2, double side3)
/** Return the area of the triangle. */
public static double area(
double side1, double side2, double side3)
Write a test program that reads three sides for a triangle and uses the isValid method to test if the input is valid and uses the area method to obtain the area.
The program displays the area if the input is valid. Otherwise, it displays that the input is invalid. The formula for computing the area of a triangle is given in Programming Exercise 2.19.
Design a class named QuadraticEquation for a quadratic equation ax2 + bx + c = 0. The class contains:
■■ Private data fields a, b, and c that represent three coefficients.
■■ A constructor with the arguments for a, b, and c.
■■ Three getter methods for a, b, and c.
■■ A method named getDiscriminant() that returns the discriminant, which is b2 – 4ac.
■■ The methods named getRoot1() and getRoot2() for returning two roots of the equation
These methods are useful only if the discriminant is nonnegative. Let these methods return 0 if the discriminant is negative.
Draw the UML diagram for the class then implement the class. Write a test program that prompts the user to enter values for a, b, and c and displays the result based on the discriminant. If the discriminant is positive, display the two roots. If the discriminant is 0, display the one root. Otherwise, display “The equation has no roots.” See Programming Exercise 3.1 for sample runs.
Design a class named LinearEquation for a 2 × 2 system of linear equations:
The class contains:
■■ Private data fields a, b, c, d, e, and f.
■■ A constructor with the arguments for a, b, c, d, e, and f.
■■ Six getter methods for a, b, c, d, e, and f.
■■ A method named isSolvable() that returns true if ad – bc is not 0.
■■ Methods getX() and getY() that return the solution for the equation.
Draw the UML diagram for the class then implement the class. Write a test program that prompts the user to enter a, b, c, d, e, and f and displays the result. If ad – bc is 0, report that “The equation has no solution.” See Programming Exercise 3.3 for sample runs.
Design a class named MyPoint to represent a point with x- and y-coordinates. The class contains:
■■ The data fields x and y that represent the coordinates with getter methods.
■■ A no-arg constructor that creates a point (0, 0).
■■ A constructor that constructs a point with specified coordinates.
■■ A method named distance that returns the distance from this point to a specified point of the MyPoint type.
■■ A method named distance that returns the distance from this point to another point with specified x- and y-coordinates.
■■ A static method named distance that returns the distance from two MyPoint objects.
Draw the UML diagram for the class then implement the class. Write a test program that creates the two points (0, 0) and (10, 30.5) and displays the distance between them.
Write a program that prompts the user to enter an integer m and find the smallest integer n such that m * n is a perfect square. (Hint: Store all smallest factors of m into an array list. n is the product of the factors that appear an odd number of times in the array list. For example, consider m = 90, store the factors 2, 3, 3, and 5 in an array list. 2 and 5 appear an odd number of times in the array list. Thus, n is 10.) Here is a sample run of the program:
The bin packing problem is to pack the objects of various weights into containers. Assume each container can hold a maximum of 10 pounds. The program uses an algorithm that places an object into the first bin in which it would fit. Your program should prompt the user to enter the total number of objects and the weight of each object. The program displays the total number of containers needed to pack the objects and the contents of each container. Here is a sample run of the program:
Does this program produce an optimal solution, that is, finding the minimum number of containers to pack the objects?
Write a program that prompts the user to enter the length from the center of a pentagon to a vertex and computes the area of the pentagon, as shown in the following figure.
The formula for computing the area of a pentagon is
where s is the length of a side. The side can be computed using the formula s = 2r sin π/5, where r is the length from the center of a pentagon to a vertex. Round up two digits after the decimal point. Here is a sample run:
Enter the length from the center to a vertex: 5.5
The area of the pentagon is 71.92