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) | ||
| ("diet " + drink).length() | ||
| drink.substring(1, 4).toUpperCase() | ||
| drink.equals("coke") | ||
| quantity + 10 + "" | ||
| (10 <= quantity) || ((quantity % 2) == 0) | ||
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 + ".");
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.");
String name;
System.out.print("Enter your name: ");
Console.in.readLine(); // ERROR!
System.out.println("Your name starts with " + name.substring(0, 1));
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;
String name; String letter;
name = "Dorothy";
letter = name.substring(length() - 1, length()); // ERROR!
System.out.println("This name ends with: " + letter);
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) {
} }
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);
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());
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 ");
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...
} }