|
Spring Semester 2005
|
First I want to be sure everybody knows where the solutions for Homework One can be found.
I would first name the string so I can refer to it later:"10:45"
So now, fromString a; a = "10:45"
a, I would be able to extract:
and something similar would give me (as a substring) the number of minutes.String hour; hour = a.substring(0, 2)
Note that hour is not a number. It is a string representing a number.
To obtain numbers from strings I need to use a parsing routine.
Thus, for example, I could get the integer number 10 (from "10")
and in the same fashion we would be able to obtainint h; h = Integer.parseInt(hour)
45 (from "45")
So we should count now on having defined and initialized h and m,
two int variables.
Now we need to simulate ten minutes.
Essentially this means:
After eachm = m + 1; m = m + 1; m = m + 1; ... m = m + 1; // ten times
m = m + 1; we need to ask if m has reached
60 or not.
If m has reached 60 we need to reset it to 0 and
add one to the counter of hours.
So each increase in m might generate an increase in h as well.
That's why we also need to detect when h becomes 24.
When that happens we should simply reset the counter for hours (make it zero).
Notice how keeping track of the minutes and hours is similar to keeping score in Problems Four and Five.
So now the structure of your program could be:
(a) define and initializeOne final note:h,m;
(b)m = m + 1;
(c) ifmreaches one hour do two things:(d) print the current time of the clock
- reset
mand incrementhby1- reset
hifhis now24
(e) repeat steps (b), (c) and (d) ten times.
AssumeOther approaches are possible, too. Hope these comments are useful.int h = 3, m = 8; // 3:08am, basicallyHow do you print
03:08out ofhandm?Printing
h + ":" + mwon't work (it gives3:8, lacking leading zeros).So I'd do this to add leading zeros:
It's what we did when we padded with blanks and zeroes above and in Homework One.String a = "00" + h; System.out.println(a.substring(a.length() - 2));
(0.3 times 200 is 30 percent of the total number of calories).
Thus
There are two such examples below.
String and give it a name.
Suppose we're working on this input value:String a; a = console.readLine();
Then what the problem wants me to do is to calculatea = "123";
One can extract the individual characters in two ways:1 + 2 + 3
which evaluates to an integer but not thea.charAt(0) + a.charAt(1) + a.charAt(2)
6 we were hoping for. Or we could extract substrings:
which evaluates to a string just like the one we started from.a.substring(0, 1) + a.substring(1, 2) + a.substring(2, 3)
Note that
evaluates toa.charAt(0) - '0'
1 (as needed) and
also evaluates to theInteger.parseInt(a.substring(0, 1))
1 that we need. So using this method throughout you can get the individual values of the constituent digits.
So now the only problem is: what if the string is not 10 characters (digits) long.
0 (zeroes) like in Homework One.
"123" with 0 (zeroes)? I carefully define
which is the largest string ofString reserve = "0000000000";
0's I will need in this problem.
a and I determine its a.length() to be 3.
reserve.substring(0, 7) and add it to a.
7 is 10 - a.length().
a of "0000000123" I can refer to its digits one by one. I only need to use one of the methods described before, to sum up its digits and report the sum.
Determining the percent used in calculating the taxes resembles the conversion in Problem Ten.
So let's look at that problem now. (A flowchart is essential at this stage).
So now if one gives us a value of
A( 4.0)A-( 3.7)B+( 3.3)B( 3.0)B-( 2.7)C+( 2.3)C( 2.0)C-( 1.7)D+( 1.3)D( 1.0)D-( 0.3)F( 0.0)
3.9 what letter grade is closer to it? Moreover, how would we go about systematically determining a letter for a score?
A flow chart is what's needed here.
Let's look at an example for the time being:
2.85
A?
3.85
3.85 for an A? Can you think of a good reason?
3.85 is equally distant from 4.0 (A) and 3.7 (A-).
!(2.85 >= 3.85) so the letter won't be an A.
A-?
A- we'd need to have at least a 3.5 average.
3.5 you'd be closer to B+).
!(2.85 >= 3.5) so we need to keep going (not an A-).
!(2.85 >= 3.15) so we need to keep going (not a B+ either).
(2.85 >= 2.85) so the grade is a B
Truncating or rounding should be easy, just like in Homework One.
Here's how we simulate a pair of dice:
In your problem you need to generate random integers betweenint d1, d2; d1 = (int)(Math.random() * 6); d2 = (int)(Math.random() * 6);
-50 and 50. So the program behaves like this:
(a) set up some useful variables:So this is not a difficult problem. Let's move on.correct,total,n1,n2
(b) initializecorrectandtotalwith0(c) generate a new value for
n1
(d) generate a new value forn2
(e) ask the question (what'sn1plusn2?)
(f) read the answer (where, in what variable?)
(g) (in situations like these we realize that we need to define a variableansweras well).
(h) if the answer is correct increment the variablecorrect
(i) increment the variabletotal(j) repeat steps (c)-(i) ten times.
(Report
correctandtotalevery time).
What doesn % 19give us for any positiveint n;?
user has the user's score and comp has the one for the computer.
if (user has chosen paper AND computer has chosen rock OR
user has chosen rock AND computer has chosen scissors OR
user has chosen scissors AND computer has chosen paper ) {
// computer has lost, user has won
increment user by one
} else { // user has lost (computer won) or computer and user tied
if (user has chosen rock AND computer has chosen paper OR
user has chosen scissors AND computer has chosen rock OR
user has chosen paper AND computer has chosen scissors ) {
// computer has won, user has lost
increment comp by one
} else { // tie
// nothing happens
}
}
I would use integer values of
0 for paper,
1 for rock, and
2 for scissors
This would make it easier for us to generate random choices for the computer.
Of course, when reporting we should convert to strings (using the actual names).
Mon Feb 7 13:37:08 EST 2005