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,...
Assignment 4: Objects that contain objects In this assignment, you will implement a Java application, called...
Assignment 4: Objects that contain objects In this assignment, you will implement a Java application, called BankApplication, that can be used to manage bank accounts. The application is to be implemented using three classes, called Account, Customer, and BankDriver. The description of these classes is below. Problem Description class Account The Account class keeps track of the balance in a bank account with a varying annual interest rate. The Account class is identified as follows: • two double instance variables,...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter...
[For Java Programming: Objects Class] Write a Java application program that prompts the user to enter five floating point values from the keyboard (warning: you are not allowed to use arrays or sorting methods in this assignment - will result in zero credit if done!). Have the program display the five floating point values along with the minimum and maximum values, and average of the five values that were entered. Your program should be similar to (have outputted values be...
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
java please Write a program that creates an ArrayList and adds 5 circle objects to the...
java please Write a program that creates an ArrayList and adds 5 circle objects to the list , and display all elements in the list by invoking the object’s toString() method.
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