Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

JAVA PROGRAMMING 2

COMPSCI 2001

This question concerns Java class and method design.                          (20 marks total)

First, read the following description of a fictional sport.

This sport involves teams of players. Every player on the team has a name (a string) and a number (an integer), and every player is either a defensive player or an offensive player.

A valid team in this sport consists of 7 players, and must include at least 2 defensive players and at least 2 offensive players.

All players can move around the field of play, but offensive players can only move forwards, while defensive players can only move backwards. Only an

offensive player is allowed to take a shot at the opponent’s goal.

Now, answer the questions that follow.

(a)  Provide a definition for a Player class that stores the player’s name, number, and defens- ive/offensive status. Include a constructor that initialises all of the fields, and a complete set

of get methods, but no set methods.                                                                        [4]

(b)  This part deals with creating and manipulating a team of players.

(i) Show a line of Java code declaring and initialising a variable of an appropriate type to  hold a team.                                                                                                  [2] (ii) Write a validate() method that takes one parameter, a team represented as in part  (i), and returns true if the team has a valid number and type of players, and false if not.                                                                                                              [4]

(c)  In this part, you are asked to write additional methods of the Player class.

(i) Write a shoot() method that has the following behaviour:

• If the player is an offensive player, the method prints "She  shoots,  she  scores!"

• If the player is a defensive player, the method throws an                                   UnsupportedOperationException (note that this is an unchecked exception) [2]

(ii) Write a getMovingDirection() method that has the following behaviour:

• If the player is an offensive player, return "Forwards"

• If the player is a defensive player, return "Backwards" [1]

(d)  The client has specified that the existing, all-in-one Player class should be replaced with a well-designed set of classes that represent offensive and defensive players separately. Describe all changes that would be necessary to your answers in parts (a)–(c) to implement this change.

You may illustrate your answer with fragments of Java code, but this is not required.    [7]

2.           This question asks you to read and demonstrate that you understand Java code(20 marks total)

(a)  Consider the following sequence of natural numbers:

5, 8, 11, 14, 17, 20, 23, 26, 29, 32, . . .

Suppose that we want to write a Java program that generates and prints the first n numbers of this sequence, where n ∈ {1, 2, 3, . . .}. The following class is a (buggy) attempt at this task.

Study the class and answer the questions that follow.

1   public  class  NumberSequence  {

2           public  int []  generateSequence(int  n)  {

3                    int []  sequence  =  new  int [n];

4                    for (int  i  =  0;  i  <  n;  i++)  {

5                            sequence[i]  =  3 *i; 6                    }

7            }

8           public  static  void  main(String[]  args)  {

9                    int  n  =  10;

10                    String  sep  =  ",  ";

11                    int []  sequence  =  generateSequence(n);

12                    for (int  i  =  0;  i  <  n;  i++){

13                            System .out .print(sequence[i]  +  sep);

14                            if (i  ==  n- 1)  {

15                                     System .out .print(" . . . "); 16                             }

17                    }

18            }

19   }

(i) Explain why the above class is buggy. For full marks, you need to identify and explain all the issues in the methods computeSequence() and main().                       [3]

(ii) Describe how to fix the above class so that it correctly generates the sequence of numbers given above.  You are not allowed to change the signature of any of the methods in class NumberSequence.

You may illustrate your answer with fragments of Java code, but this is not required. [7]


(b)  Look at the following class and then answer the questions that follow.

1   public  class  Book  {

2           public  String  title  =  "Unknown";

3           public  void  Book()  {

4                    this .title  =  "Java  Programming  2"; 5            }

6           public  static  void  main(String[]  args)  {

7                    Book  b  =  new  Book();

8                    System .out .println(b .title);

9            }

10   }

(i) Explain why the main method prints“Unknown”instead of“Java Programming 2”. [3]

(ii) Explain how to fix it so that it prints“Java Programming 2”. You may illustrate your

answer with fragments of Java code but this is not required.                                [2]



(c)  Consider the following class and answer the questions that follow.

1   public  class  Increments  {

2           public  static  void  integerAdd(int  x,  int  dx)  {

3                    x  =  x  +  dx; 4            }

5

6           public  static  void  main(String[]  args)  {

7                    int  a  =  0;

8                    integerAdd(a,2);

9                    System .out .println(a); 10                    //  a  is  still  0

11            }

12   }

(i) Explain why a has the value 0 at line 10.                                                          [2]

(ii) Outline the changes required to make the code work as intended (i.e., so that a has

value 2 at line 10). Your proposed fix must not create a new class. You may illustrate your answer with fragments of Java code but this is not required.                         [3]


3.           This question asks you to understand and discuss Java programming concepts (10 marks total)

(a)  In the following Java code, identify all of the things that are primitive types, classes, and objects.    [3]

int  radius  =  6;

double []  i  =   [1 . 0,  4 . 0,  5 . 0,  6 . 0,  2 . 0];

Double  d  =  new  Double(100 .5);

List<Integer>  list  =  new  LinkedList<>();

Book  b;

Song  s  =  null;

(b)  Consider the following Java code:

package  sets;

public  class  A  {

ACCESS_MODIFIER  String  s  =  "JP2";

}

Let B be another Java class, and suppose that B wants to access the field member s of class A. Describe what happens at runtime when ACCESS_MODIFIER is set to public ,  private , and protected under the following scenarios:

(i) B is a subclass of A and is in not the same package                                            [1] (ii) B is not a subclass of A and is in the same package                                            [1]

(c)  In early versions of Java, interfaces did not include any concrete (non-abstract) methods. However, in more recent versions, interfaces can also include default methods, which have concrete method bodies. One of the first default methods to be added was stream(), which was added to the Collection interface (the parent of List, Set, and many other Collections types).

In your own words, briefly discuss one advantage and one disadvantage of default methods in interfaces. What would have been the consequences for Java programmers if stream() had been added to the Collection interface without a default implementation?         [5]