程序代做CS代考 Java junit SOFT2201/COMP9201 Tutorial 1 Java Revision

SOFT2201/COMP9201 Tutorial 1 Java Revision
Introduction
You are going to be assigned to a breakout room on zoom by your tutor. Introduce yourself to the rest of the class, what degree you are in, why you are interested in programming and what you did over the break. Make sure you know your tutor’s name by the end of the tutorial.
Edstem
We use EdStem for our forum, challenges and assignments. please get familiar with Edstem as it will be used heavily through out the semester and is typically the place where announcements are made. Please make sure you can login (Canvas → Ed on the left side menu) and reply to the welcome post. If you cannot login, please notify your tutor so they can address this issue.
Question 1: Swapping Values
Giving the following code segments, discuss with your group members what the output will be, can you explain why or why not certain values are not swapped?
Case 1:
public class Program {
public static void swap(int x, int y) { int temp = x;
x = y;
y = temp; }
public static void main(String[] args) { int a = 10;
int b = 20;
swap(a, b); System.out.println(a); System.out.println(b);
1

} }
Case 2:
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]);
} }
Case 3:
class Box {
public int value;
public Box(int v) { value = v; }
}
public class Program {
public static void swap(Box a, Box b) { int temp = a.value;
a.value = b.value;
b.value = temp;
}
public static void main(String[] args) { Box x = new Box(500);
Box y = new Box(600);
swap(x, y); System.out.println(x.value); System.out.println(y.value);
} }
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1
Page 2 of 13

Question 2: Reverse
Write a function that reverses an integer array,
I 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[]) {
} }
Example
//Your code here, must perform an inplace reverse
//If you have an array that is:
{ 1, 2, 3, 4, 5, 6, 7, 8 }
//Output
{ 8, 7, 6, 5, 4, 3, 2, 1 }
Question 3: Comparing and sorting
Implement a program that will sort a collection of Strings by the length of the string. 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[] {
“One”,
“Two”,
“Three”,
“Four”,
“Five”,
“Six”,
“Seven”
});
Collections.sort(strings, /* Your comparator here */);
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1 Page 3 of 13

for(String s : strings) { System.out.println(s);
}
Your program should output something like the following:
Two
Six
One
Four
Five
Three
Seven
Now try replacing your Comparator with one that will do a reverse lexicographical ordering. Your program should output the following:
Two
Three
Six
Seven
One
Four
Five
Question 4: Union
You are tasked with writing a method that will compute the union between two arrays. Using methods such as countDuplicates, contains or count, 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) { }
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1
Page 4 of 13

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. Example 1
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
Example 2
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
Example 3
int[] x = null; int[] y = {6, 8, 9};
int[] result = union(x, y); //nulldmesg
Gradle
We recommended you get access to sdkman to download a package manager for java 11 and gradle. Through out the semester you are expected to develop your applications over the semester using gradle to help link to dependencies, test your code and provide a release configuration for your application. You can also find an announcement on Ed with more details about gradle installation.
Question 5: Using gradle
You are tasked with creating a simple gradle project so you are familiar with how the build system operates.
Firstly, start by creating a project folder using terminal.
$ mkdir my_project
$ cd my_project
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1 Page 5 of 13

SOFT2201/COMP9201 Java Revision To initialise a gradle project, use the init sub-command to get started.
$ gradle init
Once gradle init has been called, you will be prompted with the type of project you want to create. We want to create a java-application with groovy as the build tool language and junit as our test framework. Note that the menus will be slightly different, depending on your exact version of gradle. Another common variation is that you may need to first select application and then select java. Experiment with the options to explore the possibilities.
$ gradle init
Select type of project to generate:
1: basic
2: cpp-application
3: cpp-library
4: groovy-application
5: groovy-library
6: java-application
7: java-library
8: kotlin-application
9: kotlin-library
10: scala-library
Enter selection (default: basic) [1..10] 6
Select build script DSL:
1: groovy
2: kotlin
Enter selection (default: groovy) [1..2] 1
Select test framework:
1: junit
2: testng
3: spock
Enter selection (default: junit) [1..3] 1
Project name (default: hello):
Source package (default: hello):
BUILD SUCCESSFUL in 16s
2 actionable tasks: 2 executed
Once your project has been set up, you can readily build your project using the command gradle
buildandrunyourprogramusinggradle run.
Software Design and Construction 1 Page 6 of 13

Java Language and IDE
During the semester you will be required to implement your assignments using the Java Programming Language. You will be expected to use an Integrated Development Environment (IDE) to assist you with developing software over the semester.
Although an IDE is not required, you are encouraged to use IntelliJ IDEA from JetBrains. Tutorials will contain excerpts from the IntelliJ IDEA from JetBrains to help linking to software libraries. IntelliJ utilises gradle as its build system
Question 6: IntelliJ and Gradle
SOFT2201/COMP9201 Java Revision
(a) Step1: Welcome Page
(b) Step2: Create Project
Assuming you have installed IntelliJ, we will set up a simple project for the next exercise. Firstly create a new project from the initial prompt. Once presented with the Step 2 window, select Gradle
Software Design and Construction 1 Page 7 of 13

(a) Step3: Select Gradle
(b) Step4: Enter Artifactid
Ensure Java is enabled under Additional Libraries and Frameworks. Afterwards, we will need to specify an artifact id, this can be the same as the java package name, in this case we will call it my.hello.
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1 Page 8 of 13

(a) Step5: Gradle Configuration
(b) Step6: Src Code
Afterwards, we will need to specify what we will want to import and a gradle configuration, assuming you have installed gradle correctly, you can leave the settings as they are. At this point you will have created a gradle project within IntelliJ, depending on the version of gradle you are using, you may already be given a simple Hello, World example, if not, it might be best to ensure that your project is configured correctly. You will need to right click on java and create a Package, in this case we want to create the parent package my and the sub-package hello, where your App class will reside.
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1 Page 9 of 13

Afterwards, you can execute your program, if the run option is immediately present, you can utilise this, if not, you will need to use Run….
This option will present you with an option to specify the class to execute.
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1 Page 10 of 13

Question 7: Trains, Cargo and Stations
TrainLink has decided to develop a cargo freighting service between regional centres of Australia. A cargo train will deliver cargo from each station on its itinerary. An itinerary contains a list of stations to visit, with the train starting at the first station and ending at the last.
Write Java code to implement the functionalities as required above and use the following unit test to for the basic test cases check your solution.
import java.util.ArrayList; import java.util.Arrays;
package soft2201.tutorial1.test; public class FreightServiceTest {
private Train train;
@Before
public void setup() {
ArrayList stations = new ArrayList<>(Arrays.asList(
) );
new Station(“Moree”, new ArrayList( Arrays.asList(
new Cargo(“Barley”),
new Cargo(“Avocados”),
new Cargo(“Truck Engine”), new Cargo(“Drone”)
) )),
new Station(“Gunnedah”, new ArrayList( Arrays.asList(
new Cargo(“DVDs”),
new Cargo(“Textbooks”), new Cargo(“Soybean”)
) )),
new Station(“Murrundai”, new ArrayList()), new Station(“Scone”, new ArrayList(
Arrays.asList(
new Cargo(“Oats”), new Cargo(“Barley”)
) ))
Itinerary itinerary = new Itinerary(stations); train = new Train(“Big Blue”, itinerary,
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1
Page 11 of 13

); }
@Test
new ArrayList( Arrays.asList(
) )
public void testStationConstruction() {
Station station = new Station(“Newcastle”, new ArrayList(
Arrays.asList(
new Cargo(“Wine”)
) ));
assertNotNull(station.cargo());
assertNotNull(station.cargo().get(0));
assertEquals(“Wine”, station.cargo().get(0).getName());
}
@Test
public void testItineraryConstruction() {
Itinerary itinerary = new Itinerary(new ArrayList(
Arrays.asList(
new Station(“Newcastle”, new ArrayList()), new Station(“Wyong”, new ArrayList())
) ));
assertEquals(“Newcastle”, itinerary.stations().get(0).getName());
assertEquals(“Wyong”, itinerary.stations().get(1).getName());
}
@Test
public void testStationAccess() {
assertEquals(4, train.itinerary().size()); assertNotNull(train.itinerary().getStation(0)); assertNotNull(train.itinerary().getStation(1)); assertNotNull(train.itinerary().getStation(2)); assertNotNull(train.itinerary().getStation(3));
}
@Test
public void testItineraryAccess() { assertNotNull(train.itinerary().getStation(0)); assertNotNull(train.itinerary().getStation(1));
SOFT2201/COMP9201 Java Revision
new Deliverable(new Cargo(“Steel”, “Moree”)),
new Deliverable(new Cargo(“Copper”, “Murrundai”)), new Deliverable(new Cargo(“Coal”, “Scone”))
Software Design and Construction 1
Page 12 of 13

assertNotNull(train.itinerary().getStation(2));
assertNotNull(train.itinerary().getStation(3));
}
@Test
public void testStartingStationVisit() { assertNotNull(train.getCurrentStation()); assertEquals(train.getCurrentStation().getName(), “Moree”);
}
@Test
public void testSimpleDelivery() {
Station moree = train.getCurrentStation(); assertEquals(“Moree”, train.getCurrentStation().getName()); assertEquals(3, train.deliverables().size()); assertEquals(“Steel”, train.deliverables().get(0).getName()); assertEquals(“Copper”, train.deliverables().get(1).getName()); assertEquals(“Coal”, train.deliverables().get(2).getName()); assertEquals(4, moree.cargo().size());
train.deliver();
assertEquals(2, train.deliverables().size()); assertEquals(“Copper”, train.deliverables().get(0).getName()); assertEquals(“Coal”, train.deliverables().get(1).getName()); assertEquals(5, moree.cargo().size());
} }
Onceyouhaveimportedthetestintoyourproject,rungradle testtorunyourunittests.
• What functionality is/isn’t currently being tested?
• Have all code paths been executed? Suggest what components have not been tested and what branches have been executed
• What kind of cases are the current batch of test cases not considering?
• Implement a few more test cases to increase your code coverage and to ensure as much func- tionality is tested
SOFT2201/COMP9201 Java Revision
Software Design and Construction 1 Page 13 of 13