Question: Preliminaries Create a project submission folder, in the form Lab#FirstNameLastName. For example, mine would be Lab8MaxFowler Create an Eclipse Java Project. Add a Java class

Preliminaries

Create a project submission folder, in the form Lab#FirstNameLastName. For example, mine would be Lab8MaxFowler

Create an Eclipse Java Project.

Add a Java class named Circle to your project

Add a Java class named Triangle to your project

Add a Java class called Main to your project

Exercises

Part 1) The Circle class 6 Points

In the first part of the lab, we are writing a class to represent a Circle. It should not have a main method.

Give Circle two fields.

radius, a double storing the circles radius. This should be private and non-static (dont use the static keyword)

PI, a public final double constant set to 3.14159

The class should have the following two kinds of Constructors (for reference, see section 6.4):

Constructor 1: Write a constructor that accepts a double argument, and use it to set the radius of the circle

Constructor 2: Write a constructor that accepts no arguments and set the circles radius to 0.0.

The Circle class should contain the following methods. All of them are public and none of them are static. In all cases where PI is used, use the constant set in Part (a).

setRadius take a double argument, set radius to that value

getRadius take no arguments, return the current value of the circles radius

getArea take no arguments, return the area (PI * radius * radius) of the circle.

getDiameter take no arguments, return the diameter (radius * 2)

getCircumference take no arguments, return 2 * PI * radius

Part 2) The Triangle class 6 Points

In the second part of the lab, we are writing a class to represent a Triangle. It should not have a main method.

Give Triangle two fields. Both should be private and non-static

base a double value for the base width of the triangle

height a double value for the height of the triangle

The class should have the following two kinds of Constructors

Constructor 1: Write a constructor that takes two double arguments and uses them to assign the base and height respectively

Constructor 2: Write a constructor that takes no arguments and sets both base and height to 0.0

The class should have the following methods. All of them should be public and none of them are static.

setBase takes a double argument, set base to that value

getBase take no arguments, return the current value of the triangles base

setHeight takes a double argument, set height to that value

getHeight take no arguments, return the current value of the triangles height

getArea take no arguments, return the area (1/2 * base * height) of the circle

Part 3) The Main class 8 points

The Main class has no other methods than main. Main should accomplish all of the tasks listed below.

Make a Random object. This will be used in some cases.

Use a Scanner and the console to ask the user what kind of object they want to make a Circle or a Triangle. The user should enter the entire word, circle or triangle, but case should not matter (this means case should be ignored!)

In the case of wanting to make a Circle

Ask the user for the Circles radius

If the radius is negative, use the no argument constructor. Otherwise, use the argument constructor and pass the radius to a new circle.

Print out the circles current area, diameter, and circumference (and identify them for the user)

If the radius is 0, set it to a random number between 1 and 20

Tell the user the new radius that the circle has been given, then reprint the area, diameter, and circumference

In the case of wanting to make a Triangle

Ask the user for the Triangles base width

Ask the user for the Triangles height

If either of the numbers is negative, use the no argument constructor. Otherwise, use the argument constructor and pass the base and height to a new triangle.

Print of the triangles area

If the base or height is 0, set them both to a random number between 1 and 20

Tell the user the new base and height, then reprint the triangles area

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!