0% found this document useful (0 votes)
52 views

Tugas PBO JAVA 10a N B Polymorphishm New

The document provides instructions and code examples for a programming assignment involving polymorphism. It includes: 1. Creating classes like Employee, Technician, Manager, Circle, Sphere, and adding polymorphic methods like getHolidays() that return different values based on the object type. 2. Creating an abstract Employee class with an abstract displayDetails() method, and subclasses that implement it differently. 3. Creating abstract RoundShape and subclasses Circle and Sphere, with each subclass implementing an abstract area() method differently to return the correct area. 4. Notes to name files correctly, copy code examples, and add a Cylinder subclass with an area method for a hollow cylinder.

Uploaded by

Tito Ardiansyah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views

Tugas PBO JAVA 10a N B Polymorphishm New

The document provides instructions and code examples for a programming assignment involving polymorphism. It includes: 1. Creating classes like Employee, Technician, Manager, Circle, Sphere, and adding polymorphic methods like getHolidays() that return different values based on the object type. 2. Creating an abstract Employee class with an abstract displayDetails() method, and subclasses that implement it differently. 3. Creating abstract RoundShape and subclasses Circle and Sphere, with each subclass implementing an abstract area() method differently to return the correct area. 4. Notes to name files correctly, copy code examples, and add a Cylinder subclass with an area method for a hollow cylinder.

Uploaded by

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

Tugas 10

Buatlah program berdasarkan diagram di samping kanannya dan salin code yang diberikan

a. Polymorphism dengan bantuan function di main()


Class yang harus dibuat :
1. Class main()
2. Class employee
3. Class technician
4. Class manager

public class Employee


{
// constant
protected final static float
holidayEntitlement = 20.0f;
// instance variables
protected String employeeName;
protected String employeeDept;
protected int lengthOfService;
// constructor
public Employee(String name, String department, int yearsService)
{
employeeName = name;
employeeDept = department;
lengthOfService = yearsService;
}
// instance methods
public String getName() { return employeeName;}
public String getDepartment(){return employeeDept;}
public int getLengthOfService(){return lengthOfService;}
public float getHolidays(){return holidayEntitlement;}
}

public class technician extends employee {


// instance variable
protected float holidays;
// constructor
public technician(String name, String department, int yearsService)
{
super(name, department, yearsService);
// panggil constructor employee
} // dan inisialisasi
// overridden instance method
@Override
public float getHolidays()
{
int service = this.getLengthOfService();
if (service > 5)
holidays = (float)(holidayEntitlement+0.5f*(service-5));
else
holidays = (float)holidayEntitlement;
return holidays;
}
@Override
public String toString()
{
String temporary = new String("TECHNICIAN\t"+super.toString());
return temporary;
}
}
Catatan :
a. beri nama file/folder dengan identitas  NIM, NAMA dan Nomer TUGAS  Nim_Nama_
tugas_10a1

b. copy-paste dari Nim_Nama_ tugas_10a1 rename menjadi Nim_Nama_ tugas_10a2 


gunakan class abstract yg di dalam ada abstract methods  abstract void
displayDetails() yg berada di dalam public abstract class employee
b. Polymorphism dengan abstract function
Class yang harus dibuat :
1. Class main()
2. Class roundshape
3. Class circle
4. Class sphere

public abstract class RoundShape


{// coordinates of center represented by an
inner class
protected class Center{
int x,y;
}
protected Center C = new Center();
protected float radiusOfCircle;
// constructor
public RoundShape(int xCenter, int yCenter, float radius){
C.x=xCenter;
C.y=yCenter;
radiusOfCircle = radius;
}
// abstract method
abstract public float area();
}

public class Circle extends RoundShape


{// constructor
public Circle(int xCenter, int yCenter, float radius)
{
super(xCenter, yCenter, radius);
}
// return area of circle
public float area(){
float areaOfCircle =
(float)(Math.PI*Math.pow((double)radiusOfCircle,2.0));
return areaOfCircle;
}
}

public class Sphere extends RoundShape


{
// constructor
public Sphere(int xCenter, int yCenter, float radius)
{
super(xCenter, yCenter, radius);
}
// return surface area of sphere
public float area()
{
float surfaceArea =
(float)(4.0*Math.PI*Math.pow((double)radiusOfCircle,2.0));
return surfaceArea;
}
}

public class Nim_Nama_ tugas_10b


{
public static void main(String[] args)
{
Window screen = new
Window("Example_7.java");
screen.showWindow();
Circle c = new Circle(5,5,2.5f);
Sphere s = new Sphere(5,5,2.5f);

Tampilkan center-nya (selain area/volume


tambahkan tampilan pusatnya)

// display details about the circles


DecimalFormat out = new DecimalFormat ("0.##");
screen.write("Area of circle "+out.format(c.area())+"\n");
screen.write("Area of sphere "+out.format(s.area()));
}
}

Catatan :
a. beri nama file/folder dengan identitas  NIM, NAMA dan Nomer TUGAS  Nim_Nama_
tugas_10b1

b. copy-paste dari Nim_Nama_ tugas_10b1 rename menjadi Nim_Nama_ tugas_10b2 


tambahkan class turunan  class tabung  area=t*2*phi*r (luas permukaan tabung yang
berlubang di kedua sisi atas dan bawah) dengan t : tinggi dan r : jari-jari
public tabung(int xCenter, int yCenter, float radius, float tinggi)
{
super(xCenter, yCenter, radius);
t=tinggi;
}

You might also like