Loyola College in Maryland

CS 201 - Computer Science I
Fall 2003


Loyola College > Department of Computer Science > CS 201 > Homework > Exam 1 Practice Problems

Problem 0: Review old quizzes, homework assignments, and labs.

Problem 1: Suppose we have two variables declared as follows.

  int degreesF;
  double degreesC;
The following statements are attempts to apply the temperature conversion formula f = 32 + 1.8 * c to degreesC and store the result (with fractional part truncated) in degreesF. For each statement, write ``ILLEGAL'' if it is not a valid statement in Java, ``INACCURATE'' if it is valid but does not produce the correct result (for example, when degreesC is initialized to 46.1 then degreesF should be 114), or ``GOOD'' if it is valid and produces the correct result.

Problem 2: Write a program that prompts the user to input the equation of a line in the form y=mx+b and displays just the slope and intercept. For example, if the input is y=6x+4, the output should be slope = 6, intercept = 4. You may make reasonable assumptions about the form of the input (for example, the slope and intercept should always be given explicitly, so y=0x+0 and y=1x+1 would be OK but y=0 and y=x would not be OK).

Problem 3: Add a method to the Line2D class from Lab 4. The new method should calculate the distance between a line and a point. Given a line y=mx+b and a point (x, y), you would first find the x-coordinate of the closest point on the line, which is

(y + 1/m * x + b) / (m + 1/m)

Next, you would find the y-coordinate of the line for that x-coordinate. Finally, you would find the distance between the original point and the point whose coordinates you just computed.

Problem 4: Write a program that uses your Line2D class to display the distance between a point and a line in a dialog box. The point and the line should be entered by the end user. You should create a Line2D object and use its methods to determine the result.

Problem 5: What is the output of this program?

EvergreenCard.java

/**
 * An debit card account.
 *
 * @author Jim Glenn
 * @version 0.1 10/3/2003
 */

public class EvergreenCard
{
    /**
     * The balance in this account.
     */

    private double balance;

    /**
     * The name of the holder of this account.
     */

    private String holder;

    /**
     * Creates an Evergreen card account with the default initial
     * value of $50.
     *
     * @param name the name of the holder of this account
     */

    public EvergreenCard(String name)
    {
	holder = name;
	balance = 50.00;
    }

    /**
     * Creates an Evergreen card account with the given initial balance.
     *
     * @param name the name of the holder of this account
     * @param init the initial balance of the new account
     */

    public EvergreenCard(String name, double init)
    {
	holder = name;
	balance = init;
    }

    /**
     * Returns the name of the holder of this account.
     *
     * @return the name of the holder of this account.
     */

    public String getHolder()
    {
	return holder;
    }

    /**
     * Determines the balance in this account.
     *
     * @return the balance in this account
     */

    public double getBalance()
    {
	return balance;
    }

    /**
     * Makes a purchase from this account.
     *
     * @param amt the amount of the purchase
     */

    public void makePurchase(double amt)
    {
	balance = balance - amt;
    }

    /**
     * Deposits money in this account.
     *
     * @param amt the amount of money to deposit
     */

    public void makeDeposit(double amt)
    {
	balance = balance + amt;
    }

    /**
     * Returns a string representation of this account.  The string will
     * contain the name of the account holder and the balance.
     *
     * @return a string represenation of this account
     */

    public String toString()
    {
	return holder + " $" + balance;
    }
}

CardTest.java

public class CardTest
{
    /**
     * Test driver for the EvergreenCard methods.
     *
     * @param args ignored
     */

    public static void main(String[] args)
    {
	EvergreenCard mine = new EvergreenCard("Jim");
	EvergreenCard yours = new EvergreenCard("Rob", 100.00);

	mine.makePurchase(7.50);
	yours.makeDeposit(25.00);
	mine.makePurchase(15.00);

	System.out.println(mine);
	System.out.println(yours);

	double amt = mine.getBalance();
	System.out.println("amt = " + amt);

	EvergreenCard copy = yours;

	mine.makeDeposit(yours.getBalance());
	copy.makePurchase(70.00);
	copy = mine;
	copy.makeDeposit(70.00);

	System.out.println(mine);
	System.out.println(yours);
	System.out.println(copy);
    }
}

These files can also be downloaded from EvergreenCard.java and CardTest.java.