CS 1054: Introduction to Programming in JAVA
CS 1054 Introduction to Programming in Java (Spring 2008)
Lab 2:
Using Objects
Lab Objectives
- To learn how to create and manipulate objects using BlueJ.
- To learn how to write programs that test and use objects.
Instructions
- Download ObjectExamples.zip ,
save the file, and then extract it in a folder you can locate.
This is a BlueJ project that contains code for three classes:
LightBulb, Car, and BankAccount.
- Make sure these classes are compiled properly:
right-click on each icon and choose "Compile".
- Create one object of each class (right-click on the icon,
and select "new ..."). Execute methods on the objects as
your instructor did in class (right-click on the red icons, and select
the appropriate methods).
- For the BankAccount class, interactively within BlueJ, carry out the following:
- Create a BankAccount object called bob.
- Create a BankAccount object called mary.
- Deposit 1000 dollars to bob's account.
- Deposit 2000 dollars to mary's account.
- Withdraw 150 dollars from mary's account.
- Deposit 200 dollars to bob's account.
- check the balance of bob's account (it should be 1200.0)
- check the balance of mary's account (it should be 1850.0)
- Within the same project, create a Java class (select "New Class") called BankAccountTester (BankAccountTester.java).
Use the following code (cut and paste, if you like, but make sure you place the correct name and date):
// -------------------------------------------------------------------------
/**
* This class tests the BankAccount class;
* it creates two bank account objects and calls deposit, withdraw
* and getBalance methods on the objects
*
* @author your-pid
* @version (place the date here)
*/
public class BankAccountTester
{
public static void main( String args[] )
{
BankAccount bob = new BankAccount();
BankAccount mary = new BankAccount();
bob.deposit( 1000.00 );
mary.deposit( 2000.00 );
mary.withdraw( 150.00 );
bob.deposit( 200.00 );
double bobsBalance = bob.getBalance();
double marysBalance = mary.getBalance();
System.out.print( "Bob's balance is " );
System.out.println( bobsBalance );
System.out.print( "Mary's balance is " );
System.out.println( marysBalance );
}
}
You will notice that the code is a program that does exactly the same thing you
did in the previous item, except that you are doing it through a separate Java program,
instead of interactively through BlueJ.
- You will now do something similar for the Car class.
First, interactively through BlueJ:
- Create a single Car object called myRaceCar
- Drive 300 miles
- Load 2 gallons of gas
- Drive 30 miles
- Check the amount of gas left (should be 3.8 gallons left)
- Check the distance travelled (should be 330 miles total)
- Now, here's the hardest part: Write a CarTester program
(use BankAccountTester as a pattern) that carries out exactly the
same actions as the previous item.
- Check with your TA regarding grading instructions.
He will need to make sure that you have carried out all these steps.