Preliminaries Create a project submission folder, in the form Lab#FirstNameLastN
ID: 3839949 • Letter: P
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 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 circle’s radius. This should be private and non-static (don’t 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 circle’s 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 circle’s 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 triangle’s base
setHeight – takes a double argument, set height to that value
getHeight – take no arguments, return the current value of the triangle’s 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 Circle’s 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 circle’s 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 Triangle’s base width
Ask the user for the Triangle’s 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 triangle’s 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 triangle’s area
Explanation / Answer
Circle.java
public class Circle {
//Declaring instance variables
private double radius;
public final double PI= 3.14159;
//Parameterized constructor
public Circle(double radius) {
this.radius = radius;
}
//Zero argumented constructor
public Circle() {
super();
this.radius=0.0;
}
//getters and setters
public double getRadius() {
return radius;
}
public void setRadius(double radius) {
this.radius = radius;
}
//This method will calculate the area of the circle
public double getArea()
{
return PI * radius * radius;
}
//This method will calculate the diameter of the circle
public double getDiameter()
{
return 2*radius;
}
//This method will calculate the circumference of the circle
public double getCircumference()
{
return 2 * PI * radius;
}
}
______________________
Triangle.java
public class Triangle {
//Declaring instance variables
private double base;
private double height;
//Parameterized constructor
public Triangle(double base, double height) {
super();
this.base = base;
this.height = height;
}
//Zero argumented constructor
public Triangle() {
super();
this.base = 0.0;
this.height = 0.0;
}
//getters and setters
public double getBase() {
return base;
}
public void setBase(double base) {
this.base = base;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//this method will calculate the area
public double getArea() {
return (0.5 * base * height);
}
}
_____________________
Main.java
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
//Declaring variables
double radius, base, height;
int randNum;
Circle c;
Triangle t;
// Creating a random Class object
Random r = new Random();
// Scanner object is used to get the inputs entered by the user
Scanner sc = new Scanner(System.in);
String shape;
System.out.print("Enter shape :");
shape = sc.next();
if (shape.equalsIgnoreCase("circle")) {
System.out.print("Enter the radius :");
radius = sc.nextDouble();
//if the radius is negative
if (radius < 0) {
c = new Circle();
System.out.printf("Area of the circle is :%.2f ", c.getArea());
System.out.printf("Diameter is :%.2f ", c.getDiameter());
System.out.printf("Circumference is :%.2f ",
c.getCircumference());
} else if (radius == 0) {
randNum = r.nextInt((20 - 1) + 1) + 1;
c = new Circle(radius);
System.out.println("The New Radius is :" + radius);
System.out.printf("Area of the circle is :%.2f ", c.getArea());
System.out.printf("Diameter is :%.2f ", c.getDiameter());
System.out.printf("Circumference is :%.2f ",
c.getCircumference());
} else {
c = new Circle(radius);
System.out.println("The Radius is :" + radius);
System.out.printf("Area of the circle is :%.2f ", c.getArea());
System.out.printf("Diameter is :%.2f ", c.getDiameter());
System.out.printf("Circumference is :%.2f ",
c.getCircumference());
}
} else if (shape.equalsIgnoreCase("triangle")) {
System.out.println("Enter the base :");
base = sc.nextDouble();
System.out.println("Enter the height :");
height = sc.nextDouble();
if (base == 0 || height == 0) {
base = r.nextInt((20 - 1) + 1) + 1;
height = r.nextInt((20 - 1) + 1) + 1;
t = new Triangle(base, height);
System.out.println("The New Base is :" + base);
System.out.println("The New Height is :" + height);
System.out.printf("Area :%.2f ", t.getArea());
} else if (base < 0 || height < 0) {
t = new Triangle();
System.out.printf("Area :%.2f ", t.getArea());
} else {
t = new Triangle(base, height);
System.out.println("Area :" + t.getArea());
}
} else {
System.out.println("** Invalid Input **");
}
}
}
________________________
Output:
Enter the radius :5.5
The Radius is :5.5
Area of the circle is :95.03
Diameter is :11.00
Circumference is :34.56
__________________
Output#2:
Enter shape :triangle
Enter the base :
0
Enter the height :
5.5
The New Base is :7.0
The New Height is :17.0
Area :59.50
_______________Thank You