• Tidak ada hasil yang ditemukan

In the screen shot on the left below, the user has pressed the mouse button three times

N/A
N/A
Protected

Academic year: 2023

Membagikan "In the screen shot on the left below, the user has pressed the mouse button three times"

Copied!
25
0
0

Teks penuh

(1)

i) Declare and create an array of String elements called allStrings which is large enough to contain 300 String elements:

ii) Assign the String, "Relax" to the 10th element of the allStrings array:

iii) Assign the length of the 10th element of the allStrings array to the int variable called len:

int len;

(6 marks)

(2)

Question 2 (15 marks)

Complete the code (in the spaces provided) for the following applet which executes as follows:

• When the user presses the mouse, a Point object is created (storing the location of the mouse press) and added to an array of Point objects. In the applet a square is drawn at the position of each Point object in the array; the mouse press position is at the middle

position of the square. The size of each square drawn in the applet increases by one pixel as we move through the array. eg. the square at the first Point object in the array is of size 2 pixels, the square at the second Point object is of size 3 pixels, the square at the third Point object is of size 4 pixels etc.

• Whenever the user presses the "MOVE LEFT" button the x value of all the Point objects in the array decreases by 2; this means that all the points are drawn 2 pixels to the left of their previous position.

The following screen shots show the completed applet in action.

In the screen shot on the left below, the user has pressed the mouse button three times. At each location that the mouse was pressed, a small blue square is drawn with the middle of the square at the position of the mouse press. Each time the mouse button is pressed, the new square that is drawn is one pixel larger than the previous square. In the screen shot on the right below, the user has continued pressing the mouse many times from the top left towards the bottom right of the applet window. The squares drawn at the position of the mouse presses become larger from the top left to the bottom right of the applet.

In the screen shot on the left below, the user has pressed the "MOVE LEFT" button ten times, and all the squares have moved 20 pixels towards the left of the applet. In the screen shot to the right, the user has continued to press the "MOVE LEFT" button another ten times, and the squares have moved another 20 pixels further to the left.

(3)

import java.applet.*;

import java.awt.event.*;

public classQ2extends Applet implements ActionListener,MouseListener{

public static final int MAX_POINTS = 1000;

private Button moveLeftB;

private Point[] thePoints;

private int upToInArray;

public void init() {

addMouseListener(this);

moveLeftB = new Button("MOVE LEFT");

moveLeftB.addActionListener(this);

add(moveLeftB);

upToInArray = 0;

thePoints = new Point[MAX_POINTS]; // create the array } // end of the init() method

public void paint( Graphics g ) { int x,y;

int size = 2; // initial size of the squares g.setColor(Color.blue);

(4)

public void actionPerformed(ActionEvent e) {

} // end of the actionPerformed() method public void mousePressed(MouseEvent e) {

} // end of the mousePressed() method

public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } } // end of the applet

(15 marks)

(5)

Complete the sumOver() method which returns the sum of all the entries in a 2 dimensional array of ints which are greater than a specified amount. An example array definition and two sample calls to the method are shown on the next page.

private int sumOver(int[][] theInts, int over) {

} // end of sumOver() method

(6)

To demonstrate how the sumOver() method should work, two example calls to this method are shown below.

The first call obtains the sum of all entries in the array which are over 50 and the second call obtains the sum of all entries in the array which are over 100.

int[][] theArray = { {2, 4, 16, 80, 27}, {12, 45, 120, 18, 27}, {20, 24, 70, 8, 130}

};

int theSum;

theSum = sumOver(theArray, 50);

System.out.println("Sum of all entries over 50 is " + theSum);

theSum = sumOver(theArray, 100);

System.out.println("Sum of all entries over 100 is " + theSum);

The output from these two calls is shown below:

(10 marks)

(7)

The source code for a user-defined class called SecurityEntry is given below. Note that the variables securityNo and doors are declared static:

public class SecurityEntry {

private static int securityNo;

private static String[] doors = {"Doors 101,222",

"Doors 301,402",

"Doors 503,702"};

private int iDNumber;

private String name;

private int doorsIndex;

private int securityCardNo;

public SecurityEntry(int numID, String theName, int index) { iDNumber = numID;

name = theName;

doorsIndex = index;

securityCardNo = securityNo;

securityNo++;

}

public String toString() { String temp;

temp = name + ": " + iDNumber+ "\n";

temp = temp + "Card: " + securityCardNo+ " ";

temp = temp + doors[doorsIndex]+"\n\n";

return temp;

}

public void setDoorsIndex(int index) { doorsIndex = index;

}

public static void setUpSecurityNo(int firstNumber) { securityNo = firstNumber;

}

} // end of SecurityEntry class definition

This class is used by an applet called Q4. The source code for this applet is given on the next the page.

(8)

Below is the source code for the applet Q4 which uses the SecurityEntry class. What output is displayed when the applet Q4 is run?

import java.applet.*;

public class Q4 extends Applet{

private SecurityEntry a, b, c;

public void init() {

SecurityEntry.setUpSecurityNo(450);

a = new SecurityEntry(9876,"Joe Biggs",2);

b = new SecurityEntry(9723,"Ann Sato",1);

c = new SecurityEntry(9561,"Mary Pin",1);

b.setDoorsIndex(0);

System.out.println(a.toString());

System.out.println(b.toString());

System.out.println(c.toString());

} // end of init() method }// end of applet

Display the output of the applet Q4 in the window below:

(8 marks)

(9)

(i) What is the output when the following section of code is executed?

int value = 0;

for (int i=0; i<25; i+=10) {

System.out.println("value: "+value);

for (int j=i; j<20; j+=5) { value++;

} }

System.out.println("Final value: " + value);

(10)

( i i )

What is the output when the following section of code is executed?

String symbol1 = "X";

String symbol2 = "*";

for (int i=1; i<5; i++) {

System.out.print(i + ". ");

for (int j=6-i; j>2; j--) { if ((i + j)%2 == 0)

System.out.print(symbol1);

else

System.out.print(symbol2);

}

System.out.println();

}

(8 marks)

(11)

Write a method called swapHalves() which is passed an array of ints as parameter and returns a new array that has every element in the second half of the original array swapped with every element in the first half of the original array.

You can assume that the array passed to this method as a parameter always contains an even number of elements. For example, the diagram below shows what would happen to the elements of an array of size 10:

1 0

2 1

3 2

4 3

5 4

6 5

7 6

8 7

9 8

10 9

1 0

2 1

3 2

4 3

5 4

6

5 7

6 8

7 9

8 10

9 swapHalves()

All of the elements in the second half of the array have been swapped wth the elements in the first half of the array. Another example, this time with an array of size 4, is given below:

1 0

2 1

3 2

4 3

swapHalves()

2 0 1

1 2 3

3 4

The swapHalves() method returns an array of ints, so when you write the method you must create a new array of the correct size, assign the appropriate values to each element of the array, and then return it.

An example of how the method would be called is given below:

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

int[] b = swapHalves(a);

System.out.println(a[0] + "," + a[1] + "," + a[2] + "," + a[3]);

System.out.println(b[0] + "," + b[1] + "," + b[2] + "," + b[3]);

This would produce the output:

1,2,3,4 3,4,1,2

(12)

Complete the code for the swapHalves() method in the space provided below.

Note: You can assume that the array passed to the method as a parameter, theArray, will have an even number of elements, and will always contain at least two elements.

private int[] swapHalves(int[] theArray) {

}

(10 marks)

(13)

This question requires you to complete the code for a class called Student. A detailed description of the applet, Q7, which uses the Student class is given on the next few pages. You should read this description before attempting to complete the code for the Student class below.

public class Student {

private String name; // the name of the student private int testMark; // test mark between 0 and 50 private int examMark; // exam mark between 0 and 50 public Student() {}

public void setName(String theName) {

}

public void setTestMark(int theTest) {

}

public void setExamMark(int theExam) {

}

public String getName() {

}

public boolean passedTest() {

}

public boolean passedExam() {

}

public boolean didBetterThan(Student other) {

(14)

The Student class from the previous page is used by the following applet that compares the final marks for two students taking a course at University. The final mark for a student is the sum of their test mark and their exam mark. Both the test and exam mark are out of 50, so the final mark is a mark out of 100.

The applet allows the user to enter the test and exam marks for two students, and then for each student the applet prints out whether that student passed or failed the test and exam, and finally prints out which of the students achieved the higher final mark.

The screen shots below show how the applet behaves. When the applet first starts, the TextFields are empty, and the TextArea prompts the user for input:

When the user has entered the names and the marks for the two students, and then presses the return key in one of the TextFields, the TextArea displays information for each student stating whether the student passed or failed the test and the exam, and also stating which student achieved a higher final mark. You can assume that the user will only enter values between 0 and 50 for the test and exam mark.

For example, the screen shot below shows the applet after the test and exam scores have been entered in the TextFields for two students:

The test and the exam are out of 50, so the TextArea displays the word "passed" if the test or exam mark is 25 or more, otherwise it displays the word "failed".

In the example above, the final line in the TextArea displays "Adriana did better than Paul" because Adriana's final mark: 19 + 41 = 60 is greater than Paul's final mark: 25 + 24 = 49.

(15)

TextArea will state that the students "did exactly the same":

In the screen shot above:

39 + 36 is equal to 25 + 50

and so the TextArea displays "Paul and Adriana did exactly the same".

For this question you need to complete the Student class (on page 14), which is used to store the name, test mark and exam mark for a particular student. All of the instance variables and method headings for the Student class have been provided for you. You simply need to write the body for the methods of this class.

Note that the constructor method is empty, and does not need to be changed. The instance variables for an object of type Student are initialised when the set methods are called.

The complete source code for the applet described above, which uses the Student class, is given below:

import java.awt.*;

import java.applet.*;

import java.awt.event.*;

public class Q7 extends Applet implements ActionListener { /* The TextFields */

TextField tName1, tName2, tTest1, tTest2, tExam1, tExam2;

/* The TextArea */

TextArea output;

/* The Students */

Student student1, student2;

private TextField customTextField(String text) { TextField temp = new TextField(text);

(16)

public void init() {

add(new Label("STUDENT ONE - name: "));

tName1 = customTextField(" ");

add(new Label("test: "));

tTest1 = customTextField("0 ");

add(new Label("exam: "));

tExam1 = customTextField("0 ");

add(new Label("STUDENT TWO - name: "));

tName2 = customTextField(" ");

add(new Label("test: "));

tTest2 = customTextField("0 ");

add(new Label("exam: "));

tExam2 = customTextField("0 ");

student1 = new Student();

student2 = new Student();

output = new TextArea(4, 40);

output.setText("Enterthe namesofthe studentsandtheir\n");

output.append("testand exammarksinthe aboveTextFields");

add(output);

}

private void showStatusFor(Student s) { output.append(s.getName());

if (s.passedTest())

output.append(" passed the test and ");

else

output.append(" failed the test and ");

if (s.passedExam())

output.append("passed the exam.\n");

else

output.append("failed the exam.\n");

}

public void actionPerformed(ActionEvent e) { student1.setName(tName1.getText());

student1.setTestMark(Integer.parseInt(tTest1.getText()));

student1.setExamMark(Integer.parseInt(tExam1.getText()));

student2.setName(tName2.getText());

student2.setTestMark(Integer.parseInt(tTest2.getText()));

student2.setExamMark(Integer.parseInt(tExam2.getText()));

output.setText("");

showStatusFor(student1);

showStatusFor(student2);

if (student1.didBetterThan(student2)) output.append(student1.getName() +

" did better than " + student2.getName());

else if (student2.didBetterThan(student1)) output.append(student2.getName() +

" did better than " + student1.getName());

else

output.append(student1.getName() + " and " +

student2.getName() + " did exactly the same");

} }

(15 marks)

(17)

Write a method called largestRectangle(), which is passed a Vector of Rectangle objects as a parameter, and returns a reference to the Rectangle object in the Vector which has the largest area (ie. the largest value for width * height).

To demonstrate how this method should work, an example applet is given below which creates a Vector of Rectangle objects in it's init() method, and then makes a call to the

largestRectangle() method. Carefully examine the source code for this example applet, which creates a Vector containing the Rectangles with (x, y, width, height) values as follows:

0. (10, 20, 20, 30) 1. (50, 60, 11, 18) 2. (100, 15, 4, 200) 3. (5, 85, 12, 60)

When this particular Vector is passed to the largestRectangle() method, a reference to the third Rectangle (ie. at index 2) in the Vector is returned, because this is the Rectangle with the largest area: 800.

Below is the output and the source code for the example applet. If the largestRectangle() method is written correctly, the output from the example applet below would be:

You must complete the largestRectangle() method in the space provided on the next page.

Note: You can assume that the Vector, v, that is passed to the largestRectangle() method as a parameter, always contains at least one Rectangle object.

import java.awt.*;

import java.util.*;

import java.applet.*;

public class Q8 extends Applet { public void init() {

Vector v = new Vector();

v.addElement(new Rectangle(10, 20, 20, 30));

v.addElement(new Rectangle(50, 60, 11, 18));

v.addElement(new Rectangle(100, 15, 4, 200));

v.addElement(new Rectangle(5, 85, 12, 60));

(18)

private Rectangle largestRectangle(Vector v) {

} }

(10 marks)

(19)

Complete the paint() method for the applet on the next page using a nested loop so that the applet displays the graphics below. Each filled rectangle is 20 pixels wide and 20 pixels high.

Note: the background grid lines are supplied to help you see the graphics; each small square in the grid is 10 pixels wide and 10 pixels high. You are NOT required to draw these grid lines.

(20)

import java.awt.*;

import java.applet.*;

public class Q9 extends Applet{

public void paint( Graphics g ) { g.setColor(Color.black);

final int SIZE = 20; //size of square

final int DIST = SIZE*3; //distance between squares final int START_X = 20;

final int START_Y = 20;

} // end of paint() method } // end of the applet

(10 marks)

(21)

What is the output of the following applet:

import java.applet.*;

public class Q10 extends Applet { public void init() {

String s1 = new String("successful");

String s2 = new String("stressful");

String s3 = new String("account");

System.out.println(s1.indexOf('u'));

System.out.println(s2.lastIndexOf('s'));

System.out.println(s3.lastIndexOf('b'));

String s4 = s1 + s2;

System.out.println(s4.indexOf('c'));

System.out.println(s4.lastIndexOf("ss"));

s1 = s3;

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

if (s1.equals(s3))

System.out.println("true");

else

System.out.println("false");

} }

(22)

Overflow Sheet 1

Write the question number next to your answer.

You must ALSO indicate in the alloted space that you have used the overflow sheet

(23)

Write the question number next to your answer.

You must ALSO indicate in the alloted space that you have used the overflow sheet

(24)

Rough Working This sheet will NOT be marked

(25)

This sheet will NOT be marked

Referensi

Dokumen terkait

Berdasarkan hasil pengamatan melalui observasi, pembelajaran dilakukan dengan tatap muka dan pembelajaran online (daring), guru sudah menerapkan model pembelajaran