CSIT115 Class 10

Return Class exercise: Automobile.java

Project p2 is posted. Will discuss next week.

 

Handout: APIs for Example Objects: nice compressed representation of what objects can do.

 

Review of our Objects so far: see new links on class web page for “final” versions of Point, PhoneEntry, and Automobile

 

Point:   final version pg. 531, without Point() constructor (online Point5_fancy version at Chap 8 sources)

“Encapsulated” because it has private fields

State: a Point holds x and y coordinates of a point

Behavior:  getters getX(), getY() for x and y values

                    Plus another accessor: distanceFromOrigin, toString()

                   Plus mutators translate(dx, dy), setLocation(x,y)

 

PhoneEntry:…

Automobile:…

 

Now let’s consider groups of Automobiles for the inventory of the dealer.

 

Recall create an array of 2 Points called “pair”, from hw2:

Point[] pair = new Point[2];

 

Create an array of 3 Automobiles—

Automobile[] cars = new Automobile[3];

 

Now have cars[0], cars[1], and cars[2] to work with, Automobile ref’s, no objects pointed to yet.

cars[0] = new Automobile(“Ford”, “Taurus”, 2012, “1G…”); 

 

<picture of array with one object being ref’d>

 

Look at a full program: AutomobileMain.java

 

Look at array calculations there.

 

// count Fords

 

// find oldest car's year

 

 // find oldest car

 

For long-term storage, we use files

 

Using files of data

Typical format: one line for each object

 

Example file: inventory.dat

1G1JF27W8GJ178227, Chevrolet, Cruze, 2011,

1G1KK13WBB2657894, Ford, Taurus, 2010,

Scanner in = new Scanner(new File(“inventory.dat”)); 

 

Reading this file: line-by-line, then take apart line: see pg. 401.

 

Pg. 401: uses data shown on   pg 397

File hours2.dat:

101 Erica 7.5 8.5 10.25 8 8.5

783 Erin 10.5 11.5 12 11 10.75

format

Id name hours-for-each-day

 

Program HoursWorked2: read this file and report total hours for each employee.

Strategy: read a line with a Scanner, process it (a String now) with another Scanner.

 

Now this code has no way to return the full data on a line to its caller because this is in Chap 6, before objects.

Now with objects, we can collect all the data from the line and put it in an object.

 

Let’s do that with Automobile.

 

The plan:

   Use one Scanner to read lines, like HoursWorked2

     For each line, call processLine(String textLine), get back Automobile object based on that line

 

More on this next time.