In this section you will find the answers to the review questions and exercises. Try your best to work out the answers on your own before looking up the answers. Also, your solutions to the programming exercises may be different from the solutions shown here. That is one of the interesting aspects of programming: there are many ways to write a program that will produce the same result.
Solutions to Review Questions
- System.out.println will print a newline character after the printed line. System.out.print does not print a newline character.
- Program execution begins in the main method.
- Escape sequences exist because the characters they represent either cannot be typed on the keyboard, or have no single-character representation.
- The proper format is Camel case. It makes things easier to read.
- newNumber, someDigit, hahaBoolean
- You create your declaration statement this way:
- boolean test
- double digit
- int number
- boolean test = true;
- Variables and literals are used to store values. Plain and simple. We need place holders to manipulate data.
- The primitive variables are:
- boolean
- char
- int
- double
- boolean test = true;
- char letter = 'D';
- int numberTest = 6;
- double testNumber1 = 9.879;
- The table is as follows:
Operator Precedence -Highest Precedence *, /, %+, -=Lowest Precedence - We use brackets to separate long statements, and it makes things easier to read. Also, operations are done first in brackets (they take highest precedence). Use them often, neatly and properly.
- The results of the casts are (errors indicated where necessary):
-
50 -
25L(casting does not round, it merely drops the decimal portion) -
55000000000000000L - Error,
1.23e240is outside the range offloattypes. - Error,
2500000000Lis outside the range ofinttypes -
6.789E7 -
600.0
-
- Recall that escape characters such as
\\and\nmay look longer than they really are. They are interpreted by the computer as single characters.- 19
- 16
- 10
Solutions to Exercises
1.
public class BrokenProgram
{
public static void main(String args[])
{
int value1;
int value2;
int result;
value1 = 25;
value2 = 5;
result = value1 / value2;
System.out.println("When you divide " + value1 + " by " + value2 + ", the result is " + result);
}
}
2.
public class Wrong_Sum
{
public static void main(String[] args)
{
int value1 = 10;
int value2 = 10;
int sum;
// Note: this code was corrected 2009-Sep-28
// The important thing is that the output should have a consistent trio of values.
sum = ++value1 + --value2; // Should be a pre-decrement
// /* Or you can do this */
// value1++;
// value2--;
// sum = value1 + value2;
System.out.println("value1 = " + value1 + ", value2 = " + value2);
System.out.println("sum = " + sum);
}
}
3.
import javax.swing.JOptionPane;
public class TaxCal
{
public static final double GST = .06; //federal tax
public static final double PST = .07; //provincial tax
public static void main(String args[])
{
double drinkPrice; //price of a drink
double sandwichPrice; //price of a sandwich
double sum; //sum of drink and sandwich prices
double tax; //amount of tax on the drink and sandwich
//ask the user to input the cost of a drink and then the cost of a sandwich
drinkPrice = Double.parseDouble(JOptionPane.showInputDialog("Enter the price of a drink:"));
sandwichPrice = Double.parseDouble(JOptionPane.showInputDialog("Enter the price of a sandwich:"));
//calculate the total cost with taxes
sum = drinkPrice + sandwichPrice;
tax = sum * (GST + PST);
sum = sum + tax;
//print the total cost
System.out.println("The total cost with tax is: " + sum);
}
}
4.
public class StringCreator
{
public static void main(String []args)
{
int x=10;
int y=21;
//We must put an empty string before the variable x.
//Without this string we would be trying to append a String onto an int which the compiler does not know how to do
//We could also make this more readable by making a third variable z and assign x+y to it
String result="" + x+ "+" + y + "=" + (x+y);
System.out.println(result);
}
}



Solutions

