0% found this document useful (0 votes)
33 views2 pages

Programming 2: Tutorial 1: Java Building Blocks

This document provides instructions for completing exercises in a Java programming tutorial. The exercises involve: 1. Creating simple "Hello World" and bank account classes with fields and main methods to print messages. 2. Adding additional fields to customer and bank account classes, creating objects, and printing field values using direct access instead of getters/setters. 3. Adding getters and setters, setting field values, and printing fields using getters to demonstrate their better use over direct field access.

Uploaded by

munashe chibaya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
33 views2 pages

Programming 2: Tutorial 1: Java Building Blocks

This document provides instructions for completing exercises in a Java programming tutorial. The exercises involve: 1. Creating simple "Hello World" and bank account classes with fields and main methods to print messages. 2. Adding additional fields to customer and bank account classes, creating objects, and printing field values using direct access instead of getters/setters. 3. Adding getters and setters, setting field values, and printing fields using getters to demonstrate their better use over direct field access.

Uploaded by

munashe chibaya
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 2

Programming 2

Tutorial 1 : Java Building Blocks

Preparation
1. Ensure you use a text editor and you have JDK 8 installed on your machine
2. Create a folder CT216 and another one called T1 inside it so that you have folder structure:
CT216\T1

Exercises
1. Hello world
a) Create a sub-folder inside T1 called "ex1"
b) Create a class called HelloWorld as given in the course notes and compile it and run it

2. Classes, fields, packages


a) Create a sub-folder inside T1 called "ex2"
b) Create a class to represent a Bank Account within a package called "account"
c) Add the following instance fields for the bank account class, selecting reasonable data types
and field names to use:
 account name
 account balance : to represent the amount of money in the account
 status of the account: e.g. active, closed, frozen
 a field to represent whether or not the account can overdraw
d) Create a main method in the bank account class that just prints "In bank account main"
e) Compile and run the bank account class
f) Create a class to represent a Customer of the bank within a package called "customer"
g) Add the following instance fields for the customer class, selecting reasonable data types and
field names to use:
 name
 type : to represent type of account, e.g. "savings", "current", "credit card"
 gender
h) Create a main method in the customer class that just prints "In customer main"
i) Compile and run the customer class

3. Packages, Imports , Methods and Objects

This exercise is a follow-up to Ex 2, and uses material from Ex 2; thus Ex 2 must have been completed
successfully.

a) Create a folder called "ex3" in side T1


b) Reset the "CLASSPATH" variable on your computer so that classes in this exercise do not
clash with those from previous exercises
c) Copy all contents inside "ex2" into "ex3". After this, you must be able to compile and run the
two classes for bank account and customer that were done in Ex 2
d) Add a class field to the bank account to represent the maximum balance allowed for any
bank account
e) Compile the bank account class and run it
f) Add the following instance fields in the customer class (select appropriate types and field
names):
 for the "date created" to represent the date when the customer was created in
the system
 for bank account to represent the bank account held by the customer
g) Add the following class fields in the customer class (select appropriate types and field
names):
 for maximum number of allowed bank accounts for any customer
h) Compile and run the customer class
i) Change the main method of the customer class by adding more code to:
i. Create an object of class bank account and add a line of code to print it
ii. Create an object of class customer and a line of code to print it
j) Compile and run the customer class. What is printed out for the objects? What does it
mean?
k) Add the following lines of code to the main method of the customer class:
i. to print the name of the customer
ii. to print the maximum allowed number of bank accounts for any customer
iii. to print the value of the "bank account" instance field of the customer object
l) Compile and run the customer class. What results were printed for fields? Why are they
those values when you did not set them explicitly?
m) Add getters and setters for all the fields of the bank account and customer classes
n) Compile both classes and run them to ensure that they are still working
o) Add code in main class of the customer class to set the values of the 3 fields printed in (k)
above. Then print the fields again, but this time using getters to retrieve the values. Which
method of getting values is better; the one used in (k) and the new one used on this step?
Explain.

You might also like