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 W7 
OBJECT-ORINETED DESIGN 
The key topic for this week is static and instance members of the class, the introduction to ArrayList, 
and File IO.  
 
TASK 
1. Learn and understand the differences between the static and instance members 
2. Practice using ArrayList 
3. File IO 
  
 
TUTORIAL EXERCISES 
1. Trace what happens at each of these instructions and explain what will the arraylist strings 
contains after the following instructions: 
ArrayList names = new ArrayList(); 
names.add("A"); 
names.add(0, "B"); 
names.add(0,"C"); 
names.remove(1); 
2. BankAccount Class: Analyze the BankAccount class as below and answer the questions:  
/** 
   A bank account has a balance that can be changed by 
   deposits and withdrawals. 
*/ 
public class BankAccount 
{   
     private int accountNumber; 
     private double balance; 
 
   /** 
      Constructs a bank account with a zero balance 
      @param anAccountNumber the account number for this account 
   */ 
   public BankAccount(int anAccountNumber) 
   {    
      accountNumber = anAccountNumber; 
      balance = 0; 
   } 
   /** 
      Constructs a bank account with a given balance 
      @param anAccountNumber the account number for this account 
      @param initialBalance the initial balance 
   */ 
   public BankAccount(int anAccountNumber, double initialBalance) 
   {    
      accountNumber = anAccountNumber; 
      balance = initialBalance; 
   } 
   /** 
      Gets the account number of this bank account. 
      @return the account number 
   */ 
   public int getAccountNumber() 
   {    
      return accountNumber; 
   } 
 
 
 
 
2 
COMP9103 Software Development in Java        Tutorial and Lab W7
   /** 
      Deposits money into the bank account. 
      @param amount the amount to deposit 
   */ 
   public void deposit(double amount) 
   {   
      double newBalance = balance + amount; 
      balance = newBalance; 
   } 
   /** 
      Withdraws money from the bank account. 
      @param amount the amount to withdraw 
   */ 
   public void withdraw(double amount) 
   {    
      double newBalance = balance - amount; 
      balance = newBalance; 
   } 
   /** 
      Gets the current balance of the bank account. 
      @return the current balance 
   */ 
   public double getBalance() 
   {    
      return balance; 
   } 
} 
a. What is the value of b.balance after the following operations? 
BankAccount b = new BankAccount(10); 
b.deposit(5000); 
b.withdraw(b.getBalance() /2); 
b. If b1 and b2 are two objects of the BankAccount class, consider the following instructions: 
b1.deposit(b2.getBalance()); 
b2.deposit(b1.getBalance()); 
Are the balances of b1 and b2 now identical? Explain. 
c. Create an ArrayList that will contain BankAccount objects and store it in a variable called 
accounts. Then add a new BankAccount object in the name of Mary and with an initial 
balance of 100.  
 
3. Explain the errors in the following code segment  
public class Car { 
    private int speed; 
          private final int SPEEDLIMIT 
         //... 
    public static boolean goFaster() { 
         If (speed < SPEEDLIMIT) { 
                  speed++; 
             return true; 
   } 
    else return false;                  
     } 
 
 
 
  
 
  
3 
COMP9103 Software Development in Java        Tutorial and Lab W7 
LABORATORY EXERCISE  
 
File IO 
Create a class which includes methods to create a file and search the file for a given value..  
a. Write a method that creates a file (the file name needs to be specified by the parameter) to 
accommodate strings that will be obtained from user interactive inputs by Scanner.  
b. Define a method to:  
i. read in a file (name is specified as the parameter)  
ii. search the file for the string specified by the parameter  
iii. and finally display the searching result and the position of the string in the file on screen 
display. 
c. a main() method to invoke the methods.  
 
 
 
Homework Exercise: Work on your Assignment 
1. Design and encode the class(es) for your assignment, which include(s): 
a. Fields;  
b. Constructors; 
c. Methods (can be stubs but with comments to explain their functionalities) 
 
2. Encode a corresponding testing class for each of your classes to: 
a. create the objects  
b. add the verified objects to the list 
c. remove an object from the list  
 
3. Read in and process the input file and instruction file.