联系方式

  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-23:00
  • 微信:codinghelp

您当前位置:首页 >> Java编程Java编程

日期:2020-10-29 10:19

COMP9103 Quiz 2

? This closed-book assessment is worth 5% of the total for the course.

? This quiz comprises three sections. Marks total 25.

? Time allowed: 50 minutes.

? Answer all questions using blue or black pen on this quiz paper.

? This paper may not be removed from the classroom.

The objectives of Quiz 2 are mainly to test your understanding of concepts of OOP, class and

objects, ArrayList, File IO, and your ability to program and to trace Java code.

DO NOT TURN OVER THIS PAGE

UNTIL YOU ARE TOLD TO

SECTION A: The question in this section is worth 7 marks in total.

Suppose you are a car dealer and have both new and used cars of different brands for sale. Your current

task is to write the class (called Car) for keeping information about cars.

Define the Car class by specifying:

(1) class header;

(2) fields including a car’s brand, base price, number of years in use;

(3) two most necessary constructors (that will be used by a dealer company to add cars);

(4) a method to test whether a car is new – if the number of years in use is 0, the car is brand new;

otherwise, if the number of years in use greater than 0, the car is a used car. The method will

return true for a new car, otherwise return false;

(5) a method to calculate and return a car’s price: the base price for a new car, and the price will

decrease at rate of 1% per year as to the base price for a used car.

Write your Car class here:

2/5

SECTION B: The question in this section is worth 11 marks in total.

Given the BankAccount class as below

/**

* A bank account has a balance that can be changed by deposits and

* withdrawals.

*/

public class BankAccount {

private double balance;

private String owner;

/**

* Constructs a bank account with a zero balance.

*/

public BankAccount() {

balance = 0;

owner=null;

}

/**

* Constructs a bank account with a given balance.

*/

public BankAccount(String name) {

balance = 0;

owner=name;

}

public BankAccount(String name, double bl) {

balance = bl;

owner=name;

}

/**

* Deposits money into the bank account.

*/

public void deposit(double amount) {

if (amount > 0)

balance = balance + amount;

else System.out.println(owner + ": " + "Wrong operation!");

}

/**

* Withdraws money from the bank account.

*/

public void withdraw(double amount) {

if (balance >= amount)

balance = balance - amount;

else System.out.println(owner + ": " + "No enough balance!");

}

/**

* Gets the current balance of the bank account.

* @return the current balance

*/

public double getBalance() {

return balance;

}

public String getOwner() {

return owner;

}

}

3/5

(1) Assume we have a client class that will use the BankAccount class. The main() method of this

client class is defined as below. Specify what will be printed after the following statements are

executed.

public static void main(String args[]) {

ArrayList<BankAccount> accounts = new ArrayList<BankAccount>();

accounts.add(new BankAccount("Tom", 100));

accounts.add(new BankAccount("Ben", 1000));

accounts.add(new BankAccount("Peter"));

accounts.add(2, new BankAccount("Jerry", 30));

accounts.get(3).deposit(-10);

accounts.remove(1);

accounts.get(1).withdraw(90);

for (BankAccount acnt : accounts) {

System.out.println(acnt.getOwner() + "'s balance: "

+ acnt.getBalance());

}

}

Write your answer here:

4/5

(2) Assume now we have Bank class. Define ownersBalnsNoLessThan() method of the

Bank class to extract and return a list of accounts’ owners whose balance is not less than a given

amount.

private ArrayList<BankAccount> accounts;

//reference variable for BankAccount list

/**

* return a list of accounts’ owners whose balance is not less

* than a given amount specified as the parameter

*/

// question (2): write your method header and method body

Write your answer here:

5/5

Section C: The question in this section is worth 7 marks in total.

Write a countWords() method which reads a given file, counts and returns the number of times that

a user specified word occurs in the file.

Write your answer here:

END OF EXAMINATION


版权所有:留学生编程辅导网 2018 All Rights Reserved 联系方式:QQ:99515681 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。