Question 7.5: Now repeat the above example to print the area of 10 squares.
Hint-Use array of objects
//Main class
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.print("Enter lenght: ");
int length = obj.nextInt();
System.out.print("Enter breadth: ");
int breadth = obj.nextInt();
Rectangle r = new Rectangle(length,breadth);
r.Area();
r.perimeter();
System.out.print("Enter the Square: ");
int square = obj.nextInt();
Square s = new Square(square);
s.Area();
s.perimeter();
Square[] a = new Square[10];
int k=square+1;
for(int i=0;i<10;i++){
a[i] = new Square(k);
k++;
}
for(int i=0;i<10;i++){
a[i].Area();
a[i].perimeter();
}
}
}
//Rectangle Class
package com.company;
public class Rectangle {
public int length;
public int breadth;
Rectangle(){
}
public Rectangle(int length,int breadth) {
this.length = length;
this.breadth = breadth;
}
public void Area(){
System.out.println(this.breadth*this.length);
}
public void perimeter(){
System.out.println(2*(length+breadth));
}
}
//Square class
package com.company;
public class Square extends Rectangle{
int side;
public Square(int s){
super(s,s);
}
}