Learn-Java.com

  • Increase font size
  • Default font size
  • Decrease font size
Home Solutions

Solutions

E-mail Print PDF

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

  1. System.out.println will print a newline character after the printed line. System.out.print does not print a newline character.
  2. Program execution begins in the main method.
  3. Escape sequences exist because the characters they represent either cannot be typed on the keyboard, or have no single-character representation.
  4. The proper format is Camel case. It makes things easier to read.
    • newNumber, someDigit, hahaBoolean
  5. You create your declaration statement this way:
    • boolean test
    • double digit
    • int number
    Also, you can add the equals sign right after, to assign values.
    • boolean test = true;
  6. Variables and literals are used to store values. Plain and simple. We need place holders to manipulate data.
  7. The primitive variables are:
    • boolean
    • char
    • int
    • double
    Some examples are
    • boolean test = true;
    • char letter = 'D';
    • int numberTest = 6;
    • double testNumber1 = 9.879;
  8. The table is as follows:
    Operator Precedence
    - Highest Precedence
    *, /, %
    +, -
    = Lowest Precedence
  9. 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.
  10. The results of the casts are (errors indicated where necessary):
    1. 50
    2. 25L (casting does not round, it merely drops the decimal portion)
    3. 55000000000000000L
    4. Error, 1.23e240 is outside the range of float types.
    5. Error, 2500000000L is outside the range of int types
    6. 6.789E7
    7. 600.0
  11. Recall that escape characters such as \\ and \n may look longer than they really are. They are interpreted by the computer as single characters.
    1. 19
    2. 16
    3. 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);
}
}

 

 

 

Last Updated on Wednesday, 09 December 2009 07:40  

Search This Site


Newsflash


"Welcome to Learn Java.com. A learning website created to teach programming concept through Java. Learn java.com is a beginner guide to programming and it is also an introductory guide to Java."