Create a Bitbucket private repository.
Name it “CUS1156_Lab2”. Clone it in your local java Eclipse
workspace. You will add the java files after you have completed
them in Part 1 and Part 2 to this location. Part 1: Abstract
classes From class, an abstract class represents some generic
concept. Subclasses then provide their own specific implementations
of any abstract methods of the abstract class.
1. Implement an abstract class called Shape it was discussed in the lecture. Include an abstract method double area() that returns the area of the shape.
2. Implement concrete subclasses of Shape that represents a square and a circle. Implement the area() abstract method in each of these two subclass Include any accessor methods (i.e. “get” methods) needed and a toString() method in each subclass.
3. Control classes contain a main method. Write a test control class that tests these classes by setting up an array of shapes and initializing each element of the array to a different shape, either a circle or a square. Then print out the details of each shape, including its class (see getClass() method at Object level) and the area of the shape.
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
Note: if there is any extra features needed for Shape class or its sub classes, just let me know. I have written this based on the specifications mentioned in this question only.
// Shape.java
public abstract class Shape {
/**
* abstract method to calculate and return the area of the shape
*/
public abstract double area();
}
// Square.java
public class Square extends Shape {
private double side;
// default constructor
public Square() {
side = 0;
}
/**
* @param side
* representing length of one side
*/
public Square(double side) {
this.side = side;
}
// getters and setters
public double getSide() {
return side;
}
public void setSide(double side) {
this.side = side;
}
@Override
public double area() {
//area of square: side^2
return side * side;
}
@Override
public String toString() {
//returning a string containing class name, side and area
return getClass().getSimpleName() + " [side: " + side + ", area: "
+ area() + "]";
}
}
// Circle.java
public class Circle extends Shape {
private double radius;
// default constructor
public Circle() {
radius = 0;
}
/**
* constructor taking value for radius
* @param radius
* radius of the circle
*/
public Circle(double radius) {
this.radius = radius;
}
//getters and setters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
@Override
public double area() {
//area of circle=Pi*r^2
return Math.PI*radius*radius;
}
@Override
public String toString() {
//returning a string containing class name, radius and area
return getClass().getSimpleName() + " [radius: " + radius+ ", area: "
+ area() + "]";
}
}
// Test.java
public class Test {
public static void main(String[] args) {
// creating an array of 4 shapes
Shape shapes[] = new Shape[4];
// creating a square using default constructor, assigning to first
// location
shapes[0] = new Square();
// creating a square with side=12
shapes[1] = new Square(12);
// creating a circle using default constructor
shapes[2] = new Circle();
// creating a circle with radius=15
shapes[3] = new Circle(15);
// looping through the array and printing shapes, which will call the
// toString() methods of each object to print its class name,
// attribute(s) and area
for (int i = 0; i < shapes.length; i++) {
System.out.println(shapes[i]);
}
}
}
/*OUTPUT*/
Square [side: 0.0, area: 0.0]
Square [side: 12.0, area: 144.0]
Circle [radius: 0.0, area: 0.0]
Circle [radius: 15.0, area: 706.8583470577034]
Create a Bitbucket private repository. Name it “CUS1156_Lab2”. Clone it in your local java Eclipse workspace....
I want to write a superclass with one private attribute and one method in JAVA The super class will later derive into 2 subclasses each with its own private attributes and each with a method that OVERRIDES the superclass method with each subclasses with one method ( that will do something) unique to that subclass. ALL classes must have constructors initializing its attributes and getter/setter methods. Then, in the main method, keep initializing objects of type superclasses based on user's...
TASK 1: Create class diagram for abstract class Shape. TASK 2: Create class diagrams for abstract subclasses TwoDimensionalShape and ThreeDimensionalShape which extend abstract superclassShape. TwoDimensionalShape contains method getArea, and ThreeDimensionalShape contains methods getArea and getVolume. TASK 3: Create class diagrams for concrete classes Circle, Square, Triangle, Sphere, Cube and Tetrahedron.
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...
plz
write if it is in another class or package
Question 1: 1. Create a new project in Eclipse (File > New > Java Project.) Name it Homework2Q1 2. Create a package for your classes 3. Create an abstract superclass with only non default constructor(s) 4. Create two different subclasses of that superclass 5. Create another class that is not related (you need a total of four classes up to this point) 6. Create an interface 7. Make the class...
Language is JAVA.
Clarification for the Shape class (highlighted):
The following requirements specify what fields you are expected
to implement in your Shape class;
- A private String color that specifies the color of the
shape
- A private boolean filled that specifies whether the shape is
filled
- A private date (java.util.date) field dateCreated that
specifies the date the shape was created Your Shape class will
provide the following constructors;
- A two-arg constructor that creates a shape
with...
Purpose: To write an Object-Oriented application using abstraction, inheritance, encapsulation, and polymorphism to handle an inheritance structure of various shapes. Details: Implement the following Shape hierarchy: a box around each shape name and arrows pointing to the shape from each (Circle, Square, Triangle) Shape Circle Square Triangle Use the following as a guide: The Shape class should be abstract and contain two abstract methods: getArea – Which calculates the area of the shape and returns that value as a...
This is a java program that runs on the Eclipse.
Java Programming 2-1: Java Class Design Interfaces Practice Activities Lesson objectives: Model business problems using Java classes Make classes immutable User Interfaces Vocabulary: Identify the vocabulary word for each definition below. A specialized method that creates an instance of a class. A keyword that qualifies a variable as a constant and prevents a method from being overridden in a subclass. A class that it can't be overridden by a subclass,...
Java Lab
In this lab, you will be implementing the following class hierarchy. Your concrete classes must call the superclass constructor with the proper number of sides (which is constant for each concrete shape). The perimeter method is implemented in the superclass as it does not change based on the number of sides. The area method must be overridden in each subclass as the area is dependent on the type of polygon. The areas of an equilateral triangle, square, and...
Q2) Interface Create a program that calculates the perimeter and the area of any given 2D shape. The program should dynamically assign the appropriate calculation for the given shape. The type of shapes are the following: • Quadrilateral 0 Square . Perimeter: 4xL • Area:LXL O Rectangle • Perimeter: 2(L+W) • Area:LxW Circle Circumference: I x Diameter (TT = 3.14) Area: (TT xD')/4 Triangle (assume right triangle) o Perimeter: a+b+c O Area: 0.5 x base x height (hint: the base...
Create a new package in assignment called shapes. All work for this part should be done in the shapespackage. Create a new interface called Shape. Add double area() and double perimter()methods to the Shape interface. Create two classes called Circle and Rectanglewhich implement Shape. Add reasonable fields to the Circle and Rectangle classes which will allow you to calculate the area and perimeter of each shape. Add constructors to Circle and Rectangle so you can initialize your fields. Implement the...