Q2. Write a program (Abstract+Interface) Define a class named Point containing: Two private instance variables x and y of type int. A full parametrized constructor Getters for all instance variables Define an abstract class named Shape containing: An instance variable named s of type Point. A full parametrized constructor A method named getS that returns the value of instance variable An abstract method named Area that has a return type double. Define an interface named Printable containing a method print without parameters and returning void. Define a class named Rectangle which is extended from class Shape, implements the interface printable and containing: Three instance variables named Length, Width of type int and an instance variable named d of type Point. A constructor with two parameters of type Point which initializes all the instance variables. The lengthis the difference in y-coordinates of any top and bottom point. length= Math.abs(s.getY()-d.getY()); The widthis the difference in x-coordinates of any left and right point. width= Math.abs(s.getX()-d.getX()); An overridden method named Area which returns the calculated area of the rectangle. An overridden method named print which print the pattern of a rectangle using the ‘*’ character according to the length and the width. Define a class named Line which implements the interface printable and containing: Two instance variables named start and end of type Point. A constructor with two parameters of type Point which initializes all the instance variables. A method getLength returning the length of a line ( The distance between start and end). Math.sqrt(Math.pow(start.getX()-end.getX(),2)+Math.pow(start.getY()-end.getY(),2)) An overridden method named print which print the pattern of a line using the ‘*’ character according to the length of the line. Define a class named Text which implements the interface printable and containing:
Q2. Write a program (Abstract+Interface) |
Define a class named Point containing:
- Two private instance variables x and y of type int.
- A full parametrized constructor
- Getters for all instance variables
Define an abstract class named Shape containing:
- An instance variable named s of type Point.
- A full parametrized constructor
- A method named getS that returns the value of instance variable
- An abstract method named Area that has a return type double.
Define an interface named Printable containing a method print without parameters and returning void.
Define a class named Rectangle which is extended from class Shape, implements the interface printable and containing:
- Three instance variables named Length, Width of type int and an instance variable named d of type Point.
- A constructor with two parameters of type Point which initializes all the instance variables.
- The lengthis the difference in y-coordinates of any top and bottom point.
length= Math.abs(s.getY()-d.getY());
- The widthis the difference in x-coordinates of any left and right point.
width= Math.abs(s.getX()-d.getX());
- An overridden method named Area which returns the calculated area of the rectangle.
- An overridden method named print which print the pattern of a rectangle using the ‘*’ character according to the length and the width.
Define a class named Line which implements the interface printable and containing:
- Two instance variables named start and end of type Point.
- A constructor with two parameters of type Point which initializes all the instance variables.
- A method getLength returning the length of a line ( The distance between start and end).
Math.sqrt(Math.pow(start.getX()-end.getX(),2)+Math.pow(start.getY()-end.getY(),2))
- An overridden method named print which print the pattern of a line using the ‘*’ character according to the length of the line.
Define a class named Text which implements the interface printable and containing:
- One instance variables named Content of type String.
- A full parametrized constructor.
- An overridden method named print which print the text.
Define a third class named TestDemo contains a main method that test the functionality of the above classes.
-define 2 object from class point p1 and p2, p1=(2,4),p2=(9,14).
-define arrayList of type printable, the first element is object from class Rectangle, the second element is object from class Line and the third element is an object from class Text.
- call the method print to print all the elements of the arrayList.
Output:
********** ********** ********** ********** ********** ********** ********** This is a printable text ************* |
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 1 images