forked from Denisolt/CSCI-160
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab5.java
More file actions
164 lines (138 loc) · 5.22 KB
/
Lab5.java
File metadata and controls
164 lines (138 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.util.Scanner;
/**
Denisolt Shakhbulatov
10/13/2015
*/
public class Lab5
{
public static void printMenu()
{
System.out.println("This is a geometry calculator");
System.out.println("Choose what you would like to calculate");
System.out.println("1. Find the area of a circle");
System.out.println("2. Find the area of a ractangle");
System.out.println("3. Find the area of a triangle");
System.out.println("4. Find the circumference of a circle");
System.out.println("5. Find the perimeter of a rectangle");
System.out.println("6. Find the perimeter of a triangle");
System.out.print(" Enter the number of your choce: ");
}
public static void main (String [] args)
{
int choice; //the user's choice
double value = 0; //the value returned from the method
char letter; //the Y or N from the user's decision to exit
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
Scanner keyboard = new Scanner(System.in);
do
{
printMenu();
choice = keyboard.nextInt();
if (choice==1)
{
radius = getPositive();
//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);
}
else if (choice==2)
{
length = getPositive();
width = getPositive();
//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);
}
else if (choice==3)
{
height = getPositive();
base = getPositive();
//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);
}
else if (choice==4)
{
radius = getPositive();
//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);
}
else if (choice==5)
{
length = getPositive();
width = getPositive();
//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);
}
else if (choice==6)
{
side1 = getPositive();
side2 = getPositive();
side3 = getPositive();
//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);
}
else
{
System.out.println("You did not enter a valid choice.");
}
keyboard.nextLine(); //consumes the new line character after the number
System.out.println("Do you want to exit the program (Y/N)?: ");
String answer = keyboard.nextLine();
letter = answer.charAt(0);
}
while (letter != 'N' && letter != 'n');
}
public static double circleArea(double radius)
{
double value = (Math.PI*(radius*radius));
return value;
}
public static double rectangleArea(double length, double width)
{
double value = length*width;
return value;
}
public static double triangleArea(double base, double height)
{
double value = ((height*base)/2);
return value;
}
public static double CircleCircumference(double radius)
{
double value = (Math.PI*2*radius);
return value;
}
public static double rectanglePerimeter(double length, double width)
{
double value = ((2*length)+(2*width));
return value;
}
public static double trianglePerimeter(double side1, double side2, double side3)
{
double value = (side1+side2+side3);
return value;
}
public static double getPositive()
{
Scanner kb = new Scanner(System.in);
double num;
do
{
System.out.print("Enter a positive value: ");
num = kb.nextDouble();
}
while(num<0);
return num;
}
}