Lab 2 Introducing Classes PDF
Lab 2 Introducing Classes PDF
Lab 2 Introducing Classes PDF
Object Oriented
Programming in Java
Laboratory 2
Introducing Classes
Steve Marriott
ICP 1023/2123
Laboratory Exercises
Introduction
In this laboratory session you will learn how to:
define a class;
create instances of the class you have defined;
use mutator and access methods;
use static and non-static methods.
Before working through these notes have a quick look at the lecture slides (Lectures 1 and 2)which
introduce the topic of classes and objects. Use the code in the lecture slides to help you develop a
solution to the assessed laboratory work in these notes. Also, keep a copy of the course text besides
you for reference purposes.
These notes contain assessed laboratory work.
The code for Counter and CounterTester can be found in the slides for Lecture 1.Modify
CounterTester so that the following output is generated.
Semester 2, 2015
ICP 1023/2123
Laboratory Exercises
Check that the value of both balances is correct. Make sure that you perform a calculation with
respect to each balance before you actually run the program. These two expected values constitute
test data.
Semester 2, 2015
ICP 1023/2123
Laboratory Exercises
Addendum
If BankAccount contained another instance variable e.g. accountNumber then the toString
method should create the following string:
BankAccount[balance = 500, accountNumber = 734089427]
Semester 2, 2015
ICP 1023/2123
Laboratory Exercises
forename
surname
age
height (in metres)
gender
Each piece of information must be stored using private variables of appropriate types. As the
access type of these instance variables is private, appropriate get and set methods for each
variable should be provided.
Write a toString() method to display the contents of each instance variable. A sample return
string for this method looks like the following:
Person[foreName=joe, surName = smith, age = 25, height = 1.57, gender =
male]
Also write a format()method which constructs a string containing formatting information such
that when the string is printed the data values are neatly aligned in columns and thus suitable for
screen display similar to the following.
smith
joe
25
1.57
male
davis
sian
18
1.73
female
...
...
...
...
...
To see how to write a format method look at Horstmanns invoicing case study in Chapter 11, Big
Java. Note the difference between a toString() and a format() method.
Save your source code in a file called Person.java. For a desription of what a toString method
should do, see HorstmannsBig Java: Late Objects (p.446). Note also that both methods return an
object of type String.
Write a test driver called PersonTester.java which tests all of the methods in your class.This class
must include a main method to allow execution of the program. Do not supply test values at runtime but follow the approach presented in the lecture slides with respect to test drivers. Also
provide test narrative in your output.
/Continued on next page
Semester 2, 2015
ICP 1023/2123
Laboratory Exercises
Although static variables have not been covered in the lecture course this topic is easily mastered.
See the Oracle tutorial site for more information:
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
By contrast, the use of static methods has been discussed and method invocation should follow the
pattern below:
ClassName.methodName; e.g. Math.pow(2,3);
Your main method in PersonTester should create five instances (i.e. objects) of your Person
class and then display their details together with the final value of personCountwhichshould
hold the value five.
The details of the five persons MUST NOT be read from the keyboard at run-time but instead
supplied through the test driver as parameters to an appropriate constructor.
Note that static variables are sometimes called class variables because they are shared by all
instances of the class in question.
Semester 2, 2015
ICP 1023/2123
Laboratory Exercises
Submission
Use Blackboard to submit your source code files. Each source code file must
The deadline for submitting your work is posted on Blackboard. Late submissions will be penalised
in line with School policy.
Marks for this laboratory exercise are awarded with respect to the following:
absence of comments;
failing to follow usual conventions regarding class, variable and method names;
You should submit the following Java source files via Blackboard
Person.java
PersonTester.java
Do not submit anything else it will not be marked. When submitting work it is your responsibility to
ensure that all work submitted is:
Please note that there are severe penalties for submitting work which is not your own. If you have
used code which you have found on the Internet or from any other sourcethen you must signal that
fact with appropriate program comments. Note also that to obtain a mark you must attend a
laboratory session and be prepared to demonstrate your program and answer questions about the
coding. Non-attendance will result in your work not being marked.
Steve Marriott
Semester 2, 2015
ICP 1023/2123
Laboratory Exercises
Appendix 1: BankAccount
/**
A bank account has a balance that can be changed by
deposits and withdrawals.
WARNING: CODE MAY DELIBERATELY CONTAIN ERRORS
WARNING: CODE IS INCOMPLETE
*/
public class BankAccount
{
//
Constructs a bank account with a zero balance.
public void BankAcount()
{
balance = 0;
}
//
Constructs a bank account with a given balance.
public BankAccount(doulbleinitialBalance)
{
balance = ???;
}
// Deposits money into the bank account.
public void deposit(double amount)
{
double newBalance = balance + amount;
balance = newBalance;
}
// Withdraws money from the bank account.
public void withdraw(double amount)
{
double newBalance= ???;
balance = ???;
}
// Gets the current balance of the bank account.
public doublegetBalance()
{
return ???;
}
private double balance;
}
Semester 2, 2015
ICP 1023/2123
Laboratory Exercises
/**
A class to test the BankAccount class.
*/
public class BankAccountTester
{
// Tests the methods of the BankAccount class.
public static void main(String[] args)
{
BankAccountjacksAccount = new BankAccount();
// to be completed
}
}
Semester 2, 2015