CS II Lesson 7

Professor Abdul-Quader

Project 1 Discussion / Intro to Classes

Project Hints

Handle the input first!

  • End the program with an error message if the input is invalid
  • Do not let the program crash / throw an exception on its own.

Hints: while loop

  • Keep track of where you currently are (currentStreet, currentAve)
  • Start at the startStreet, startAve
  • Use a while loop.
  • As long as you aren’t at the endpoint, keep going.
  • Loop condition?
    • currentStreet != endStreet && currentAve != endAve?
    • currentStreet != endStreet || currentAve != endAve?
    • Test it out: (1, 1) -> (1, 3)?
    • Which one is still true?

Hints: Navigation

  • Check if you can go east / west, north / south
    • Hint: \(x\) is even if \(x \% 2 == 0\).
  • Nuance: can’t go east or west on 1 Street / 1 Ave
  • Check if you want to go east / west, north / south
    • How do you know? (Draw this out.)

Simple Ideas

  • Start with simple ideas
  • If you want to go east and can, go east (as far as you need to)
  • Otherwise, if you want to go west and can, go west
  • Otherwise…
  • What if you can’t go anywhere you want to go?
    • Simple idea that works: pick any direction you can go, and just go (1 block)
    • Program loops around and tries again.

Complicated Ideas?

  • Get something that works first
  • That may take you the entire week.
  • If you have time, figure out how to optimize:
    • Test it out as much as possible
    • Find all the edge cases that your code doesn’t handle
    • Try to handle them
    • More if statements?
    • Elegant idea that solves all your problems? (Maybe not.)

Your Ideas?

What are some of the best ideas you all have had so far that seem to be working?

Presentation 1

  • Weeks of 3/2 and 3/9: demo your project.
  • Give a “big picture” explanation of the algorithm for how you solved the problem.
  • Show / explain how your code implements that algorithm.
  • Rubric / spreadsheet sign-up on BrightSpace.

Resize Array

Problem?

int[] numbers = new int[5]; // hope that 5 is big enough!
Scanner sc = new Scanner(System.in);
int index = 0;
System.out.println("Input in the first number: ");
while (sc.hasNextInt()) {
    numbers[index] = sc.nextInt();
    index++;
    System.out.println("Input in the next number: ");
}

Exercise

Write a method which takes in an integer array as a parameter, creates a new array that is twice as big, copies over all the elements of the old array and returns the new array.

public static int[] resizeArray(int[] originalArray) {
    // what goes in here?

}

Starter code.

Main method

Use it with the following main method:

int[] numbers = new int[5];
Scanner sc = new Scanner(System.in);
int index = 0;
System.out.println("Input in the first number: ");
while (sc.hasNextInt()) {
    int number = sc.nextInt();
    if (index >= numbers.length) {
        numbers = resizeArray(numbers);
    }
    numbers[index] = number;
    index++;
    System.out.println("Input in the next number: ");
}

Generalize?

  • Every time we use arrays, we need to do this?
  • Separately, for each type of array? int[] vs String[] vs int[][] etc?
  • Better way? Built-in solution: ArrayList

ArrayList class

import java.util.ArrayList;
// must import the above!

// in main:
ArrayList<String> stringList = new ArrayList<>();
for(int i = 0; i < 100; i++) {
    stringList.add("hello!");
}
System.out.println(stringList.size());

ArrayList<Integer> integerList = new ArrayList<>();
for(int i = 0; i < 20; i++) {
    integerList.add(i * i);
}
System.out.println(integerList.get(15));

ArrayList class

  • Built-in class that handles “resizeable arrays”
  • Can use with any type!

Upcoming

  • Using ArrayLists.
  • ArrayList is a data type created using a class
  • Define our own data types?
  • Read chapters 9 - 11.
  • Project 1 due Sunday.
// reveal.js plugins