CS 1110/1111: Introduction to Programming

Lecture 7

Announcements

Homework 1

Quiz 3 question 2 broken by collab, will be ignored in grade

Talking Points

System.out.println, Strings, and a basic program

public class Simple {
    public static void main(String[] args) {
        System.out.println("Emotional injection");
        System.out.println("Through rising inflection");
    }
}

Where can you add/remove spaces and where can't you? What else can you change?

Style: indentation, one statement per line, } on a line by itself.

Style: we'll put { on the same line as whatever comes before it (unlike book). This will reduce how often you put a ; where it doesn't belong (NEVER put a ; before a {).

Primitives: just values, ideas, not something you can talk to or ask to do things
Book lists many, we'll mostly use just four: int, double, boolean, char

Operators: mostly do what you expect, and only to primitives…
Numeric: addition +, subtraction -, multiplication *, division /, and modulo % (a % b is the remainder of a divided by b)
We'll see comparison and Boolean operators later on
We'll ignore binary operators completely this semester

public class PrimitivePlay {
    public static void main(String[] args) {

        System.out.println(3);

        int x = 4;
        System.out.println(x);
        System.out.println(x * 2 - 3);
        System.out.println(x * (2 - 3));
        x = x + 68; // increases x by 68
        System.out.println(x);

        double phi = 1.618033988749894848;
        System.out.println(phi);
        System.out.println(phi % 1.5);
        System.out.println(x % 7);

        char c = 'x';
        System.out.println(c);
        char c = 68;
        System.out.println(c);
    }
}

Objects: entities you have to create and can talk about and to.
We've seen Turtle and World already
String is special, it uses quotes (") instead of new
Book discusses Scanner, we'll use it a lot
System.out and System.in are objects that System makes for us (we don't use new on them ourselves)
We'll also see DecimalFormat
Many more to come!

Concatenation: In a strange exception to the rules, the only operator that works on non-primitives is +, and then only if one of its operands is a String. In that case, Java turns both operands into Strings and sticks them together to make a new, longer String.

public class PlusIsStrange {
    public static void main(String[] args) {
        System.out.println(3 + 4 + "5" + 6 + 7 + 0.12345678901234567890);
    }
}

Variables: typename variablename; creates a box named variablename
Name only exists inside whatever curly braces it was defined inside of (exceptions to this rule will come up later)
If the type is a primitive type, the value goes inside the box directly
If the type is an object type (or class), an address to where the object is located goes inside the box instead
x = y means put in box x a copy of whatever was in box y (e.g., the same value (primitive) or the same address (object))

Scanners can read from a single input source. You have to pick one when you make them. We'll usually use System.in, which is what the keyboard types in the Console tab in Eclipse.
They can do two kinds of things: they can see if there's another thing to read (e.g., hasNextInt or hasNextLine) or they can actually get that next thing (e.g., nextInt or nextLine).
Scanners are not part of the Java default environment; to get at them you need to import java.util.*; (Eclipse will usually do this for you).

public class GuessingGame {
    public static void main(String[] args) {
        Scanner keyboardInputFromTheConsole = new Scanner(System.in);
        
        // We usually give a "prompt" before getting input so the user knows what to type
        // Prompts should be print (not println) with a trailing space
        System.out.print("Guess what number I'm thinking of: "); 
        
        int number = keyboardInputFromTheConsole.nextInt(); // will wait for input to appear
        
        System.out.println("Wrong! I was thinking of " + (number + 1));
    }
}

APIs

An API is the list of methods that are provided with a programming language or programming tool.

For example, if s, t, and u are Strings then you can do the following:

CodeAction
s.length()returns the number of characters in s
s.replace(t, u)returns a new String that is like s except that every occurrence of t has been replaced with u instead
s.charAt(0)returns the first character in s
s.charAt(3)returns the fourth character in s
Integer.parseInt(s)returns the number that s represents (e.g., if s were "134" it would return 134

There are lots more; see the API in the course wiki on Collab for examples.

Copyright © 2014 by Luther Tychonievich. All rights reserved.