IN JAVA
For the following questions, “define a class” means the following:
• Create an appropriately-named file containing the Java class definition, including the following:
– A block comment describing the file (and hence the class), as always
– Any instance variables, as required by the question
– Accessor (getter) and mutator (setter) methods for any instance variables
– Constructors as appropriate or as required by the question
– A toString method that prints instances of the class informatively
– Any additional methods as required by the question. All methods other than getters and setters MUST have a descriptive comment before their definition.
• Create a separate appropriately-named file containing a different Java class with a main method that tests your new class by doing the following:
– Create at least two instances of the class.
– Change at least one of them using the class’ methods.
– Print all the objects informatively
Document how to run your code and interpret the output in your README.txt file.
1. Define the following class hierarchy for shapes:
(a) Define an abstract class Shape. Shapes have a color (which can be a string for now) and a boolean indicating whether or not they are filled. These should be properly encapsulated.
(b) Define an abstract class Shape2D that extends Shape. This class should define the abstract method getArea() that returns the area of a a Shape2D.
(c) Define a class Rectangle that extends Shape2D. A rectangle has a height and a width. Implement the getArea() method appropriately. Use the @Override annotation.
(d) Define a class Square that extends Rectangle. The class should have an appropriate constructor and also override setter methods as approrpiate to preserve “squareness.”
(e) Define a class Ellipse that extends Shape2D. An ellipse has a semi-major axis a and a semi-minor axis b (look it up if needed). The area of an ellipse is A = πab.
(f) Define a class Circle that extends Ellipse. You need appropriate contructor(s) and setter(s) for the class.
Illustrate all of these with a main method in a separate test class.
abstract class Shape
{
private String color;
private boolean filled;
public Shape(String color,boolean filled) // constructor
{
this.color = color;
this.filled = filled;
}
}
abstract class Shape2D extends Shape
{
public Shape2D(String color,boolean filled)
{
super(color,filled); // call to base class
constructor
}
public abstract double getArea();
}
class Rectangle extends Shape2D
{
private double height,width;
public Rectangle(String color,boolean filled,double
height,double width)
{
super(color,filled);
this.height = height;
this.width = width;
}
public double getArea()
{
return height*width;
}
}
class Square extends Rectangle
{
private double side;
public Square(String color,boolean filled,double side)
{
super(color,filled,side,side);
}
}
class Ellipse extends Shape2D
{
private double axisA,axisB;
public Ellipse(String color,boolean filled,double
axisA,double axisB)
{
super(color,filled);
this.axisA = axisA;
this.axisB = axisB;
}
public double getArea()
{
return 3.14*axisA*axisB;
}
}
class Circle extends Ellipse
{
private double radius;
public Circle(String color,boolean filled,double radius)
{
super(color,filled,radius,radius);
}
public double getRadius()
{
return radius;
}
}
class TestShapes
{
public static void main (String[] args)
{
Rectangle r = new
Rectangle("blue",true,3.5,5.4);
System.out.println("\nArea of rectangle :
"+r.getArea());
Square s = new Square("red",false,4.5);
System.out.println("\nArea of square : "+s.getArea());
Ellipse e = new Ellipse("yellow",true,3.8,4.1);
System.out.println("\nArea of circle : "+e.getArea());
Circle c = new Circle("pink",true,4.9);
System.out.println("\nArea of circle : "+c.getArea());
}
}
Output:
Area of rectangle : 18.900000000000002 Area of square : 20.25 Area of circle : 48.9212 Area of circle : 75.3914
Do ask if any doubt. Please upvote.
IN JAVA For the following questions, “define a class” means the following: • Create an appropriately-named...
In Java For the following questions, “define a class” means the following: • Create an appropriately-named file containing the Java class definition, including the following: – A block comment describing the file (and hence the class), as always – Any instance variables, as required by the question – Accessor (getter) and mutator (setter) methods for any instance variables – Constructors as appropriate or as required by the question – A toString method that prints instances of the class informatively –...
Hey, I have this homework from my data structures class using java that I am having difficulties coding. I had to create an abstract class Shape, which contains two protected instance variables color(String) and filled(boolean). Getter and setter for all the instance variables, and toString(). Two abstract methods getArea() and getPerimeter(). The part that I am not sure how to code is: for the subclasses of Shape which are Circle and Rectangle I have to override the abstract methods getArea()...
in java
Define a class named ComparableNames thats extends Person and
implements comparable
Define a class named ComparableNames that extends Person (defined below) and implements Comparable. Override the compare To method to compare the person on the basis of name. Write a test class to find the order of two ComparableNames. class Person String name; Person00 Person(String n) name=n: Hint: in class ComparableName, you only need a constructor with arg to initialize the data filed defined in the super class...
Write a Java program with the following requirements: Rectangle that implements Shape2D - instance variables height, width Block that implements Shape3D - instance variables height, width, depth Add appropriate parameterized constructors for each class. Implement the abstract methods to make each class a Shape2D, Shape3D, or both. Make the main method use each of these to print out the area of a rectangle, and the area and volume of a block.
This is in Java, thanks! d Define two subclasses as below: 1) “Advanced Java” with an additional field called “grades_in_class” 2) “Web Technology” with an additional field called “grades_Quizzes”. Define the required constructors and getter methods in both classes. e Override the computeGrade() in both classes as below: 1. In Advanced Java, Fgrade= 40%* avg_exams+ 40%* avg_Assignments + 20* grades_in_class 2. In Web Technology, Fgrade= 30%* avg_exams+ 50%* avg_Assignments + 20*grades_Quizzes Note: Fgrade is a local variable in the computeGrade()...
Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...
Go to the Java API and review the Rectangle class briefly. As directed below, create a subclass of the Rectangle class called BetterRectangle as described below. Create another class called RectangleTester, which fully tests the BetterRectangle, by displaying a menu that allows a user to process data for multiple rectangles until they choose to quit. Be sure to include sample runs as block comments in your tester source code file. P9.10 The Rectangle class of the standard Java library does...
JAVA Create a Java project to implement a simple Name class. This class will have the following class variable: First Name, Middle Name, Last Name, and Full Name Create the accessor/getter and mutator/setter methods. In addition to these methods, create a toString() method, which overrides the object class toString() method. This override method prints the current value of any of this class object. Create a main() method to test your project.
Create another Java class named EmployeeMain within the same project, which includes the main method. a. Include another class named Employee in the same Java file. This class has the following instance variables and instance methods. Define all the instance/static variables with private access modifier and constructors, instance/static methods with public access modifier. Instance Variables empID: int employeeName: String basicSalary: double Constructor Set the value for empID. Instance Methods Get and set methods for all instance variables. displayEmployee: Display the...
Create an Item class, which is the abstract super class of all Items. Item class includes an attribute, description of type String for every item. [for eg. Book] A constructor to initialize its data member of Item class. Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details. Declare a constant RATE with value 0.25 Declare a method called calculateExtraCharge(), which returns a double value. Create the...