Design a class named Circle that extends the class GeometricObjects . The Circle
ID: 3773950 • Letter: D
Question
Design a class named Circle that extends the class GeometricObjects. The Circle class contains:
A double data fields called radius , with default values of 1.0, to denote the radius of the circle
A no-arg constructor that creates a default circle.
A constructor that creates a circle with the specified radius.
A getter method to get the instance variable.
A method named getPerimeter() that returns the perimeter of the circle. Perimeter of the circle can be calculated as 2*radius*3.14.
A method named getArea() that returns the area of the circle. Area of the circle can be calculated as 3.14*radius*radius.
A method named toString() that returns a String description of circle as follows:
return super.toString() + “ Circle : radius = “ + radius + “ area is : “ + getArea() + “ perimeter is : “ + getPerimeter();
Create two classes Circle and GeometricObjects and implement the classes.
Write a test program called TestCircleYourName, that prompts the user to enter the radius of a circle, a color and a boolean value to indicate whether the circle is filled. The program should create a Circle object with the given radius and set the color and filled properties using the inputs. The program should display the area, perimeter, color , and filled values (true or false to indicate whether the object is filled or not). The toString() method should print the values of radius, area and perimeter of the circle.
Please use the classes from the file Week4_4_SampleCode for Assignment_Problem1, as a template for this code. Please use the GeometricObjects code( as given in the sample code) , as your super class. You will be creating a new sub class called Circle and new tester class called TestCircleYourName. You need not include the subclass called Rectangle in your assignment.
In your TestCircleYourName, under the main method you will test:
Use all the constructors to create objects.
For each object , you will output its value of color, filled and use the toString() method to print the radius, area and perimeter.
For each object, you will reset the color, filled and radius using the setters and then print the new values of color and . Use the toString() method to print the new radius, area and perimeter.
The Person, Student, Employee classes. Design a class named Person and its two subclasses, Student and Employee.
A has instance variables called name, phone number and email, all of which are Strings. A , which is a subclass of Person , has status (freshman, junior etc), also declared as a String.
An has , office number (declared as int), salary (declared as int)
Implement the , and classes. Have a full-arg constructor for each class. Test all these classes using a class. In your test , you will test the full arg constructors of each class and for each object of that class, you will obtain the values of the instance variables. You will also use all the setters of a class for the object.
// you will be using Scanner class to obtain user inputs. You need to import this class first
import java.util.Scanner;
public class TestPersonPratibhaMenon {
public static void main(String args[]){
//Create a new scanner object to read inputs
Scanner scanner = new Scanner(System.in);
//Select the user choice for Student or Employee objects.
System.out.println("Enter 1)To create a student 2)To create an employee:");
int choice = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter name:");
String name = scanner.nextLine();
System.out.print("Enter address:");
String address = scanner.nextLine();
System.out.print("Enter phone number:");
String phone = scanner.nextLine();
System.out.print("Enter email:");
String email = scanner.nextLine();
if(choice == 1){
System.out.print("Enter student's status:");
String status = scanner.nextLine();
Student student1 = new Student(name, address, phone, email, status);
System.out.print(student1.toString());
}
else{
System.out.print("Enter office number:");
int office = scanner.nextInt();
scanner.nextLine();
System.out.print("Enter salary:");
int salary = scanner.nextInt();
scanner.nextLine();
Employee employee1 = new Employee(name, address, phone, email, office, salary);
System.out.print(employee1.toString());
}
}
}
4_4 Sample Code
I. Code for Geometric Objects
public class GeometricObjects{
/*Construct Geometric Object with specified color and filled value*/
public GeometricObjects(String color, boolean filled){
this.color = color;
this.filled = filled;
}
/* Return Color*/
public String getColor(){
return color;
}
/*Return filled. since filled is boolean we name it isFilled*/
public boolean isFilled(){
return filled;
}
/*Set new color*/
public void setColor(String color) {
this.color = color;
}
/*Set new filled*/
public void setFilled(boolean filled){
this.filled = filled;
}
/* toString method that returns the string representation of object. This method also fetches the values of color and filled--- i.e. works like a getter too*/
public String toString(){
return "Object color is: " + color + " object filled is: " + filled ;
}
}
II. Code for Rectangle
public class Rectangle extends GeometricObjects {
private double width;
private double length;
/* Default constructor - required only if tester class uses it*/
public Rectangle(){
/* same as writing super()in the body of this constructor;*/
}
/* Constructor with partial list of arguments*/
public Rectangle(double width, double length) {
/* same as writing super(color, filled) in addition to the lines below;*/
this.width = width;
this.length = length;
}
/* Constructor with full list of arguments*/
public Rectangle(double width, double length, String color, boolean filled) {
/*same as including super(); in addition to the lines below*/
this.width = width;
this.length = length;
setColor(color);
setFilled(filled);
}
/*getter for width*/
public double getWidth(){
return width;
}
/*getter for length*/
public double getLength(){
return length;
}
/*getter for width*/
public void setWidth(double width){
this. width = width;
}
/*getter for length*/
public void setLength(double length){
this.length = length;
}
/*getarea method*/
public double getArea(){
return width*length;
}
/*getPrimeter method*/
public double getPerimeter(){
return 2*(width+length);
}
/*@override toString()*/
/* toString method that returns the string representation of object*/
public String toString(){
return "rectangle length is: " +length + "and width is: " + width + " .Area is : " + getArea()+ " and Perimeter is: " +getPerimeter();
}
}
III. Code for Testerclass
public class TestRectanglePratibhaMenon {
public static void main(String[] args) {
Rectangle rectang1 = new Rectangle();
/*System.out.println("Rectangle color is " + rectang1.getColor());
System.out.println("Rectangle filled value is " + rectang1.isFilled());
System.out.println("Rectangle width is " + rectang1.getWidth());
System.out.println("Rectangle length is " + rectang1.getLength());*/
System.out.println(rectang1.toString());
rectang1.setColor("green");
rectang1.setFilled(false);
rectang1.setWidth(20);
rectang1.setLength(20);
System.out.println(rectang1.toString());
Rectangle rectang2 = new Rectangle(2,3);
/*System.out.println("Rectangle color is " + rectang2.getColor());
System.out.println("Rectangle filled value is " + rectang2.isFilled());
System.out.println("Rectangle width is " + rectang2.getWidth());
System.out.println("Rectangle length is " + rectang2.getLength());*/
System.out.println(rectang2.toString());
rectang2.setColor("blue");
rectang2.setFilled(true);
rectang2.setWidth(30);
rectang2.setLength(20);
System.out.println(rectang2.toString());
Rectangle rectang3 = new Rectangle(2,3, "green", true);
System.out.println(rectang3.toString());
rectang3.setColor("blue");
rectang3.setFilled(true);
rectang3.setWidth(40);
rectang3.setLength(20);
System.out.println(rectang3.toString());
}
Explanation / Answer
Answer:
Note: User given code is used.
import java.io.*;
import java.lang.*;
import java.util.*;
class GeometricObjects
{
String color;
boolean filled;
/*Construct Geometric Object with specified color and filled value*/
public GeometricObjects(String color, boolean filled){
this.color = color;
this.filled = filled;
}
/* Return Color*/
public String getColor(){
return color;
}
/*Return filled. since filled is boolean we name it isFilled*/
public boolean isFilled(){
return filled;
}
/*Set new color*/
public void setColor(String color) {
this.color = color;
}
/*Set new filled*/
public void setFilled(boolean filled){
this.filled = filled;
}
/* toString method that returns the string representation of object. This method also fetches the values of color and filled--- i.e. works like a getter too*/
public String toString(){
return "Object color is: " + color + " object filled is: " + filled ;
}
}
class Circle extends GeometricObjects
{
private double circleRadius;
/* Default constructor */
public Circle()
{
super("Yellow",true);
}
/* Constructor with partial list of arguments*/
public Circle(double circleRadius)
{
super("Yellow",true);
this.circleRadius = circleRadius;
}
/* Constructor with full list of arguments*/
public Circle(double circleRadius, String color, boolean filled)
{
super(color,filled);
this.circleRadius = circleRadius;
setColor(color);
setFilled(filled);
}
/*getter for radius*/
public double getCircleRadius(){
return circleRadius;
}
/*getter for length*/
public void setCircleRadius(double circleRadius){
this.circleRadius = circleRadius;
}
/*getarea method*/
public double getArea(){
return 3.14*circleRadius*circleRadius;
}
/*getPrimeter method*/
public double getPerimeter()
{
return 2*3.14*circleRadius;
}
/*@override toString()*/
/* toString method that returns the string representation of object*/
public String toString()
{
return "Circle radius: " +circleRadius + " Area is : " + getArea()+ " and Perimeter is: " +getPerimeter();
}
}
public class TestCircleXXX
{
public static void main(String[] args)
{
Circle cc1;
Scanner sn=new Scanner(System.in);
System.out.println("Enter radius:");
double circleRadius=sn.nextDouble();
System.out.println("Enter color:");
String color=sn.next();
System.out.println("Enter filled value:");
boolean filledVal=Boolean.parseBoolean(sn.next());
cc1=new Circle(circleRadius, color, filledVal);
System.out.println(cc1.toString());
System.out.println("Circle fill color:"+cc1.getColor());
System.out.println("Is Circle filled:"+cc1.isFilled());
}
}
Sample Output:
sh-4.3$ javac TestCircleXXX.java
sh-4.3$ java -Xmx128M -Xms16M TestCircleXXX
Enter radius:
120.0
Enter color:
Red
Enter filled value:
true
Circle radius: 120.0 Area is : 45216.0 and Perimeter is: 753.6
Circle fill color:Red
Is Circle filled:true
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.