Consider the elevator class as defined below public

This preview shows page 13 - 20 out of 30 pages.

18) Consider the Elevator class as defined below: public class Elevator { private int numFloors; private int currentFloor; public Elevator(int height) { numFloors = height; currentFloor = 0; } public void moveUp() { if (currentFloor < numFloors - 1) { currentFloor++; } } public String toString() { return "[" + currentFloor + "]"; } } What output would be produced by the following code that creates two Elevator objects? Elevator e1, e2; e1 = new Elevator(5); e2 = new Elevator(2); e1.moveUp(); e1.moveUp(); e1.moveUp(); e2.moveUp(); e2.moveUp(); e2.moveUp(); System.out.println(e1 + " " + e2); (a) [3] [3] (b) [3] [1] (c) [3] [2] (d) [0] [0] (e) [5] [2]
Image of page 13

Subscribe to view the full document.

VERSION 00000001 - 14 - COMPSCI 101 CONTINUED 19) What is the output of the following code? Point[] pts = new Point[3]; pts[0] = new Point(100, 200); pts[1] = new Point(300, 400); pts[2] = new Point(500, 600); Point temp = pts[1]; pts[1] = pts[0]; pts[2].x = pts[1].y; pts[1].x = pts[0].y; pts[0] = temp; System.out.println(pts[0].x + " , " + pts[0].y); System.out.println(pts[1].x + " , " + pts[1].y); System.out.println(pts[2].x + " , " + pts[2].y);
Image of page 14
VERSION 00000001 - 15 - COMPSCI 101 CONTINUED 20) The following program creates a Timer object and draws a circle on the window. import java.awt.*; import java.awt.event.*; import javax.swing.*; public class MyJPanel extends JPanel implements ActionListener { private Timer t; private Point pos; private int xSpeed, ySpeed; public MyJPanel() { t = new Timer(25, this); t.start(); pos = new Point(100, 100); xSpeed = -4; ySpeed = 4; } public void actionPerformed(ActionEvent e) { pos.x = pos.x + xSpeed; pos.y = pos.y + ySpeed; repaint(); } public void paintComponent(Graphics g){ super.paintComponent(g); g.fillOval(pos.x-25, pos.y-25, 50, 50); } } Which direction would the circle start to move in when this program is executed?
Image of page 15

Subscribe to view the full document.

VERSION 00000001 - 16 - COMPSCI 101 QUESTION/ANSWER SHEET FOLLOWS THIS PAGE HAS BEEN INTENTIONALLY LEFT BLANK.
Image of page 16
VERSION 00000001 - 17 - COMPSCI 101 Question/Answer Sheet ID ……….………… CONTINUED THE UNIVERSITY OF AUCKLAND FIRST SEMESTER, 2011 Campus: City Computer Science Principles of Programming (Time Allowed: TWO HOURS) SECTION B Question/Answer Booklet Answer all questions in this section in the space provided. If you run out of space then please use the Overflow Sheet and indicate in the allotted space that you have used the Overflow Sheet. Surname: First Name(s): Student ID: Login Name (UPI): MARKERS ONLY Q1-20 (/40) Q21 (/20) Q22 (/14) Q23 (/8) Q24 (/8) Q25 (/10) TOTAL (/100)
Image of page 17

Subscribe to view the full document.

VERSION 00000001 - 18 - COMPSCI 101 Question/Answer Sheet ID ……….………… CONTINUED Question 21 [20 marks] a) Complete the code by making TWO calls to the isCorrect() method so that the output produced by the program is: 1. true 2. false public class Exam { public void start() { boolean result1 = isCorrect(4, 2, 6); //any numbers where the first //two add up to the third System.out.println("1. " + result1); boolean result2 = isCorrect(4, 2, 5); //any numbers where the first //two do not add up to the third System.out.println("2. " + result2); } private boolean isCorrect(int n1, int n2, int result) { int sum = n1 + n2; if (sum == result) { return true; } return false; } } (4 marks)
Image of page 18
VERSION 00000001 - 19 - COMPSCI 101
Image of page 19

Subscribe to view the full document.

Image of page 20
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern