0% found this document useful (0 votes)
33 views2 pages

Shape X Y Get - Data Scanner S Scanner in Out Print X S Nextdouble Y S Nextdouble

This Java code defines Shape, Triangle, and Rectangle classes to calculate area. The Shape class stores base and height values input by the user. Triangle and Rectangle classes extend Shape and override the display_area method to calculate area as 0.5 * base * height and base * height respectively. The main method uses a menu to allow the user to select a shape, get input, and display the calculated area.

Uploaded by

Sumit Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views2 pages

Shape X Y Get - Data Scanner S Scanner in Out Print X S Nextdouble Y S Nextdouble

This Java code defines Shape, Triangle, and Rectangle classes to calculate area. The Shape class stores base and height values input by the user. Triangle and Rectangle classes extend Shape and override the display_area method to calculate area as 0.5 * base * height and base * height respectively. The main method uses a menu to allow the user to select a shape, get input, and display the calculated area.

Uploaded by

Sumit Agrawal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.util.

Scanner;

class Shape {
double x,y;

void get_data() {
Scanner S=new Scanner(System.in);
System.out.print("Enter base and height:
");
x=S.nextDouble();
y=S.nextDouble();

}
}

class Triangle extends Shape {


void display_area() {
double area;
area= 0.5*x*y;
System.out.println("Area = "+ area);
}
}
class Rectangle extends Shape {
void display_area() {
double area;
area= x*y;
System.out.println("Area = "+ area);
}
}

class Area {
public static void main(String args[]) {
int choice;
while(true) {
System.out.println("\tMENU"+"\n1.
Triangle"+"\n2. Rectangle"+"\n3. Exit");
System.out.println("Enter your
choice: ");
Scanner S=new Scanner(System.in);
choice=S.nextInt();

switch(choice) {
case 1: Triangle tri=new
Triangle();
tri.get_data();
tri.display_area();
break;
case 2: Rectangle rect = new
Rectangle();
rect.get_data();
rect.display_area();
break;

case 3: System.exit(0);
default:
System.out.println("Invalid Choice Entered");
}
}

}
}

You might also like