int quantity = 5; double unitCost = 0.65; String drink = "pepsi";
For each expression in the following table, indicate its value and the type of the value.
Expression |
Value | Type |
| Numeric.round(quantity * unitCost) | 3 | int |
| ("diet " + drink).length() | 10 | int |
| drink.substring(1, 4).toUpperCase() | "EPS" | String |
| drink.equals("coke") | false | boolean |
| quantity + 10 + "" | "15" | String |
| (10 <= quantity) || ((quantity % 2) == 0) | false | boolean |
Each of the following program fragments was written twenty minutes before it was due to be turned in, and as a result contains one major error that either prevents it from running, or produces the wrong answer. Correct this error by rewriting or inserting one or more lines.
String areaCode, phoneNumber;
System.out.print("Enter your complete telephone number xxx-xxx-xxxx: ");
phoneNumber = Console.in.readLine();
phoneNumber.substring(0, 3) = areaCode; // ERROR!
System.out.println("You live in area code " + areaCode + ".");
Error: the assignment to areaCode is backwards.
int gallons;
double mpg, distance;
distance = gallons * mpg; // ERROR!
gallons = 148;
mpg = 21;
System.out.println("You can travel " + distance + " miles on " +
gallons + " gallons of gasoline.");
Error: gallons and mpg are initialized after they
are used in the distance calculation.
String name;
System.out.print("Enter your name: ");
Console.in.readLine(); // ERROR!
System.out.println("Your name starts with " + name.substring(0, 1));
Error: the user input is never stored in name.
int jellyDonuts, glazedDonuts; // ERROR!
double jellyPrice = 0.45, glazedPrice = 0.35, totalCost;
System.out.print("How many jelly donuts do you want? ");
jellyDonuts = Console.in.readDouble();
System.out.print("How many glazed donuts do you want? ");
glazedDonuts = Console.in.readDouble();
totalCost = jellyDonuts * jellyPrice + glazedDonuts * glazedPrice;
Error: the return value of readDouble is stored in an
integer.
Correction: Either change the type of jellyDonuts and glazedDonuts to be double, or use readInt instead of readDouble.
String name; String letter;
name = "Dorothy";
letter = name.substring(length() - 1, length()); // ERROR!
System.out.println("This name ends with: " + letter);
Error: the length method is not applied to an object.
Correction: letter = name.substring(name.length() - 1, name.length());
Enter an odd-length word: chickenThe letter c is in the middle.
Do not worry about error checking the input. That is, do not bother to verify that the length of the word the user types is indeed odd. Here is the program that you are to complete.
public class MiddleLetter {
public static void main(String[] args) {
System.out.print("Enter an odd-length word: ");
String word;
word = Console.in.readLine();
int len, mid;
len = word.length();
mid = len / 2;
String letter;
letter = word.substring(mid, mid + 1);
System.out.println("The letter " + letter + " is in the middle.");
} }
int x, y;
System.out.print("Enter two integers: ");
x = Console.in.readInt();
y = Console.in.readInt();
x = 2 * x;
y = y - 1;
System.out.println("x = " + x + ", y = " + y);
y = 5;
x = y;
y = y - 1;
System.out.println("x = " + x + ", y = " + y);
Answer: x = 6, y = 7
x = 5, y = 4
Time dinnerTime, examStart, examEnd; dinnerTime = new Time(1998, 2, 18, 18, 0, 0); // Feb 18, 1998 at 6 pm examStart = new Time(1998, 2, 18, 19, 0, 0); // Feb 18, 1998 at 7 pm examEnd = examStart; examEnd.addSeconds(2 * 60 * 60); System.out.println(examStart.secondsFrom(examEnd)); System.out.println(dinnerTime.getHours() - examStart.getHours()); System.out.println(examEnd.getHours() - dinnerTime.getHours());
Answer: 0
-3
3
int west = 1, language = 2;
if (west == 1) {
if (language != 2)
System.out.print("Auf Wiedersehen ");
else
System.out.print("Adios ");
}
else
System.out.print("Sayonara ");
Answer: Adios
You do not have to verify that the user input is in the expected range. Here is the program that you are to complete.
public class DaysInMonth {
public static void main(String[] args) {
int month, year;
System.out.print("Enter a month number (1-12): ");
month = Console.in.readInt();
// You fill in the rest of the code that is needed...
System.out.print("Enter a year after 1970: ");
year = Console.in.readInt();
Time t;
if (month == 12)
t = new Time(year + 1, 1, 1);
else
t = new Time(year, month + 1, 1);
t.addseconds(-24 * 60 * 60);
System.out.println(month + "/" + year + " has " + t.getDay() + " days.");
} }