Intellij can be found on the University computers. On your own computers you may download from :
https://www.jetbrains.com/idea/download/
The Community edition is free.
Launch intelliJ from the start menu of your computer.
Follow this tutorial to create your first java application: https://www.jetbrains.com/help/idea/creating-and-running-your-first-java-application.html
Stop at the heading ‘Package the application in a JAR’. We will cover this later.
If you have followed the above tutorial you will have a single Java class with the following code.
package com.example.helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWord");
}
}
Which you should be able to run by clicking the green arrow in the line numbering section next to your code to produce the output ‘hello world’.
_NOTE THAT: In Java, a ‘main’ method which conforms to the signature public static void main(String[] args)
will be the ‘entrypoint’ to your program, ie. it is the code that will run when your program is launched.__
Here are the fundamental features of any programming language. You know them from your previous modules. When you learn a new language you should always look up these topics and learn how the language implements them.
Familiarise yourself with the language fundamentals above by completing the following tasks.
In your public static void main method, delete the line which prints ‘HelloWorld’. Replace it with the follwoing
public class HelloWorld {
public static void main(String[] args) {
int a = 42;
int b = 119;
int c = a + b;
System.out.println(c);
}
}
Run the code with green arrow. Notice that the code compiles before it is run.
Now change the variable declaration of b to the following
String b = "119";
What happens? Why?
Finally change the variable declaration of c to the following
String c = a + b;
What happens? Do you get the same answer as when b and c are ints?
Just like the other languages you know, you can define collections of variables that can be referenced as a single variable, such as lists or dictionaries in Python or vectors in c++.
Create an ArrayList as follows (there are various collection types in java) by amending your code to the following. Read the comments to help you understand the code and note how single line and multi-line comments are indicated by \\
and /* */
respectively.
package com.example.helloworld;
// You need to import the ArrayList library
import java.util.ArrayList;
public class HelloWorld {
public static void main(String[] args) {
/* ArrayLists are collection objects that can be dynamic in size.
The type of thing that is going to be stored in the ArrayList needs to be specified in the angled brackets.
Also note the keyword 'new'.
*/
ArrayList<String> myList = new ArrayList<String>();
// Add elements to myList
myList.add("one");
myList.add("two");
myList.add("three");
// A simple 'for' loop can iterate over myList, creating a new variable list with every iteration of the loop
for (String listElement : myList) {
System.out.println(listElement);
}
}
}
Task 1:
Amend the code so that you also print the array index for each of the array elements. HINT: In a Java for loop you cannot access the index directly. Instead you can use the indexOf() method of the ArrayList, so for our example try:
myList.indexOf(listElement)
Why do the index numbers not correspond to the strings?
Task 2:
Now amend the contents of the list so that it contains animal names instead of strings of number names, for example, cat, dog, rabbit
Task 3: Basic debugging
Lets pretend you are not seeing the output you expect in your loop. You will need to find out if there is an error in the contents of the list OR if your looping code is incorrect. The first thing to check is that the contents of the myList are as expected before entering the loop so you will want to examine it. The most basic way to do this is to use System.out.println on the entire object ie: add this code before the loop starts and look at the results in the console.
System.out.println(myList);
Add some code to the above exercise such that there is only output if the number is “three” as below
for (String listElement : myList) {
// Note use of print instead of println to suppress the line break
if (listElement.equals("three")) {
System.out.println ("Got three");
}
else if (listElement.equals("two")) {
System.out.println ("Got two");
}
else {
System.out.println("Got something else");
}
}
What happens if you use the customary ==
instead of the .equals() method in this code?
==
.You will create the student class as represented by this class diagram. You will create two student objects in your ‘main’ function and output correct information about them.
This code should run but its not giving you useful results. Go back into Student.cs and amend the getStudentInfo() method so that the name and course of each student is printed correctly.
Hint: You can use the + operator with strings in java, OR you can use ‘string interpolation’ to embed variables into strings. For example the following code will output:
Hello LisaH !
String teacher = "LisaH";
String hello = String.format("Hello %s", teacher);
System.out.println(hello);
See: https://www.geeksforgeeks.org/java-program-to-illustrate-string-interpolation/
Implement the remaining methods of the Dog class used in the week 1 slides and improve the logic of the bark() function in any way that you can think of.