Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Programming: 2. Create a Shape class: a. Create a new class called Shape. Do this in a new file. b. It should have a

JAVA Programming:

2. Create a Shape class:

a. Create a new class called Shape. Do this in a new file.

b. It should have a protected String instance variables called name.

c. Create a single parameter constructor which can pass a name to create a Shape object.

d. Create a getter and setter method for this instance variable.

3. Create a Square subclass:

a. Create a subclass for the Shape class called square.

b. It should have 2 protected Point instance variables.

c. Create a 3-parameter constructor, which can pass a name, and 2 Point objects to create a Square object.

d. Create a getter method called getLength. This method should take the X values of the two Point objects in the Square, calculate the length of the square and return the value for length.

Hint: To ensure your height value is always positive, you can use the absolute value method (Math.abs())from the Math library built in to Java(java.lang.Math;).

e. Create a getter method called getHeight. This method should take the Y values of the two Point objects in the Square, calculate the height of the square and return the value for height.

f. Create a getter method called getArea. This method should calculate the area using the getHeight and getLength methods, then return the value of the area. Hint: Simply use the syntax getHeigth() and getLength() to obtain the values needed to calculate the area.

g. Create a getter method called getPerimeter. This method should calculate the perimeter using the getHeight and getLength methods, then return the value of the perimeter.

h. Demonstrate your class and methods work by creating a Square object using the 2 Point objects you created in Part 1 and a name of your choice. Print the length, height, area and perimeter of your Square object.

3. Create a Circle subclass:

a. Create a subclass for the Shape class called Circle.

b. It should have 2 protected instance variables, a Point (the center of your circle) and a radius.

c. Create a 3-parameter constructor which can pass a name, a Point object and a radius to create a Circle object.

d. Create a getter method which returns the value of the radius called getRadius.

e. Create a getter method that returns the center of your circle. Hint: You will be returning a Point object for this method.

f. Create a getter method called getArea. This method should calculate the area using the getRadius method and the value 3.14 for Pi. Then return the value of the area. Note: Since we are working with Pi, we will need to define our radius as a double. Simply redefine your radius in your method as a double.

g. Create a getter method called getCircumference. This method should calculate the circumference of your circle using the getRadius method and the value 3.14. Then, return the value of the circumference.

h. Demonstrate your class and methods work by creating a Circle object using the first Point object you created in Part 1, a radius of 1 and a name of your choice. Print the name, length, height, area and perimeter of your circle.

Note: Use a separate Java file for the each of the classes, subclasses and testing. In total, you should submit 5 Java files.

image text in transcribed

Sample Output: My first point is: 0,0 My second point is: 1,1 My second point is now: 2,2 The name of my square is: My Square The length of my square is: 2 The height of my square is: 2 The area of my square is: 4 The permiter of my square is: 8 The name of my circle is: My Circle The radius of my circle is:1 The center of my circle is at: 0,0 The area of my circle is: 3.14 The circumference of my circle is 6.28

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2015 Porto Portugal September 7 11 2015 Proceedings Part 3 Lnai 9286

Authors: Albert Bifet ,Michael May ,Bianca Zadrozny ,Ricard Gavalda ,Dino Pedreschi ,Francesco Bonchi ,Jaime Cardoso ,Myra Spiliopoulou

1st Edition

3319234609, 978-3319234601

More Books

Students also viewed these Databases questions