I need to complete the following lab in Java: In this lab, you will receive a sk
ID: 3756342 • Letter: I
Question
I need to complete the following lab in Java:
In this lab, you will receive a skeleton of the domain class, GeometricShape, and the driver class, GeometryDriver.
In the GeometryDriver class, you will be given an empty main method, and you will be asked to code the following within it:
1.) Define local variables to hold the following data:
userChoice (from a menu of options: 1 = Circle Perimeter; 2 = Rectangle Perim.; 3 = Triangle Perim.)
radius
side1, side2, and side3
length, width
perimeter
keyboard (a Scanner object)
2.) 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");
3.) Get the user’s input;
a default geometric shape is created before the switch to allow for a single print afterwards.
4.) Use a switch statement to evaluate what the user entered (1, 2 or 3)
a. Obtain the remaining data from the user, and re-create the appropriate geometric object
b. The number of arguments that are passed to the constructor determines which constructor will be executed (1 argument = circle’s constructor, 2 arguments= rectangle’s constructor,
3 arguments = triangle’s constructor).
5. Call the calculatePerimeter() method
6. Print the contents of the geometric object and print its perimeter
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author
*/
public class GeometryDriver
{
public static void main(String[] args)
{
//In main, define local variables to hold the following data:
// 1.) userChoice (from a menu of options, 1 = Circle Perimeter; 2 = Rectangle Perimeter; 3 = Triangle Perimeter
// 2.) radius
// 3.) side1, side2, and side3
// 4.) length, width
// 5.) perimeter
// 6.) keyboard (a Scanner object)
Scanner keyboard = new Scanner(System.in);
int option;
//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");
//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.
option = keyboard.nextInt();
//The code below is a default shape, so only 1 sout statement
//is needed at the end of the switch statement.
GeometricShape aShape = new GeometricShape(-1, -1);
switch (option)
{
case 1:
//ask user for the radius
//re-create a GeometricShape with the radius
//put logic here to call the perimeter method
break;
case 2:
//ask user for the length and width
//re-create a GeometricShape with the length and width
//put logic here to call the perimeter method
break;
case 3:
//ask user for side1, side2, and side3
//re-create a GeometricShape with the the 3 sides
//put logic here to call the perimeter method
break;
default:
//Give message to user that option is not valid.
}
//Print the geometric shape and its perimeter
}
}
Explanation / Answer
------------------------------------------------------GeometryDriver.java-------------------------------------------------------
import java.util.Scanner;
/**
*
* @author
*/
public class GeometryDriver{
public static void main(String[] args){
//In main, define local variables to hold the following data:
int radius;
int side1,side2,side3;
int length,width;
double perimeter;
// a Scanner object
Scanner keyboard = new Scanner(System.in);
int option;
//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");
option = keyboard.nextInt();
switch (option)
{
case 1:
System.out.println("Please enter radius of circle : ");
radius=keyboard.nextInt();
GeometricShape circle = new GeometricShape(radius);
perimeter = circle.calculatePerimeter(radius);
System.out.println("Perimeter of a Circle : "+perimeter);
break;
case 2:
System.out.println("Please enter length and width of Rectangle : ");
length=keyboard.nextInt();
width=keyboard.nextInt();
GeometricShape rectangle = new GeometricShape(length,width);
perimeter = rectangle.calculatePerimeter(length,width);
System.out.println("Perimeter of a Rectangle : "+perimeter);
break;
case 3:
System.out.println("Please enter side1, side2 and side3 of Triangle : ");
side1=keyboard.nextInt();
side2=keyboard.nextInt();
side3=keyboard.nextInt();
GeometricShape triangle = new GeometricShape(side1,side2,side3);
perimeter = triangle.calculatePerimeter(side1,side2,side3);
System.out.println("Perimeter of a Triangle : "+perimeter);
break;
default:
System.out.println("Please select a valid option");
}
keyboard.close(); //closing the Scanner object
}
}
---------------------------------------------------------GeometricShape.java------------------------------------------
/*
* In this class constructor overloading and method overloading is used
* for getting and calculating the perimeter of different shapes.
*/
public class GeometricShape {
int radius;
int side1,side2,side3;
int length,width;
//constructor for circle
public GeometricShape(int radius) {
this.radius = radius;
}
//constructor for rectangle
public GeometricShape(int length, int width) {
this.length = length;
this.width = width;
}
//constructor for triangle
public GeometricShape(int side1, int side2, int side3) {
this.side1 = side1;
this.side2 = side2;
this.side3 = side3;
}
//calculating perimeter of circle
public double calculatePerimeter(int radius) {
return (2*3.14*radius); // perimeter of a circle 2*PI*r
}
//calculating perimeter of rectangle
public double calculatePerimeter(int length, int width) {
return 2*(length+width); // perimeter of a Rectangle 2*(l+w)
}
//calculating perimeter of triangle
public double calculatePerimeter(int side1, int side2, int side3) {
return (side1+side2+side3); // perimeter of a Triangle side1+side2+side3
}
}
----------------------------------------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
1
Please enter radius of circle :
10
Perimeter of a Circle : 62.800000000000004
*********************************************************************************************************************
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
2
Please enter length and width of Rectangle :
10 10
Perimeter of a Rectangle : 40.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
3
Please enter side1, side2 and side3 of Triangle :
10 10 10
Perimeter of a Triangle : 30.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
Please select a valid option
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.