Declare a local String variable id and initialize it with your 8-digit student ID in string form. What is the output of the following program segment?

computer science

Description

Question 10

 

(a)        Declare a local String variable id and initialize it with your 8-digit student ID in string form. What is the output of the following program segment?

 

String str = id + " "; // two spaces System.out.println(str.indexOf("5") + " + " + str.substring(0,2) + "\n"

 

+ str.trim().substring(5)+ "#");

 

 

(b)        The class List below is in java.util. What is the output of the following program segment?

 

List<String> aList = new Vector<>();

 

aList.add("one");

aList.add("two");

aList.add("three");

aList.add(2, "one");

aList.remove("one");

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

 

 

Question 11

 

Write a method recursive(char aa, char bb) of a class Chart which returns a string starting from aa, concatenating with characters having values aa+1, aa+2, ... and ending with bb using recursion, where aa <= bb. For example, the call aChart.recursive('b', 'e') returns the string:

 

bcde

 

where aChart is an object of the class Chart.

 

 

Question 12

 

(a)        To find out the opinions of Hong Kong people on an issue, a series of dialogue boxes are designed as follows.

 

(1)       The first box is:

 

 

 

 

 

 

 

 

 

It is produced by the showConfirmDialog(null, String message) method of JOptionPane which returns an integer (the value of which is YES_OPTION, NO_OPTION or CANCEL_OPTION when the "Yes", "No" or "Cancel" button is clicked respectively). The message is the string "Do you agree the government should set up an independent committee to look into the unrest?".

 

(2)       If "Cancel" is clicked, the following message is output and the survey terminates.

 

 

 

 

 

 

 

 

 

(3)        If "Yes" or "No" is clicked, a new box appears:

 

 

 

 

 

 

 

 

 

If the person clicks "OK", then whatever entered is stored in a string variable reason.

 

(4)        If the person clicks "Cancel", the following message appears and the survey ends after the person clicks "OK".

 

 

 

 

 

 

 

 

Write a Java program segment to produce this series of dialogue boxes. Remember to declare any variable(s) needed.

 

 

 



Related Questions in computer science category