HI, Please find my implementation.
Please let me know in case of any issue.
public abstract class RegularPolygon {
private int numSides;
private int sideLength;
public RegularPolygon(int numSides, int sideLength) {
this.numSides = numSides;
this.sideLength = sideLength;
}
public void setNumSides(int numSides) {
this.numSides = numSides;
}
public int getNumSides() {
return numSides;
}
public void setSideLengths(int sideLength) {
this.sideLength = sideLength;
}
public int getSideLengths() {
return sideLength;
}
public int perimeter() {
return numSides*sideLength;
}
public abstract double area();
public String toString() {
String s = "I am a shape with " + numSides + " sides of length: ";
s += "\nI am a polygon.";
return s;
}
}
class Hexagon extends RegularPolygon{
public Hexagon(int sideLength) {
super(6, sideLength);
}
public double area(){
return 3*Math.pow(3, 1.0/3.0)*getSideLengths()*getSideLengths()/2.0;
}
public String toString() {
String s = "I am a shape with " + getNumSides()+ " sides of length: ";
s += "\nI am a Quadrilateral.";
return s;
}
}
///////////////////////////////////////////////////////////////////////////////
class Square extends RegularPolygon{
public Square(int sideLength) {
super(4, sideLength);
}
public double area(){
return getSideLengths()*getSideLengths();
}
public String toString() {
String s = "I am a shape with " + getNumSides()+ " sides of length: ";
s += "\nI am a Square.";
return s;
}
}
////////////////////////////////////////////////////////////////////////////////////
// Triangle class
class Triangle extends RegularPolygon{
public Triangle( int sideLength) {
super(3, sideLength);
}
public double area(){
return Math.pow(3, 1.0/3.0)*getSideLengths()*getSideLengths()/4.0;
}
public String toString() {
String s = "I am a shape with " + getNumSides()+ " sides of length: ";
s += "\nI am a Triangle.";
return s;
}
}
public class RegularPolygonTest{
public static void main(String[] args) {
RegularPolygon[] shapes = {
new Square(3),
new Triangle(6),
new Triangle(7)
};
for(RegularPolygon p : shapes){
System.out.println(p.toString());
System.out.println("Perimeter: "+ p.perimeter());
System.out.println("Area: "+p.area());
System.out.println();
}
}
}
/*
Sample run:
I am a shape with 4 sides of length:
I am a Square.
Perimeter: 12
Area: 9.0
I am a shape with 3 sides of length:
I am a Triangle.
Perimeter: 18
Area: 12.980246132766673
I am a shape with 3 sides of length:
I am a Triangle.
Perimeter: 21
Area: 17.66755723626575
*/
Java Lab In this lab, you will be implementing the following class hierarchy. Your concrete classes...
JAVA: Quadrilateral Inheritance Hierarchy Write an inheritance hierarchy for classes Quadrilateral, Parallelogram, Rectangle, and Square. Use Quadrilateral as the superclass of the hierarchy. Create and use a Point class to represent the points in each shape. Make the hierarchy as deep as possible, i.e. more than two levels. Specify the instance variables and methods for each class. The private instance variables of Quadrilateral should be the x-y coordinate pairs for the four endpoints of the Quadrilateral. A program that instantiates...
Purpose: The purpose of this lab is for you to design and implement several classes that use Inheritance. The problem: The program must handle a collection of different 2-dimensional shapes: triangles, rectangles and circles. All shapes have a color (use a Stringl and are either filled or not (use a boolean). All shapes must calculate and return their perimeter, and area. toStrina) for all shapes must implement the standardized formatting for inheritance. You are required to implement each of the...
java language
Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...
Java code for the following inheritance hierarchy figure..
1. Create class Point, with two private
instance variables x and y that represented for the coordinates for
a point.
Provide constructor for initialising two instance variables.
Provide set and get methods
for each instance variable,
Provide toString method to return formatted
string for a point coordinates.
2. Create class Circle, its inheritance from
Point.
Provide a integer private
radius instance variable.
Provide constructor to
initialise the center coordinates and radius for...
java.
Question 3: The payment transaction scenario could be represented by the following hierarchy: Payments VISA MASTERCARD Paypal Create Payments as a superclass and VISA, MASTERCARD, PAYPAL as subclasses Create an interface PaymentsInterface with one method called payment Info. Create classes VISA, MASTERCARD, PAYPAL that implement Payments For each of the three classes (VISA, MASTERCARD, PAYPAL), create one constructor Constructors are used to create objects with initial balance with US dollars (USD). The interface class method "paymentinfo" should be overridden...
N-Sided Polygon An n-sided polygon is a planed figure whose sides have the same length and whose angles have the same degree. Write a class called NSidedPolygon that contains the following components: Private members to store the name of a polygon, the number of sides, and the length of each side. (You are expected to select the appropriate data types for each member.) A constructor that accepts the number of sides and the length of each side. A private method...
We have introduced Abstract Classes and Interfaces in the lectures. In this lab, we would like to provide few questions to review these topics and hands-on to solve them. Write a class named Octagon that extends GeometricObject and implements the Comparable and Cloneable interfaces. Assume that all eight sides of the octagon are of equal length. The area can be computed using the following formula: area = (2 + 4 Squareroot 2) * side * side Write a test program...
Lab 1: InheritanceTest Write a program called InheritanceTest1.java to support an inheritance hierarchy for class Point–Square–Cube. Use Point as the superclass of the hierarchy. Specify the instance variables and methods for each class. The private data of Point should be the x-y coordinates, the private data of Square should be the sideLength, and the private data of Cube should be depth. Provide applicable accessor methods, mutator methods, toString() methods, area() method, and volume() method to all classes. Write a program...
In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...
(JAVA) Implement a Triangle class. Any triangle can be represented by its THREE sides. Therefore, your class will have THREE private member variables → side1, side2 and side3. Use the double data type to represent the triangle sides. In addition, please provide public methods that perform the following FIVE tasks: ▪ An input method that obtains the appropriate values for the three sides from the user. While entering the values of the three sides, please remember the triangle property that...