L202026100117 - Albert Richard Halim - 20211115
L202026100117 - Albert Richard Halim - 20211115
NOTE: Put all the source code and answers into a word file, and upload the word file (just
ONE Word file! name it with “your ID_name_date”, e.g. 20191963****_***_20211115.doc).
}
}
2. Define an abstract class named Employee. Define following classes to inherit the
abstract class,
a) Boss class, indicate the boss who get paid with a fixed salary without
considering working hours;
b) CommissionWork class, indicate the workers who some basic salary and
floating wages according to sales;
c) PieceWorker class, indicate those who are paid according to the numbers of
products;
d) HourlyWorker class, indicate those get paid according to their working hours.
Define above four classes, to implement the abstract methods declared in the abstract
class:
- getPaid() method without any parameter but have a return value for total salary,
and,
- display() method for output the information about the employee.
And define another class with main method (also called test class or driven class), to
test the methods defined.
obj1.display();
System.out.println("Salary : " + obj1.getPaid());
obj2.display();
System.out.println("Salary : " + obj2.getPaid());
obj3.display();
System.out.println("Salary : " + obj3.getPaid());
obj4.display();
System.out.println("Salary : " + obj4.getPaid());
}
}
Salary : 225.0
DISCUSSION AND CONCLUSION
First Create Employee as abstract class, the abstract method display and
getPaid. After that create an extension class from the Employee abstract
Class. Each class create overloading method where method display
displaying the name of class and method getpaid return salary with
specific requirement. Last create main class that has main method
running all the method of each class.
System.out.println((circle1.compareTo(circle2) == 1 ?
"Circle1 " : "Circle2 ") + "is larger then "
+ (circle1.compareTo(circle2) < 1 ? "Circle1 " :
"Circle2 "));
}
}
public Square() {
this(10);
}
5. (Math: The Complex class) A complex number is a number of the form where a
and b are real numbers and i is 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
formula:
You can also obtain the absolute value for a complex number using the following
formula:
Design a class named Complex for representing complex numbers and the methods
add, subtract, multiply, divide, and abs for performing complex-number operations,
and override the toString method for returning a string representation for a complex
number. The toString method returns a + bi as a string. If b is 0, it simply returns a.
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 and imaginary part of the complex
number, respectively.
Write a test program that prompts the user to enter two complex numbers and
display the result of their addition, subtraction, multiplication, and division. Here is a
sample run:
Enter the first complex number: 3.5 5.5
Enter the second complex number: -3.5 1
3.5 + 5.5i + -3.5 + 1.0i = 0.0 + 6.5i
3.5 + 5.5i - -3.5 + 1.0i = 7.0 + 4.5i
3.5 + 5.5i * -3.5 + 1.0i = -17.75 + -15.75i
3.5 + 5.5i / -3.5 + 1.0i = -0.5094 + -1.7i
|3.5 + 5.5i| = 6.519202405202649
public Complex() {
this(0, 0);
}
public Complex(double a) {
this.a = a;
this.b = 0;
}