COMP S311 Online Exam Time Allowed: 3 hours java编程考试代做 Instructions - Answer ALL questions in Part I. [60%] - Answer no more than FOUR (4) questions in Part II. [40%] - The total mark...View details
EBU4201 Introductory Java Programming
Java编程考试代考 Instructions Before the start of the examination 1) Place your BUPT and QM student cards on the corner of your desk so
Time allowed 2 hours
Answer ALL questions
NOT allowed: electronic calculators and electronic dictionaries.
Before the start of the examination
1) Place your BUPT and QM student cards on the corner of your desk so that your picture is visible.
2) Put all bags, coats and other belongings at the back/front of the room. All small items in your pockets, including wallets, mobile phones and other electronic devices must be placed in your bag in advance. Possession of mobile phones, electronic devices and unauthorised materials is an offence.
3) Please ensure your mobile phone is switched off and that no alarm will sound during the exam. A mobile phone causing a disruption is also an assessment offence.
4) Do not turn over your question paper or begin writing until told to do.
During the examination Java编程考试代考
1) You must not communicate with or copy from another student.
2) If you require any assistance or wish to leave the examination room for any reason, please raise your hand to attract the attention of the invigilator.
3) If you finish the examination early you may leave, but not in the first 30 minutes or the last 10 minutes.
4) For 2 hour examinations you may not leave temporarily.
5) For examinations longer than 2 hours you may leave temporarily but not in the first 2 hours or the last 30 minutes.
At the end of the examination
1) You must stop writing immediately – if you continue writing after being told to stop, that is an assessment offence.
2) Remain in your seat until you are told you may leaveEBU4201 Paper A
Question 1 Java编程考试代考
In this question you are asked to write a Java class to represent the concept of probability. This class must hold a single (encapsulated) variable representing the probability of something occurring, e.g. a probability of 0.1 indicates that there is 10% chance of something happening.
You must write the Probability class, including: TWO constructors (Probability), get/set methods (i.e. setProbability() and getProbability()), and the toString() method. The skeleton structure of the class is given in Figure 1 – several parts of the code are missing, which you must fill in. These are indicated by the gaps (labelled A–I) and TODO.
· You must write the access modifiers and return types, for all the methods/constructors.
· You must also write the body of the methods (TODO means that you need to write something in the method body).
· When setting the probability value, your code must ensure that the value set is between 0 and 1 inclusive (i.e. it must be p>=0 and p<=1, where p is the probability).
Given the Probability class in part a) of the question, you must now write a series of test code to demonstrate that the class functions correctly. Write the following test cases:
i) Write test cases to test that the constructor which takes a double as an argument. This must include the Java code, as well as a short description of what the code does. (2 marks)
ii) Write test cases to test that the set method is correct. This must include the Java code, as well as a short description of what the code does. (2 marks)
Briefly describe the concept of encapsulation, why it is important, and how it is implemented in a Java class. Is there any case where you would not encapsulate data? If so, explain your answer. [3 marks]
In Java, there is a keyword abstract which has two distinct uses. Describe what the abstract keyword means, when it is used to define an abstract class, and an abstract method. [3 marks]
Question 2 Java编程考试代考
The questions below refer to the concepts of inheritance and arrays in Java:
i) What is method overriding? How do you override a method in a superclass? (2 marks)
ii) Write a class declaration for a class called Lion that inherits from (i.e. extends) a class called Animal. (1 mark)
iii) Write the Java constructor for the Animal class mentioned in part ii). It should accept the name of the Animal as a parameter. You can assume that a method called setName(String name) already exists in the class Animal. (2 marks)
iv) Write the Java constructor for the Lion class that extends Animal (first mentioned in part ii)). The constructor should also accept the name of the Lion as a parameter. (1 mark)
v) Write a Java class called Zoo. It must contain an array that holds a list of Animal objects (first mentioned in part ii)) and a method called addAnimal that adds new Animal objects into the array. (3 marks)
The questions below relate to interfaces in Java:
i) Can an interface in Java contain a method implementation? (1 mark)
ii) Imagine that you are writing the software for a graphics application (e.g. Microsoft Paint). Write the code for an interface called IGraphicsShape. This interface must include one void method called drawShape(). (2 marks)
iii) Write a class called Square. This class must implement the IGraphicsShape interface written in part ii). The drawShape() method should print out the message “Drawing Square” when invoked. (2 marks)
iv) State TWO similarities between interfaces and abstract classes. (2 marks)
The questions below relate to memory management in Java:
i) Java maintains TWO memory spaces, the Stack and the Heap. Which memory space is used to store objects? (1 mark)
ii) Objects in Java are automatically removed from memory by the garbage collector. When is an object marked for garbage collection? (1 mark)
iii) Look at the code fragment in Figure 2. By the end of the code, is the String containing the word “Hello” eligible for garbage collection? (1 mark)
iv) Indicate TWO ways in which you can make the object containing the word “World” (in Figure 2) eligible for garbage collection. (2 marks)
v) Look at the code fragment in Figure 3. What will be the output of this code? Explain your answer. (3 marks)
vi) Explain the purpose of the equals() method in the String class. (1 mark)
Question 3 Java编程考试代考
The questions below refer to the concept of scope and lifetime of variables in Java:
i) What is the scope of a local variable? Support your answer with a short coding example demonstrating the scope of a local variable. (3 marks)
ii) What are the main differences between local variables and instance variables? (3 marks)
iii) How do you refer to an instance variable when a local variable with the same name exists within the same scope? Provide a short coding example. (3 marks)
The code fragment in Figure 4 is an example of a class with a method that uses recursion:
i) Explain the concept of recursion in Java. (2 marks)
ii) What is the output of the code in Figure 4? (2 marks)
iii) Rewrite the foo() method so that it achieves the same output but uses iteration (i.e. a loop), instead of recursion. (6 marks)
The questions below refer to wrapper classes in Java, which are used to represent primitive data types as objects.
i) What abstract class do all numerical wrapper classes in Java extend? (1 mark)
ii) Name TWO wrapper classes. Write the necessary code to instantiate each of them. (3 marks)
iii) Provide an example of a case when a wrapper class is necessary. (2 marks)
Question 4 Java编程考试代考
a) The questions below refer to exceptions and assertions in Java:
i) What is the output of the program in Figure 5 (lines numbered 1-13) when assertions are disabled and when assertions are enabled? Justify your answers. (4 marks)
ii) The code in Figure 6 (lines numbered 1-14) is a modified version of the program in Figure 5. What is the output of the program in Figure 6? Justify your answer. (3 marks)
Consider the Java statement in Figure 7 and answer the following questions:
i) Write a Java statement that stores the value 200 in the second row of the third column of the array. (1 mark)
ii) Write Java code to store the numbers 1 2 3 4 5 in the first row, numbers 6 7 8 9 10 in the second row, and so on until 21 22 23 24 25 in the last row.
Note: You are not required to write a complete Java program. (3 marks)
Answer the following questions about File Input/Output in Java:
i) Java I/O is usually defined using streams. Briefly describe the concept of streams in Java. (1.5 marks)
ii) Write a Java program called PriceWriter. This class is responsible for writing a list of prices into a file. This class must create a file named prices.dat and write a list of prices (e.g. 90, 5, 20) into the file. Each price should be written to the file on a separate line preceded by a dollar ($) sign. Figure 8 gives an example of the resulting prices.dat file’s contents:
You can assume that all the prices are available in an array of integer values (you must declare this array in your own code and add some example prices into it). The file should be created in the same directory as the program. If the file already exists, then the program must display the message “File already exists” and then terminate the program. (7.5 marks)
This question is about Graphical User Interfaces in Java.
Write a block of Java code that creates a JButton and places it inside of a JFrame. The JButton should contain the text “Hello“. Add appropriate event handing code so that the text changes to “Bye Bye” after somebody clicks the button.
Note: You are not required to write a complete Java program. [5 marks]