讲解COMPS311F、辅导Java、辅导data、Java程序设计调试 讲解数据库SQL|辅导R语言程序

- 首页 >> C/C++编程
COMPS311F, COMPS312F Assignment 1
Note that this assignment is not counted towards your final score. If you want to submit the
assignment, please do so by 16 Nov 2018 so that your tutor may have time to do the marking
and return to you before the test. Since the assignment is not counted towards your final
score, so please do not copy from others. Do whatever you can.
Question 1 [15 marks]
State whether each of the followings is true or false about Java:
(a) All Java keywords are reserved words. [3]
F
(b) No two methods of a class can have the same signature. [3]
T
(c) A static method of a class cannot be overridden by a subclass. [3]
T
(d) A private method of a class cannot be overridden by a subclass. [3]
T
(e) A protected method of a class cannot be overridden by a subclass. [3]
F
Question 2 [19 marks]
You can assume that the following static methods of the class Bank are available for you to use:
public static String getPassword(String cardNo)
public static int getBalance(String cardNo)
public static void setBalance(String cardNo, int balance)
The method getPassword(cardNo) returns the password for the account with the corresponding
cardNo of an ATM card. The method getBalance(cardNo) returns the balance of the account
with the corresponding cardNo in dollars.
The method setBalance(cardNo,balance) is used to set the balance of the account with the
corresponding cardNo.
Write a Java class which represents an ATM machine. The class should look like this:
public class ATM {
.....
public void insertCard(String cardNo, String password) throws InvalidPasswordException {
...
}
public void getMoney(int amount) throws NotEnoughBalanceException,
PasswordNotCheckedException, NotMultipleOfHundredsException {
....
}
public void ejectCard() {....}
}
The method insertCard(cardNo,password) is invoked after the user has inserted the card into
the ATM machine and typed in the password. The first parameter is the card number of the
ATM card. The second parameter is the password input by the user. The method will check
whether this password is correct. If not, the InvalidPasswordException will be thrown. If yes,
the class will record that the card password has been verified.
The getMoney(amount) method is invoked when the user wants to get money from the ATM
machine. The only parameter is the amount in dollars that the user wants. The method will
first check that the password has been verified. If not the PasswordNotCheckedException will
be thrown. Then, it will check whether the amount input is positive and a multiple of 100. If
not, the NotMultipleOfHundredsException will be thrown. Then, it will check whether the user
has enough money in the account for this request. If not, the NotEnoughBalanceException will
be thrown. If yes, the amount of money will be deducted from the account balance.
The method ejectCard() is invoked when the user has finished the transaction. This method
will clear the record that the password has been checked.
Question 2
10 marks for insertCard,
6 marks for getMoney,
3 mark for ejectCard
public class ATM {
private boolean passwordChecked = false;
private String cardNo;
public void insertCard(String cardNo, String password) throws InvalidPasswordException {
if (!Bank.getPassword(cardNo).equals(password)) {
throw new InvalidPasswordException();
}
passwordChecked=true;
this.cardNo=cardNo;
}
public void getMoney(int amount) throws NotEnoughBalanceException,
PasswordNotCheckedException,
NotMultipleOfHundredsException {
if (!passwordChecked) {
throw new PasswordNotCheckedException();
}
if (amount%100!=0) {
throw new NotMultipleOfHundredsException();
}
if (amount>Bank.getBalance(cardNo)) {
throw new NotEnoughBalanceException();
}
Bank.setBalance(cardNo,Bank.getBalance(cardNo) - amount);
}
public void ejectCard() {
passwordChecked=false;
}
}
Question 3 [15 marks]
Write a multithreaded program which reads in serval files at the same time. The files contain a
number of integers which have been written to them using the writeInt() method of
DataOutputStream. Each file will be handled by one thread. You need to find the maximum
and minimum of all the integers in the files. You can assume that there is at least one integer
in a file and there is at least one input file. For example, after executing the following
command:
java MaxMin file1 file2 file3
the program will create three threads to read in integers in file1, file2 and file3. Then the
output may look like this:
minimum: -348
maximum: 4037
[Hint: You need two synchonized methods. One for updating the current minimum and
maximum when an integer is read. One for reporting the finish of a thread. If all threads
have finished, then the minimum and maximum will be printed.]
Question 3
public class MinMax {
private int min = Integer.MAX_VALUE;
private int max = Integer.MIN_VALUE;
private int noUnfinished;
public synchronized void reportFinished() {
if (--noUnfinished == 0) {
System.out.println("minimum:" + min + "\nmaximum:" + max + "\n");
}
}
public synchronized void setMinMax(int i) {
if (i < min) {
min = i;
} else if (i > max) {
max = i;
}
}
public class MyThread extends Thread {
private DataInputStream input;
public MyThread(String file) {
try {
input = new DataInputStream(new FileInputStream(file));
} catch (Exception e) { }
}
public void run() {
try {
while (true) {
int no = input.readInt();
setMinMax(no);
}
} catch (Exception e) {
} finally {
reportFinished();
}
}
}
public MinMax(String st[]) {
this.noUnfinished=st.length;
for (String s : st) {
new MyThread(s).start();
}
}
public void main(String st[]) {
new MinMax(st);
}
}
6 marks for creating a thread for each file,
4 marks for reading the integers and compare with the current record.
5 marks for writing out the numbers.
Question 4 [18 marks]
State the problem in each of the following Java code fragments.
(a) ...
public void fun() {
if (a==4) {
notifyAll();
}
}
...
(a) the notifyAll() method should be inside a synchronized method or a
synchronized statement.
(b) ...
public void fun(int a[], int b[]) {
if (a.length!=b.length) {
throw new Exception(
"the two array must be of equal size");
}
.....
}
...
(b) the thrown Exception is not handled.
(c) ...
int i;
if (a==3) {
i=a+3;
} else {
a=i+3;
}
...
(c) i is not initialized before use.
(d) ...
for (int i=0;i<10;i++) {
...
}
if (i==8) {
...
}
...
(d) i is not accessable outside the for loop.
(e) ...
int i=4;
for (int j=0;j<10;j++) {
int i=j+4;
...
}
...
(e) i is defined again which is not allowed.
(f) ...
int a[]=new int[10];
for (int i=1;i<=10;i++) {
a[i]=i;
}
...
(f) there will be an out of bound exception when i is 10.
Question 5 [33 marks]
Write a multithreaded Java server which has the following properties:
• It listens to requests at port 12345.
• When a request arrives,
o it creates a new thread to serve the request.
o each client repeatedly sends in integers to the client with the writeInt()
method of DataOutputStream until the client disconnects from the server.
The server will keep a record of the maximum of all the integers it receives
from all the clients. This record is set to Integer.MIN_VALUE initially.
When the server receives an integer from a client, it will compare the
integer with the record. If the value is larger than the record, the record
will be updated and a string "Your integer is the largest at the moment."
will be sent to the client. Otherwise, the string "The current maximum is
xxx." will be sent to the client where xxx is the current maximum.
o Both the server and client use the DataInputStream and DataOutputStream
for communication.
• Note that you only need to provide the implementation for the server, not the
client.
[Hint: you need a synchronized method to deal with the current maximum value as multiple
threads are trying to change this value.]
[33]
Question 5
public class Server {
private int maximum=Integer.MIN_VALUE;
synchronized public String reply(int v) {
if (v>maximum) {
maximum=v;
return "Your integer is the largest at the moment.";
} else {
return "The current maximum is "+maximum+".";
}
}
public class MyThread extends Thread {
private DataInputStream input;
private DataOutputStream output;
private Socket s;
public MyThread(Socket s) {
this.s = s;
try {
input = new DataInputStream(s.getInputStream());
output = new DataOutputStream(s.getOutputStream());
} catch (Exception e) {
}
}
public void run() {
try {
while (true) {
int v=input.readInt();
output.writeUTF(reply(v));
}
} catch (Exception e) {
try {
s.close();
} catch (Exception ee) {

}
}
}
}
6 marks for setting the synchronized method for checking the max.
6 marks for setting a thread for a file,
18 marks for reading in the integers, checking, replying,
3 mark for ending the thread when an exception is thrown.
public void main() {
try {
ServerSocket ss=new ServerSocket(12345);
Server server=new Server();
while (true) {
Socket s=ss.accept();
MyThread th=new MyThread(s);
th.start();
}
} catch (IOException ex) {
}
}

public static void main(String st[]) {
Server s=new Server();
s.main();
}
}

站长地图