0% found this document useful (0 votes)
76 views12 pages

L202026100117 - Albert Richard Halim - 20211115

The document describes four programming exercises involving abstract classes, interfaces, and inheritance in Java: 1. Define interfaces and classes to calculate the volume and area of circles and spheres. 2. Create an abstract Employee class and subclasses for different types of employees. 3. Extend the Circle class to implement Comparable and compare circles by area. 4. Design a Colorable interface and Square class that extends GeometricObject and implements coloring methods.

Uploaded by

Mr. X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views12 pages

L202026100117 - Albert Richard Halim - 20211115

The document describes four programming exercises involving abstract classes, interfaces, and inheritance in Java: 1. Define interfaces and classes to calculate the volume and area of circles and spheres. 2. Create an abstract Employee class and subclasses for different types of employees. 3. Extend the Circle class to implement Comparable and compare circles by area. 4. Design a Colorable interface and Square class that extends GeometricObject and implements coloring methods.

Uploaded by

Mr. X
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Tutorial 6 Abstract classes, Interfaces

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).

Attempt ALL questions. DO NOT COPY CODE FROM THE OTHERS!

1. Define an interface, called VolumeArea, within which there’re:


- a static and final variable PI (with the value 3.14159), and
- two abstract methods volume(double radius) and area(double radius).
Write another class MyCircle which implements the interface VolumeArea.
Overriding the two methods:
- volume(double radius) to return a value zero (as the volume of a circle is
0), and
- area(double radius) to return the area of the circle.
Write the third class MySphere, also implements VolumeArea. Overriding the two
methods:
- volume(double radius) to calculate and return the volume of the sphere
(using (4π*r*r*r)/3);
- area(double radius) to calculate and return the surface area of the sphere
(using 4π*r*r).
Draw a UML diagram that involves VolumeArea, MyCircle, and MySphere. Write a
main class with the main method to test the methods in MyCircle and MySphere, set
the radius of the circle and sphere both to 3.5. And display the results.

SOURCE CODE (TEXT)


interface VolumeArea {
    static final double PI = 3.14159;

    public double volume(double radius);

    public double area(double radius);


}

class MyCircle implements VolumeArea {


    public double volume(double radius) {
        return 0;
    }

    public double area(double radius) {


        return radius * radius * PI;
    }
}

class MySphere implements VolumeArea {


    public double volume(double radius) {
        return (4 * PI * radius * radius * radius) / 3;
    }

    public double area(double radius) {


        return 4 * PI * radius * radius;
    }
}

public class Main {


    public static void main(String[] args) {
        double radius = 3.5;
        MyCircle test = new MyCircle();
        MySphere test1 = new MySphere();

        System.out.println("Circle volume : " + test.volume(radius));


        System.out.println("Circle Area : " + test.area(radius));
        System.out.println("Sphere volume : " +
test1.volume(radius));
        System.out.println("Sphere Area : " + test1.area(radius));

    }
}

EXECUTION RESULTS (TEXT OR SNAPSHOTS)


Circle volume : 0.0
Circle Area : 38.4844775
Sphere volume : 179.5942283333333

Sphere Area : 153.93791


DISCUSSION AND CONCLUSION
First create interface Volume area with two method and
static final PI, then creating class circle and class
shpehre which implement two method from Volumeara. At main
declare the radius 3.5 and run the method of each class.

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.

SOURCE CODE (TEXT)


abstract class Employee {
    static final double fixed_salary = 600;
    double basic_salary = 5;

    public abstract double getPaid();

    public abstract void display();


}

class Boss extends Employee {


    public double getPaid() {
        return fixed_salary;
    }

    public void display() {


        System.out.println("Employee : Boss");
    }
}

class CommisionWork extends Employee {


    double sales = 10;

    public double getPaid() {


        return basic_salary * sales;
    }

    public void display() {


        System.out.println("Employee : CommisionWork");
    }
}

class PieceWorker extends Employee {


    double products = 15;

    public double getPaid() {


        return basic_salary * products;
    }

    public void display() {


        System.out.println("Employee : PieceWorker");
    }
}

class HourlyWorker extends Employee {


    double hours = 45;

    public double getPaid() {


        return basic_salary * hours;
    }

    public void display() {


        System.out.println("Employee : HourlyWorker");
    }
}

public class Test_Class {


    public static void main(String[] args) {
        Boss obj1 = new Boss();
        CommisionWork obj2 = new CommisionWork();
        PieceWorker obj3 = new PieceWorker();
        HourlyWorker obj4 = new HourlyWorker();

        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());
    }
}

EXECUTION RESULTS (TEXT OR SNAPSHOTS)


Employee : Boss
Salary : 600.0
Employee : CommisionWork
Salary : 50.0
Employee : PieceWorker
Salary : 75.0
Employee : HourlyWorker

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.

3. (The ComparableCircle class) Define a class named ComparableCircle that


extends Circle (find from attached files) and implements Comparable. Draw the
UML diagram and implement the compareTo method to compare the circles on the
basis of area. Write a test class to find the larger of two instances of
ComparableCircle objects.

SOURCE CODE (TEXT)


class ComparableCircle extends Circle implements Comparable<Circle> {
    public ComparableCircle(double radius) {
        super.radius = radius;
    }

    public int compareTo(Circle obj) {


        if (this.getArea() == obj.getArea()) {
            return 0;
        } else if (this.getArea() > obj.getArea()) {
            return 1;
        } else {
            return -1;
        }
    }
}

public class TestCompare {


    public static void main(String[] args) {

        ComparableCircle circle1 = new ComparableCircle(5);


        ComparableCircle circle2 = new ComparableCircle(15);

        System.out.println((circle1.compareTo(circle2) == 1 ?
"Circle1 " : "Circle2 ") + "is larger then "
                + (circle1.compareTo(circle2) < 1 ? "Circle1 " :
"Circle2 "));
    }
}

EXECUTION RESULTS (TEXT OR SNAPSHOTS)

Circle2 is larger then Circle1


DISCUSSION AND CONCLUSION
First I import the java.util and java.lang, then import the
class GeometricObject and Circle. After that I change the
accessibility of radius in circle class from private to
protected so the extended class ComparableCircle can acces
the radius. On the ComparableCircle create the constructor
and compareTo. On main creating 2 Comparable obj Circle,
then compare it which one have the higher area. Lastly,
print the result.

4. (The Colorable interface) Design an interface named Colorable with a void


method named howToColor(). Every class of a colorable object must implement the
Colorable interface. Design a class named Square that extends GeometricObject
(find from attached files) and implements Colorable. Implement howToColor to
display the message Color all four sides.
Draw a UML diagram that involves Colorable, Square, and GeometricObject .
Write a test program that creates an array of five GeometricObjects. For each object
in the array, display its area and invoke its howToColor method if it is colorable.

SOURCE CODE (TEXT)


interface Colorable {
    public void howToColor();
}
class Square extends GeometricObject implements Colorable {
    private double side;

    public Square() {
        this(10);
    }

    public Square(double side) {


        this.side = side;
    }

    public void howToColor() {


        System.out.println("Color all four sides");
    }

    public double getArea() {


        return side * side;
    }

    public double getPerimeter() {


        return side * 4;
    }
}

public class Driver_class {


    public static void main(String[] args) {
        GeometricObject[] obj = new GeometricObject[5];
        obj[0] = new Square();
        obj[1] = new Square(1);
        obj[2] = new Square(2);
        obj[3] = new Square(3);
        obj[4] = new Square(4);

        for (int i = 0; i < obj.length; i++) {


            System.out.println("Square " + (i + 1));
            System.out.println("area = " + obj[i].getArea());
            if (obj[i] instanceof Colorable) {
                ((Colorable) obj[i]).howToColor();
            }
        }
    }
}
EXECUTION RESULTS (TEXT OR SNAPSHOTS)
Square 1
area = 100.0
Color all four sides
Square 2
area = 1.0
Color all four sides
Square 3
area = 4.0
Color all four sides
Square 4
area = 9.0
Color all four sides
Square 5
area = 16.0

Color all four sides


DISCUSSION AND CONCLUSION
At First I import the GeometricObject class, then create
interface Colorable with void howToColor method. Then create
the Square class that content with constructor and
overloading method of howToColor, getarea and get perimeter.
Within the Driver_class, main creat 5 new obj of
GeometricObject from Square. Then print out the area of each
obj and compare it if it Colorable. Last print the result.

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

SOURCE CODE (TEXT)


import java.util.Scanner;

public class Complex {


    protected final double a;
    protected final double b;

    public Complex() {
        this(0, 0);
    }

    public Complex(double a) {
        this.a = a;
        this.b = 0;
    }

    public Complex(double a, double b) {


        this.a = a;
        this.b = b;
    }

    public double getRealPart() {


        return a;
    }
    public double getImaginaryPart() {
        return b;
    }

    public String toString() {


        if (b == 0) {
            return a + "";
        }
        if (a == 0) {
            return b + "i";
        }
        if (b < 0) {
            return a + " - " + (-b) + "i";
        }
        return a + " + " + b + "i";
    }

    public Complex Plus(Complex one, Complex two) {


        double real = one.a + two.a;
        double imag = one.b + two.b;
        return new Complex(real, imag);
    }

    public Complex Min(Complex one, Complex two) {


        double real = one.a - two.a;
        double imag = one.b - two.b;
        return new Complex(real, imag);
    }

    public Complex Times(Complex one, Complex two) {


        double real = one.a * two.a - one.b * one.b;
        double imag = one.b * two.a + one.a * one.b;
        return new Complex(real, imag);
    }

    public Complex Div(Complex one, Complex two) {


        double real = (one.a * two.a + one.b * one.b) /
(Math.pow(two.a, 2) + (Math.pow(two.b, 2)));
        double imag = (one.b * two.a - one.a * one.b) /
(Math.pow(two.a, 2) + (Math.pow(two.b, 2)));
        return new Complex(real, imag);
    }
    public double abs() {
        return Math.hypot(a, b);
    }

    public static void main(String[] args) {


        Scanner input = new Scanner(System.in);

        System.out.print("Enter the first two number : ");


        double one = input.nextDouble();
        double two = input.nextDouble();

        System.out.print("Enter the Second two number : ");


        double three = input.nextDouble();
        double four = input.nextDouble();

        Complex obj1 = new Complex(one, two);


        Complex obj2 = new Complex(three, four);

        System.out.println(obj1.toString() + " + " + obj2.toString()


+ " = " + obj1.Plus(obj1, obj2));
        System.out.println(obj1.toString() + " - " + obj2.toString()
+ " = " + obj1.Min(obj1, obj2));
        System.out.println(obj1.toString() + " * " + obj2.toString()
+ " = " + obj1.Times(obj1, obj2));
        System.out.println(obj1.toString() + " / " + obj2.toString()
+ " = " + obj1.Div(obj1, obj2));
        System.out.println(obj1.toString() + " = " + obj1.abs());
    }
}

EXECUTION RESULTS (TEXT OR SNAPSHOTS)


Enter the first two number : 1 2
Enter the Second two number : 4 5
1.0 + 2.0i + 4.0 + 5.0i = 5.0 + 7.0i
1.0 + 2.0i - 4.0 + 5.0i = -3.0 - 3.0i
1.0 + 2.0i * 4.0 + 5.0i = 10.0i
1.0 + 2.0i / 4.0 + 5.0i = 0.1951219512195122 + 0.14634146341463414i

1.0 + 2.0i = 2.23606797749979


DISCUSSION AND CONCLUSION
Here we construct Complex class with three type of constructor then
create an arithmetic method +,-,*,/ and method of abs that return hypot
( square root the sums of squares)and the toString to return the String
of a + bi . Then at the main method we create 2 obj which each obj have
one real and one imag value. Then call the method using one of the obj
then insert the 2 obj required by the method. Lastly print all the
result.

You might also like