• Tidak ada hasil yang ditemukan

System.out.println( "n\"n\n mark) (3)e) What is printed by the following

N/A
N/A
Protected

Academic year: 2023

Membagikan "System.out.println( "n\"n\n mark) (3)e) What is printed by the following"

Copied!
26
0
0

Teks penuh

(1)

SUMMER SEMESTER, 2005 Campus: City

COMPUTER SCIENCE Principles of Programming (Time allowed: TWO hours)

NOTE: Attempt ALL questions

Write your answers in the space provided

There is space at the back for answers that overflow the allotted space No calculators are permitted

Surname:

Forenames:

Student ID number:

Login name:

Q1

(/10) Q4

(/10) Q7

(/10)

Q10

(/10) Q2

(/10) Q5

(/10) Q8

(/6)

Q11

(/12) Q3

(/5) Q6

(/12) Q9

(/5)

TOTAL

(/100)

(2)

Question 1 (10 marks)

a) What is printed by the following?

System.out.println( 5 + 5 + "5" + 5 + 5 );

(1 mark)

b) What is printed by the following?

int a = 5;

int b = 2;

double d = (double)(a / b);

System.out.println( d );

(1 mark)

c) What is printed by the following?

System.out.println( 9 + 3 * (10 % 6) / 2 );

(1 mark)

d) What is printed by the following?

System.out.println( "n\"n\n\\"+"\"" );

(1 mark)

(3)

e) What is printed by the following?

String[] words = {"cat", "go", "a"};

System.out.println( words[words[1].length()] );

(1 mark)

f) What is printed by the following?

Point p = new Point(100, 100);

p.translate(101, 99);

System.out.println(p.x + " , " + p.y);

(1 mark)

g) What is printed by the following?

int[] nums = {1,2,3,4,5};

int[] moreNums;

moreNums = nums;

moreNums[3] = 1000;

System.out.println(nums[3]);

(1 mark)

(4)

h) What is printed by the following?

int i = 5;

while (i > 0) { i = i - 2;

System.out.println(i);

}

(1 mark)

i) What is printed by the following?

boolean x = false;

boolean y = (x && !x) || (x || !x);

System.out.println(y);

(1 mark)

j) What is printed by the following?

int z = Math.max(Math.max(4,5), Math.min(7,6));

System.out.println(z);

(1 mark)

(5)

Question 2 (10 marks)

a) What is the output produced by the following code?

String s1 = new String("Happiness");

String s2 = s1.substring(2, 4);

System.out.println( s1.indexOf(s2) );

(2 marks)

b) What is the output produced by the following code?

String s1 = new String("apple");

String s2 = new String("apple");

if( s1.equals(s2) && s2 == s1) System.out.println( "line 1" );

else if( s1.equals(s2) && s2 != s1) System.out.println( "line 2" );

else

System.out.println( "line 3" );

(2 marks)

c) What is the output produced by the following code?

String s1 = new String("a");

for (int i=0; i<3; i++) s1 = s1 + s1;

System.out.println( s1.length() );

(2 marks)

(6)

d) What is the output produced by the following code?

Point[] pts = new Point[3];

pts[0] = new Point(1,2);

pts[1] = new Point(3,4);

pts[2] = new Point(5,6);

Point temp = pts[1];

pts[1] = pts[0];

pts[2].x = temp.y;

System.out.println(pts[0].x + pts[1].x + pts[2].x);

(2 marks)

e) What is the output produced by the following code?

Rectangle r1 = new Rectangle(40, 30, 20, 10);

Rectangle r2 = new Rectangle(10, 30, 20, 40);

System.out.println(r1.intersects(r2));

(2 marks)

(7)

Question 3 (5 marks)

For this question, you need to complete the isPrime() method shown below. This method should return true if the value passed to the method as a parameter is a prime number (i.e. a number greater than 1 which can be divided by only 1 and itself without leaving a remainder) and false otherwise.

You can assume that the value passed as a parameter to this method will always be greater than 1.

private boolean isPrime( int value ) {

(5 marks) }

(8)

Question 4 (10 marks)

Write a method called hideMiddle() that accepts a String as a parameter. The method returns a String which has the same length as the parameter String. The first and the last characters of the String which is returned by the method are the same as the first and last characters of the parameter String, but all the middle characters should be replaced by the character: '*'.

NOTE: If the hideMiddle() method is passed a parameter with length less than three characters then the parameter String is returned unchanged by the method.

For example, when the start() method below is executed with the completed hideMiddle() method, the output would be:

H***y b**y p****e

public void start() {

String word1 = hideMiddle(“Happy”);

String word2 = hideMiddle(“busy”);

String word3 = hideMiddle(“people”);

System.out.println(word1 + " " + word2 + " " + word3);

}

Complete the hideMiddle() method below:

private hideMiddle( ){

}

(10 marks)

(9)

Question 5 (10 marks)

Draw the output produced by the following code in the grid given at the bottom of the page.

The grid lines are not part of the output but are there to help you place the drawing in the correct position. The size of each square in the grid is 10 pixels by 10 pixels.

import java.awt.*;

import javax.swing.*;

public class DrawJPanel extends JPanel{

public void paintComponent(Graphics g){

super.paintComponent(g);

draw(g, 30, 20, 10);

}

private void draw(Graphics g, int x, int y, int size){

g.setColor(Color.BLACK);

g.drawLine(x, y+size*5, x+size*3, y);

g.fillRect(x, y+size*2, size, size);

g.drawOval(x+size, y, size, size);

g.drawOval(x+size*2, y+size*2, size*2, size*2);

g.drawString("Wow", x, y);

} }

(10 marks)

10 20 30 40 50 60 70 80 90 100 110 130 120 140 0

10 20 30 40 50 60 70 80 90 100

120

140 110

130

(10)

Question 6 (12 marks)

The JPanel defined on the next page contains four components:

• A JTextField which contains an integer indicating the number of weekday hours,

• A JTextField which contains an integer indicating the number of weekend hours,

• A JTextField which contains a String indicating the total pay.

• The "CALCULATE PAY" JButton.

Below is a screenshot of the JPanel when it is first displayed.

Whenever the user presses the "CALCULATE PAY" button, the total pay is calculated and

displayed in the "Pay" JTextField. The pay rate for each weekday hour is $15 and the pay rate for each weekend hour is $25.

You are required to complete the JPanel definition on the next page so that the JPanel behaves as described above. You must use the variables and constants given in the code.

The screenshots below show the JPanel after the user has entered values in the top two JTextFields and pressed the "CALCULATE PAY" button.

(11)

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class PayJPanel extends JPanel {

private JTextField weekdayHrsT, weekendHrsT, payT;

private JLabel weekdayHrsL, weekendHrsL, payL;

private JButton calculatePayB;

public PayJPanel() {

weekdayHrsT = new JTextField(5);

weekendHrsT = new JTextField(5);

payT = new JTextField(10);

calculatePayB = new JButton("CALCULATE PAY");

weekdayHrsL = new JLabel("Weekday Hours");

weekendHrsL = new JLabel("Weekend Hours");

payL = new JLabel("Pay");

weekdayHrsT.setText("10");

weekendHrsT.setText("2");

payT.setText("$200");

add(weekdayHrsL);

add(weekdayHrsT);

add(weekendHrsL);

add(weekendHrsT);

add(payL);

add(payT);

add(calculatePayB);

}

public void ( ) {

final int WEEKDAY_PER_HOUR = 15;

final int WEEKEND_PER_HOUR = 25;

(12 marks) }

}

(12)

Question 7 (10 marks)

For this question, you need to complete a method called process2D().

This method accepts a two-dimensional int array as a parameter and returns a new two-dimensional int array.

The array which is returned by the method contains the same number of elements as the parameter array but each integer element is one greater than or one smaller than the corresponding element in the parameter array. The return array element is one less than the corresponding parameter array element if the parameter array element is zero or greater. The return array element is one greater than the corresponding parameter array element if the parameter array element is less than zero.

For example, when the start() method below is executed with the completed process2D() method, the output would be:

2 2 -3 4 4 -5 -2 -1 -5 2 3 -3

1 1 -2 3 3 -4 -1 0 -4 1 2 -2

The start() method that produces the above output is shown below:

public void start() {

int[][] nums1 = { { 2, 2, -3, 4}, { 4, -5, -2, -1},

{-5, 2, 3, -3}

};

int[][] nums2 = process2D( nums1 );

print2D(nums1);

print2D(nums2);

}

The print2D() method called in the above example simply prints all the elements of a 2- dimensional array, one row at a time. The source code for this method is not given.

(13)

Complete the process2D() method in the space provided below:

private int[][] process2D(int[][] nums) { int rows = nums.length;

int cols = nums[0].length;

(10 marks) }

private void print2D(int[][] nums) { /*

This method displays the elements of the 2D array row by row.

Code not included.

*/

}

(14)

Question 8 (6 marks)

a) What is the output produced when the start() method below is executed?

public void start() {

for (int i = 24; i < 30; i = i + 3) {

for (int j=10; j<i; j=j+10) {

System.out.println( (i + j) );

}

System.out.println("next");

} }

(6 marks)

(15)

Question 9 (5 marks)

What is the output of the start() method shown below?

public void start() {

int[] nums = {1, 2, 3, 4};

for (int i = 0; i < nums.length; i++) { nums[(i*2+1) % 4] = nums[i];

}

for (int i = 0; i < nums.length; i++) { System.out.print(nums[i] + " ");

} }

Show the output in the box below:

(5 marks)

(16)

Question 10 (10 marks)

Consider the Person class defined below:

public class Person {

private String name;

private boolean male;

private int age;

public Person(String n) { name = n;

male = true;

age = 20;

}

public Person(String n, boolean m, int a) { name = n;

male = m;

age = a;

}

public String title() { if (male) {

return "Mr. ";

}

else {

return "Miss. ";

} }

public int ageDifference(Person other) { return age - other.age;

}

public void increaseAge(int a) { age += a;

}

public String toString() {

return name + " (" + age + ")";

} }

This Person class is used by the program that is shown on the following page. You need to determine what the output of the program will be.

(17)

The PersonProgram class is given below:

public class PersonProgram { public void start() {

Person s1 = new Person("John");

Person s2 = new Person("Sarah", false, 18);

Person clone;

System.out.print(s1.title());

System.out.println(s1);

System.out.print(s2.title());

System.out.println(s2);

s1.increaseAge(-3);

System.out.println(s1);

clone = s1;

clone.increaseAge(s2.ageDifference(s1));

System.out.println(s1);

System.out.println(s2);

} }

Show the output of the program in the space provided below. Notice also that a space on the right has been provided for you to draw a diagram of the objects and their references which you might find useful. There are no marks allocated for this diagram, but if you do not obtain the correct output, then you may receive partial credit for the diagram you have drawn.

Show the output of the program here

Draw a diagram of the objects and the references here

(10 marks)

(18)

Question 11 (12 marks)

For this question, you need to complete a class called CircleCollection which is used by the program described below.

When the program first starts, a blank window is displayed:

Whenever the mouse button is pressed, a circle is drawn which is centred at the location of the mouse press. The radius of the circle that is drawn is such that the very top of the circle is at the top of the screen (i.e. the top of the circle has a y-position of 0).

For example, the screenshot below shows the window just after the mouse button has been pressed.

Notice that the circle which is drawn is centred at the position of the mouse press. Also notice that the top of the circle just touches the top of the window.

(19)

The screenshots below show the window after the mouse button has been pressed several times.

(20)

The code for the JPanel class for this program has been provided below:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class MyJPanel extends JPanel implements MouseListener { private CircleCollection circles;

public MyJPanel() {

setBackground(Color.white);

circles = new CircleCollection();

addMouseListener(this);

}

public void paintComponent(Graphics g) { super.paintComponent(g);

circles.draw(g);

}

public void mousePressed(MouseEvent e) { circles.add(e.getPoint());

repaint();

}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

public void mouseExited(MouseEvent e) {}

public void mouseClicked(MouseEvent e) {}

}

Notice that this class uses an object of type CircleCollection to store and display the circles.

You need to complete the code for this CircleCollection class.

HINTS:

• You should store the positions of each circle in an array of Points.

• You can calculate the radius of each circle based on the y-position of the centre of the circle – there is no need to store this radius.

• You can assume the maximum number of circles you will need to store is 1000.

Complete the code for the CircleCollection class on the following page.

(21)

import java.awt.*;

public class CircleCollection {

(12 marks) }

(22)

OVERFLOW PAGE

(If you have used this page, please indicate clearly under the relevant question that you have overflowed to this page)

(23)

OVERFLOW PAGE

(If you have used this page, please indicate clearly under the relevant question that you have overflowed to this page)

(24)

OVERFLOW PAGE

(If you have used this page, please indicate clearly under the relevant question that you have overflowed to this page)

(25)

ROUGH WORKING (WILL NOT BE MARKED) (You may use this page for rough working)

(26)

ROUGH WORKING (WILL NOT BE MARKED) (You may use this page for rough working)

Referensi

Dokumen terkait

LIMBAH KACA SEBAGAI BAHAN ALTERNATIF SUBSTITUSI PARSIAL SEMEN UNTUK CAMPURAN BETON&#34;, Jurnal Teknik Sipil,