I worte the code according to the requirement.And also I wrote the code to continue the program
until user enters 4 to quit the program.
If you want me to do any modifications other than these I will modify.
________________________________________
GeometricShape.java
public class GeometricShape
{
private double side1, side2, side3; //These are the variables necessary to calculat the perimeter
of a triangle
private double radius; //Radius is needed to calculate the perimeter of a circle
private double length, width; //Length and Width are needed to calculate the perimeter of a
rectangle
private boolean isTriangle = false;
private boolean isRectangle = false;
private boolean isCircle = false;
//Constructor for a triangle
public GeometricShape(double aSide1, double aSide2, double aSide3)
{
side1 = aSide1;
side2 = aSide2;
side3 = aSide3;
isTriangle = true;
}
//Task #1a - Finish writing an Overloaded Constructor for a rectangle here: (don\'t forget to set
isRectangle to true)
public GeometricShape(double aLength, double aWidth)
{
this.length=aLength;
this.width=aWidth;
isRectangle=true;
}
//Task #1b - Write an Overloaded Constructor for a circle here: (don\'t forget to set isCircle to
true)
public GeometricShape(double radius)
{
this.radius=radius;
isCircle=true;
}
public double getRadius()
{
return radius;
}
public double getSide1()
{
return side1;
}
public double getSide2()
{
return side2;
}
public double getSide3()
{
return side3;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public void setRadius(double aRadius)
{
radius = aRadius;
}
public void setSide1(double aSide1)
{
side1 = aSide1;
}
public void setSide2(double aSide2)
{
side2 = aSide2;
}
public void setSide3(double aSide3)
{
side3 = aSide3;
}
public void setLength(double aLength)
{
length = aLength;
}
public void setWidth(double aWidth)
{
width = aWidth;
}
public double getPerimeter()
{
double perimeter =0;
//Calculate the perimeter of the Geometric object depending on what the object is.
//Remember the following formulas:
//Perimeter of a Triangle = Side1 + Side2+ Side3
//Perimeter of a Rectangle = (2 * length) + (2 * width)
//Perimeter of a Circle = 2 * Math.PI * radius
if (isTriangle == true)
{
//Complete formula for triangle perimeter
perimeter = side1 + side2 + side3;
}
else if (isRectangle == true)
{
//Task 2a: Complete formula for rectangle perimeter
perimeter= (2 * length) + (2 * width);
}
else if (isCircle == true)
{
//Task 2b: Complete formula for circle perimeter
perimeter=2 * Math.PI * radius;
}
//Continue with the rest of the else if logic here.
//change return statement to match the perimeter of the shape
return perimeter;
}
public String toString()
{
if (isTriangle == true)
{
//Return \"It\'s a Triangle: \" and concatenate all the attributes of a triangle
return \"It\'s a Triangle, with side1 = \" + side1 + \" side2 = \" + side2 + \" side3 = \"+ side3;
}
else if (isRectangle == true)
{
//Task 3a: return \"It\'s a Rectangle: \" a.
I worte the code according to the requirement.And also I wrote the c.pdf
1. I worte the code according to the requirement.And also I wrote the code to continue the program
until user enters 4 to quit the program.
If you want me to do any modifications other than these I will modify.
________________________________________
GeometricShape.java
public class GeometricShape
{
private double side1, side2, side3; //These are the variables necessary to calculat the perimeter
of a triangle
private double radius; //Radius is needed to calculate the perimeter of a circle
private double length, width; //Length and Width are needed to calculate the perimeter of a
rectangle
private boolean isTriangle = false;
private boolean isRectangle = false;
private boolean isCircle = false;
//Constructor for a triangle
public GeometricShape(double aSide1, double aSide2, double aSide3)
{
side1 = aSide1;
side2 = aSide2;
side3 = aSide3;
isTriangle = true;
}
//Task #1a - Finish writing an Overloaded Constructor for a rectangle here: (don't forget to set
isRectangle to true)
public GeometricShape(double aLength, double aWidth)
{
this.length=aLength;
this.width=aWidth;
isRectangle=true;
}
//Task #1b - Write an Overloaded Constructor for a circle here: (don't forget to set isCircle to
3. public void setRadius(double aRadius)
{
radius = aRadius;
}
public void setSide1(double aSide1)
{
side1 = aSide1;
}
public void setSide2(double aSide2)
{
side2 = aSide2;
}
public void setSide3(double aSide3)
{
side3 = aSide3;
}
public void setLength(double aLength)
{
length = aLength;
}
public void setWidth(double aWidth)
{
width = aWidth;
}
public double getPerimeter()
{
double perimeter =0;
//Calculate the perimeter of the Geometric object depending on what the object is.
//Remember the following formulas:
4. //Perimeter of a Triangle = Side1 + Side2+ Side3
//Perimeter of a Rectangle = (2 * length) + (2 * width)
//Perimeter of a Circle = 2 * Math.PI * radius
if (isTriangle == true)
{
//Complete formula for triangle perimeter
perimeter = side1 + side2 + side3;
}
else if (isRectangle == true)
{
//Task 2a: Complete formula for rectangle perimeter
perimeter= (2 * length) + (2 * width);
}
else if (isCircle == true)
{
//Task 2b: Complete formula for circle perimeter
perimeter=2 * Math.PI * radius;
}
//Continue with the rest of the else if logic here.
//change return statement to match the perimeter of the shape
return perimeter;
}
public String toString()
{
if (isTriangle == true)
{
//Return "It's a Triangle: " and concatenate all the attributes of a triangle
return "It's a Triangle, with side1 = " + side1 + " side2 = " + side2 + " side3 = "+ side3;
5. }
else if (isRectangle == true)
{
//Task 3a: return "It's a Rectangle: " and concatenate all the attributes of a rectangle
return "It's a Rectangle, with Length = "+length+" Width = "+width;
}
else if (isCircle == true)
{
//Task 3b: return "It's a Circle: " and concatenate all the attributes of a circle
return "It's a Circle , with Radius = "+radius;
}
else
return "unknown shape";
}
}
___________________________________________
GeometryDriver.java
import java.util.Scanner;
public class GeometryDriver
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
int option;
do
{
//Display the following menu
displayMenu();
option = keyboard.nextInt();
processMenuSelection(option);
System.out.println("_________________________________");
}while(option!=4);
6. }
public static void displayMenu()
{
//Display the following menu
System.out.println("Welcome to the Geometry Calculator! "
+ "In this program we will use a menu to decide what kind of shape we will create. "
+ " 1.Create and Calculate Perimeter of a Circle"
+ " 2. Create and Calculate Perimeter of a Rectangle"
+ " 3. Create and Calculate Perimeter of a Triangle"
+ " 4. Exit");
}
public static void processMenuSelection(int option)
{
//Use a switch statement to determine the option that the user selected.
//Depending on the option that the user selected, ask the user for the appropriate information
needed to create the
//a specific geometric object. Then, call the getPerimeter() method to calculate the perimeter for
the geometric object created.
//Print the object created and its perimeter.
double userInputSide1, userInputSide2, userInputSide3; //These are the variables necessary to
calculat the perimeter of a triangle
double userInputRadius; //Radius is needed to calculate the perimeter of a circle
double userInputLength, userInputWidth;
switch (option)
{
case 1:
//ask user for the radius
System.out.print("Enter radius of circle: ");
7. userInputRadius = keyboard.nextDouble();
//create a GeometricShape with the radius
GeometricShape myCircle = new GeometricShape(userInputRadius);
//put logic here to call the perimeter method
System.out.println("Perimeter of the Circle :"+myCircle.getPerimeter());
System.out.println(myCircle);
break;
case 2:
//ask user for the length and width
System.out.print("Enter length of a rectangle: ");
userInputLength = keyboard.nextDouble();
System.out.print("Enter width of a rectangle: ");
userInputWidth = keyboard.nextDouble();
//create a GeometricShape with the length and width
GeometricShape myRectangle = new GeometricShape(userInputLength, userInputWidth);
//put logic here to call the perimeter method
System.out.println("Perimeter of the Rectangle :"+myRectangle.getPerimeter());
System.out.println(myRectangle);
break;
case 3:
//Using the constructors above as an example, complete the logic for a triangle:
//Task 4a: Ask user for side1, side2, and side3
System.out.print("Enter Trangle's Side 1 : ");
userInputSide1= keyboard.nextDouble();
System.out.print(" Enter Trangle's Side 2 : ");
userInputSide2= keyboard.nextDouble();
System.out.print(" Enter Trangle's Side 3 : ");
userInputSide3= keyboard.nextDouble();
//Task 4b: Create a GeometricShape with the the 3 sides
GeometricShape myTraingle=new GeometricShape(userInputSide1, userInputSide2,
8. userInputSide3);
//Task 4c: Put logic here to call the perimeter method
System.out.println("Perimeter of the Traingle :"+myTraingle.getPerimeter());
//Task 4d: Print the triangle object
System.out.println(myTraingle);
break;
case 4:
// Say goodbye to user.
System.out.println("Good bye!");
break;
default:
//Give message to user that option is not valid.
System.out.println("Invalid option selected. Only 1 - 4 are valid.");
}
}
}
__________________________________________________
Output:
Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.
1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
3. Create and Calculate Perimeter of a Triangle
4. Exit
1
Enter radius of circle: 5.5
Perimeter of the Circle :34.55751918948772
It's a Circle , with Radius = 5.5
_________________________________
Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.
9. 1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
3. Create and Calculate Perimeter of a Triangle
4. Exit
2
Enter length of a rectangle: 5
Enter width of a rectangle: 6
Perimeter of the Rectangle :22.0
It's a Rectangle, with Length = 5.0 Width = 6.0
_________________________________
Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.
1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
3. Create and Calculate Perimeter of a Triangle
4. Exit
3
Enter Trangle's Side 1 : 4
Enter Trangle's Side 2 : 5
Enter Trangle's Side 3 : 6
Perimeter of the Traingle :15.0
It's a Triangle, with side1 = 4.0 side2 = 5.0 side3 = 6.0
_________________________________
Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.
1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
3. Create and Calculate Perimeter of a Triangle
4. Exit
4
Good bye!
_________________________________Thank You
Solution
I worte the code according to the requirement.And also I wrote the code to continue the program
10. until user enters 4 to quit the program.
If you want me to do any modifications other than these I will modify.
________________________________________
GeometricShape.java
public class GeometricShape
{
private double side1, side2, side3; //These are the variables necessary to calculat the perimeter
of a triangle
private double radius; //Radius is needed to calculate the perimeter of a circle
private double length, width; //Length and Width are needed to calculate the perimeter of a
rectangle
private boolean isTriangle = false;
private boolean isRectangle = false;
private boolean isCircle = false;
//Constructor for a triangle
public GeometricShape(double aSide1, double aSide2, double aSide3)
{
side1 = aSide1;
side2 = aSide2;
side3 = aSide3;
isTriangle = true;
}
//Task #1a - Finish writing an Overloaded Constructor for a rectangle here: (don't forget to set
isRectangle to true)
public GeometricShape(double aLength, double aWidth)
{
this.length=aLength;
this.width=aWidth;
isRectangle=true;
}
//Task #1b - Write an Overloaded Constructor for a circle here: (don't forget to set isCircle to
true)
public GeometricShape(double radius)
11. {
this.radius=radius;
isCircle=true;
}
public double getRadius()
{
return radius;
}
public double getSide1()
{
return side1;
}
public double getSide2()
{
return side2;
}
public double getSide3()
{
return side3;
}
public double getLength()
{
return length;
}
public double getWidth()
{
return width;
}
public void setRadius(double aRadius)
12. {
radius = aRadius;
}
public void setSide1(double aSide1)
{
side1 = aSide1;
}
public void setSide2(double aSide2)
{
side2 = aSide2;
}
public void setSide3(double aSide3)
{
side3 = aSide3;
}
public void setLength(double aLength)
{
length = aLength;
}
public void setWidth(double aWidth)
{
width = aWidth;
}
public double getPerimeter()
{
double perimeter =0;
//Calculate the perimeter of the Geometric object depending on what the object is.
//Remember the following formulas:
//Perimeter of a Triangle = Side1 + Side2+ Side3
//Perimeter of a Rectangle = (2 * length) + (2 * width)
13. //Perimeter of a Circle = 2 * Math.PI * radius
if (isTriangle == true)
{
//Complete formula for triangle perimeter
perimeter = side1 + side2 + side3;
}
else if (isRectangle == true)
{
//Task 2a: Complete formula for rectangle perimeter
perimeter= (2 * length) + (2 * width);
}
else if (isCircle == true)
{
//Task 2b: Complete formula for circle perimeter
perimeter=2 * Math.PI * radius;
}
//Continue with the rest of the else if logic here.
//change return statement to match the perimeter of the shape
return perimeter;
}
public String toString()
{
if (isTriangle == true)
{
//Return "It's a Triangle: " and concatenate all the attributes of a triangle
return "It's a Triangle, with side1 = " + side1 + " side2 = " + side2 + " side3 = "+ side3;
}
else if (isRectangle == true)
14. {
//Task 3a: return "It's a Rectangle: " and concatenate all the attributes of a rectangle
return "It's a Rectangle, with Length = "+length+" Width = "+width;
}
else if (isCircle == true)
{
//Task 3b: return "It's a Circle: " and concatenate all the attributes of a circle
return "It's a Circle , with Radius = "+radius;
}
else
return "unknown shape";
}
}
___________________________________________
GeometryDriver.java
import java.util.Scanner;
public class GeometryDriver
{
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
int option;
do
{
//Display the following menu
displayMenu();
option = keyboard.nextInt();
processMenuSelection(option);
System.out.println("_________________________________");
}while(option!=4);
}
15. public static void displayMenu()
{
//Display the following menu
System.out.println("Welcome to the Geometry Calculator! "
+ "In this program we will use a menu to decide what kind of shape we will create. "
+ " 1.Create and Calculate Perimeter of a Circle"
+ " 2. Create and Calculate Perimeter of a Rectangle"
+ " 3. Create and Calculate Perimeter of a Triangle"
+ " 4. Exit");
}
public static void processMenuSelection(int option)
{
//Use a switch statement to determine the option that the user selected.
//Depending on the option that the user selected, ask the user for the appropriate information
needed to create the
//a specific geometric object. Then, call the getPerimeter() method to calculate the perimeter for
the geometric object created.
//Print the object created and its perimeter.
double userInputSide1, userInputSide2, userInputSide3; //These are the variables necessary to
calculat the perimeter of a triangle
double userInputRadius; //Radius is needed to calculate the perimeter of a circle
double userInputLength, userInputWidth;
switch (option)
{
case 1:
//ask user for the radius
System.out.print("Enter radius of circle: ");
userInputRadius = keyboard.nextDouble();
16. //create a GeometricShape with the radius
GeometricShape myCircle = new GeometricShape(userInputRadius);
//put logic here to call the perimeter method
System.out.println("Perimeter of the Circle :"+myCircle.getPerimeter());
System.out.println(myCircle);
break;
case 2:
//ask user for the length and width
System.out.print("Enter length of a rectangle: ");
userInputLength = keyboard.nextDouble();
System.out.print("Enter width of a rectangle: ");
userInputWidth = keyboard.nextDouble();
//create a GeometricShape with the length and width
GeometricShape myRectangle = new GeometricShape(userInputLength, userInputWidth);
//put logic here to call the perimeter method
System.out.println("Perimeter of the Rectangle :"+myRectangle.getPerimeter());
System.out.println(myRectangle);
break;
case 3:
//Using the constructors above as an example, complete the logic for a triangle:
//Task 4a: Ask user for side1, side2, and side3
System.out.print("Enter Trangle's Side 1 : ");
userInputSide1= keyboard.nextDouble();
System.out.print(" Enter Trangle's Side 2 : ");
userInputSide2= keyboard.nextDouble();
System.out.print(" Enter Trangle's Side 3 : ");
userInputSide3= keyboard.nextDouble();
//Task 4b: Create a GeometricShape with the the 3 sides
GeometricShape myTraingle=new GeometricShape(userInputSide1, userInputSide2,
userInputSide3);
17. //Task 4c: Put logic here to call the perimeter method
System.out.println("Perimeter of the Traingle :"+myTraingle.getPerimeter());
//Task 4d: Print the triangle object
System.out.println(myTraingle);
break;
case 4:
// Say goodbye to user.
System.out.println("Good bye!");
break;
default:
//Give message to user that option is not valid.
System.out.println("Invalid option selected. Only 1 - 4 are valid.");
}
}
}
__________________________________________________
Output:
Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.
1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
3. Create and Calculate Perimeter of a Triangle
4. Exit
1
Enter radius of circle: 5.5
Perimeter of the Circle :34.55751918948772
It's a Circle , with Radius = 5.5
_________________________________
Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.
1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
18. 3. Create and Calculate Perimeter of a Triangle
4. Exit
2
Enter length of a rectangle: 5
Enter width of a rectangle: 6
Perimeter of the Rectangle :22.0
It's a Rectangle, with Length = 5.0 Width = 6.0
_________________________________
Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.
1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
3. Create and Calculate Perimeter of a Triangle
4. Exit
3
Enter Trangle's Side 1 : 4
Enter Trangle's Side 2 : 5
Enter Trangle's Side 3 : 6
Perimeter of the Traingle :15.0
It's a Triangle, with side1 = 4.0 side2 = 5.0 side3 = 6.0
_________________________________
Welcome to the Geometry Calculator!
In this program we will use a menu to decide what kind of shape we will create.
1.Create and Calculate Perimeter of a Circle
2. Create and Calculate Perimeter of a Rectangle
3. Create and Calculate Perimeter of a Triangle
4. Exit
4
Good bye!
_________________________________Thank You