Review for Exam 2

  1. Two (or more) of the files that are found in a completed Java project have the extensions .java and .class respectively. How are the contents created for these files and what kind of code is in them?
  2. A variable name declared in a Java program represents one of two categories of data. Name these categories? Give an example of a declaration for each using Java statements. In addition, draw pictures of the results of the declarations in memory.
  3. List five of the Java primitive types.
  4. Assuming that the following code will execute within the program it comes from, what exactly is printed out by each statement?

System.out.println(6 + "34");

System.out.println("34" + ("6" + "7"));

System.out.println("34" + (6 + 7));

  1. Using the Java source code (attached to the next page) for the Sphere class write out that which is printed by the Java source code found below.

String str;

double x = 2.89, y = 15.0;

Sphere tennisBall = new Sphere (x);

Sphere basketBall = new Sphere (y);

str = basketBall.toString();

System.out.println("\nThe respective values of x and y are " + x + " and " + y);

System.out.println("\ntennisBall references " + tennisBall);

System.out.println("\nbasketBall references " + str);

  1. Draw a picture of memory at the instance of the last statement in the source code of the previous question.
  2. Define the Java terms statement, method and class and their relationships to each other.
  3. Using the attached source code, put the names of the Sphere class methods under the appropriate method category.
  4. Constructor
  5. Accessor
  6. Mutator
  7. Which statement is used to make simple decisions in Java?
  8. What would be the value of bonus after the following statements are executed?

int bonus, sales = 85000;

char dept = 'S';

if (sales > 100000)

if (dept == 'R')

bonus = 2000;

else

bonus = 1500;

else if (sales > 75000)

if (dept == 'R')

bonus = 1250;

else

bonus = 1000;

else

bonus = 0;

  1. What will be the values of x and y as a result of the following code?

int x = 25, y = 8;

x += y++;

  1. When a method's return type is a class, what is actually returned to the calling program?
  2. What will be the value of x after the following code is executed?

int x = 10;

while (x < 100)

{

x += 10;

}
SPHERE CLASS CODE

/**

* Sphere class to be used in

* the CS 146 Exam 1

*/

public class Sphere

{

private double diameter;

public Sphere(double d)

{

// initialise instance variables

diameter = d;

}

public void setDiameter(double d)

{

diameter = d;

}

public double getDiameter()

{

return diameter;

}

public double getSurfaceArea()

{

return 4.0*Math.PI*(Math.pow(diameter/2,2));

}

public double getVolume()

{

return Math.PI*(Math.pow(diameter,3))/6.0;

}

public String toString()

{

return ("a sphere of diameter " + Double.toString(diameter));

}

}

1