程序代写案例-CSSE2002 /

欢迎使用51辅导,51作业君孵化低价透明的学长辅导平台,服务保持优质,平均费用压低50%以上! 51fudao.top
Page 1 of 17



Preview Test: CSSE2002 / CSSE7023 Semester One Final Examination 2020

Test Information
Description CS
SE2002 Programming in the Large
Exam Duration: 120 minutes + 30 minutes additional time
Materials permitted include one A4 sheet (double-sided) of typed or hand-written
notes, blank paper for rough work, writing implements (pen, pencil, eraser or ruler), a
water bottle.
Instructions Answer all questions. Marks are indicated for each question.
Timed Test This test has a time limit of 2 hours and 31 minutes. This test will save and be submitted
automatically when the time expires.
Warnings appear when half the time, 5 minutes, 1 minute, and 30 seconds remain. [The
timer does not appear when previewing this test]
Multiple
Attempts
Not allowed. This test can only be taken once.
Force
Completion
This test can be saved and resumed at any point until the time has expired. The timer will
continue to run if you leave the test.

Your answers are saved automatically.

QUESTION 1
Please use this space to specify any assumptions you have made in completing the exam. Clearly indicate
to which questions those assumptions relate. You may also include queries you may have made with
respect to a particular question, should you have been able to ‘raise your hand’ in an examination room.

Note 1: This was an online only exam. Information in this document is copied from the blackboard test.

Note 2: This exam paper does not seem to be indexed by the UQ library, so is provided separately on blackboard.
Page 2 of 17

QUESTION 2 2 MARKS
What would the following code print?

Integer[] a1 = {1, 2, 3};
Integer[] a2 = {1, 3, 2};
Integer[] a3 = {1, 2, 3};

List l1 = Arrays.asList(a1);
List l2 = Arrays.asList(a2);
List l3 = Arrays.asList(a3);

System.out.println(l1 == l2);
System.out.println(l1 == l3);
System.out.println(l1.equals(l2));
System.out.println(l1.equals(l3));



false
false
false
false


false
false
false
true


false
true
false
true


false
false
true
true

Page 3 of 17

QUESTION 3 2 MARKS
What would the following code print?

Integer[] a = {1, 2, 3, 4};
List l = new ArrayList<>(Arrays.asList(a));
ListIterator li = l.listIterator();

while(li.hasNext()) {
int i = li.next();
if(i % 2 == 0) {
li.add(i + 1);
}
}

System.out.println(l);




The code would not compile. It is not possible to add items to a list using an iterator.


[1, 2, 3, 4, 3, 5]


[1, 2, 3, 4]


[1, 2, 3, 4, 2, 4]


[1, 2, 2, 3, 4, 4]


[1, 2, 3, 3, 4, 5]


Page 4 of 17

QUESTION 4 2 MARKS
What would be the output when the main() method is executed?

enum OS {MAC, WINDOWS, LINUX, VMS}

public class Enums {
public static String getStatus(OS os) {
String result = "";
switch (os) {
case MAC:
result += "Designer?";
break;
case WINDOWS:
result += "Coder?";
break;
case LINUX:
result += "Hacker?";
break;
case VMS:
result += "COBOL Coder?";
break;
default:
result += "What?";
}
return result;
}

public static void main(String... args) {
System.out.println(getStatus(OS.WINDOWS));
}
}



Coder?Hacker?COBOL Coder?What?


Coder?


There is a compile error in the code.


What?


The code would throw an exception.

Page 5 of 17

QUESTION 5 2 MARKS
What would be the output when the main() method is executed?

public class Z {
public static void z(int[] a, int[] b) {
a = new int[a.length];
for (int i = 0; i < b.length; i++) {
a[i] = b[i];
}
}

public static void main(String... args) {
int[] x = {1, 2, 3, 4};
int[] y = {5, 6, 7, 8};
z(x, y);

for (int i = 0; i < x.length; i++) {
System.out.print(x[i]);
}
System.out.println();
for (int i = 0; i < y.length; i++) {
System.out.print(y[i]);
}
}
}



5678
1234


1234
5678


null
1234


null
5678


5678
5678

Page 6 of 17

QUESTION 6 2 MARKS
For the code in the previous question (5), what would happen if the array y was:

int[] y = {5, 6, 7, 8, 9};




The method z would produce a result similar to the result in question 9, but would include the extra
value in y.


The code would not compile, as the arrays are different lengths.


The method z would ignore the extra value in y and the result would be the same as in question 9.


The method z would throw an exception.

Page 7 of 17

QUESTION 7 2 MARKS
What would be the output when the main() method is executed?

public class L {
public static void main(String... args) {
int i = 42;
int t = 0;

do {
if(i % 7 == 0) {
i = i / (t + 1);
} else {
i -= 1;
}
t += 1;
} while(i > 0);

System.out.println(t);
}
}



6


4


5


7


There would not be any output, as it is an infinite loop.

Page 8 of 17

QUESTION 8 3 MARKS
For the following code, select which statements would execute successfully at the line indicated by the
comment "// Statement Goes Here".

class A {
}

class B extends A {
}

class C extends A {
}

class D extends B {
}

public class Inherit {
public static void main(String... args) {
A a = new A();
A b = new B();
A c = new C();
A d = new D();

// Statement Goes Here
}
}



A r1 = (A)c;


B r2 = (B)d;


B r3 = (B)c;


C r4 = (C)d;


D r5 = (B)d;


D r6 = (D)d;

Page 9 of 17

QUESTION 9 12 MARKS
Assume that the following four classes are all in separate files within the same package.

public class A {
protected C c;
protected D d;
protected int i = 0;

public A(C c, D d) {
this.c = c;
this.d = d;
}

public void doSomething() {
c.doC();
}

public void doNext() {
doOther();
}

protected void doOther() {
System.out.println(i);
}
}
public class B extends A {
public B(C c, D d) {
super(c, d);
}

@Override
public void doSomething() {
super.doSomething();
d.doD();
}

@Override
protected void doOther() {
i = 1;
}
}
public class C {
private A a;
private D d;

public C(D d) {
this.d = d;
}

public void setA(A a) {
this.a = a;
}

public void doC() {
System.out.println("C");
}

public void doMore() {
Page 10 of 17

a.doSomething();
d.doD();
}
}
public class D {
private A a;
private C c;

public void setA(A a) {
this.a = a;
}

public void setC(C c) {
this.c = c;
}

public void doD() {
System.out.println("D");
}

public void doMore() {
a.doNext();
c.doC();
}
}

Do these classes exhibit high or low coupling? Provide a detailed justification for your answer. Your
answer should cover all aspects of the coupling in the code.

Page 11 of 17

QUESTION 10 3 MARKS
For the classes in the previous question (9), assume the following main method is inside class A.

public static void main(String... args) {
D d = new D();
C c = new C(d);
A a1 = new A(c, d);
A a2 = new B(c, d);
c.setA(a1);
d.setA(a2);
d.setC(c);

a1.doNext();
a2.doSomething();
a2.doNext();
}

What would be the output when the main() method is executed?


0
D



0
C



0
C
D



0
D
1



0
C
D
1



0
C
1


Page 12 of 17

QUESTION 11 10 MARKS
In the Model-View-Controller (MVC) approach to designing a Graphical User Interface (GUI), explain how
the model and the user interface interact with each other. What is the purpose of following this approach
to designing a GUI?

Page 13 of 17

QUESTION 12 12 MARKS
Assume that the following four classes are all in separate files within the same package.

public class A {
/**
* @require values != null && targets != null
* && values.size() == targets.size()
* && variance >= 0 && variance < 1
* @ensure \forall int i; 0 <= i && i < values.size();
* \result is the list of all elements in values
* where Math.abs(values[i] - targets[i]) <= variance
*/
public List filter(List values, List targets,
double variance) { }
}

public class B extends A {
/**
* @require values != null && targets != null
* && values.size() == targets.size()
* && variance >= 0 && variance <= 1
* @ensure \forall int i; 0 <= i && i < values.size();
* \result is the list of all elements in values
* where Math.abs(values[i] - targets[i]) < variance
*/
@Override
public List filter(List values, List targets,
double variance) { }
}

public class C extends A {
/**
* @require values != null && targets != null
* && values.size() == targets.size()
* && variance > 0 && variance < 1
* @ensure \forall int i; 0 <= i && i < values.size();
* \result is the list of all elements in values
* where Math.abs(values[i] - targets[i]) <= variance
*/
@Override
public List filter(List values, List targets,
double variance) { }
}

public class D extends A {
/**
* @require values != null && targets != null
* && values.size() <= targets.size()
* && variance >= 0 && variance < 1
* @ensure \forall int i; 0 <= i && i < values.size();
* \result is the list of all elements in values
* where Math.abs(values[i] - targets[i]) <= variance
*/
@Override
public List filter(List values, List targets,
double variance) { }
}
Page 14 of 17


The Liskov substitution principle requires that a subtype's overridden methods must satisfy the
supertype's method specifications. It guarantees that code expecting the supertype method behaves
correctly even though the calls go to the subtype implementation.

1. Does class B satisfy the substitution principle with respect to class A? Explain why or why not.


2. Does class C satisfy the substitution principle with respect to class A? Explain why or why not.


3. Does class D satisfy the substitution principle with respect to class A? Explain why or why not.

Page 15 of 17

QUESTION 13 18 MARKS
For the following method:

/**
* @require values != null && targets != null
* && values.size() <= targets.size()
* && variance >= 0 && variance < 1
* @ensure \forall int i; 0 <= i && i < values.size();
* \result is the list of all elements in values
* where Math.abs(values[i] - targets[i]) <= variance
*/
public List filter(List values, List targets,
double variance) {
List result = new ArrayList<>();

for(int i = 0; i < values.size(); i++) {
if(Math.abs(values.get(i) - targets.get(i)) <= variance) {
result.add(targets.get(i));
}
}

return result;
}

1. Provide a complete set of black box test cases for the filter method. For each test case, provide the
input, expected output and a justification for each case.
2. Provide a complete set of white box test cases for the filter method. For each test case, provide the
input, expected output and a justification for each case.


Page 16 of 17

QUESTION 14 16 MARKS
The following class provides an iterative implementation of a method that counts the number of
consonants that appear in a string.

public class CountConsonants {
static boolean isConsonant(char ch) {
ch = Character.toUpperCase(ch);

return !(ch == 'A' || ch == 'E' ||
ch == 'I' || ch == 'O' ||
ch == 'U') && ch >= 65 && ch <= 90;
}

static int totalConsonantsIterative(String str) {
int numConsonants = 0;
for (int i = 0; i < str.length(); i++) {
if (isConsonant(str.charAt(i))) {
++numConsonants;
}
}
return numConsonants;
}

static int totalConsonantsRecursive(String str, int strLengthToTest) {
}
}

1. In the totalConsonantsRecursive method, implement the same functionality as is provided
by totalConsonantsIterative using recursion instead of iteration.
2. Write a Javadoc comment for totalConsonantsRecursive. The Javadoc should describe the method
and provide all the tags that are appropriate for the method's signature.
Include @require and @ensure tags that describe appropriate pre and post conditions for the method.

Page 17 of 17

QUESTION 15 14 MARKS
Assume that the following classes representing pets are implemented correctly.

public class Pet { }
public class Cat extends Pet { }
public class Dog extends Pet { }

1. Write the code to implement a generic Kennel class that can only hold a Pet or a specified subtype of
Pet. The class should have a constructor, a method to add a pet to the kennel, and a method to retrieve a
pet from the Kennel. The method to retrieve a pet should be provided with an integer that is the location
of the pet in the kennel's collection of pets. You may use any appropriate collections from java.util.
2. Explain how you could implement a non-generic Kennel class that could hold any type of Pet objects.
What additional restriction does the generic implementation of the Kennel class provide? Why might you
want to implement the Kennel class with that restriction?


欢迎咨询51作业君
51作业君

Email:51zuoyejun

@gmail.com

添加客服微信: abby12468