COMP2511

23T1 Week 1

WEDNESDAY 1PM - 4PM (W13B)

FRIDAY 11AM - 2PM (F11A)


Slides by Alvin Cherk (z5311001)

Today

  • General Tutorial/Lab Introduction
  • Java Syntax
  • Object-Oriented Paradigm

Introduction

  • My name is Alvin
  • 4rd Year Computer Science / Mechatronics student
  • I like mechanical keyboards, Genshin Impact, Gundams, Valorant.
  • Full Stack Developer
     
  • Email: a.cherk@unsw.edu.au (Please try using the forums before emailing me, unless its for a personal reason)
    • Include your zID somewhere so I can easily identify you
    • I will usually take 24-48 hours at most to reply. If you don't get a reply, then send a follow up email.
    • Prefix your email's title with 2511-[CLASSNAME] (e.g., 2511-F10A)
  • Course email: cs2511@cse.unsw.edu.au
    • Don't like how something is handled? You're welcome to escalate it to course admins. (Do talk to me first if possible).

How will it work?

My Suggestions

  • Get to know the people in your tutorial. Make friends. You will be working in a pair for assignment 2/3 (optional for assignment 3).
     
  • Make sure you are keeping up with lecture content
     
  • Read your course outline. The course has changed!
     
  • Plan out your term, COMP2511 can take up a lot of time!
     
  • Please start assignments/projects early! (Looking at assignment 1)
     
  • Ask lots of questions
     
  • Prepare to learn how to read documentation & google on your own

Ice Breaker

  • Name (and preferred name)
  • Degree
  • Favourite language & why
    Or
    Random interesting fact

Git

Git Revision

  • git add
    • Stage files
  • git commit
    • Commit the staged files as a snapshot
  • git push
    • Push your new commits to an online origin (GitLab, BitBucket, GitHub)
  • git status
    • State of current repository & branch
  • git log
    • History of current branch

Differences between Java, C, Python, JS/TS

  • Syntax
    • C, Java, JS/TS use { and } to describe code blocks (also scopes)
    • Python uses whitespaces (tabs/indentations)
       
  • Classes:
    • Java, Python, JS/TS support Object Oriented programming (OOP)
      • Supports classes and inheritance
    • C does not support classes. Closest things are pure 'data classes' called structs
    • All code within Java needs to exist within a class
    • JS/TS and Python can have runnable code outside classes

Differences between Java, C, Python

  • Types:
    • Java, C and TypeScript are statically typed
    • Python and JavaScript are dynamically typed
       
  • Memory:
    • C allows you to manually allocate memory
    • Java, Python, JS/TS have automatic memory management
       
  • Compilation
    • C compiles into machine code
    • Java and Python compiles into byte code, which is interpreted
    • TypeScript transpiles to JavaScript which is then interpreted

VSCode Setup

If you are using VSCode on CSE machines (VLAB or in-person), please ensure you are using 2511 code

cvx02 % code --version
1.67.2
c3511e6c69bb39013c4a4b7b9566ec1ca73fc4d5
x64
vx02 % 2511 code --version
1.74.3
97dec172d3256f8ca4bfb2143f3f76b503ca0534
x64

If you don't some Java Extensions (notably the test runners) will not work properly.

See the Setup Troubleshooting Confluence page if you have encounter any issues 

Code Demo

HelloWorld.java

Code Demo

Make a simple Java program that prints "Hello World" in the HelloWorld class.

Code Demo

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");
    }
}

Code Demo

Sum.java

Code Demo

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();
    }
}

Code Demo

Loop.java

Code Demo

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);
        }
    }
}
  • Use the for-range loop unless you need access to index control
  • You can use a index based loop if you need to change the value while looping over a collection of items
  • Style marks will be lost in assignments if index loops are used when for-range loops could have been used

Code Demo

Shouter.java

Code Demo

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);
    }
}

Feedback