23T3 Week 1
Monday 11AM - 2PM (M11B)
Wednesday 12PM -3PM (W12A)
Slides by Alvin Cherk (z5311001)
git add
git commit
git push
git status
git log
When you commit
something, you are effectively saving the staged files as a new snapshot and signing it with your name and email.
You have to configure your git identity if you haven't done it before.
git config --global user.name "Your Name Here"
git config --global user.email "z555555@unsw.edu.au"
You then can add whatever email you have set in user.email on GitLab, so that it recognises all the commits that have been pushed to GitLab.
Please do this, its important Git etiquette.
It also allows your tutor to track the work you have done in your pair assignment.
{
and }
to describe code blocks (also scopes)Ensure that you open the correct folder
example: cs2511-project
Good
Bad
Name of folder open
The folder I want to actually open
Ensure you have the correct Java extension installed
Ensure that you are running code using the "Run" button
Always use this to run code in this course
HelloWorld.java
Make a simple Java program that prints "Hello World" in the HelloWorld
class.
package example;
/**
* Prints "Hello World" to the console.
*/
public class HelloWorld {
public static void main(String[] args) {
// Does it need a \n?
// No, .println appends a \n to your string when it prints
System.out.println("Hello World");
}
}
Sum.java
Inside a new file called Sum.java
, write a program that uses the Scanner
class which reads in a line of numbers separated by spaces, and sums them.
package example;
import java.util.Arrays;
import java.util.Scanner;
/**
* Write a program that uses the `Scanner` class
* which reads in a line of numbers separated by spaces,
* and sums them.
*/
public class Sum {
public static void main(String[] args) {
/**
* new - Creates a new Scanner object. (Think of it like C Malloc, but Java's
* garbage collection frees it)
* Scanner is an object that allows us to specify an input
* System.in == stdin in C
*/
Scanner scanner = new Scanner(System.in);
/**
* Keeps reading input until it sees a \n
*
* Splits each string into an array of strings
*/
String[] numbers = scanner.nextLine().split(" ");
int sum = 0;
for (String number : numbers) {
sum += Integer.parseInt(number);
}
System.out.println("The sum is " + sum);
// Advanced
// Using streams
int streamSum = Arrays.asList(numbers).stream().mapToInt(x -> Integer.parseInt(x)).sum();
System.out.println(String.format("The sum is %d", streamSum));
/**
* Frees I/O resources
* Java's garbage collector only manages memory, not other resources
*/
scanner.close();
}
}
Loop.java
public class LoopExample {
public static void main(String[] args) {
String[] myStrings = { "Hello", "World", "No" };
// Index based looping
for (int i = 0; i < myStrings.length; i++) {
String current = myStrings[i];
System.out.println(current);
}
// For-range / for-in loop
for (String current : myStrings) { // Very python like
System.out.println(current);
}
}
}
Why do we use classes?
Ignoring OOP, Inheritance, Polymorphism, classes are useful for:
Shouter.java
Inside a new file Shouter.java
, Write a program that stores a message and has methods for getting the message, updating the message and printing it out in all caps. Write a main()
method for testing this class.
package example;
public class Shouter {
private String message;
public Shouter(String message) {
this.message = message;
}
public String getMessage() {
// NOTE: You don't have to use the keyword `this`
// But I use it because of clarity
return this.message;
}
public void setMessage(String newMessage) {
this.message = newMessage;
}
public String toString() {
return String.format("Shouter message = %s", this.message);
}
public void printMe() {
System.out.println(this.message);
}
public void shout() {
System.out.println(this.message.toUpperCase());
}
public void printAndShout() {
// NOTE: You don't have to use the keyword `this`
// But I use it because of clarity
this.printMe();
this.shout();
}
public static void main(String[] args) {
Shouter s = new Shouter("This is my message");
s.printMe();
s.shout();
// When printing objects, Java will try and stringify
// In this case, it calls the .toString() method
System.out.println(s);
}
}