49
Programming in the Real World

Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Programming in the Real World

Page 2: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Ceçi n'est pas une Java

import acm.program.*;

public class MyProgram extends ConsoleProgram { public void run() { println("Hello, world!"); }}

Page 3: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

The ACM Libraries

● Throughout this class we've been using the ACM libraries.● acm.program.*

– ConsoleProgram, GraphicsProgram, etc.● acm.graphics.*

– GOval, GRect, etc.

● acm.util.*– RandomGenerator

– ErrorException

Page 4: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

The ACM Libraries

● The ACM libraries exist to simplify many common Java techniques.

● However, the ACM libraries aren't widely used outside of CS106A.

● Good news: The topics from the latter half of the quarter (file reading, arrays, ArrayList, HashMap, interactors, etc.) use only standard Java.

● We do need to cover a few last-minute details of the Java language.

Page 5: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

“Hello, World” Without the ACM

Page 6: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Starting up the Program

● In standard Java, program execution begins inside a method called

public static void main(String[] args)

● The ACM libraries contain this method in the Program class.

● When you're not using the ACM libraries, you will have to implement this method yourself.

Page 7: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Starting up the Program

In standard Java, program execution begins inside a method called

public static void main(String[] args)

The ACM libraries contain this method in the Program class.

When you're not using the ACM libraries, you will have to implement this method yourself.

Page 8: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

What About Windows?

Page 9: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Steps to Create a Window

● Create a new JFrame, which actually represents the window object.

● Add any components or interactors to the frame as you normally would.

● Set the size of the window by calling

frame.setSize(width, height)● Tell Java to quit when we close the program by calling

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)● Show the window by calling

frame.setVisible(true)

Page 10: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

What about Graphics?

● You can create components that can display graphics by extending JComponent and writing

public void paintComponent(Graphics g)

● You can then call methods to draw on the window when the window is resized or moved.

● Note: the default graphics system is not object-oriented.

Page 11: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

static Methods

● A static method is a method that's specific to a class, rather than instances of that class.

● Examples:● Character.isLetter● RandomGenerator.getInstance

● Because the method is specific to the class rather than any instance, there is no receiver object.

Page 12: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

public static void main

● Because main is static, there is no instance of your class that it operates relative to.

● Common technique: Have main create an instance of the class and work from there.

● This is done automatically by the ACM libraries.

Page 13: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

How are you supposed toremember all these methods?

Page 14: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

http://docs.oracle.com/javase/7/docs/api/

Page 15: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Time-Out for Announcements!

Page 16: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Assignment 7

● Assignment 7 (NameSurfer) due today at 3:15PM.● Due Wednesday at 3:15PM with one late

period.● Due Friday at 3:15PM with two late periods.● Hard deadline: next Monday at 3:15PM.

Page 17: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

A Note on Happiness and Staying Sane

Page 18: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Assignment 8

● Assignment 8 (FacePamphlet) goes out today and is due next Tuesday, March 17 at 8:30AM.● Put everything together and build a social network!● Only one new concept tested (iterators), and we'll talk

about them in a second.● Our hope: This gives you a way to get extra practice

with the material and increase your average assignment grade.

● Note the unusual due date.● This is a hard deadline – no late submissions will

be accepted.● You cannot use late periods on this assignment.

Page 19: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Iterators

● To visit every element of a collection, you can use the “for each” loop:

for (ElemType elem: collection) {

}

● Alternatively, you can use an iterator, an object whose job is to walk over the elements of a collection.

● The iterator has two commands:● hasNext(), which returns whether there are any more elements

to visit, and● next(), which returns the next element and moves the iterator

to the next position.

Page 20: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

Page 21: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

Page 22: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

Page 23: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

Page 24: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 25: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 26: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

hasNext()?

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 27: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 28: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

next()!

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 29: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

next()!

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 30: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 31: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 32: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

hasNext()?

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 33: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 34: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

next()!

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 35: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

next()!

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 36: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 37: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 38: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

hasNext()?

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 39: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 40: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

next()!

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 41: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

next()!

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 42: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iteriter

Page 43: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iteriter

Page 44: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

hasNext()?

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 45: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

Done!

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 46: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

Java Iterators

137 42 2718

ArrayList<Integer> myList = /* … */

Iterator<Integer> iter = myList.iterator();while (iter.hasNext()) { int curr = iter.next();

/* … use curr … */}

iter

Page 47: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

A Word of Warning

Page 48: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

A Word of Warning

● The following will loop forever on a nonempty collection:

while (collection.iterator().hasNext()) {

/* … */

}

● Every time that you call .iterator(), you get back a new iterator to the start of the collection.

Ibex Kitty

Page 49: Programming in the Real World - Stanford Universitystanford.edu/class/archive/cs/cs106a/cs106a.1154/... · The ACM libraries exist to simplify many common Java techniques. However,

A Word of Warning

● The following will loop forever on a nonempty collection:

while (collection.iterator().hasNext()) {

/* … */

}

● Every time that you call .iterator(), you get back a new iterator to the start of the collection.

Ibex Kitty