Skip to content

Week 1L - Lecture

How do you learn?

Learning Styles

Weekw 1: Getting Started with Programming

  • Programs are written as High-Level Language Source code
    • Procedural source code consists of:
      • Statements: System.out.println("Hello world");
      • Expressions: int a = 2;
      • Executed from Top-to-Bottom, Left-to-Right
    • HLL were created to make coding faster and more human-readable.
    • Hundres of languages, but Java is very popular and makes learning other languages easier.
    • Languages like Java have many APIs, Libraries, and GUI tools to make development easier.
  • Modern programming is always done in an IDE
    • Useful tools for programmer
      • Real-time Syntax checking
      • Auto-Documentation tools
      • Code Completion
      • Console
      • Debugger
      • AI Generative code assistance

Exercise 1 - Get familiar with IDE

  • Open your IDE, start a NEW Project
  • Add or find the Main.java file created for the project, what do you think the following do?
  • Note the line public class Main
  • Note the main method inside that Main.java file.
  • Note any // lines.
  • Note the ; terminating each code line.
  • Now locate the following.
    • Build/Run Buttons
    • Project/Settings
    • SDK/System settings
    • Console
  • Keep a lookout for Syntax errors/warnings in IDE

Exercise 2 - What does the following program print

Exercise2.java
1
2
3
4
5
6
public class Exercise2 {
    public static void main(String[] args) {
        // The statements used by the program start below!
        System.out.println("Welcome to CIS-12 - you will learn to program!");
    }
}

Simple Printing

  • Stdin, Stdout, Stderr
  • Java’s System.out.println(""); will output stdout to console!

Anatomy of a Java Program

Week1.java
1
2
3
4
5
6
public class Week1 {
    public static void main(String[] args) {
        // The statements used by the program start below!
        System.out.println("I have something to say!");
    }
}
  • Every java file must be named the same as the class with a .java extension.
  • Execution always starts in main method.
  • Code blocks are contained within { and } brackets.
  • Single line comments are denoted with //
    • This is one way to document your code and leave yourself notes.

Exercise 3 - My First Print

YourNameHere.java
1
2
3
4
5
public class YourNameHere {
    public static void main(String[] args) {
        // Write a program that prints your first and last name here.
    }
}
  • Run the program via Build/Run
  • Run the program command line.
  • sout

Multiple Line Printing

  • You could simply put multiple print statements.
MultipleLinePrint.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public class MultipleLinePrint {
    public static void main(String[] args) {
        System.out.println("Hello world!");
        System.out.println("Hello universe!");
        System.out.println("Hello world!\nHello universe!");
        System.out.println("""
            Hello world!
            Hello universe!
        """);
    }
}

A Glance Into Your Future

What is System.out.println() Really?

  • System is a static class.
  • out is a class/static object (e.g. only one instance for the whole system Stdout!).
  • println is a method in the out object that can be called via “.” because out is an object.
  • “Text to be printed…” is a parameter of data-type String
    • A parameter is data passed to a method so the method can use it and is always in the ( ) parenthesis.
  • ; Separates the statements and expressions in your source code.

Comments

  • So far we’ve seen single line comments //
  • There is also a multi-line comment:
    Multi-Line Comment
    1
    2
    3
    4
    5
    6
    /*
    Comment
    text
    goes
    here.
    */
    

Reading Input

What is input?

  • Remember Stdin?
  • It is text typed by a user that is always read as a String
  • There are many ways to read input, but we’ll mostly use the Scanner class.

Excercise 4 - Guess What?

ScannerIntro.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
import java.util.Scanner;

public class ScannerIntro {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Write Something: ");
        String message = scanner.nextLine();
        System.out.println(message);
    }
}
What it does, in human… Statement exec line-by-line
  • Introduce the scanner tool used for reading user input
  • Create a tool for reading user input and name it scanner
  • Print “Write Something: “
  • Read the string written by the user, and assign it
  • to program memory “String message = (string that was given as input)”
  • Print the message written by the user
What it does, in programmer…
  • Imports the Scanner class from the Java SDK libraries
  • Creates a Java Object of type Scanner so we can access its method for reading input.
  • Prints to StdOut a message requesting input
  • Uses “dot” notation to call the scanner object’s nextLine method to read from StdIn until a newline (i.e. \n) is encountered.
  • Assigns the returned typed line captured in method nextLine and stores it in variable message, which is of data-type String
  • Prints to StdOut the typed message out of the variable message.

Exercise 5 - User Input

Exercise5.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.util.Scanner;

public class Exercise5 {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Write a message: ");
        // Write a program that asks the user to write a string.
        // When the user has typed a string and pressed the enter key,
        // the program should print the string that was provided by the user.
    }
}

What is a String Anyway?

  • Why do we say String where most people would say Text?
  • String is just shorthand for saying charachters strung together like beads on a necklace.
    • And that’s how a computer views text
  • We’ve used strings in printing to StdOut and reading from StdIn to store the string in a variable
  • So what’s a variable (Don’t worry, we’ll revisit this)!
    • A storage location for some type of data (So far we’ve only seen String)
    • They are declared like so String message = "Hello";
      • String is the type
      • message is the variable name
      • = is an assignment operator just like MATH!
        • It assigns the String “Hello” to the storage/memory location pointed to by message**.
    • Variables are used to reference the data at their storage location throughout a program.
    • Varaibles are used for Re-Use and repetition.
String Operations
  • Can we join strings together?
    • YES, through concatenation…
      ConcatStr.java
      1
      2
      3
      4
      5
      6
      public class ConcatStr {
      
          public static void main(String[] args) {
              System.out.println("Hello " + "world!");
          }
      }
      
  • You can also use variables, and you can do it any number of times!
    ConcatStr.java
    1
    2
    3
    4
    5
    6
    7
    8
    9
    public class ConcatStr {
    
        public static void main(String[] args) {
            String message1 = "Hello world!";
            String message2 = "and the universe!";
    
            System.out.println(message1 + " ... and the galaxy ... " + message2);
        }
    }
    
Exercise 6 - String Concatenation
Bond.java
1
2
3
4
5
6
7
8
9
public class Bond {

    public static void main(String[] args) {
        String start = "My name is ";
        String end = ", James Bond";

        // Modify the program so that it prints the contents of the variable start and end, and the printed text is the following: My name is Bond, James Bond
    }
}
Reading Strings
ReadingStrings.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//Introduce the Scanner tool used for reading
import java.util.Scanner;

public class ReadingStrings {

    public static void main(String[] args) {

        //Create the tool for reading, assign it to variable caller "scanner
        Scanner scanner = new Scanner(System.in);

        //Print user a message "Write a message: "
        System.out.println("Write a message: ");

        // nextLine method reads the user's input and returns a string
        // If we then want to use the string in the program, it must be saved to a string variable
        String message = scanner.nextLine();

        // A value saved to a variable can be used repeatedly
        System.out.println(message);
        System.out.println(message);
    }
}
Exercise 7
TimesThree.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import java.util.Scanner;

public class TimesThree {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("Write a message: ");
        // Write a program that asks the user to type a string.
        // When the user has typed a string and pressed enter,
        // the program must print the user's string three times.
    }
}
Exercise 8
LiteralConcatenation.java
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
import java.util.Scanner;

public class LiteralConcatenation {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Write a program that prompts the user for their name with the message "What's your name?".
        // When the user has written their name, the program has to print "Hi "
        // followed by the user's name.
    }
}
Waiting for Input
  • What does the nextLine method really due?
    • Puts process thread into a wait state that will get notified asynchronously, via an event, when the enter key on the keyboard is pressed.
    • So what does this mean? Take the following program for example:
      Program.java
       1
       2
       3
       4
       5
       6
       7
       8
       9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      import java.util.Scanner;
      
      public class Program {
      
          public static void main(String[] args) {
              Scanner scanner = new Scanner(System.in);
      
              System.out.println("Write the first string:");
              String first = scanner.nextLine();
              System.out.println("Write the second string:");
              String second = scanner.nextLine();
              System.out.println("Write the third string:");
              String third = scanner.nextLine();
      
              System.out.println("You wrote:");
              System.out.println(first);
              System.out.println(second);
              System.out.println(third);
          }
      }
      
    • Thie program will pause and go idle at each call of nextLine, its context will be switched out (Think 0% CPU usage).
    • Upon a Keyboard Enter key being pressed, an event will be generated that will context switch it back.
Excercise 9
  • Write a program that asks the user for a name and a job to do. Your program then prints a short story as follows:
    1
    2
    3
    4
    5
    6
    7
    I will tell you a story, but first, I need some info!
    What is your name? Trevor
    What is their job? developer
    Here is the story:
    Once upon a time there was Trevor, who was a developer.
    On the way to work, Trevor reflected on AI.
    Perhaps Trevor will not be a developer forever.