-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCh10Ex1thru4.java
More file actions
166 lines (136 loc) · 4.31 KB
/
Ch10Ex1thru4.java
File metadata and controls
166 lines (136 loc) · 4.31 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
165
166
import java.util.Scanner;
/**
TODO Write a one-sentence summary of your class here.
TODO Follow it with additional details about its purpose, what abstraction
it represents, and how to use it.
@author TODO Your Name
@version TODO the date
@author Period - TODO Your Period
@author Assignment - Ch10_IO
@author Sources - TODO list collaborators
*/
public class Ch10Ex1thru4
{
Scanner scan;
/**
* Constructs a Scanner for input from the console.
*/
public Ch10Ex1thru4()
{
scan = new Scanner( System.in );
}
/**
* Constructs a Scanner to evaluate passed input (used for testing).
* @param str input for the various methods
*/
public Ch10Ex1thru4( String str )
{
scan = new Scanner( str );
}
/**
TODO Description
*/
public void areaOfACircle()
{
System.out.print("Input the radius: ");
int radius = scan.nextInt();
double area = Math.PI * Math.pow(radius, 2.0);
System.out.println("The radius is: "+ radius + " The area is: " + area);
}
/**
TODO Description
*/
public void centsToDollars()
{
System.out.println("Input the cents: ");
int cents = scan.nextInt();
int dollars = cents / 100;
cents = cents % 100;
System.out.println("That is " + dollars+ " dollars and " + cents + "cents.");
}
/**
TODO Description
*/
public void correctChange()
{
int cents = scan.nextInt();
int dollar = cents / 100;
cents = cents - 100*dollar;
int quarters= cents / 25;
cents = cents - 25*quarters;
int dimes= cents / 10;
cents = cents - 10*dimes;
int nickels = cents / 5;
cents = cents - 5*nickels;
System.out.println("Your change is: "+dollar+" dollar, "+quarters + " quarters, " + dimes + " dimes, " + nickels + " nickels, and" + cents + " cents");
}
/**
TODO Description
*/
public void ohmsLaw()
{
System.out.println("Input voltage: ");
int voltage= scan.nextInt();
double amps;
System.out.println("Input resistance: ");
int resistance= scan.nextInt();
amps = (voltage + 0.0) / resistance;
System.out.println(amps);
}
/**
Testing method: instantiates a Lesson10Exercises object and then invokes
each method.
@param args command-line parameters (not used)
*/
public static void main( String[] args )
{
Scanner kbd = new Scanner( System.in );
boolean done = false;
Ch10Ex1thru4 exercise = new Ch10Ex1thru4();
do
{
System.out.println();
System.out.println();
System.out.println( "Make a selection" );
System.out.println();
System.out.println( " (1) Area of a Circle" );
System.out.println( " (2) Cents to Dollars" );
System.out.println( " (3) Correct Change" );
System.out.println( " (4) Ohm's Law" );
System.out.println( " (Q) Quit" );
System.out.println();
System.out.print( "Enter a choice: " );
String response = kbd.nextLine();
if ( response.length() > 0 )
{
System.out.println();
switch ( response.charAt( 0 ) )
{
case '1':
exercise.areaOfACircle();
break;
case '2':
exercise.centsToDollars();
break;
case '3':
exercise.correctChange();
break;
case '4':
exercise.ohmsLaw();
break;
default:
if ( response.toLowerCase().charAt( 0 ) == 'q' )
{
done = true;
}
else
{
System.out.print( "Invalid Choice" );
}
break;
}
}
} while ( !done );
System.out.println( "Goodbye!" );
}
}