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

Assignment Oop 4

The document contains a Java program that defines an abstract class 'shape' with methods to get dimensions and calculate area. It includes two subclasses, 'rectangle' and 'triangle', which implement the area calculation. The main class allows users to choose between calculating the area of a rectangle or triangle, or exiting the program.

Uploaded by

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

Assignment Oop 4

The document contains a Java program that defines an abstract class 'shape' with methods to get dimensions and calculate area. It includes two subclasses, 'rectangle' and 'triangle', which implement the area calculation. The main class allows users to choose between calculating the area of a rectangle or triangle, or exiting the program.

Uploaded by

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

import java.util.

*;

abstract class shape


{
double x,y;
public void getdata()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the base : ");
x=sc.nextDouble();
System.out.println("Enter the height : ");
y=sc.nextDouble();
}
abstract void area();
}
class rectangle extends shape
{
void area()
{
this.x=x;
this.y=y;
double a=x*y;
System.out.println("area="+a);
}

class triangle extends shape


{
void area()
{
this.x=x;
this.y=y;
double a=0.5*x*y;
System.out.println("area="+a);
}
}

public class a4
{
public static void main(String args[])
{
int o;
while(true)
{
System.out.println("ENTER YOUR CHOPICE \n1.rectangle \n2.triangle
\n3.exit");
Scanner sc1=new Scanner(System.in);
o=sc1.nextInt();

switch (o)
{
case 1:
{
shape s1=new rectangle();
s1.getdata();
s1.area();
break;
}
case 2:
{
shape s2=new triangle();
s2.getdata();
s2.area();
break;
}

case 3:
{
System.out.println("thank you");
System.exit(0);
break;
}
default:
{
System.out.println("Enter appropriate choice :
");
}

You might also like