CS代考 SOFT2201/COMP9201 Week 2 Tutorial

SOFT2201/COMP9201 Week 2 Tutorial
Java Revision

Introduction

Copyright By PowCoder代写 加微信 powcoder

You are going to be assigned to a group (on campus)/breakout room (on Zoom) by your tutor. Intro-
duce yourself to your classmates: what degree you are in, why you are interested in software design
and construction, and so on. Make sure you know your tutor’s name by the end of the tutorial.

Java Language, Gradle and IDE

During the semester you will be required to implement your assessments using the Java Programming
Language, and be expected to develop your applications using gradle to help link to dependencies, test
your code and provide a release configuration for your application. You can find an announcement on
Ed with more details about software installation.

You will be expected to use an Integrated Development Environment (IDE) to assist you with devel-
oping software over the semester. Although an IDE is not madetatory, you are encouraged to use
IntelliJ IDEA from JetBrains. IntelliJ utilises gradle as its build system.

Now your tutor is going to use the following Java example to illustrate how to use Gradle and IntelliJ.

Example: Swapping Values

Before you execute the program, please keep the questions in mind: what the output will be, can you
explain why or why not certain values are not swapped?

public class Program {

public static void swap(int x, int y) {
int temp = x;

public static void main(String[] args) {
int a = 10;
int b = 20;

SOFT2201/COMP9201 Java Revision

swap(a, b);
System.out.println(a);
System.out.println(b);

Based on the above program, we are now changing the variables into an array as follows. What will
be your answer to the above same questions?

public class Program {

public static void swap(int[] array) {
int temp = array[0];
array[0] = array[1];
array[1] = temp;

public static void main(String[] args) {
int[] arr = {50,100};
swap(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);

Question 1: Reverse

Write a function that reverses an integer array.

We would encourage you write a reverse function that is in place. By this we mean that you do not
copy the contents into another array and only use the array given.

public class Reverse {
public static void reverse(int array[]) {

//Your code here, must perform an inplace reverse

//If you have an array that is:
{ 1, 2, 3, 4, 5, 6, 7, 8 }

{ 8, 7, 6, 5, 4, 3, 2, 1 }

Software Design and Construction 1 Page 2 of 7

SOFT2201/COMP9201 Java Revision

Question 2: Comparing and sorting

Implement a program that will sort a collection of Strings in reverse order. Do not reverse the list but
create a Comparator to order the collection.

import java.util.List;
import java.util.Collections;

public class App {

public static void main(String[] args) {
List strings = Arrays.asList(new String[] {

Collections.sort(strings, /* Your comparator here */);

for(String s : strings) {
System.out.println(s);

Your program should output the following:

Software Design and Construction 1 Page 3 of 7

SOFT2201/COMP9201 Java Revision

Question 3: Union

You are tasked with writing a method that will compute the union between two arrays. You will need
to detect overlap between the two sets and ensure you are not including an element that appears in
both sets more than once.

You have been provided a scaffold for the above problem

public class ArrayUnion {

public static int[] union(int[] a, int[] b) {
return null;

public static void main(String[] args) {

You will need to check if there is a duplicate element in both sets as you should only return one
element, not both.

Your method must return null if either array is null.

int[] x = {3, 2, 7};
int[] y = {3, 8, 9};

int[] result = union(x, y);
//{2, 3, 7, 8, 9}, it is not necessary to sort the array

int[] x = {2, 2, 7};
int[] y = {1, 9};

int[] result = union(x, y);
//{1, 2, 7, 9}, it is not necessary to sort the array

int[] x = null;
int[] y = {6, 8, 9};

int[] result = union(x, y);
//nulldmesg

Software Design and Construction 1 Page 4 of 7

SOFT2201/COMP9201 Java Revision

Inheritance

Classes are a fundamental construct within java, they allow the programmer to aggregate data and
functionality under a defined type. This feature isn’t just for reusability and simplicity, we are able
to safely read and write to memory by ensuring we adhere to the type system rules and satisfy the
constraints enforced by the compiler.

Within Java, all classes inherit from the Object type, providing a definition for toString and
equals which can be overridden.

Interfaces and Abstract Classes

Interfaces within java allow the programmer to create a type specifying abstract methods. When we
specify that a class implements an interface, it must ensure a method body for each method specified
in the interface.

interface Drivable {
public boolean start();

public boolean stop();

public void move();

In the Drivable example, any type that implements this interface must also implement start,
stop and move, allowing us to categorise objects of different types under one. Within our applica-
tion, types such as Car, Truck and Train can implement the Drivable type.

Similar to an interface, an abstract class can be used to provide a type association with
common properties, however within Java, a class is limited to inheriting from just one class where it
can implement many interfaces.

• If our application contained a Car, Truck and Train, would we group these classes using an
interface or abstract class?

Software Design and Construction 1 Page 5 of 7

SOFT2201/COMP9201 Java Revision

Question 4: Polymorphism

You have been given the following java code, you may assume that the program compiles successfully.

interface Animal {
void talk();

public class Zoo {

public static void makeNoise(List animals) {
for(Animal a : animals) {

public static void outputToString(List objects) {
for(Object a : objects) {

System.out.println(a);

public static void addAnimal(List animals, Animal a) {
animals.add(a);

• The following classes: Lion, Wolf, Bear and Cat, implement the Animal interface, what
method are they required to implement?

• When we invoke the method makeNoise, what object types can be contained within parameter

• When we invoke the method outputToString, what object types can be contained within
parameter objects?

• Can we call talk for each object in the outputToString method?

• When we invoke the method addAnimal, what object types can be contained within parameter
animals and what types can be passed to a?

Software Design and Construction 1 Page 6 of 7

SOFT2201/COMP9201 Java Revision

Question 5: Mapping out the heirarchy

Given the following code, draw an object graph, noting the inheritance heirarchy with a fellow class

//Cabinet.java
public class Cabinet {

public boolean broken;
public boolean assembled;

public Cabinet() {
broken = false;
assembled = false;

//Tool.java
public abstract class Tool {

public abstract void use(Cabinet c);

//Hammer.java
public class Hammer extends Tool {

public void use(Cabinet c) {
c.broken = true;

//Mallet.java
public class Mallet extends Hammer {}

//Screwdriver.java
public class Screwdriver extends Tool {

public void use(Cabinet c) {
c.assembled = true;

Software Design and Construction 1 Page 7 of 7

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com