Class Notes (835,133)
COMP 1405 (38)
Lecture

# comp1005/1405_c14_26102010.pdf

4 Pages
164 Views

School
Department
Computer Science
Course
COMP 1405
Professor
Christine Laurendeau
Semester
Fall

Description
Lecture 14 Reminder: COMP 1005 Test 2, Thursday 28, 2010 Content: Chapter 4 – 6 Class 1: //The following class demonstrates the usage of ArrayList and is required for //the Ch6_4AutoshowTestProgram program import java.util.ArrayList; class Ch6_4Autoshow { String name ; ArrayList cars; Ch6_4Autoshow(String n) { this.name = n; this.cars = new ArrayList(); } public String toString() { return ("Autoshow " + this.name + " with " + this.cars.size() + "cars"); } String mostCommonColor() { int loc, newCount; int bestCountSoFar = 0; String bestColorSoFar = ""; ArrayList colors = new ArrayList(); ArrayList counts = new ArrayList(); for (Ch6_4Car c: this.cars) { if (colors.contains(c.color)) { loc = colors.indexOf(c.color); newCount = counts.get(loc) + 1; counts.set(loc, newCount); } else { colors.add(c.color); newCount = 1; counts.add(newCount); }if (newCount > bestCountSoFar) { bestCountSoFar = newCount; bestColorSoFar = c.color; } } return bestColorSoFar; } ArrayList carsWithMake(String m) { ArrayList answer; answer = new ArrayList(); for (Ch6_4Car c: this.cars) { if (c.make.equals(m)) answer.add(c); } Lecture 14 return answer; } ArrayList differentMakes() { ArrayList answer = new ArrayList(); for (Ch6_4Car c: this.cars) { if (!answer.contains(c.make)) answer.add(c.make); } return answer; } Ch6_4Car fastestCar() { if (this.cars.isEmpty()) return null; Ch6_4Car fastest = this.cars.get(0); for (Ch6_4Car c: this.cars) { if (c.topSpeed > fastest.topSpeed) fastest = c; } return fastest; } void printBySpeed() { Ch6_4Car f = null; ArrayList copy; copy = new ArrayList(this.cars); while (!copy.isEmpty()) { f = copy.get(0); for (Ch6_4Car c: copy) if (c.topSpeed > f.topSpeed) f = c; } System.out.println(f); copy.remove(f); } void printByMake() { Ch6_4Car f = null; ArrayList copy; copy = new ArrayList(this.cars); while (!copy.isEmpty()) { f = copy.get(0); for (Ch6_4Car c: copy) if (c.make.compareTo(f.make) < 0) f = c; } System.out.println(f); copy.remove(f);
More Less

Related notes for COMP 1405
Me

OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Join to view

OR

By registering, I agree to the Terms and Privacy Policies
Just a few more details

So we can recommend you notes for your school.