CS46A Review

Name ___________________________________

Last 4 digits of student ID: _________________

Directions: All problems refer to the Java programming language. Provide complete but concise answers.
 
1)	 
a)	What is a class? A class defines the methods you can use on an object

b)	What is a method? A method is a sequence of instructions that accesses the internal data of an object

c)	What is an object? An object is an instantiation of a class.

d)	What are instance fields? The “state” of an object

e)	Which variables should be declared as final? Constants

f)	What is the difference between a local variable and an instance field? Local variable belongs to a method, while instance field belongs to a class

g)	What do local variables and parameter variables have in common and how do they differ? Similar in that they both belong to a method, differ in the way they are initialized
h)	Provide syntactically correct and logically correct Java code for determine whether x is equal to y, assuming that
1)	x and y are both of type double?

For EPSILON a small number,
if(Math.abs(x – y) < EPSILON)

2)	x and y are both of type int?

if(x == y)

3)	x and y are both String objects?

		if(x.equals(y))

2)	
a)	What are the values of luckyNumber and unluckyNumber after the following code has executed?
	
int unluckyNumber = 13;
int luckyNumber = unluckyNumber;
unluckyNumber = 7;

luckyNumber is 13
unluckyNumber is 7

b)	What are the coordinates of the upper left corner of box after the following code has executed?

Rectangle box = new Rectangle(5, 10, 20, 30);
Rectangle box2 = box;
box2.translate(15, 25);

box is at (20,35)

3)	
a)	Write the following mathematical expression in Java.

a = p * Math.pow(1 + r/100, y);

b)	Write the following Java expression in mathematical notation.

dm = m * (Math.sqrt(1 + v / c) / (Math.sqrt(1 – v / c) – 1));

	

4)	What are the values of the following expressions? For each part assume that:

double x = 2.5;
double y = -1.5;
int m = 18;
int n = 4;
String a = “Hello”;
String b = “World”;
boolean t = true;
boolean f = false;

a)	x + n * y - (x + n) * y   6.25

b)	5 * x – n / 5   12.5

c)	(int) Math.round(x)  3

d)	a + b   HelloWorld

e)	1 – (1 – (1 – (1 – n))))   4

f)	!t   false

g)	t && f   false

h)	t || f   true

5)	Consider the Java class Car outlined below. A Car object has a fuel efficiency (measured in miles per gallon) and an amount of gas in the gas tank. The efficiency is specified in the constructor and the initial amount of gas is 0. The class has three methods: drive, which reduces the gas as determined by the number of miles driven and the fuel efficiency; getGas, which returns the amount of gas in the tank; and addGas which adds gas to the tank. Fill in the methods drive and getGas, below

public class Car
{
	public Car(double initialEfficiency)
	{
		efficiency = initialEfficiency;
		gas = 0;
	}

	/**
	   reduce gas based on miles driven
	   @param miles
	*/
	public void drive(int miles)
	{
		double gasUsed = (double)miles / efficiency;
		gas = gas – gasUsed;
	} 

	/**
	   returns amount of gas in tank
	   @return gas
	*/
	public double getGas()
	{
		return(gas);
	}
		
	public void addGas(double gallons)
	{
		double newGas = gallons + gas;
		gas = newGas;
	}
	
	private double efficiency;
	private double gas;	

}//end of class

6)	Provide documentation comments for the methods drive and getGas in the previous problem. Your comments must be in a style appropriate for javadoc.

7)	Consider a class Product, where a product has a name and a price; for example new Product(“Toaster”, 29.95). The Product class has method getName, getPrice and setPrice. Implement the class Product.
 
public class Product
{
	public Product(String productName, double productPrice)
	{
		name = productName;
		price = productPrice;
	}

	public String getName()
	{
		return(name);
	} 

	public double getPrice()
	{
		return(price);
	}
		
	public void setPrice(double newPrice)
	{
		price = newPrice
	}
	
	private String name;
	private double price;	

}//end of class

8)	You are required to write a method printOrder that compares three integers and prints the numbers in increasing order. Your method should have 3 parameters (the numbers to be compared).

	public void printOrder(int x, int y, int z)
	{
		if(x <= y && x <= z) // x is smallest
		{
			if(y <= z) // y is middle
			{
				System.out.println(x,y,z);
			}
			else // z is middle
			{
				System.out.printlin(x,z,y);
			}
		}
		else if(y <= x && y <= z) // y is smallest
		{
			if(x <= z) // x is middle
			{
				System.out.println(y,x,z);
			}
			else // z is middle
			{
				System.out.printlin(y,z,x);
			}
		}
		else // z is smallest
		{
			if(x <= y) // x is middle
			{
				System.out.println(z,x,y);
			}
			else // y is middle
			{
				System.out.println(z,y,x);
			}
		}
	}