Question: in java thanks. Define the following three classes: 1) Circle a) Must have a private attribute called radius of type double. b) Must have a
in java thanks.
Define the following three classes: 1) Circle
-
a) Must have a private attribute called radius of type double.
-
b) Must have a constructor which takes a single parameter of type double. It
should set the class attribute radius to this value.
-
c) Must have a method called circumference, which takes no parameters,
and returns a double. The circumference of a circle is calculated by multiplying 2 * Pi * radius. Note: both Java and C# have a constant called Math.PI which is equal to Pi.
-
d) Must have a method called area, which takes no parameters and returns a double. The area of a circle is calculated by multiplying Pi * radius^2. The Math class in both Java and C# have a method for calculating one number raised to a power. Youll need to look up this method and use it to raise the radius to the second power.
2) Triangle
-
a) Must have 3 private attributes to hold side1, side2 and side3. Each of
which should be of type double.
-
b) Must have a constructor that takes in 3 parameters and sets the 3
attributes
-
c) Must have an overloaded constructor that takes no parameters and sets
the 3 attributes to sizes 3, 4 and 5.
-
d) Must have a method called perimeter which takes in no parameters and
returns a double. The perimeter of a triangle is calculated by adding
side1+side2+side3
-
e) Must have a method called area which takes no parameters and returns a
double. The area of a triangle is calculated as follows (Youll need to look up the square root function in the Math object in Java or C#):
-
i) Calculate p=(side1+side2+side3)/2
-
ii) Take the square root of (p*(p-side1)*(p-side2)*(p-side3)).
-
-
f) Must have a method called height which takes no parameters and returns a double. The height of a triangle is calculated as follows:
-
i) Find the smallest of the 3 sides (side1, side2 or side3)
-
ii) Multiple the area of the triangle by 2, then divide that by the
smallest side.
-
-
g) Override the toString/ToString method in this class. Have it return a string
that gives the details of the triangle. For example: Triangle has sides x, y
and z. It has an area of X and perimeter of Y.
3) Rectangle
-
a) Must have 2 private attributes height and width. Both of which should be
of type double.
-
b) Must have a constructor which takes in 2 parameters and sets the 2
attributes of the class
-
c) Must have a method called perimeter which takes no parameters and
returns a double. The perimeter of a rectangle is calculated by adding
2*height + 2*width
-
d) Must have a method called area which takes no parameters and returns a
double. The area of a rectangle is calculated by multiplying the height by the width.
Create a driver program which does the following:
-
1) Using a loop print out the area and circumference of circles with radius 1, 2, 3 ...
9, 10. Here is what the first line should look like:
Area of a circle with radius 1 is 3.14159265358979 circumference is 6.28318530717959
-
2) Using nested loops, print out the area and perimeter of rectangles with width 1, 2 and 3 and heights 1, 2, and 3. Ie, youll print out the area and perimeter for a rectangle 1x1, 1x2, 1x3, 2x1, 2x2, 2x3, 3x1, 3x2, and 3x3. The first line should look like:
Area of a rectangle 1 by 1 is 1 it's perimeter is 4
-
3) Create a triangle with sides 18, 30 and 24. Calculate its area, perimeter and height and print them as follows:
Area of a triangle 18x30x24 is 216 it's perimeter is 72 and it's height is 24
-
4) Create a triangle of size 3,4,5 by calling the constructor with no parameters. Calculate its area, perimeter and height and print like the one above.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
