The Super Savitch Study Guide

courtesy of Adam Fleming

The material presented in HW4 WILL be on the midterm

Midterm will cover Chapters 6-9 and JoptionPane

Sample Midterm is a sample of form not content.

Midterm Review:

double[] list = {4, 1, 9, 3, 2};

System.out.println(list.length); // output? 5 largest index of the array? 4

char[] greet = {‘h’,’e’,’l’,’l’,’o’}; // greet points to an array

char[] cuss = greet;

cuss[4] = ‘!’;

// what is now stored in greet? "hell!"

// cuss and greet are the same object

 

public class Mammal{
  public void speak(){
     System.out.println(getWords());
  }

  public String getWords(){
    return "nothing yet";
  }
}

public class Person extends Mammal{
  public String getWords(){
    return "hello world.";
  }

  public void shout(){
    System.out.println("HEY!");
  }
}

public class Dog extends Mammal{
  public String getWords(){
    return "bark";
  }

  public void chase(){
    System.out.println("chasing….");
  }
}

Mammal M = new Mammal();
Person P = new Person();< BR > Dog D = new Dog();< BR >
M.speak(); // "nothing yet"

P.speak(); // "hello world"
P.shout(); // "HEY!"
D.speak(); // "bark"
D.chase(); // "chasing…."

M = new Dog(); // This is okay – a Dog IS A Mammal
M.speak(); // output? "bark"
M.chase(); // output? ** compile time error **
M = P;
M.speak(); // output? "hello world"
M.shout(); // output? ** compile time error **

// Why do we get the compile time errors? because chase and shout are not methods in the Mammal Class. Why does it print out the persons words and dogs words? That is the dynamic binding happening.

 

try{

// normal stuff

// sketchy-exception-generating-code

// throw new Exception_Type("mabye a message");

// other stuff

}

catch(Exception_Type some_name)

{

// do stuff

// perhaps use some_name.getMessage(), which

// returns the message from the throw, or a default message

}

 

  • Remember these steps when you use files:

 

Relevant examples from the practice midterm:

Consider the following which might appear in a Java program:
char[ ] b = new char[10];
The expression b[0] can be used

a. to hold one value of type char .
b. as an argument to plug in for a parameter of type char .
c. as an argument to plug in for a parameter of type char[ ] .
d. only a. and b.
e. only a and c
f. none of the above.

The number of possible elements in an array a is equal to.
a. a.length()
b. a.length
c. a.size
d. a.size()
e. none of the above

Which of the following are reference types?

a. char
b. String
c. double[]
d. exactly two of the above. (You do not have to say which two.)
e. all of the above.
f. none of the above.

Consider the following which might appear in a Java program:
double[ ] a = new double[12];

a. the array a has thirteen indexed variables numbered 0 through 12.
b. the array a has twelve indexed variables numbered 0 through 11.
c. the array a has twelve indexed variables numbered 1 through 12.
d. the array a has eleven indexed variables numbered 1 through 11.
e. none of the above.

If a and b are array variables (that name some arrays) then the effect of
a = b;

a. is to copy the elements in the array b to the indexed variables of the array a .
b. is to copy the elements in the array a to the indexed variables of the array b .
c. is to make a and b two names for the same array,
d. nothing. It is illegal to use = with arrays.
e. none of the above.

Every exception class

a. has a method named getMessage .
b. has a method named getText .
c. has a method named getActionCommand .
d. all of the above.
e. none of the above.