Question

In: Computer Science

Java Program Make an interface called interface1 that has tow data fields- default_length=1 and Pi=3.14 Make...

Java Program

Make an interface called interface1 that has tow data fields- default_length=1 and Pi=3.14

Make a second interface called interface2 that has one method: double getPerimeter(),

Make an abstract class GeometricObject that implements the 2 interfaces. And make one method getArea as an abstract method.

Make a subclass of GeometricObject called Square. The square class will inherit the superclass methods and implements the required abstract methods.

Make a subclass of GeometricObject called Triangle. (This is an equilateral triangle.) Triangle class will inherit the superclass methods and implements the required abstract methods.

In another class called test,using polymorphism call, declare 6 objects. The objects should be enclosed in an ArrayList of GeometricObject.

GeometricObject obj1=new Square(2);

Geometric Object obj2= new Triangle(4);

ArrayList<GeometricObject> arr=new ArrayList();

Write a for loop to extract the objects and print out their data.

Solutions

Expert Solution

import java.util.*;

interface interface1
{
public double default_length=1;
public double Pi=3.14;
}
interface interface2
{
   public double getPerimeter();
}
abstract class GeometricObject implements interface1,interface2// abstract base class
{

//abstract methods
abstract public double getArea();

}
class Triangle extends GeometricObject
{
private double side;
public Triangle()
{
side = default_length;
}
public Triangle(double side)
{
this.side = side;
}


//A method named getArea() that returns the area of this triangle.
public double getArea()
{
double s = 3*side/2;
double area = Math.sqrt(s*(s-side)*(s-side)*(s-side));
return area;
}
//A method named getPerimeter() that returns the perimeter of this triangle.
public double getPerimeter()
{
return 3*side;
}
//A method named toString() that returns a string description for the triangle.
public String toString()
{
return "Equilateral Triangle side : "+side +" Area : "+getArea()+" Perimeter : "+getPerimeter();
}


}
class Square extends GeometricObject
{
private double side;

public Square()
{
   side = default_length;
}

public Square(double side)
{
this.side = side;
}
public String toString()
{
return "Square side : "+side+" Area : "+getArea()+" Perimeter : "+getPerimeter();
}


public double getArea()
{
return side*side;
}
//A method named getPerimeter() that returns the perimeter of this triangle.
public double getPerimeter()
{
return 4*side;
}
}


class Test
{
public static void main (String[] args)
{
  
GeometricObject obj1=new Square(2);

GeometricObject obj2= new Triangle(4);

GeometricObject obj3= new Triangle(6);

GeometricObject obj4= new Triangle();

GeometricObject obj5=new Square(5);

GeometricObject obj6=new Square();

ArrayList<GeometricObject> arr=new ArrayList();

arr.add(obj1);
arr.add(obj2);
arr.add(obj3);
arr.add(obj4);
arr.add(obj5);
arr.add(obj6);

for(int i = 0; i < arr.size(); i++) {
System.out.println(arr.get(i));

}

}
}

Output:

Square side : 2.0 Area : 4.0 Perimeter : 8.0
Equilateral Triangle side : 4.0 Area : 6.928203230275509 Perimeter : 12.0
Equilateral Triangle side : 6.0 Area : 15.588457268119896 Perimeter : 18.0
Equilateral Triangle side : 1.0 Area : 0.4330127018922193 Perimeter : 3.0
Square side : 5.0 Area : 25.0 Perimeter : 20.0
Square side : 1.0 Area : 1.0 Perimeter : 4.0

Do ask if any doubt. Please up-vote.


Related Solutions

****JAVA Program**** Make a LandTract class with the following fields: • length - an int containing...
****JAVA Program**** Make a LandTract class with the following fields: • length - an int containing the tract's length • width - an int containing the tract's width The class should also have the following methods : • area - returns an int representing the tract's area • equals - takes another LandTract object as a parameter and returns a boolean saying whether or not the two tracts have the same dimensions (This applies regardless of whether the dimensions match...
Im trying to get a GUI interface in java where there is four text fields for...
Im trying to get a GUI interface in java where there is four text fields for the first name, last name, department, and phone number of employee in a program. Then also have a radio button below for Gender (Male/Female/Other) and a list for the Title (Mr./Ms./Mrs./Dr./Col./Prof.). At the very bottom of the frame there has to be buttons for printing, submitting and exiting but for whatever reason when I tried it nothing appears in the frame regardless of what...
JAVA code Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface...
JAVA code Create a new class called BetterDiGraph that implements the EditableDiGraph interface See the interface below for details. EditableDigraph below: import java.util.NoSuchElementException; /** * Implements an editable graph with sparse vertex support. * * */ public interface EditableDiGraph {    /** * Adds an edge between two vertices, v and w. If vertices do not exist, * adds them first. * * @param v source vertex * @param w destination vertex */ void addEdge(int v, int w); /** *...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface...
This is for a java program. Payroll System Using Inheritance and Polymorphism 1. Implement an interface called EmployeeInfo with the following constant variables: FACULTY_MONTHLY_SALARY = 6000.00 STAFF_MONTHLY_HOURS_WORKED = 160 2. Implement an abstract class Employee with the following requirements: Attributes last name (String) first name (String) ID number (String) Sex - M or F Birth date - Use the Calendar Java class to create a date object Default argument constructor and argument constructors. Public methods toString - returning a string...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called...
Program Specifications The built-in Java Math methods make some calculations much easier. Write a program called "DoTheMath" that accepts as input three floating-point numbers x, y, and z (define them as double) and outputs several calculations: x to the power of y x to the power of (y to the power of z) The absolute value of x The square root of (x*y to the power of z). Sample Run: Enter the values for x, y, z: -3.7 -3 5...
Java program Statement: Provide a user interface to the invoice program in Section 12.3 that allows...
Java program Statement: Provide a user interface to the invoice program in Section 12.3 that allows a user to enter and print an arbitrary invoice. Do not modify any of the existing classes. ..... ..... ..... /** Describes an invoice for a set of purchased products. */ public class Invoice { /** Adds a charge for a product to this invoice. @param aProduct the product that the customer ordered @param quantity the quantity of the product */ public void add(Product...
Write a Circle Class that has the following fields: • radius: a double • PI: a...
Write a Circle Class that has the following fields: • radius: a double • PI: a final double initialized with the value 3.14159 • Constructor. Accepts the radius of the circle as an argument • Constructor. A no-arg constructor that sets the radius field to 0.0. • setRadius. A mutator method for the radius field. • getRadius. An accessor method for the radius field. • getArea. Returns the area of the circle, which is calculated as area = PI *...
Java OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the...
Java OVERVIEW This program primarily focuses on the implementation of a ArrayList type interface and the necessary methods to implement the ArrayList. It also includes polymorphism and class comparison. INSTRUCTIONS Your deliverable will be to turn in three files. The files will be named Card.java, PremiumCard.java and the last file will be called CardArrayList.java. For this assignment, any use of a data control structure other than a simple Arrary or String will result in no credit. I am aware that...
Write the program in Java (with a graphical user interface) and have it calculate and display...
Write the program in Java (with a graphical user interface) and have it calculate and display the mortgage payment amount from user input of the amount of the mortgage, the term of the mortgage, and the interest rate of the mortgage. Allow the user to loop back and enter new data or quit. You need to include Calculate, Reset, and Exit buttons on your GUI. Please insert comments in the program to document the program. Allow the user to enter...
Write a java program using the following information Design a LandTract class that has two fields...
Write a java program using the following information Design a LandTract class that has two fields (i.e. instance variables): one for the tract’s length(a double), and one for the width (a double). The class should have:  a constructor that accepts arguments for the two fields  a method that returns the tract’s area(i.e. length * width)  an equals method that accepts a LandTract object as an argument. If the argument object holds the same data (i.e. length and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT