Quotient Remainder 1878 divided by 8 --> 234 6 234 divided by 8 --> 29 2 29 divided by 8 --> 3 5 3 divided by 8 --> 0 3The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes last). In this example, the base 8 answer is 3526 (that is 1878

- The program will only work correctly for base 10 numbers that fit in
4 digits in the new base. We know that in base 2 the maximum unsigned integer
that will fit in 4 bits is 1111
_{2}which equals 15 in base 10 (or 2^{4}- 1). In base 8, the maximum number is 7777_{8}which equals 4095 in base 10 (or 8^{4}- 1). In general, the maximum base 10 number that fits in 4 base b digits is b^{4}- 1. Add an assignment statement to the program to compute this value for the base that is input and assign it to the variable*maxNumber*. Add a statement that prints out the result (appropriately labeled). Compile and run the program to make sure it is correct so far. - Now it is time to add the code to do the conversion.
The comments below guide you through the calculations --
replace them with the appropriate Java statements. Note that you will need
to declare a new variable to hold the quotient.
// First compute place0 -- the units place. Remember this comes // from the first division so it is the remainder when the // base 10 number is divided by the base (HINT %). // Then compute the quotient (integer division / will do it!) and // store the result into the quotient variable. // Now compute place1 -- this is the remainder when the quotient // from the preceding step is divided by the base. // Then compute the new quotient and store the result into // the quotient variable. // Repeat the idea from above to compute place2 and the next quotient // Repeat again to compute place3

- So far the program does not print out the answer. Recall that the
answer is the sequence of remainders written in reverse order -- note that this
requires concatenating the four digits that have been computed. Since they
are each integers, if we just add them the computer will perform arithmetic
instead of concatenation. Instead, we can use a variable of type String
and then use the + operator to do string concatenation. (Remember that if
either parameter to + is a string, it does concatenation instead of addition.)
Near the top of the program a variable named
*baseBNum*has been declared as an object of type String and initialized to an empty string. Add statements to the program to concatenate the digits in the new base to*baseBNum*and then print the answer. Compile and run your program. Test it using the following values: Enter 2 for the base and 13 for the base 10 number -- the program should print 1101 as the base 2 value; enter 8 for the base and 1878 for the number -- the program should print 3526 for the base 8 value; enter 3 for the base and 50 for the number -- the program should print 1212. - Don't print your program yet! You are going to add something to it later.

Color myColor = new Color(255, 0, 255);The statement

- Change the constructor so the color code is (0,0,0) --- absence of color. What color should this be? Run the program to check.
- Try a few other combinations of color codes to see what you get. Page 793 of the text shows you the codes for the pre-defined colors in the Color class.
- Now we will modify the program to generate random colors.
Notice on page 793 of the text that there is a constructor for the Color
class that takes a single integer as an argument. The first 8 bits
of this integer are ignored while the last 24 bits define the color --
8 bits for red, 8 for green, and the last 8 bits for blue. Hence,
the bit pattern
00000000000000001111111100000000

should represent pure green. Its base 10 value is 65280. Change the declaration of the*myColor*object toColor myColor = new Color (65280);

Compile and run the program. Do you see green? - Now add the following statements to the program:
- declare
*generator*to be an object of type Random (the import statement is already there); - declare
*colorCode*to be a variable of type int; - assign
*colorCode*a random integer value (use the*nextInt()*method); - replace the number 65280 in the Color constructor above with the
variable
*colorCode*

- declare
- The Color class has methods that return the individual color codes (for
red, green, and blue) for a Color object. For example,
redCode = myColor.getRed();

returns the code for the red component of the myColor object (redCode should be a variable of type int). The methods that return the green and blue components are*getGreen*and*getBlue*, respectively. Add statements to the program, similar to the above, to get the three color codes (you need to declare some variables). Then add statements such aspage.drawString("Red: " + redCode, ____ , ____ );

to label the rectangle with the three color codes (fill in the blanks with appropriate coordinates so each string is drawn inside the rectangle -- you also need to set the drawing color to something such as black so the strings will show up). Compile and run the program to make sure it works. Reload several times to see the different colors and their corresponding codes displayed. - Print the final version of your applet.

if (grade >= 90) System.out.println ("Congrats! You made an A!");In this example the condition is

if (numStudents != 0) testAverage = sumOfGrades / numStudents;In this example, the condition is

An *if ... else ... * statement is used when you want to
do one thing when a condition is true but something else when
it is false. Some examples are

toss = Math.abs (generator.nextInt()) % 2; if (toss == 0) System.out.println ("Tails"); else System.out.println ("Heads");In this example the condition is

if (numStudents > 0) testAverage = sumOfGrades / numStudents; else System.out.println ("You must have at least one student!!");

The file WaterBill.java contains a program to compute a customer's water and sewer bill. The charge for water is based on consumption -- a customer who uses no more than 7500 gallons in a month pays $0.002 per gallon but a customer who uses more pays $0.002 per gallon for the first 7500 gallons plus $0.0035 per gallon for each gallon over 7500. Note that the charge depends on a condition -- whether or not the amount of water used is over 7500 gallons. Hence an if is needed to compute the charge for the water. Do the following:

- Save the file to your directory and open it in emacs.
- Study the code and find the
*if... else...*that computes the charge for the water -- there are two different formulas used for the calculation depending on whether*numGals*is less than or equal to CUTOFF (7500) or not. Also notice near the bottom of the program an*if*that prints a message for customers who use less than half of 7500. - Run the program several times. Enter numbers greater than 7500 (such as 10000), numbers between 7500 and half of 7500 (such as 6000), and numbers less than half of 7500 (such as 3000).
- Suppose the charge for sewer services is also based on water usage. A person who uses less than 7500 gallons of water pays a flat rate of $7.50 for sewer but a person who uses more pays $0.001 per gallon. Add an if ... else... to the program to compute the sewer charge. Update other parts of the program to take this into account -- update the calculation of the total bill and add a statement to print the sewer charge in the bill.
- Suppose senior citizens get a break on the utility tax. The tax rate for
senior citizens is 8.5% but it is 12% for all others. Add statements to
the program to compute the tax. This requires that you find out if the
customer is a senior citizen! Use a variable of type char for this. You need
to do the following:
- Add a prompt that asks the user to answer the question "Are you a senior
citizen?" by entering a y or n. Use the Keyboard class to read in the answer
(as a char not a String); store the answer in the variable
*senior*. - Write an if to compute the utility tax.
- Add the utility tax to the total bill.
- Print the utility tax as part of the bill.

- Add a prompt that asks the user to answer the question "Are you a senior
citizen?" by entering a y or n. Use the Keyboard class to read in the answer
(as a char not a String); store the answer in the variable
- Add an if statement that prints a warning to customers who use more than 3 times the cut off (7500) gallons. Warn them that they are subject to a $500 fine if their excessive water use continues (just print a message -- don't add the fine to their bill)!
- Be sure your program works correctly, then print it.

Print a copy of your program.

- The three programs (BaseConvert.java, Colors.java, and WaterBill.java).
- Tar your directory and email it to your instructor at roanoke.edu with the
subject
**cpsc120 lab5**.