关键词 > Java代写

Introduction to Programming

发布时间:2021-01-04


THE UNIVERSITY OF SUSSEX

BSc and MComp 1st YEAR EXAMINATION

January 2019 (A1)

Introduction to Programming



Assessment Period: January 2019 (A1)


DO NOT TURN OVER UNTIL INSTRUCTED

TO BY THE LEAD INVIGILATOR


Candidates should answer TWO questions out of THREE.

If all three questions are attempted only the first two answers will be marked.


The time allowed is TWO hours.

Each question is worth 50 marks.


At the end of the examination the question paper and/or answer book, used or

unused, will be collected from you before you leave the examination room.


1. Consider the following Java program, consisting of the class Friends. Notice this is defined to have one constructor and two methods, one named replace, and one named toString.

public class Friends {

String man, woman;

String[] names = {"xi", "yu", "max", "jen",

"dave", "anne", "johny", "olive"};

public Friends(int k) {

for (int i = 0; i < names.length; i += 2) {

if (names[i].length() > k) {

man = names[i];

woman = names[i+1];

break; } }

}

public String replace(String name) {

String newFriend;

for (int i = 0; i < names.length; i++) {

if (names[i].equals(name)) {

newFriend = names[(i+2) % names.length];

break;

}

}

return newFriend;

}

public String toString() {

String s = man + " is friends with " + woman;

return s;

}

}


(a) Given this class definition, what would be printed out by the code System.out.println(new Friends(2))? Use text and/or diagrams to show how you derive the answer.

[20 marks]

(b) Produce an English description of the replace method, including an explanation of the effect achieved by use of the ‘%’ operator. Suggest an alternative way of achieving this effect.

[15 marks]

(c) Given fr is the object constructed by new Friends(3), what would be returned by the call fr.replace(fr.man)? Use text and/or diagrams to show how you derived your answer.

[15 marks]


2.

(a) Write a Java program that consists of a definition for a public class called Token. There should be two instance variables. One should be of type char, and be named root. The other should be a one-dimensional array of Token, and be named multi. The class should have one constructor with a formal parameter of type char named root. This constructor should use the “this” keyword to assign its actual parameter to the instance variable of the same name. The other constructor should take a String. This should create and assign to multi an array of Token, each element of which is constructed on a distinct char from the specified String. (Remember that s.charAt(i) will return the i’th char from String s.)

[20 marks]

(b) Add a method to the class named getRandom(), which returns a Token. This should behave as follows. If the object on which the method is called has a null value for its multi variable, it should return a Token constructed on the char ‘z’. If the multi variable is not null, the method should return one of the Token objects from the multi array selected at random. (Remember that Math.random() produces a random double value in the range 0.0..1.0.)

[15 marks]

(c) Write an additional method for the class defined above, named nMultis. This should take a char and return an int. The behavior of the method should be as follows. If the object’s multi array is null, the method should return 0. Otherwise, it should go through the array to discover how many of the Token objects have a value of the root variable identical to the method’s argument. This int value should then be returned.

[15 marks]


3.

(a) An important concept in Java programming is that of “overloading”. What do we mean by this term? In what situations is overloading a useful programming strategy?

[10 marks]

(b) During the writing of a program, you are confronted with a coding problem very similar to one you’ve already solved in another part of the program. One way to take advantage of this is by pasting in the code you wrote previously. Comment on the viability of this strategy. If there is an alternative approach, explain what it is.

[10 marks]

(c) When you are reading code, you may come across use of a standard class that you have not encountered before. Describe where you need to look (e.g., online) to identify the ways in which the class can be used.

[10 marks]

(d) Variables are said to have both a scope and a lifetime. What do these terms mean and how do the concepts differ? Give an example to illustrate restriction of scope.

[10 marks]

(e) Explain one of the mechanisms that Java provides for enabling a program to be executed from the command line, as an application.

[10 marks]