1. Evaluate the following expressions. Show your work.
(a) int i = 10 + 4 * 8 - 6 / 2 ; (b) int i = 16 % 5 ; (c) int i = (int)Math.round(1 + 1/2 + 1/4 + 1/8); (d) String s = "123" + "456";
2. Given the following Java string definition:
String phrase = "No news is good news.";what is the output of each of the following Java statements?
(a) System.out.println(phrase.length()); (b) System.out.println(phrase.charAt(5)); (c) System.out.println(phrase.substring(3,7).toUpperCase()); (d) System.out.println(phrase.substring(0,3) + phrase.substring(11));
3. Using the random method of the Math class,
(a) show how to initialize a double variable named value that contains a random floating point value in the range [0, 100).(b) show how to initialize an int variable named number that contains a random integer from the set {12, 18, 24, 30, 36, 42}.
(c) complete the following simple Java program so that it prints out THREE random uppercase letters on the same line of the console (screen).
public class LetterGenerator { public static void main(String[] args) { // fill in the main method } public static void displayLetter() { // This method displays ONE random uppercase letter // Fill in the missing code in the blank } }
4. The if statement can alter the flow of execution of a program, determining whether to execute certain statements or not.
(a) Determine the value that will be output by the following code fragment:int x = 4; int y = 9; if (x % 2 == 1) y *= 2; y += 2; System.out.println(y);(b) Determine the value that will be output by the following code fragment:
boolean rain = true; boolean snow = false; if (!rain || snow) System.out.println("HAPPY"); else System.out.println("SAD");(c) Convert the following if statement to a logically-equivalent statement using DeMorgan's Law assuming age is an int variable and married is a boolean variable:
if (age < 50 && married) System.out.println("Qualifies for extra tax deduction.");(d) Draw a flowchart of the if statement shown above.
5. Electricity is billed based on the number of kilowatt-hours (kWh) used by a customer. To determine how many kilowatt-hours are used by a customer, we find the difference between the meter reading of a customer from the previous month to the meter reading for the current month. For example, if the meter reads 15100 for the previous month and 16232 for the current month, the customer used 16232-15100 = 1132 kWh. Note that a meter reading is always 5 digits, and the meter "wraps around" after 99999 back to 00000.
Once the reading is calculated, compute the charge for the customer based on the following table:
| Kilowatt-Hours Used | Charge |
| Less than 1000 | 7 cents per kWh |
| 1000-2500 | $70, plus 5 cents per kWh for each kWh above 1000 |
| More than 2500 | $145, plus 3 cents per kWh for eaach kWh above 2500 |
Write a simple Java program that uses the Scanner class to read in the two valid meter readings as integers (you may assume a meter reading of 00235 would be entered as 235). It should then print out the number of kilowatt-hours used and the charge for the user in dollars and cents. Do your computations using cents only and then display in dollars and cents when you're done.
Sample output for two runs of the program (user input in italics):
Please input last month's meter reading [0-99999]: 15100 Please input this month's meter reading [0-99999]: 16232 You used 1132 kilowatt-hour(s). Your charge is $76.60 Please input last month's meter reading [0-99999]: 99900 Please input this month's meter reading [0-99999]: 1 You used 101 kilowatt-hour(s). Your charge is $7.07Your Program:
import java.util.*;
public class ElectricBillGenerator
{
public static void main(String[] args)
{
// complete the missing program code below:
}
}