讲解:EBU6042、Java、JAVA、DOMC/C++|R

EBU6042 Paper A ‐ SOLUTIONS 2016/17Question 1a) The questions below refer to JavaScript and the DOM:[9 marks]i) Indicate what the DOM abbreviation stands for and name THREE examples of objects found inthe DOM.(4 marks)ii) Consider the JavaScript code fragment in Figure 1 and fill in the THREE gaps in the sentencesbelow; you are only required to write down what the blanks marked 1 to 3 correspond to.Figure 1With regards to the code in Figure 1, if the user enters a response and clicks the OK button, thenthe value of num will be _____1_____. If instead the user enters a response and clicks the Cancelbutton, the value of num will be _____2_____. If the user clicks the OK button without enteringa response, then the value of num will be _____3_____.(3 marks)iii) Briefly describe the concept of event handler and indicate a typical use of onSubmit in awebpage.(2 marks)Do not write inthis columni) 4/9DOM = Document Object Model [1 mark]THREE examples of objects in the DOM: [3*1 mark: 3 correctly identified objects]window, location, checkbox, submit, reset, button, radio, textii) 3/91. whatever value the user entered [1 mark]2. Null [1 mark]3. an empty string [1 mark]iii) 2/9Event handler: this is a command that JavaScript uses to deal with actions performedby the user while visiting a webpage. [1 mark]onSubmit does form validation using JavaScript, when the user submits an HTMLform. OR onSubmit triggers a function to run when a form is submitted. [1 mark]9marksnum = prompt(Please enter a positive number: , ); EBU6042 Paper A‐ SOLUTIONS 2016/17 b) Consider the HTML code shown in Figure 2 and answer the questions below: [16 marks]i) Assume that the code in Figure 2 is stored in file riverBusPlanner_home.html. Sketch howthe contents of this file would be displayed by a web browser. The webpage is for a company thatruns a river bus service, which allows people to travel by boat along the River Thames in London.(8 marks)ii) Write the code for the JavaScript function processInit() that is activated by clicking on thePlanner button, such that the following is checked:1. That the user has filled in the email address and that it contains a ‘@’ sign in a position otherthan at the start; if that field is empty or does not include a ‘@’ sign, then an alert box shouldbe generated with an appropriate message.2. That the start and end river bus stops are different; if they are the same, an alert box shouldbe generated with an appropriate message.If the above two checks are satisfied, then the webpage riverBusPlanner_journeys.htmlis loaded. Note: It is not necessary to know the contents of this second HTML page.Your answer must also state where you would need to add this JavaScript function, in the HTMLcode describing the webpage in Figure 2.(8 marks)Figure 2 Thames River Bus Thames River Bus Service: Journey Planner Email address: Time of day: Peak time Off-peak time Choose a river bus stop for the start and end of your journey: Start& Canary Wharf Greenland Masthouse Terrace & & & & & & & End& Masthouse Terrace Greenland Canary Wharf Press Planner to look at available route options: Do not write inthis columni) 8/16[1 mark]: page title and header are both shown;[1 mark]: email address textbox is shown, preceded by the appropriate label;[2 marks]: 2 radio buttons on the same line, each followed by the appropriate label;[2 marks]: 2 menus shown on the same line, each preceded by the correct label, and each with the appropriate default option shown;[1 mark]: Planner button is shown;[1 mark]: the sketched page has a sensible overall layout.ii) 8/16function processInit() { firstForm = document.forms[formInit]; if (formInit.emailAddress.value == ) alert(Please fill in your email address!); else if (formInit.emailAddress.value.indexOf(@) alert(Invalid email address!); else if (formInit.start.value == formInit.end.value) alert(Your origin and destination stops must be different!); else window.location.href=riverBusPlanner_journeys.html;} EBU6042?Paper?A?‐?SOLUTIONS 2016/17Page?4?of?17Do not write inthis column[1 mark: correct function declaration][2 marks: 4 checking conditions ? 1) start-end check, 2) empty email, 3) if @ sign exists, 4) @ sign not first character; the last two could be 1 or 2 conditions][3*1 marks: 3 alert boxes with sensible messages][1 mark: an attempt at redirection]The function processInit() should be placed in the section of the HTMLpage [1 mark].16marksQuestion marking: 9 16 25Question 2a) Consider the statements below about JSPs, servlets and sessions. Identify the statements that areFALSE and correct them.[7 marks]1. A session object is created the first time that a browser requests a servlet or JSP from a website.2. The current session object is available only to servlets.3. A servlet communicates with the servlet engine in the web server via a request and responseobject of the HttpServletRequest and HttpServletResponse classes respectively, in theform of streams.4. JSPs use delimiters such as to include Java code that the JSP engine in the web serverthen converts directly to HTML code.5. In the Tomcat web server, the web.xml file has the tag that identifies thename and the type of the servlet engine used by the web server.Do not write inthis columnFALSE statements: 2, 4, 5 [3*1 mark]Correction of FALSE statements:2. The current session object is available to both servlets and JSPs. [1 mark]4. JSPs use delimiters such as to include Java code that the JSP engine inthe web server then converts to a servlet. [1 mark]5. In the Tomcat web server, the web.xml file has the tag thatidentifies the class name of the serlvet or its alias name. [2 marks]7marksb) Consider the webpage shown in Figure 3 and answer the questions below:[15 marks]i) Assume that a user’s personal data is sent to a JSP called StoreData.jsp, once the Submitbutton is clicked. Write the code for a JavaBean called UserData that will store the user’s detailson the server side; you can assume that the JavaBean is stored in a package called user, and thatthe user data parameters are called name and age.(10 marks)ii) Write the code for the JSP called StoreData.jsp that uses the JavaBean you wrote in part i)to collect the user’s data. This JSP then loads another JSP called DisplayData.jsp, uponclicking Continue.Note: You are not required to write the code for DisplayData.jsp.(5 marks)Figure 3Do not write inthis columni) 10/15package user; [1 mark]import java.io.*;public class UserData implements Serializable { [2 marks] private String name; [1 mark] private int age; [1 mark] public UserData() { } [1 mark] public void setName(String value) { name = value; } [1 mark] public void setAge(int value) { age = value; } [1 mark] public String getName() { return name; } [1 mark] public int getAge() { return age; } [1 mark]} Do not write inthis columnii) 5/15[2 marks] [1 mark] Continue [1 mark] [1 mark: sensible overall structure]15marksc) Servlets have THREE lifecycle methods.[3 marks]i) Describe what the init() method does and when it is used.(2 marks)ii) Name the other TWO servlet lifecycle methods.(1 mark)Do not write inthis columni) 2/3The init() method is used by the container to initialise the servlet; it is invokedonly once in the lifecycle of the servlet. [2 marks]ii) 1/3Other TWO servlet lifecycle methods: service() and destroy(EBU6042作业代写、Java编程作业调试、代写JAVA语言作业、代做DOM留学生作业 帮做C/C++编程|代做R语言) [2*0.5 mark]3marksQuestion marking: 7 15 3 25Question 3a) The questions below refer to threads:[12 marks]i) What is a daemon thread? Which method in Thread is used to make a thread a daemon?(2 marks)ii) Write the code for a thread called PrintThread that accepts a String as a parameter in itsconstructor. When running, PrintThread should print its String to the screen.(4 marks)iii) Write a main() method that constructs 100 new instances of PrintThread and then starts themexecuting concurrently.(4 marks)iv) Will the output of main() be deterministic, i.e. will the console output always be the same?Justify your answer.(2 marks)Do not write inthis columni) 2/12A daemon thread is a thread that is automatically terminated when its parent dies.[1 mark]The method is setDaemon(true). [1 mark]ii) Note: The student can implement the thread by either extending the Thread class 4/12 OR by implementing Runnable.public class PrintThread extends Thread { [1 mark] private String toPrint = null; public PrintThread(String toPrint) { [1 mark] this.toPrint=toPrint; } public void run() { [1 mark] System.out.println(toPrint); [1 mark] }} // End of class Do not write inthis columniii) 4/12public static void main(String[] args) { [1 mark] for (int i=0; i PrintThread thread = new PrintThread(Thread-+i); [1 mark] thread.start(); [1 mark] OR (alternative approach – also worth 2 marks) Note: Please ensure the approach matches the student’s answer to part ii). MyRunnable runnable = new MyRunnable(Thread- + i); Thread thread = new Thread(runnable); thread.start(); }}iv) 2/12The output will be non-deterministic. [1 mark]The order is based on when the threads get scheduled, which may change each timethe code is executed. [1 mark]12marksb) The code in Figure 4 is a simplified form of a read-write lock.[9 marks]Figure 4i) Explain the difference between a simple lock and a read-write lock. Why might a read-write lockachieve better performance?(2 marks)ii) Explain the use of wait() in general, and its use in method lockWrite() of Figure 4 inparticular.(4 marks)iii) Explain the use of notify() and notifyAll() in general. Also explain how notifyAll()is used in method unlockRead() of Figure 4 in particular.(3 marks)Do not write inthis columni) 2/9A read-write lock does not allow multiple threads to read or write data at the sametime. [1 mark]However, concurrent reads are usually acceptable, so this is not a good idea. [1 mark]public class ReadWriteLock { private int readers = 0; private int writers = 0; private int writeRequests = 0; public synchronized void lockRead() throws InterruptedException { while(writers > 0 || writeRequests > 0) { wait(); } readers++; } public synchronized void unlockRead() { readers--; notifyAll(); } public synchronized void lockWrite() throws InterruptedException { writeRequests++; while(readers > 0 || writers > 0) { wait(); } writeRequests--; writers++; } public synchronized void unlockWrite() throws InterruptedException { writers--; notifyAll(); }} EBU6042?Paper?A?‐?SOLUTIONS 2016/17Page?11?of?17Do not write inthis columnii) 4/9wait() tells the calling thread to give up the monitor and go to sleep until someother thread enters the same monitor and calls notify(). [2 marks]If there are any readers or writers wanting access to the lock, the thread calling thismethod (which wants to do a write) is put in the “wait set”. [2 marks]iii) 3/9notify() wakes up an arbitrary thread that called wait() on the same object.[1 mark]notifyAll() wakes up all the threads that have called wait() in no particularorder, so that they can try to acquire the monitor. [1 mark]In unlockRead() there is one less thread wanting read access and notifyAll()tells all the threads in the “wait set”; these could be threads wanting to do reads orwrites. [1 mark]9marksc) The questions below refer to sockets and threads:[4 marks]i) Write the Java code to create a socket that connects to a server with the IP address 10.0.0.1. Itshould connect to the server on port 80.(2 marks)ii) Write the Java code to create a server-side listening socket. It should listen on port 80.(1 mark)iii) Why is it important for a web server to be built using multiple threads?(1 mark)Do not write inthis columni) 2/4Socket s = new Socket(10.0.0.1, 80);[1 mark: IP and port; 1 mark: socket]ii) 1/4ServerSocket ss = new ServerSocket(80); [1 mark]Do not write inthis columniii) 1/4A web server must deal with multiple concurrent users. Thus, each user must beserved in its own thread. [1 mark]4marksQuestion marking: 12 9 4 25Question 4a) Write a simple webpage that looks like the one in Figure 5. Ensure that you put all HTML elementsin, so that it could be correctly loaded by a web browser.[4 marks]Figure 5Do not write inthis columnNote: Marks are allocated for opening and closing the HTML tags correctly. If a tag is opened correctly, but not closed correctly, please allocate half marks. [0.5 mark] [0.5 mark] Here is a list [1 mark] Note: Other tags are allowed, e.g. , , . [1 mark] Bread [1 mark] Crisps Meat 4marksb) The following questions relate to Java’s Remote Method Invocation (RMI).[16 marks]i) Serializable is a marker interface. What is a marker interface? What does it mean when youwrite a class that implements Serializable?(2 marks)ii) Explain the concept of skeletons and stubs in RMI. Include the following in your answer:? Does each one operate on the client or on the server?? What are the steps they take when a Remote Method Invocation is executed?(8 marks)Here is a list: Bread Crisps Meat iii) Write a remote interface for a server that adds two numbers together. The remote interface shouldcontain a single method call add(). The interface should be called AddServerInterface.(4 marks)iv) Describe what the client and server objects use the rmiregistry for in RMI. Note: You do notneed to write code.(2 marks)Do not write inthis columni) 2/16A marker interface is one that does not contain any methods; it just signalssomething. [1 mark]Implementing Serializable indicates that a class can be serialized by Java.[1 mark]ii) 8/16SKELETONA class that sits on the server side. [1 mark]Receives remote method calls and parameters. [1 mark]Invokes the method locally. [1 mark]Sends the return value back to the client. [1 mark]STUBA class that sits on the client side. [1 mark]Presents the same remote interface as the server object. [1 mark]Accepts any method invocations and forwards them to the server. [1 mark]Receives any return results. [1 mark]iii) 4/16public interface AddServerInterface [1 mark] extends Remote { [1 mark] public int add(int a, int b) [1 mark] throws RemoteException; [1 mark]} Do not write inthis columniv) 2/16The rmiregistry is used by servers to register remote objects. [1 mark]The rmiregistry is used by clients to lookup remote objects. [1 mark]16marksc) The following questions are based on the HTTP response in Figure 6.[5 marks]Figure 6i) What is the purpose of the status code in an HTTP response? What is the status code of theresponse shown in Figure 6?(2 marks)ii) What type of content is in the payload of the HTTP response in Figure 6?(1 mark)iii) Will a browser cache this HTTP response? Explain your answer.(2 marks)Do not write inthis columni) 2/5The status code tells the client what is the status of the message. [1 mark]The status code in the response is 200. [1 mark]ii) 1/5The content in this response is HTML. [1 mark]HTTP/1.1 200 OKTransfer-Encoding: chunkedDate: Sat, 13 Mar 2016 11:37:51 GMTServer: ApacheConnection: closeCache-Control: no-cacheContent-Type: text/html; charset=UTF-8Last-Modified: Sat, 15 Jan 2016 09:50:17 GMTContent-Encoding: gzip Do not write inthis columniii) 2/5The browser will not cache the content [1 mark], because the Cache-Controlheader is set to no-cache [1 mark].5marksQuestion marking: 4 16 5 25转自:http://ass.3daixie.com/2019012368012149.html

你可能感兴趣的:(讲解:EBU6042、Java、JAVA、DOMC/C++|R)