The Wrong Way
Our classes are ready. Now we can write the main program. If we did not know about polymorphism, we would have had to do something like the program below.
xxxxxxxxxx
46
}
// This is not good practice
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter number of shapes to calculate : ");
int n = sc.nextInt();
ArrayList<Double> areas = new ArrayList<>();
for(int i = 0; i<n; i++) {
System.out.print("Enter the shape type : \n1) Rectangle\n2) Square\n3) Circle\nInput : ");
int select = sc.nextInt();
double area;
switch(select) {
case 1:
Rectangle rect = new Rectangle();
rect.inputFromUser();
area = rect.getArea();
areas.add(area);
break;
case 2:
Square square = new Square();
square.inputFromUser();
area = square.getArea();
areas.add(area);
break;
case 3:
Circle circle = new Circle();
circle.inputFromUser();
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment
xxxxxxxxxx
50
})
// This is not good practice
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
})
console.log("Enter number of shapes to calculate : ")
readline.question(answer => {
const n = parseInt(answer)
const areas = []
for(let i = 0; i<n; i++) {
console.log("Enter the shape type : \n1) Rectangle\n2) Square\n3) Circle\nInput : ")
readline.question(shape => {
const select = parseInt(shape)
switch(select) {
case 1:
let rect = new Rectangle();
rect.inputFromUser();
let area = rect.getArea();
areas.push(area);
break;
case 2:
let square = new Square();
square.inputFromUser();
area = square.getArea();
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment