Previous Lecture Lecture 9 Next Lecture

Lecture 9, Mon 04/29

Midterm Review, File IO

Midterm Review

Logistics
- Bring studentID and writing utensil
- No electronic devices
- No notes, no books

Format
- Mix of questions on lectures, homeworks, readings,
and labs
- Short Answers
    - Briefly define, describe, state, ...
- Write code
- Fill in the blank, complete the table, select answers, ....
- Given code, write the output
- Given a statement, tell me if it's true or false
    - If false, state why
- ...
- Expect it to take ~1 hour, but you have the entire class period (5 - 6:15pm)
- Will cover a broad range of topics since the start of class, but probably not everything.

Topics
- Will cover everything up to Wednesday's lecture (4/24)

Java Basics
- Variable names
- Primitive Types
    - Sizes
    - Typecasting
- Objects
    - Creating new Objects
    - Object variables (references)
    - .equals (==) operator
    - Object class (Base class for all objects)
- Overflow
- Operator conversions for certain Types
    - Example: (2/3) vs. (2.0 / 3)

Strings
- Multiple ways to create new Strings
- Escape characters
    - not all, but at least the ones mentioned in lecture
- Methods
    - .length, .substring, .toUpperCase, .toLowerCase
    .charAt, .equals (how does this differ from Object.equals), .equalsIgnoreCase, .compareTo
- Concatenation
- Conversion from String to Int and vice-versa
    - Can also convert to doubles, floats, etc., but will only expect knowledge for String <-> ints
- String pool

Control Flow
- Similar to C++
- if statements
- Relational operators (==, !=, <, ...)
- Logical operators (!, &&, ||, ...)
- While, do-while, for loops
    - Break and continue

Classes
- public vs. private
- Defining our own Classes
- "this" keyword
- Accessor (getter) vs. Mutator (setter) Methods
- static variables
- static Methods
- defining Methods
- overloading methods and constructors
- Pass by value vs. Pass by reference
    - What does java do?
- Copy constructors
    - Shallow vs. Deep Copy

Program Input
- Command line arguments
- Console Input
    - Scanner object using System.in

Random Number Generation
- java.util.Random
- .nextInt(), nextInt(int), .nextDouble()

Arrays
- Objects in Java
- Array methods (.clone) and fields (.length)

ArrayLists
- Constructing ArrayLists
- .add, .set, .get, .remove
- Exceptions thrown in certain Classes
- ArrayList of ArrayLists structure

Memory Concepts
- Stack Overflow
- Recursion
- Garbage Collection
- Object references and the heap

Exception Handling
- Base Exception Object
- Try / Catch
- Exception flow through Stack
- Handling multiple exception Types
- Finally block
- Multiple Exceptions in a single catch block
- Creating exceptions (extending from Base class)

Testing
- Error cases: Normal vs. Error vs. Boundary
- Test Suite
- JUnit
    - .assertEquals, .assertFalse, .assertTrue, ...
    - JUnit assertions
        - @Test, @Before, @BeforeClass, @After, @AfterClass,
    - Testing if Exceptions were thrown correctly
        - @Test(expected=Exception.class)

Inheritance
- Extending from Base class
- Accessibility of variables and methods
- Constructor chaining
    - super (also used to call base class methods)
- Instanceof operator
- .getClass methods

Polymorphism (Dynamic Binding)
- Java Behavior
- Type assignment (sub class assigned to base class)
- Overwriting methods in subclasses
- Final methods and Classes
- Memory concepts using inheritance
- Casting object rules
    - What this looks like on the heap and what is legal

Abstract Methods
- Defining and any implications

Abstract Classes
- Defining and implications
- Can we instantiate an object of an Abstract Class?

Interfaces
- Implementing Interfaces
- Interface types and Polymorphism

Switch statements
- cases, break, default
- Using multiple values in a single case

File IO

System.out.println(System.getProperty("file.encoding"));

Scanner Object

// If the file does not exist, a FileNotFoundException is thrown
Scanner inFile = new Scanner(new File(“file.txt”));
This is line 1
This is line 2
This is line 3

Example

// get all lines in the file and print them one-by-one
String line;
while (inFile.hasNextLine()) {
    line = inFile.nextLine();
    System.out.println(line);
}

// get all words in the file and print them one-by-one
// assuming words are separated by the white space character(s)
while (inFile.hasNext()) {
    word = inFile.next();
    System.out.println(word);
}

Tokens

A note about absolute file paths

Reading a file using a URL Example

try {
Scanner inFile = new Scanner(new File("file.txt"));

    String line;
    Scanner remoteIn = null;
    try {
        URL remoteFileLocation =
        new URL("https://sites.cs.ucsb.edu/~richert/file.txt");

        URLConnection connection = remoteFileLocation.openConnection();
        remoteIn = new Scanner(connection.getInputStream());

        while (remoteIn.hasNextLine()) {
            line = remoteIn.nextLine();
            System.out.println(line);
        }
    } catch (IOException e) {
        System.out.println(e.toString());
    } finally {
        if (remoteIn != null) {
            remoteIn.close();
        }
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Delimiters

Example using “l” as a delimiter

try {
    Scanner inFile = new Scanner(new File("file.txt"));
    inFile.useDelimiter("l");

    String line;
    while(inFile.hasNext()) {
        line = inFile.next();
        System.out.println(line);
    }
} catch (IOException e) {
    e.printStackTrace();
}
///
This is 
ine 1
This is 
ine 2
This is 
ine 3

Example

// file2.txt
Richert,Wang,CS56,Goleta,93117
String s;
Scanner inFile = null;
try {
    inFile = new Scanner(new File("file2.txt"));
} catch (FileNotFoundException e) {
    System.out.println(e);
}
String [] stringArray;
// Get the entire line one-at-a-time
while (inFile.hasNextLine()) {
    s = inFile.nextLine();
    stringArray = s.split(",");
    for (int i = 0; i < stringArray.length; i++) {
        System.out.println(stringArray[i]);
    }
}

Writing data to a File

PrintWriter out = null;
try {
    out = new PrintWriter("output.txt");
    out.println("First Line!");
} catch (FileNotFoundException e) {
    // Non existent path, write access denied, …
    System.out.println(e.toString());
} finally {
    if (out != null) {
        out.close();
    }
}

Formatting Data using printf

String course = "CS 56";
System.out.println("I <3 " + course);

vs.

String course = "CS 56";
System.out.printf("I <3 %s", course);

Example

String month = "May";
int day = 6;
int year = 2019;
boolean b = true;
System.out.format("It is %b that today is the %dth of %s in the year %d.", b, day, month, year);

char grade = 'A';
double percentage = 99.8;
System.out.format("In order to get a %c, I need to score a %f%% on the final", grade, percentage);
// nobody falls in this category :)
Output: In order to get an A, I need to score 99.800000% on the final
System.out.format("In order to get a %c, I need to score %.2f%% on the final", grade, percentage);

Left/Right Justify

String first = "Richert";
String last = "Wang";
System.out.format("Hello, my name is %-10s %5s", first, last);
//Output: Hello, my name is Richert     Wang

System.out.format("Hello, my name is %-1s %5s", first, last);
//Output: Hello, my name is Richert  Wang

Writing to a File

PrintWriter out = null;

try {
    out = new PrintWriter("output2.txt");
    String first = "Richert";
    String last = "Wang";
    out.format("Hello, my name is %-10s %5s\n", first, last);
    out.format("Another line");
} catch (Exception e) {
    System.out.println(e.toString());
} finally {
    if (out != null) {
        out.close();
    }
}

Append to a File

FileWriter out = null;
try {
    out = new FileWriter("output", true);
    out.write("First Line!!!");
} catch (FileNotFoundException e) {
// Non existent path, write access denied, …
S   ystem.out.println(e.toString());
} catch (IOException e) {
    e.printStackTrace();
} finally {
    if (out != null) {
        try {
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Example Using Filewriter with printWriter to format

FileWriter out = null;
try {
    out = new FileWriter("output", true);
    String first = "Richert";
    String last = "Wang";
    PrintWriter writer = new PrintWriter(out);
    writer.format("Hello, my name is %-10s %5s", first, last);
} catch (FileNotFoundException e) {
    System.out.println(e.toString());
}