Study Guide For Chapter 4 Test

Chapter 4 Included the following topics:

  1. Computer Systems (page 75)
  2. Program Design(pp 75-77)
  3. Inheritance (pp 77-81)
  4. Class Hierarchies (pp 81-85)
  5. Polymorphism (pp 85-87)
  6. Interfaces (pp 87-92)
  7. Reusing code in your design of a class (p93-94)

Projects to practice with:

Objects

  1. Make a Shoe class that has the fields size (a double), isForMen (boolean), and name (String), with 2 or three constructors and accessor methods
  2. Make a TestingClass to see if your Shoe class is working properly
  3. implement the Measureable interface that returns the size.
  4. make a method isBigger that has a parameter Shoe other returns true if the other shoe is a larger size.
  5. make a method biggest that takes an ArrayList of Shoe and returns the largest shoe using the isBigger method.

Loops

  1. Make a while loop that prints 2,5,8,11,14,17,20
  2. Make a for loop that prints 2,5,8,11,14,17,20

Arrays

Some methods to write:

  1. arrayToString which is a string with all the values of the array separated with commas and spaces
  2. attachToEnd which makes a new array that is one longer, adding the value to the last position
  3. attachToFront which makes a new array that is one longer, adding the value to the first position
  4. max which returns the value of the biggest element
  5. min which returns the value of the smallest element

Starter Code


public class ArrayTester {
    public static String arrayToString(int[] a){
    		// your code here
    		return "yet to do";
    }
	public static int[] attachToEnd(int[] a, int x){
		// your code here
		return null;

	}
	public static int[] attachToFront(int[] a, int x){
		// your code here
		return null;

	}
	public static int min(int [] a){
		// your code here
		return Integer.MAX_VALUE;
	}
	public static int max(int [] a){
		// your code here
		return Integer.MIN_VALUE;
	}
	public static void main(String[] args) {
		int[] nums = {3,2,1};	
		System.out.print ("array nums has "+arrayToString(nums));
		System.out.println ("--Expected  [ 3, 2, 1 ]");
		nums=attachToEnd(nums, 0);
		System.out.print ("array nums has "+arrayToString(nums));
		System.out.println ("--Expected  [ 3, 2, 1, 0 ]");
		nums=attachToFront(nums, 4);
		System.out.print ("array nums has "+arrayToString(nums));
		System.out.println ("--Expected  [ 4, 3, 2, 1, 0 ]");
		System.out.print ("the max is  "+max(nums));
		System.out.println ("--Expected  4");
		System.out.print ("the min is  "+min(nums));
		System.out.println ("--Expected  0");		
	}

}

Other things to try

  1. add another method maxIndex that returns the index of the largest element
  2. add another method minIndex that returns the index of the smallest element
  3. add another method average that returns the average value of the array
  4. Do all of the above with an array of String instead of an int array
  5. Do all of the above with an ArrayList<Integer> instead of and integer Array.

Super and Subclasses

  1. Make the classes Fish, Cat, and Dog that are subclasses the Pet class. All pets should have a name and an age. Add a unique instance field for each subclass
  2. Make a Maple and a Fir what are both subclasses of Tree. All trees have a height and leafColor, but have the Maple override the leafColor method.

Interfaces

  1. Make a Measureable interface that requires the method getMeasure which returns a double. Make a few classes that implement the interface such as Book or Lumber
  2. Consider the interface
public interface Judge
{
    boolean acceptable(String text)
}
Make classes that implement the Checker interface. Here are some ideas:
  1. Odd which will only find acceptable Strings that have an odd number of letters in it.
  2. Question which approves of Strings that end in a question mark "?"
  3. Alliteration where it checks if every word in a String starts with the same letter. If is does, it would considered acceptable. For example, "We want wonderful weddings" would be acceptable, and "We want to weave" would not be acceptable since the word "to" doesn't start with the same letter. (Hint: use the String method split to chop the String into an array of words.
import java.util.ArrayList;
public class JudgeTester
{

    public static void main(String[] args)
    {
        Odd odd = new Odd();
        System.out.println(odd.acceptable("odd")+" -- Expected true");
        System.out.println(odd.acceptable("four")+" -- Expected false");

        //Alliteration poet = new Alliteration();
        //System.out.println(poet.acceptable("we wander west")+" -- Expected true");
        //System.out.println(poet.acceptable("we wander east")+" -- Expected false");
        ArrayList<Judge> judges =  new ArrayList<Judge>();
        // judges.add(odd);
        // judges.add(poet);
        for (Judge j:judges)
            System.out.println( j.acceptable("Is this ok?");
    }
}

Design

Try The ChessRating project