Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
 
 
  
 
  
1 
COMP9103 Software Development in Java         Tutorial and Lab 05 
  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 and the rest tasks to be completed at your own 
study time. 
 
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 method 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. * Assume that both Tracing class and ModifyNum class (defined as below) are in a same 
project package. Trace the main() method defined in Tracing class and state what will be 
printed. 
 
 
public class Tracing { 
  public static void main(String[] args) { 
    ModifyNum numTrace = new ModifyNum(); 
    System.out.println(numTrace.modifyNumber(3,‐4));  
  } 
} 
 
public class ModifyNum {
    public int modifyNumber(int num, int modifier) { 
      if (modifier < 0) {  
        num = num ‐ 2;    
      }    
      doubleThis(num);    
 
 
 
 
2 
COMP9103 Software Development in Java         Tutorial and Lab 05
 
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 a new static field called AccNo to represent the account number; 
- 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.  
Exercise 2: Define a Circle class 
Define a Circle class with a field named radius and two methods to calculate its area 
and perimeter respectively.  
 
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 receives their biweekly salary based on their hourly rates and how 
many hours they worked. Write the class to: 
b) Describe the data (instance variables)  
c) Define the constructor(s) 
d) Define on the methods 
 
 
      num = doubleThis(modifier + num);  
      return num;  
    }  
    public  int doubleThis(int num) {  
      return (num + num);  
    } 
} 
 
 
 
  
 
  
3 
COMP9103 Software Development in Java         Tutorial and Lab 05 
 
Exercise 3: 1st version of the necessary classes for your assignment 
Read your assignment specification and design the necessary classes to:  
a) Describe the data (instance fields/variables) 
b) Define the constructor(s) 
c) Outline the methods