Create a class called Rectangle derived from Point class. Write suitable method that accepts, displays and reads/writes the data. Write appropriate constructors and method that returns its area
import java.util.Scanner;
class Rectangle extends Point {
protected float width;
protected float height;
public Rectangle1() {
}
public Rectangle1(float width, float height) {
super(10, 20);
this.width = width;
this.height = height;
}
public Rectangle1(int x, int y, float width, float height) {
super(x, y);
this.width = width;
this.height = height;
}
public Rectangle1(Point p) {
super(p.getX_co(), p.getY_co());
width = 20.5f;
height = 15.10f;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
@Override
public void getData() {
super.getData();
Scanner in = new Scanner(System.in);
System.out.println("Enter Width:");
width = in.nextFloat();
System.out.println("Enter Height:");
height = in.nextFloat();
}
@Override
public void showData() {
super.showData();
System.out.println("Width is:" + width);
System.out.println("Height is:" + height);
}
public float getArea() {
return height * width;
}
public static void main(String args[]) {
Rectangle1 r = new Rectangle1(15, 25);
r.showData();
System.out.println("Area is:" + r.getArea());
Rectangle1 r2 = new Rectangle1(15, 25, 45, 55);
r2.showData();
System.out.println("Area is:" + r2.getArea());
Point p = new Point(12, 15);
Rectangle1 r3 = new Rectangle1(p);
r3.showData();
Rectangle1 r1 = new Rectangle1();
r1.getData();
r1.showData();
System.out.println("Area is:" + r1.getArea());
}
}
(PLEASE CHANGE CLASS NAME)
import java.util.Scanner;
class Rectangle extends Point {
protected float width;
protected float height;
public Rectangle1() {
}
public Rectangle1(float width, float height) {
super(10, 20);
this.width = width;
this.height = height;
}
public Rectangle1(int x, int y, float width, float height) {
super(x, y);
this.width = width;
this.height = height;
}
public Rectangle1(Point p) {
super(p.getX_co(), p.getY_co());
width = 20.5f;
height = 15.10f;
}
public float getWidth() {
return width;
}
public void setWidth(float width) {
this.width = width;
}
public float getHeight() {
return height;
}
public void setHeight(float height) {
this.height = height;
}
@Override
public void getData() {
super.getData();
Scanner in = new Scanner(System.in);
System.out.println("Enter Width:");
width = in.nextFloat();
System.out.println("Enter Height:");
height = in.nextFloat();
}
@Override
public void showData() {
super.showData();
System.out.println("Width is:" + width);
System.out.println("Height is:" + height);
}
public float getArea() {
return height * width;
}
public static void main(String args[]) {
Rectangle1 r = new Rectangle1(15, 25);
r.showData();
System.out.println("Area is:" + r.getArea());
Rectangle1 r2 = new Rectangle1(15, 25, 45, 55);
r2.showData();
System.out.println("Area is:" + r2.getArea());
Point p = new Point(12, 15);
Rectangle1 r3 = new Rectangle1(p);
r3.showData();
Rectangle1 r1 = new Rectangle1();
r1.getData();
r1.showData();
System.out.println("Area is:" + r1.getArea());
}
}
(PLEASE CHANGE CLASS NAME)
No comments:
Post a Comment