当前位置:天才代写 > JAVA代写 > 面向对象编程考试代考 COMP2396B代写

面向对象编程考试代考 COMP2396B代写

2022-07-07 11:41 星期四 所属: JAVA代写 浏览:87

COMP2396B: Object-Oriented Programming and Java

面向对象编程考试代考 Time: 2:30pm-4:30pm Only approved calculators as announced by the Examinations Secretary can be used in this examination.

Time: 2:30pm-4:30pm

Only approved calculators as announced by the Examinations Secretary can be used in this examination. It is the candidates’ responsibility to ensure that their calculator operates satisfactorily, and candidates must record the name and type of the calculator on the front page of the examination script.

 

Instructions:  面向对象编程考试代考

  • This paper contains 3 sections.
  • Answer ALL questions.
  • Candidates are permitted to refer to any electronic/printed/handwritten materials in the examination. Internet searching and crowdsourcing from group messages, online forums or social media, etc. are strictly forbidden.
  • For Section A and Section B, you should provide typeset or handwritten answer and upload the answer file in word or pdf format. For Section C, you should provide typeset programs and upload the .java files.
  • Write your University No. on the first page of the answer script.
  • Total Mark is 100.

 

Section A. (20%)  面向对象编程考试代考

Please copy the following table to your answer sheets and tick only ONE answer for each question below. 2% will be awarded for each correct answer.

Question a) b) c) d) e)
1
2
3
4
5
6
7
8
9
10

 

1.

Which of the followings is/are keyword(s) in Java?

i. try   ii. Class    iii. implement

iv. interface   v. synchronized   vi. Throw

a) i, iii, iv & vi

b) ii, iii & iv

c) i, iv, v & vi

d) i, ii, iii & iv

e) all of them

 

2.

Which of the following statements is/are legal in Java?

i. String[] list = new String[4];

ii. String list = new String(4);

iii. String list[4];

iv. String[][] list = new String[2][];

v. String[] list = {“Chim”, “Kiu”, “Marco”, “Joy”};

a) i & iii

b) i, iii & iv

c) ii, iii & iv

d) i, iv & v

e) all of them

 

3.  

Consider the following Java program.

 

面向对象编程考试代考
面向对象编程考试代考

 

Which of the following ways to execute the program will cause it to produce “Studying Dating Hall Residence Part-time ” in the console?

i. java University Studying Dating Hall Residence Part-time

ii. java University Dating Hall Residence Part-time

iii. java University Studying “Hall Residence” Part-time

iv. java University Dating “Hall Residence”

v. java University Studying “Hall Residence” “ ” Dating

vi. java University Dating Hall “ Residence ” Part-time

a) i

b) ii & iii

c) iii & iv

d) iv, v & vi

e) v & vi

 

4.

What is the output of the following Java program?

 

面向对象编程考试代考
面向对象编程考试代考

 

a) 5 1

b) 5 3

c) 10 1

d) 10 5

e) fails at runtime

 

5.

How many objects are created in the following Java program?

1  public class Test {
2    public static void main(String[] args) {
3      Long a = (long) 1;
4      Long b = (long) 2;
5      Long c = ++a + b++;
6      System.out.println(a + " " + b + " " + c);
7    }
8  }

a) 2

b) 3

c) 5

d) 7

e) 8

 

6.

Consider the following classes.

1 class Vehicle {
2   public void move() { System.out.println("Vehicle is moving!"); }
3 }
1 class MotorVehicle extends Vehicle {
2   public void move() { System.out.println("Motor vehicle is moving!"); }
3 }
1 class Bus extends MotorVehicle {
2   public void move(int n) { System.out.println("Bus is moving!"); }
3 }
1 class SchoolBus extends Bus {
2   public void move(int n) { System.out.println("School bus is moving!"); }
3 }

How many constructors will get invoked when creating a SchoolBus object?

a) 1

b) 2

c) 3

d) 4

e) 5

 

7.  面向对象编程考试代考

Which of the following statements is/are correct?

i. An abstract class cannot have non-abstract methods.

ii. An abstract class cannot have constructors.

iii. An abstract class cannot be instantiated.

iv. It is not legal to create a regular array of an abstract class.

a) i only

b) iii only

c) ii only

d) i & iii

e) i, iii & iv

 

8.

Given that A, C and E are classes where C is an abstract, B and D are interfaces. Which of the following statements is/are legal in Java?

i. class F implements B, C {}

ii. class F implements B {}

iii. class F extends A, E {}

iv. class F extends C implements D {}

v. class F implements B, D {}

a) ii, iv & v

b) ii, iii & v

c) ii & iii

d) i, ii & v

e) ii, iii, iv & v

 

9.

Which of the following statements is/are correct?

i. All variables live on the stack.

ii. All objects live on the heap.

iii. All objects live on the stack.

iv. A local variable is alive only when its stack frame is at the top of the stack.

v. An object becomes eligible for garbage collection when a reference to this object is set to null.

a) i only

b) ii only

c) i & ii

d) i & iii

e) ii & iv

 

10.

What is the output of the following Java program?

 

 

a) 0

b) 4

c) 8

d) fails to compile

e) fails at runtime

 

Section B. (50%)   面向对象编程考试代考

Question 1 (10%)

(a) What are the four principles of objected-oriented programming? (4%)

(b) Please match each of the following to the above four principles. (6%)

(i) You can catch the general Exception object instead of objects of sub-classes of Exception.

(ii) You only need to write JavaDoc for non-private methods and class fields.

(iii) You can extend your class from JPanel and override paintComponent() method. In this way, you don’t need to create any JPanel object in your program.

(iv) You can make your class implement the ActionListener interface and override actionPerformed(ActionEvent event) method. In this way, you don’t need to create any ActionListener object in your program.

(v) You can use an Object typed ArrayList to hold anything.

 

Question 2 (10%)  面向对象编程考试代考

Given the definitions of classes A and B.

 

 

(a) Please write down the output when the following statements are executed. (3%)

1 A x1 = new A();
2 x1.print();
3 B x2 = new B();
4 x2.print();

(b) Please explain your answer by indicating which methods are called and the changes in the values of instance variables. (7%).

 

Question 3 (10%)  面向对象编程考试代考

Consider the following program.

1 import java.io.*;
2
3 class Teacher implements Serializable {}
4
5 class GuestTeacher extends Teacher {}
6
7 public class HeadFirstTeacher {
8
9   public static void main(String[] args) {
10     Integer[] quotas = {7, 8, 9, 10};
11     Teacher t1 = new Teacher();
12     Teacher t2 = new GuestTeacher();
13     Teacher t3 = new GuestTeacher();
14     Teacher t4 = new Teacher();
15     Teacher t5 = new Teacher();
16     Integer[] duplicateQuotas = quotas;
17
18     t1 = t4;
19     t4 = null;
20     t2 = t3;
21     Teacher[] teacherAssignments = {t1, t5};
22     manage(t2);
23     // count number of objects here
24   }
25
26   static void manage(Teacher t) {
27     GuestTeacher ta = new GuestTeacher();
28     Teacher tb = t;
29   }
30
31 }

For each class used in the above program, give the number of objects that have been created at the end of the execution. In addition, give the number of the objects abandoned. Please copy the table below onto your answer script and complete it.

Classes No. of objects created No. of objects abandoned
Integer 4 0

 

 

Question 4 (10%)

(a) Briefly explain the difference between the terms “overriding” and “overloading”. (4%)

(b) What is the difference between checked exceptions and unchecked runtime exceptions? Give one example for each of the two types of exceptions. (4%)

(c) What is the meaning of Serialization? (2%)

 

Question 5 (10%)  面向对象编程考试代考

A student has implemented a simple GUI program which contains the following:

1) 3 JTextFields

  • jtf1 – input number 1
  • jtf2 – input number 2
  • jtf3 – output number

2) 1 JComboBox

  • jcb – options “+”, “–”, “*”, “/”

3) 1 JButton

  • jb – “=” button for triggering calculation

4) 1 JTextArea

  • jta – for displaying mathematics expression

For example, when the user enters “23” and “96” into jtf1 and jtf2 respectively, chooses “+” in jcb and then presses jb, “119.0” will be displayed in jtf3 and the expression “23.0 + 96.0 = 119.0” will be appended to jta as a new line.

Consider the source program below.

1 import java.awt.*;
2 import java.awt.event.*;
3 import javax.swing.*;
4
5 public class CalcGUI extends JFrame {
6
7    CalcGUI() {
8      super("Calculator");
9
10     JPanel jp = new JPanel();
11     jp.setLayout(new GridLayout(1, 5, 10, 0));
12     JTextField jtf1 = new JTextField();
13     JTextField jtf2 = new JTextField();
14     JTextField jtf3 = new JTextField();
15     jtf3.setEditable(false);
16     JComboBox<String> jcb = new JComboBox<>(new String[]{"+","-","*","/"});
17     JButton jb = new JButton("=");
18     jp.add(jtf1);
19     jp.add(jcb);
20     jp.add(jtf2);
21     jp.add(jb);
22     jp.add(jtf3);
23     JTextArea jta = new JTextArea();
24     jta.setEditable(false);  
25     JScrollPane jsp = new JScrollPane(jta);
26 
27     jsp.setHorizontalScrollBarPolicy(
28     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
29     jsp.setVerticalScrollBarPolicy(
30     JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
31     jb.addActionListener(new ActionListener() {
32       public void actionPerformed(ActionEvent event) {
33         double d1 = 0, d2 = 0;
34         String res = "", log = "";
35         try {
36           d1 = ___________________(1)___________________;
37           d2 = ___________________(2)___________________;
38           String sign = (String) jcb.getSelectedItem();
39           switch (sign) {
40             case "+": res = _______(3)_______; break;
41             case "-": res = _______(4)_______; break;
42             case "*": res = _______(5)_______; break;
43             case "/": res = _______(6)_______; break;
44           }
45           log = "" + d1 + " " + sign + " " + d2 + " = " + res + "\n";
46         } catch (Exception e) {
47           res = "NaN";
48         }
49         jtf3.setText(res);
50         jta.append(log);
51       }
52     });
53
54     getContentPane().add(BorderLayout.NORTH, jp);
55     getContentPane().add(BorderLayout.CENTER, jsp);
56     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
57     setSize(600, 200);
58     setLocationRelativeTo(null);
59     setVisible(true);
60     setResizable(false);
61    }
62
63    public static void main(String[] args) {
64      ____________________(7)____________________;
65    }
66 
67 }

(a) Please draw the layout below. (3%)

(b) Please fill in the missing parts so that the ActionListener can function as described. (7%)

 

Section C. (30%)  面向对象编程考试代考

This is a programming question. Please complete the Java program as instructed, compress all your source files (.java files) into one file and submit it to OLEX. JavaDoc is NOT required.

Given the contents of Shape.java and Movable.java below. Prepare a Java Project and add these two files inside. Shape is an abstract class that define a shape, Movable is an interface with 2 methods.

You must not make changes to these two files.

Shape.java

import java.awt.*;

public abstract class Shape {
  protected int centerX, centerY;
  protected int size = 50;

  public abstract void draw(Graphics g);

  public Shape(int centerX, int centerY) {
    this.centerX = centerX;
    this.centerY = centerY;
  }
}

Movable.java

public interface Movable {
  public void moveUp(int amount);
  public void moveDown(int amount);
}

 

Task C1 (9%)

Implement the main program, class MyShapeTester. It should show the following GUI when executed:

 

面向对象编程考试代考
面向对象编程考试代考

 

Here are detailed requirements of this GUI:

  • Titled “My Shape Tester
  • There are two buttons as seen above: “^” and “v” (small letter ‘v’)
  • Size of the frame is 250 x 250
  • Program should terminate when the GUI window is closed
  • The center part is empty

 

Task C2 (4.5%)

Create the Circle class, which is a subclass of Shape (the Shape class is given above). Implement the method void draw(Graphics g) to draw a circle centered at (centerX, centerY) with radius equals to the property size. Add properties and constructor(s) to the class if needed.

 

Task C3 (9%)  面向对象编程考试代考

Create a class, MyCanvas, a subclass of JPanel. Instantiate one instance of this class and add it to the center part of the GUI in Task C1. Do the following to the MyShapeTester class and MyCanvas class:

  • Implement method void setShape(Shape shape) in MyCanva class, so that the shape will be drawn whenever the canvas is painted.
  • Modify your program so that when the program starts, a new Circle object is added to the MyCanvas object using setShape(). Because of that, a circle will be drawn in the middle of the canvas as shown below:

 

面向对象编程考试代考
面向对象编程考试代考

 

  • Your program should still work properly if the Circle object is replaced by any other object that inherits from the Shape class.

 

Task C4 (3%)

Modify class Circle so that it implements the Movable interface. The Movable interface is given above.

  • Method void moveUp(int amount) will move the shape up by the specific amount
  • Method void moveDown(int amount) will move the shape down by the specific amount

 

Task C5 (4.5%)  面向对象编程考试代考

Modify MyShapeTester and MyCanvas class so that if the “^” or the “v” button is clicked, do the following:

  • Check if the shape in MyCanvas object has implemented the Movable interface
  • If so, execute moveUp(10) if “^” button is clicked, execute moveDown(10) if “v” button is clicked. Do nothing if the shape does not implement the Movable interface.

If all done correctly, you should be able to move the circle up and down using the two buttons

 

面向对象编程考试代考
面向对象编程考试代考

 

You should note the following:

  • If the Circle class is not implementing the Movable interface, clicking on the two buttons will have no effect.
  • If the Circle object is replaced by another object that inherits from the Shape class with or without implementing the Movable interface, your program should still work properly.
  • Execute moveUp(10) if “^” button is clicked and you must call repaint aftermoving!
  • Execute moveDown(10) if “v” button is clicked and you must call repaint after moving!
  • Only move if the shape implements Movable
  • Allow drawing of other shapes by changing the object passed to setShape()

 

面向对象编程考试代考
面向对象编程考试代考

 

 

更多代写:algorithm网课代修代写  台灣代考  英国宏观经济代考   哲学paper北美代考代写  paper作业代写  如何写essay

合作平台:essay代写 论文代写 写手招聘 英国留学生代写

 

天才代写-代写联系方式