CBIS 4210 Fall 2003 Final Exam Review Problems

1. Declaring variables: Write the Java code to create a variable of type String and store your name in it, then print your name to the screen along with the number of characters in your name.

	String myName = "Bryson Payne, M.Ed."
	System.out.println("My name is " + myName + ", and that has " +
		myName.length() + " characters in it.");

2. Object construction: Write the Java code to create an object of the Car class whose name is jetty. The Car class takes two construction parameters, a String with the name of the car ("VW Jetta TDI") and a double value for miles per gallon (49.0).

	Car jetty = new Car("VW Jetta TDI", 49.0); 

3. Method signatures: Write a method signature for a method that is called getLetterGrade that takes a parameter of type double (called numberGrade) and that returns a char with the appropriate letter grade. Give the javadoc comments for this method, including the parameter and the return.

	/** Method to return a char (character) letter grade from a number grade
	    @param numberGrade - The numerical average (e.g. 89.6)
	    @return The appropriate letter grade (e.g. 'A')
	*/
	public char getLetterGrade(double numberGrade)

4. Constant declaration: Write the Java statement to declare a double constant called GRAVITY that stores the value 9.8.

	final double GRAVITY = 9.8;

5. Math/Casting: Write a Java statement to round the double variable money to the nearest integer and store the result in an integer called cash.

	int cash = (int)(Math.round(money));

6. Graphics: Write the Java code to draw a circle 100 pixels in diameter and a square whose four sides are tangent to the circle. Assuming the window is 200 pixels by 200 pixels, center the circle and square in the window called g2.

	Ellipse2D.Double circle=new Ellipse2D.Double(50,50,100,100);
	g2.draw(circle);
	Rectangle2D.Double square=new Rectangle2D.Double(50,50,100,100);
	g2.draw(square);

7. Inheritance: Assume you have a super class called Friend that takes one construction parameter, a String called name. Write a class header for a class BestFriend that inherits from Friend.

	public class BestFriend extends Friend

8. Super/Sub Class Constructors: Continuing from problem 7, write a constructor for BestFriend that takes two parameters, name and years, an integer that will be stored in the yearsKnown field. Remember to call the superclass constructor.

	public BestFriend(String name, int yearsKnown)
	{
		super(name);
		yearsKnown = years;
	}		

9. Loops: Write a while loop that will print out all the multiples of 5 from 0 to 1000.

	for(int x=0; x<=1000; x=x+5)
		System.out.println(x);

10. Loops: Write a for loop that will print out every other character of an input String called myString (i.e., if the user types in "This is cool!", the program outputs "Ti sco!").

	for(int x=0; x<=myString.length() ; x=x+2)
		System.out.print(myString.charAt(x));