Question

In: Computer Science

In java, implement a program that returns a value of the area of the geometric objects...

In java, implement a program that returns a value of the area of the geometric objects listed below.
Use the technique of method overloading concept and also uses the interfaces for dynamic method
invocation.

Triangle: √?(? − ?)(? − ?)(? − ?)
where s=(a+b+c)/2
a,b,c are the sides of the
triangle.

Square: A^2 where A is the side

Rectangle: (a*b) where a ,b are sides
Circle: ? = ??2 where r is the radius
Cube: 6a^2 where a is the side

Solutions

Expert Solution

AreaInterface.java (Interface)

public interface AreaInterface {
public double getArea();
}

Triangle.java

public class Triangle implements AreaInterface{
private double side1, side2, side3;
  
public Triangle()
{
this.side1 = 0;
this.side2 = 0;
this.side3 = 0;
}

public Triangle(double side1, double side2, double side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}

public double getSide1() {
return side1;
}

public double getSide2() {
return side2;
}

public double getSide3() {
return side3;
}

@Override
public double getArea() {
double s = (side1 + side2 + side3) / 2;
double area = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
return area;
}
}

Square.java

public class Square implements AreaInterface{
private double side;
  
public Square()
{
this.side = 0;
}
  
public Square(double side)
{
this.side = side;
}
  
public double getSide(){ return this.side; }

@Override
public double getArea() {
return (side * side);
}   
}

Rectangle.java

public class Rectangle implements AreaInterface{
private double width, height;
  
public Rectangle()
{
this.width = 0;
this.height = 0;
}

public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}

public double getWidth() {
return width;
}

public double getHeight() {
return height;
}

@Override
public double getArea() {
return (width * height);
}
}

Circle.java

public class Circle implements AreaInterface{

private double radius;
  
public Circle()
{
this.radius = 0;
}
  
public Circle(double radius)
{
this.radius = radius;
}
  
public double getRadius(){ return this.radius; }
  
@Override
public double getArea() {
return(Math.PI * Math.pow(radius, 2));
}
}

Cube.java

public class Cube implements AreaInterface{

private double side;
  
public Cube()
{
this.side = 0;
}
  
public Cube(double side)
{
this.side = side;
}
  
public double getSide(){ return this.side;}
  
@Override
public double getArea() {
return (6 * Math.pow(side, 2));
}
}

ShapesTest.java (Main class)

public class ShapesTest {
  
public static void main(String[]args)
{
Triangle triangle = new Triangle(3, 4, 5);
System.out.println("Area of triangle: " + String.format("%.2f", triangle.getArea()));
  
Square square = new Square(4);
System.out.println("Area of sqaure: " + String.format("%.2f", square.getArea()));
  
Rectangle rectangle = new Rectangle(4, 6);
System.out.println("Area of rectangle: " + String.format("%.2f", rectangle.getArea()));
  
Circle circle = new Circle(5);
System.out.println("Area of circle: " + String.format("%.2f", circle.getArea()));
  
Cube cube = new Cube(7);
System.out.println("Area of cube: " + String.format("%.2f", cube.getArea()));
}
}

Note: Here AreaInterface is the interface class used having only one public function "getArea()" which is implemented in each of the class (except the main class). This is where the concept of Interface comes. Also, the same method (getArea) is used throughout the classes but different classes have different implementation (having different set of parameters) of the method. This is where the concept of method overloading (same method name, but different implementation) comes.

***************************************************************** SCREENSHOT **********************************************************


Related Solutions

Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area...
Writing a Java Code Requirements of the JAVA program: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle...
REQUIREMENTS OF THE JAVA PROGRAM: Your task is to calculate geometric area for 3 shapes(square, rectangle and circle). 1. You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input a. Invalid input should throw a message for the user. Example: Invalid input, please try again 2. Each options should ask users for relevant data....
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a...
JAVA program: Calculate geometric area for 3 shapes(square, rectangle and circle). You need to build a menu that allows users to enter options. Possible options are 'S' for square, 'R' for rectangle and 'C' for circle. HINT: you can use switch statement to switch on string input Invalid input should throw a message for the user. Example: Invalid input, please try again Each options should ask users for relevant data. HINT: use scanner object to take in length for square,...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a...
In java. Prefer Bluej Create a program in java that calculates area and perimeter of a square - use a class and test program to calculate the area and perimeter; assume length of square is 7 ft.
write a java program for area and perimeter of right triangle
write a java program for area and perimeter of right triangle
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
Please show solution and comments for this data structure using java.​ Implement a program in Java...
Please show solution and comments for this data structure using java.​ Implement a program in Java to convert an infix expression that includes (, ), +, -, *,     and / to postfix expression. For simplicity, your program will read from standard input (until the user enters the symbol “=”) an infix expression of single lower case and the operators +, -, /, *, and ( ), and output a postfix expression.
4 Implement a Java program that meets the following requirements • You can use the Java...
4 Implement a Java program that meets the following requirements • You can use the Java standard sequence data structure API types for sets, lists, stack,queue and priority queue as needed. All are available in the java.util package, which you will want to import in your program. 1. Argue in code comments which data structure, stack or queue, you will use to implement this method. Implement a method which creates some String objects as food orders for a small restaurant,...
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT