[go: up one dir, main page]

0% found this document useful (0 votes)
36 views7 pages

472 Assignment10

The document describes a Java program that creates an interface called Shape with a getArea() method. It then creates three classes - Rectangle, Circle, Triangle that implement the Shape interface and define getArea() for each shape's area calculation formula. The main method runs a loop prompting the user to select a shape and calculate its area until they enter 4.

Uploaded by

sejal.s.patil
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)
36 views7 pages

472 Assignment10

The document describes a Java program that creates an interface called Shape with a getArea() method. It then creates three classes - Rectangle, Circle, Triangle that implement the Shape interface and define getArea() for each shape's area calculation formula. The main method runs a loop prompting the user to select a shape and calculate its area until they enter 4.

Uploaded by

sejal.s.patil
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/ 7

Assignment_10| UCE2023472

Problem statement:
Write a Java program to create an interface Shape with the getArea()
method. Create three classes Rectangle, Circle, and Triangle that
implement the Shape interface. Implement the getArea() method for
each of the three classes.

Algorithm:
1. Define an interface ‘Shape’. Declare method public abstract
void getArea(); in the ‘Shape’ interface.
2. Import Scanner class and create its object- sc.
3. Create three classes- Rectangle, Circle and Triangle, each
implemen ng the ‘Shape’ interface.
4. In each class, define public void getArea() method having
unique defini on in each class.
5. In class Rectangle, take length and breadth as input, calculate
area using the formula and print the area on the screen as
output. In class Circle, take radius of circle as input, calculate
area using the formula and print the area on screen as output.
In class Triangle, take height and base of the triangle as input,
calculate area using the formula and print the area on the
screen.
6. In Main class, in public sta c void main method, declare
variable int n and create an object of Scanner class- s.
7. Ask user to choose the shape of which they want to calculate
area (Enter 1 to calculate area of rectangle, 2 for circle, 3 for
triangle and 4 to exit). Store the input in n.
8. In switch case for n, in case 1, create object- rec of class
Rectangle and call getArea() method using that. Break.
In case 2, create object- cir of class Circle and call getArea()
method using that. Break.
In case 3, create object- tri of class Triangle and call getArea()
method using that. Break.
9. Go to step 7 and con nue while the user does not enter 4
(while the user enters 1,2 or 3).
10. End.
Flowchart:
Code:
package Assignment10;
public interface Shape {
public abstract void getArea();
}

package Assignment10;
import java.util.Scanner;
class Rectangle implements Shape
{
Scanner sc= new Scanner(System.in);
public void getArea() {
System.out.println("Enter the length of
rectangle:");
double l=sc.nextDouble();
System.out.println("Enter the breadth of
rectangle:");
double b=sc.nextDouble();
double area=l*b;
System.out.println("Area of the rectangle having
entered length and breath,is:"+area);
}
}
class Circle implements Shape
{
Scanner sc= new Scanner(System.in);
public void getArea() {
double pi=3.14159265359;
System.out.println("Enter the radius of the
circle:");
double r=sc.nextDouble();
double area=pi*r*r;
System.out.println("Area of the circle having
entered radius,is:"+area);

}
}
class Triangle implements Shape
{
Scanner sc= new Scanner(System.in);
public void getArea() {
System.out.println("Enter the base of the
triangle:");
double b=sc.nextDouble();
System.out.println("Enter the height of the
triangle:");
double h=sc.nextDouble();
double area=0.5*b*h;
System.out.println("Area of the triangle having
entered base and height is:"+area);
}
}

public class Main {

public static void main(String[] args) {


int n;
Scanner s=new Scanner(System.in);
do {
System.out.println("Enter 1 to calculate area of
rectangle, 2 for circle and 3 for triangle and 4 to exit:");
n=s.nextInt();

switch(n) {

case 1:
{
Rectangle rec=new Rectangle();
rec.getArea();
break;
}

case 2:
{
Circle cir=new Circle();
cir.getArea();
break;
}

case 3:
{
Triangle tri=new Triangle();
tri.getArea();
break;
}

}
}while(n!=4);
}

}
Output:
Enter 1 to calculate area of rectangle, 2 for circle and 3 for
triangle and 4 to exit:
2
Enter the radius of the circle:
5.67
Area of the circle having entered radius,is:100.99874806099955
Enter 1 to calculate area of rectangle, 2 for circle and 3 for
triangle and 4 to exit:
1
Enter the length of rectangle:
7.9
Enter the breadth of rectangle:
5.32
Area of the rectangle having entered length and
breath,is:42.028000000000006
Enter 1 to calculate area of rectangle, 2 for circle and 3 for
triangle and 4 to exit:
3
Enter the base of the triangle:
12.34
Enter the height of the triangle:
9.089
Area of the triangle having entered base and height
is:56.07913
Enter 1 to calculate area of rectangle, 2 for circle and 3 for
triangle and 4 to exit:
4

You might also like