This is a geometry calculator
Choose what you would like to calculate
Task #2 Value-Returning Methods
Task #3 Calling Methods
Task #4 Java Documentation
They should include:
CODE IS BELOW
import java.util.Scanner;
/**
This program demonstrates static methods
*/
public class Geometry
{
public static void main(String[] args)
{
int choice; // The user's choice
double value = 0; // The method's return value
char letter; // The user's Y or N decision
double radius; // The radius of the circle
double length; // The length of the rectangle
double width; // The width of the rectangle
double height; // The height of the triangle
double base; // The base of the triangle
double side1; // The first side of the triangle
double side2; // The second side of the triangle
double side3; // The third side of the triangle
// Create a scanner object to read from the keyboard
Scanner keyboard = new Scanner(System.in);
// The do loop allows the menu to be displayed first
do
{
// TASK #1 Call the printMenu method
choice = keyboard.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter the radius of " +
"the circle: ");
radius = keyboard.nextDouble();
// TASK #3 Call the circleArea method and
// store the result in the value variable
System.out.println("The area of the " +
"circle is " + value);
break;
case 2:
System.out.print("Enter the length of " +
"the rectangle: ");
length = keyboard.nextDouble();
System.out.print("Enter the width of " +
"the rectangle: ");
width = keyboard.nextDouble();
// TASK #3 Call the rectangleArea method and
// store the result in the value variable
System.out.println("The area of the " +
"rectangle is " + value);
break;
case 3:
System.out.print("Enter the height of " +
"the triangle: ");
height = keyboard.nextDouble();
System.out.print("Enter the base of " +
"the triangle: ");
base = keyboard.nextDouble();
// TASK #3 Call the triangleArea method and
// store the result in the value variable
System.out.println("The area of the " +
"triangle is " + value);
break;
case 4:
System.out.print("Enter the radius of " +
"the circle: ");
radius = keyboard.nextDouble();
// TASK #3 Call the circumference method and
// store the result in the value variable
System.out.println("The circumference " +
"of the circle is " +
value);
break;
case 5:
System.out.print("Enter the length of " +
"the rectangle: ");
length = keyboard.nextDouble();
System.out.print("Enter the width of " +
"the rectangle: ");
width = keyboard.nextDouble();
// TASK #3 Call the perimeter method and
// store the result in the value variable
System.out.println("The perimeter of " +
"the rectangle is " +
value);
break;
case 6:
System.out.print("Enter the length of " +
"side 1 of the " +
"triangle: ");
side1 = keyboard.nextDouble();
System.out.print("Enter the length of " +
"side 2 of the " +
"triangle: ");
side2 = keyboard.nextDouble();
System.out.print("Enter the length of " +
"side 3 of the " +
"triangle: ");
side3 = keyboard.nextDouble();
// TASK #3 Call the perimeter method and
// store the result in the value variable
System.out.println("The perimeter of " +
"the triangle is " +
value);
break;
default:
System.out.println("You did not enter " +
"a valid choice.");
}
keyboard.nextLine(); // Consume the new line
System.out.println("Do you want to exit " +
"the program (Y/N)?: ");
String answer = keyboard.nextLine();
letter = answer.charAt(0);
} while(letter != 'Y' && letter != 'y');
}
// TASK #1 Create the printMenu method here
// TASK #2 Create the value-returning methods here
// TASK #4 Write javadoc comments for each method
}
Hi,
In the below program we implement the geometry calculator by displaying menu to user to select which operation to perform. By getting user choice we are performing the related operation. Operations we are performing are area of circle, area of rectangle, area of triangle, circumference of circle, perimeter of rectangle, perimeter of triangle. For each operation we are defining separate static methods. we also included sample test data at the end of program for reference.
Source Code:
import java.util.Scanner;
/**
This program demonstrates static methods
*/
public class Geometry
{
public static void main(String[] args)
{
int choice; // The user's
choice
double value = 0; // The method's
return value
char letter; // The user's Y or N
decision
double radius; // The radius of the
circle
double length; // The length of the
rectangle
double width; // The width of the
rectangle
double height; // The height of the
triangle
double base; // The base of the
triangle
double side1; // The first side of
the triangle
double side2; // The second side of
the triangle
double side3; // The third side of
the triangle
// Create a scanner object to read
from the keyboard
Scanner keyboard = new
Scanner(System.in);
// The do loop allows the menu to
be displayed first
do
{
// TASK #1 Call the printMenu
method
printMenu();
choice =
keyboard.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter the
radius of " +
"the circle: ");
radius =
keyboard.nextDouble();
// TASK #3 Call the
circleArea method and
// store the result in the
value variable
value =
circleArea(radius);
System.out.println("The area
of the " +
"circle is " + value);
break;
case 2:
System.out.print("Enter the
length of " +
"the rectangle: ");
length =
keyboard.nextDouble();
System.out.print("Enter the
width of " +
"the rectangle: ");
width =
keyboard.nextDouble();
// TASK #3 Call the
rectangleArea method and
// store the result in the
value variable
value = rectangleArea(length,
width);
System.out.println("The area
of the " +
"rectangle is " +
value);
break;
case 3:
System.out.print("Enter the
height of " +
"the triangle: ");
height =
keyboard.nextDouble();
System.out.print("Enter the
base of " +
"the triangle: ");
base =
keyboard.nextDouble();
// TASK #3 Call the
triangleArea method and
// store the result in the
value variable
value = triangleArea(base,
height);
System.out.println("The area
of the " +
"triangle is " +
value);
break;
case 4:
System.out.print("Enter the
radius of " +
"the circle: ");
radius =
keyboard.nextDouble();
// TASK #3 Call the
circumference method and
// store the result in the
value variable
value =
circleCircumference(radius);
System.out.println("The
circumference " +
"of the circle is " +
value);
break;
case 5:
System.out.print("Enter the
length of " +
"the rectangle: ");
length =
keyboard.nextDouble();
System.out.print("Enter the
width of " +
"the rectangle: ");
width =
keyboard.nextDouble();
// TASK #3 Call the perimeter
method and
// store the result in the
value variable
value =
rectanglePerimeter(length, width);
System.out.println("The
perimeter of " +
"the rectangle is " +
value);
break;
case 6:
System.out.print("Enter the
length of " +
"side 1 of the " +
"triangle: ");
side1 =
keyboard.nextDouble();
System.out.print("Enter the
length of " +
"side 2 of the " +
"triangle: ");
side2 =
keyboard.nextDouble();
System.out.print("Enter the
length of " +
"side 3 of the " +
"triangle: ");
side3 =
keyboard.nextDouble();
// TASK #3 Call the perimeter
method and
// store the result in the
value variable
value =
trianglePerimeter(side1, side2, side3);
System.out.println("The
perimeter of " +
"the triangle is " +
value);
break;
default:
System.out.println("You did
not enter " +
"a valid choice.");
}
keyboard.nextLine(); // Consume the new line
System.out.println("Do you want to exit " +
"the program
(Y/N)?: ");
String answer =
keyboard.nextLine();
letter =
answer.charAt(0);
} while(letter != 'Y' &&
letter != 'y');
}
// TASK #1 Create the printMenu method here
/**
* This is the menu method which displays menu items to user to
select.
* @param Nothing.
* @return Nothing.
*/
public static void printMenu(){
System.out.println("\nThis is a
geometry calculator");
System.out.println("Choose what you
would like to calculate");
System.out.println("1 for Find the
area of a circle\n2 for Find the area of a rectangle\n3 for Find
the area of a triangle\n4 for Find the circumference of a circle\n5
for Find the perimeter of a rectangle\n6 for Find the perimeter of
a triangle\n");
System.out.println("Enter the
number of your choice : ");
}
// TASK #2 Create the value-returning methods here
/**
* This is the circleArea method which calculates the area of a
circle by taking radius of circle from user.
* @param radius radius of the circle.
* @return area of a circle by pi * radius * radius.
*/
public static double circleArea(double radius){
return Math.PI * radius *
radius;
}
/**
* This is the rectangleArea method which calculates the area of a
rectangle by taking length and width of rectangle from user.
* @param length length of the rectangle.
* @param width width of the rectangle.
* @return area of a rectangle by multiplying length and
height.
*/
public static double rectangleArea(double length,
double width){
return length * width;
}
/**
* This is the triangleArea method which calculates the area of a
triangle by taking base and height of triangle from user.
* @param base base of the triangle.
* @param height height of the triangle.
* @return area of a triangle by multiplying base*height dividedby
2.
*/
public static double triangleArea(double base, double
height){
return (base*height)/2;
}
/**
* This is the circleCircumference method which calculates the
circumference of a circle by taking radius of circle from
user.
* @param radius radius of the circle.
* @return Circumference of a circle by multiplying 2 * pi *
radius.
*/
public static double circleCircumference(double
radius){
return 2 * Math.PI * radius;
}
/**
* This is the rectanglePerimeter method which calculates the
perimeter of a rectangle by taking length and width of
rectangle.
* @param length length of the rectangle.
* @param width width of the rectangle.
* @return perimeter of a rectanglr by multiplying length * 2 +
width * 2.
*/
public static double rectanglePerimeter(double length,
double width){
return 2 * length + 2 *
width;
}
/**
* This is the trianglePerimeter method which calculates the
perimeter of a triangle by taking 3 sides length of triangle.
* @param side1,side2,side3 side lengths of the rectangle.
* @return perimeter of a triangle by adding 3 sides of
triangle.
*/
public static double trianglePerimeter(double side1,
double side2, double side3){
return side1 + side2 + side3;
}
/**
Area of circle : radius - 4 (area -
50.26548245743669)
Area of rectangle : length - 5, width - 5 (area -
25.0)
Area of triangle : base - 5, height - 12 (area -
30.0)
circleCircumference: radius - 4, (circumference -
25.132741228718345)
rectanglePerimeter: length - 5, width - 5, (perimeter
- 20.0)
trianglePerimeter: side1 - 5, side2 - 6, side3 - 10,
(perimeter - 21)
*/
}
Output:
Compile : javac Geometry.java
Run : java Geometry
This is a geometry calculator
Choose what you would like to calculate
1 for Find the area of a circle
2 for Find the area of a rectangle
3 for Find the area of a triangle
4 for Find the circumference of a circle
5 for Find the perimeter of a rectangle
6 for Find the perimeter of a triangle
Enter the number of your choice :
1
Enter the radius of the circle: 4
The area of the circle is 50.26548245743669
Do you want to exit the program (Y/N)?:
n
This is a geometry calculator
Choose what you would like to calculate
1 for Find the area of a circle
2 for Find the area of a rectangle
3 for Find the area of a triangle
4 for Find the circumference of a circle
5 for Find the perimeter of a rectangle
6 for Find the perimeter of a triangle
Enter the number of your choice :
2
Enter the length of the rectangle: 5
Enter the width of the rectangle: 5
The area of the rectangle is 25.0
Do you want to exit the program (Y/N)?:
n
This is a geometry calculator
Choose what you would like to calculate
1 for Find the area of a circle
2 for Find the area of a rectangle
3 for Find the area of a triangle
4 for Find the circumference of a circle
5 for Find the perimeter of a rectangle
6 for Find the perimeter of a triangle
Enter the number of your choice :
3
Enter the height of the triangle: 12
Enter the base of the triangle: 5
The area of the triangle is 30.0
Do you want to exit the program (Y/N)?:
n
This is a geometry calculator
Choose what you would like to calculate
1 for Find the area of a circle
2 for Find the area of a rectangle
3 for Find the area of a triangle
4 for Find the circumference of a circle
5 for Find the perimeter of a rectangle
6 for Find the perimeter of a triangle
Enter the number of your choice :
4
Enter the radius of the circle: 4
The circumference of the circle is 25.132741228718345
Do you want to exit the program (Y/N)?:
n
This is a geometry calculator
Choose what you would like to calculate
1 for Find the area of a circle
2 for Find the area of a rectangle
3 for Find the area of a triangle
4 for Find the circumference of a circle
5 for Find the perimeter of a rectangle
6 for Find the perimeter of a triangle
Enter the number of your choice :
5
Enter the length of the rectangle: 5
Enter the width of the rectangle: 5
The perimeter of the rectangle is 20.0
Do you want to exit the program (Y/N)?:
y
JAVA Code Requried Copy the file java included below. This program will compile, but, when you...
I am trying to write a Geometry.java program but Dr.Java is giving me errors and I dont know what I am doing wrong. import java.util.Scanner; /** This program demonstrates static methods */ public class Geometry { public static void main(String[] args) { int choice; // The user's choice double value = 0; // The method's return value char letter; // The user's Y or N decision double radius; // The radius of the circle double length; // The length of...
Project Objectives: To develop ability to write void and value returning methods and to call them -- Introduction: Methods are commonly used to break a problem down into small manageable pieces. A large task can be broken down into smaller tasks (methods) that contain the details of how to complete that small task. The larger problem is then solved by implementing the smaller tasks (calling the methods) in the correct order. This also allows for efficiencies, since the method can...
Please, modify the code below to use the Switch Statements in Java instead of “if” statements to make the decisions. import java.util.Scanner; public class Area { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter code(C for circle, R for rectangle, S for square): "); char code = in.next().charAt(0); if(code == 'C') { System.out.print("Enter radius: "); double radius = in.nextDouble(); System.out.println("Area of circle is " + (Math.PI * radius * radius)); } else if(code == 'R') {...
java language
Problem 3: Shape (10 points) (Software Design) Create an abstract class that represents a Shape and contains abstract method: area and perimeter. Create concrete classes: Circle, Rectangle, Triangle which extend the Shape class and implements the abstract methods. A circle has a radius, a rectangle has width and height, and a triangle has three sides. Software Architecture: The Shape class is the abstract super class and must be instantiated by one of its concrete subclasses: Circle, Rectangle, or...
In Java Exercise #2: (Java) Design and implement a program (name it ComputeAreas) that defines four methods as follows: Method squareArea (double side) returns the area of a square. Method rectangleArea (double width, double length) returns the area of a rectangle. Method circleArea (double radius) returns the area of a circle. Method triangleArea (double base, double height) returns the area of a triangle. In the main method, test all methods with different input value read from the user. Document your...
write a completed program (included the main) for the Q: add an
equals method to each of the Rectangle circle and triangle classes
introduced in this chapter. two shapes are considered equal if
their fields have equivalent values. based on
public class Circle implements Shape f private double radius; // Constructs a new circle with the given radius. public Circle (double radius) f this.radius - radius; // Returns the area of this circle. public double getArea) return Math.PI *radius *...
I Need UML Design for the following Java code: Colorable interface: public interface Colorable { //a void method named howToColor(). void howToColor(); } GeometricObject class: public class GeometricObject { } Sqaure class: public class Square extends GeometricObject implements Colorable{ //side variable of Square double side; //Implementing howToColor() @Override public void howToColor() { System.out.println("Color all four sides."); } //setter and getter methods for Square public void setSide(double side) { this.side = side; } public double getSide() { return...
I have this program: It passes on certain test cases. The Demo five has the %.1f to cut down the number to one decimal place. But I get a failed attempt such as: Enter length: \n Enter width: \n You entered: 87.3, 2.0, and 174.7\n -- Rectangle info --\n Length: 87.34\n Width: 2.0\n Area: 174.68\n And I know it is because the length and area both have 2 decimal places. Could you help me fix this? Thank you. Program Content:...
Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....
The statement in the following program is in the incorrect order. Rearrange the statements so that they prompt the user to input the shape type (rectangle, circle, or cylinder) and the appropriate dimension of the shape. The program then outputs the following information about the shape: For a rectangle, it outputs the area and perimeter, for a circle, it outputs the area and circumference; and for a cylinder, it output the volume and surface area. After rearranging the statements, your...