代做COMPS311F、代写Java、代写data、Java程序设计调试代做数据库SQL|代写R语言程序

COMPS311F, COMPS312F Assignment 1Note that this assignment is not counted towards your final score. If you want to submit theassignment, please do so by 16 Nov 2018 so that your tutor may have time to do the markingand return to you before the test. Since the assignment is not counted towards your finalscore, 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]FQuestion 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 correspondingcardNo of an ATM card. The method getBalance(cardNo) returns the balance of the accountwith the corresponding cardNo in dollars. The method setBalance(cardNo,balance) is used to set the balance of the account with thecorresponding 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 intothe ATM machine and typed in the password. The first parameter is the card number of theATM card. The second parameter is the password input by the user. The method will checkwhether 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 ATMmachine. The only parameter is the amount in dollars that the user wants. The method willfirst check that the password has been verified. If not the PasswordNotCheckedException willbe thrown. Then, it will check whether the amount input is positive and a multiple of 100. Ifnot, the NotMultipleOfHundredsException will be thrown. Then, it will check whether the userhas enough money in the account for this request. If not, the NotEnoughBalanceException willbe 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 methodwill clear the record that the password has been checked.Question 210 marks for insertCard,6 marks for getMoney,3 mark for ejectCardpublic 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 anumber of integers which have been written to them using the writeInt() method ofDataOutputStream. Each file will be handled by one thread. You need to find the maximumand minimum of all the integers in the files. You can assume that there is at least one integerin a file and there is at least one input file. For example, after executing the followingcommand:java MaxMin file1 file2 file3the program will create three threads to read in integers in file1, file2 and file3. Then theoutput may look like this:minimum: -348maximum: 4037[Hint: You need two synchonized methods. One for updating the current minimum andmaximum when an integer is read. One for reporting the finish of a thread. If all threadshave finished, then t代做COMPS311F作业、代写Java实验作业、代写data课程作业、Java程序设计作业调试 代做数据库SQL|代写he minimum and maximum will be printed.]Question 3public 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 = 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 asynchronized 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 ...}if (i==8) { ...}...(d) i is not accessable outside the for loop.(e) ...int i=4;for (int j=0;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 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 receivesfrom all the clients. This record is set to Integer.MIN_VALUE initially.When the server receives an integer from a client, it will compare theinteger with the record. If the value is larger than the record, the recordwill 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 isxxx. will be sent to the client where xxx is the current maximum.o Both the server and client use the DataInputStream and DataOutputStreamfor communication.• Note that you only need to provide the implementation for the server, not theclient.[Hint: you need a synchronized method to deal with the current maximum value as multiplethreads are trying to change this value.][33]Question 5public 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(); }}转自:http://www.6daixie.com/contents/9/4981.html

你可能感兴趣的:(代做COMPS311F、代写Java、代写data、Java程序设计调试代做数据库SQL|代写R语言程序)