What's new

Closed How to make java programming using abstract class & interface

Status
Not open for further replies.

ranex11

Honorary Poster
Joined
Oct 23, 2018
Posts
177
Reaction
205
Points
180
Age
24
Type: JAVA
Compiler: Eclipse-Java 2018-09

Note: Need ko po ito kasi malapit na ang exam and sana lahat ng mga programmer sa PHC lalabas na kayu ;)

1.Write an abstract class Shape stored in package pk1 with 2 abstract methods:

a.double getArea() which computes and returns the area of the selected shape
b.double getPerimeter() which computes and returns the perimeter of the selected shape. The perimeter of circle is its circumference.

2.Write an interface ShapeInterface stored in package pk1 with 1 method:

a.String getName() which returns the name of the particular shape

3.Write a sub class Circle stored in package pk2, which inherits from Shape and implements ShapeInterface and has the following among others:

a.constant PI = 3.1416
b.private double radius
c.setter method for radius - assigns the value of radius
d.getter method for radius - returns the value of radius

4.Write a sub class Rectangle stored in package pk2, which inherits from Shape and implements ShapeInterface and has the following among others:

a.private double length
b.private double width
c.setter method for length - assigns the value of length
d.getter method for length - returns the value of length
e.setter method for width - assigns the value of width
f.getter method for width - returns the value of width

5.Write a sub class Square stored in package pk2, which inherits from Shape and implements ShapeInterface and has the following among others:

a.private double side
b.setter method for side - assigns the value of side
c.getter method for side - returns the value of side

6.Write a sub class RightTriangle stored in package pk2, which inherits from Shape and implements ShapeInterface and has the following among others:

a.private double base
b.private double height
c.setter method for base - assigns the value of base
d.getter method for base - returns the value of base
e.setter method for height - assigns the value of height
f.getter method for height - returns the value of height

7.Write a class TestShapes with main method stored in package pk3:

a.Allows the user to choose one shape from 4 choices:
1) Circle - inputs the radius and outputs the shape name, radius, area and circumference
2) Rectangle - inputs the length and width and outputs the shape name, length, width, area and perimeter
3) Square - inputs the side and outputs the shape name, side, area and perimeter
4) Right Triangle – input the length of the 2 smaller sides (base & height) and outputs the shape name, the length of the 3 sides (base, height & hypotenuse), area and perimeter

b. Write a new method inside this class to call the method which displays the shape name. (Polymorphism)
c. Use try-catch block to handle input errors
d. Use an appropriate loop to allow user to compute for more shapes.
 
Status
Not open for further replies.

Similar threads

Back
Top