DEFINE CLASSES
The key topics for this week are defining the classes including fields, constructors and methods: Instance variables of a class are pieces of information that each object of that class will need to keep a separate record for.
The purpose of the constructor is to initialize all the instance variables values and the name of the constructor is always the same as the name of the class, including upper/lower case.
The instance methods in a class are actions that will be performed by objects of that class.
Keep in mind that instance methods in a class perform actions that have something to do with the instance variables in that class.
TASK
1.Learn how to define classes
2.Practice writing classes
3.Practice writing methods
4.Practice tracing and debugging
TUTORIAL EXERCISES
The exercises marked with a * must be completed within the lab section and the rest tasks to be completed at your own study time and demonstrated to your tutor in the next week.
1.In the same class, can two methods have the exact same name? Do the method signatures /
headers have to have any difference in them?
2.* Write signatures/headers for the following function specifications:
a.The method is called findAvg. It takes in an int[] parameter and returns a
double.
b.The method is called findMode. It takes in an int[] and returns an int.
c.The method is called subString. It takes in a String, and two int formal
parameters that specify the start position and length of the substring. The method
should return a String.
3.* Trace the main method below and state what will be printed.
public static void main(String[] args) {
System.out.println(modifyNumber(3,-4));
}
public static int modifyNumber(int num, int modifier) {
if (modifier < 0) {
num = num - 2;
}
doubleThis(num);
num = doubleThis(modifier + num);
return num;
}
public static int doubleThis(int num) {
return (num + num);
}
4.Define a Circle class with a field named radius and two methods to calculate its area and
perimeter.
LABORATORY EXERCISES
* Exercise 1: Use of BankAccount Class
Download the BankAccount class from the course website.
a)Modify the BankAccount class to
-Add a new instance field called rate to represent the interest rate;
-Add new constructor(s) to initialize the interest rate as well as other instance fields if necessary;
-Add a new method to calculate the balance (assume at the end of the month) according to the interest rate.
b)Create a new public class named AccountTester in the same folder/package as the
BankAccount class.
In the AccountTester class, you are going to define two BankAccount instances/objects
for two users with different amount of initial deposit. Perform and display a sequence
transitions including: withdraw, deposit and calculate the interest.
Homework
Exercise 1: Rectangle Class
Design a class to represent rectangles which are axis-parallel in two-dimensional space. The objects created from the Rectangle class should provide basic information such as:
-width, height, and perimeter of rectangles
-area for rectangles
-x and y coordinates of the centre for rectangles
Write a class to:
a)Describe the data (instance variables).
b)Define the constructor(s).
c)Decide on the methods; write a simple list of methods. For each method (except trivial
methods) provide precise comment on what it does.
Exercise 2: Staff Class
Define a Staff class so that the Staff objects receive their biweekly salary based on their hourly rates and how many hours they worked.
(Advanced task) If they worked more than 40 hours per week, they are paid at 150% of their regulate rates for their extra working hours.
Write the class to:
b)Describe the data (instance variables).
c)Define the constructor(s).
d)Define on the methods.