(a) double d = 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"; (e) int i = Math.pow(2, 3);
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)); (e) System.out.println(phrase.replace('n', 's'));
(b) Using the random method of the Math class, show how to initialize an int variable named number that contains a random integer from the set {12, 18, 24, 30, 36, 42}.
(c) The following simple Java program prints out two 3-digit random sequences. Complete the missing code.
public class LotteryNumberGenerator { // This simple program prints out TWO 3-digit random // numbers for a lottery public static void main(String[] args) { System.out.println("Play these lottery numbers:"); // complete the main method here } // display THREE random digits from the numbers string public static void displayNumber() { String numbers = "0123456789"; System.out.___________(numbers.charAt(________________)); System.out.___________(numbers.charAt(________________)); System.out.___________(numbers.charAt(________________)); } }
boolean rain = true; boolean snow = false; if (!rain || snow) System.out.println("HAPPY"); else System.out.println("SAD");
(a) What is the output of this code fragment?
(b) Draw a flowchart of the conditional in this code fragment.
(c) Use de Morgan's law to complete the following code fragment so that it is equivalent to the one above.
boolean rain = true; boolean snow = false; if (_________________________________) System.out.println("SAD"); else System.out.println("HAPPY");
(d) What is the output of the following code fragment?
int x = 4; int y = 9; if (x % 2 == 1) y *= 2; y += 2; System.out.println(y);
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 bill 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 bill is $7.07Your Program:
import java.util.Scanner; public class ElectricBillGenerator{ public static void main(String[] args) { // complete the missing program code below: } }