Study Guide for CSC 120 Final May 1, 2008 ------------------------------------------------------------------- Describe what the following Java key words are used for: class, extends, super, this, static, void, final, private ------------------------------------------------------------------- What are the primitive data types in Java? Give sample initializations of variables holding these data types. --------------------------------------------------------------------- What is printed by the following code fragment: x = 10; y = 20; if ( x < y) System.out.println( "then statement executed"); else System.out.println( "else statement executed"); System.out.println( "when am I executed?"); ------------------------------------------------------------------- Rewrite the following loop using a while statement: for (i = 0; i < 100; i++) { sum = sum + i; } ------------------------------------------------------------------- Without compiling, describe what the following program prints. Consult the appropriate classes in Week 11. //CardTest.java testing Card. public class CardTest { public static void main(String[] args) { Card card1 = new Card(); Card card2 = new Card(Card.ACE, Card.DIAMONDS); Card card3 = new Card( 1, 2); Card card4 = new Card(2, 4); System.out.println("\ncard1\n" + card1); System.out.println("\ncard2\n" + card2); System.out.println("\ncard2 Value\n" + card2.getValue()); System.out.println("\ncard2 Suit\n" + card2.getSuit()); System.out.println("\ncard2 Value\n" + card2.getValueAsString()); System.out.println("\ncard2 Suit\n" + card2.getSuitAsString()); System.out.println("\ncard3\n" + card3); System.out.println("\ncard3\n" + card4); } } ------------------------------------------------------------------- Give an example of method overloding. ------------------------------------------------------------------- The expression (int)( 10*Math.random() ) + 2 generates integers in what range? ------------------------------------------------------------------- Write a pseudocode for binary search in a sorted array of integers ------------------------------------------------------------------- Add to PokerHand.java (see Week 11) a method isThreeOfKind that returns true if at least three cards in a poker hand have the same face value. ------------------------------------------------------------------- Look up the documentation for java.util.Scanner. Choose six methods and demonstrate how to use them in sample java code. ------------------------------------------------------------------- There are two errors in the following code segment. Find them. class Message { public static void main(String[] args) { System.out.println("Hello Class!"); printMessage() System.out.println("Goodbye."); } void printMessage() { System.out.println("A message for you: "); System.out.println("Have a nice day!"); } } ------------------------------------------------------------------- What output is produced by the following program segment? Why? (Recall that name.charAt(i) is the i-th character in the string, name.) String name; int i; boolean startWord; name = "John D. Doe"; startWord = true; for (i = 0; i < name.length(); i++) { if (startWord) System.out.println(name.charAt(i)); if (name.charAt(i) == ' ') startWord = true; else startWord = false; } ------------------------------------------------------------------- True or false? An object is an instance of a class. True or false? The default no-arg constructor is always provided by the Java compiler. True or false? A class cannot have both static and nonstatic methods. True or false? All drawing routines use a coordinate system that places the origin in the lower left corner of the component. ------------------------------------------------------------------- Creat a class Vehicle with data fields tag number, owner name, and year of manufacture. Make the data fields private; provide get and set methods for the fields. Provide constructors where any or all fields can be set. Provide a toString method. Now construct a Car class that extends Vehicle. Include addtional data fields for number of doors and color. Make sure to include the appropriate constructors and toString methods for Car. Now construct a Truck class that extends Vehicle. Include addtional data fields for number of axels and color. Make sure to include the appropriate constructors and toString methods for Truck. Finally write a driver program to create two cars and two trucks. Print out the data for these four vehicles. If you have time, print them out sorted according to their year of manufacture. ------------------------------------------------------------------- Use a GridLayout to create a JFrame with three buttons along the diagonal. You can use new JLabel(" ") to create an apparently empty grid position. ------------------------------------------------------------------- What does the following program do? Describe its usage. import java.util.*; import java.io.*; class WordCount { public static void main(String[] args) throws IOException { Scanner input = new Scanner(new FileInputStream(args[0])); int count = 0; while (input.hasNext()) { String word = input.next(); count++; } System.out.println("There were "+count+" words."); } } -------------------------------------------------------------------