Class Notes (837,971)
Canada (510,606)
COMP 1405 (38)
Lecture

comp1005/1405_c13_22102010.pdf

7 Pages
76 Views
Unlock Document

Department
Computer Science
Course
COMP 1405
Professor
Christine Laurendeau
Semester
Fall

Description
Lecture 13 Class File //An class object file for the upcoming Program class Ch6_3Team { String name; // The name of the Team int wins; // The number of games that the Team won int losses; // The number of games that the Team lost int ties; // The number of games that the Team tied Ch6_3Team (String aName) { //A constructor that takes a String data and stores it in the //name String in this class. At the same time everything else is //set to 0. this.name = aName; this.wins = 0; this.losses = 0; this.ties = 0; } public String toString() { //A toString() function that will return a set String instead of //the memory location when the class is printed return("The " + this.name + " have " + this.wins + " wins, " + this.losses + " losses and " + this.ties + " ties."); //returns the score history of the team } // Returns the total number of points for the team int totalPoints() { //An int function named totalPoints that will return an int data return (this.wins * 2 + this.ties); //returns the result of the team's (wins * 2 + ties) } // Returns the total number of games played by the team int gamesPlayed() { //An int function named gamesPlayed that will return an int data return (this.wins + this.losses + this.ties); //returns the result of the team's (wins + loses + ties) } } Lecture 13 Program 1 //The program below shows the results of the Ch6_3Team class class Ch6_3TeamTestProgram { public static void main (String args[]) { Ch6_3Team teamA, teamB; //Defines object names teamA = new Ch6_3Team("Ottawa Senators"); //Creates an object sets constructor using String "Ottawa Senators". teamB = new Ch6_3Team("Montreal Canadians"); //Creates an object sets constructor using String "Montreal Canadians". // Simulate the playing of a game in which teamA beat teamB System.out.println(teamA.name + " just beat " + teamB.name); //prints result teamA.wins++; teamB.losses++; //The int variable wins in the teamA object adds 1 //The int variable loses in the teamB object adds 1 // Simulate the playing of another game in which they tied System.out.println(teamA.name + " just tied " + teamB.name); //prints result teamA.ties++; teamB.ties++; //The int variable ties in the teamA object adds 1 //The int variable ties in the teamB object adds 1 //Now print out some statistics System.out.println(teamA); System.out.println(teamB); //Prints out the toString() of both objects System.out.print("The " + teamA.name + " have "); System.out.print(teamA.totalPoints() + " points and played "); System.out.println(teamA.gamesPlayed() + " games."); System.out.print("The " + teamB.name + " have "); System.out.print(teamB.totalPoints() + " points and played "); System.out.println(teamB.gamesPlayed() + " games."); //prints out the scores } } Lecture 13 Class File 2 //The following class object creates a arraylist object from Ch6_3Team //Will be needed for the Ch6_3TeamArrayList Program import java.util.ArrayList; //import arraylist from java library class Ch6_3League { //creates a new league class String name; ArrayList teams; //creates an arraylist object named teams from the Ch6_3Team class Ch6_3League(String n) { this.name = n; //Set the name using constructor this.teams = new ArrayList(); // Doesn’t make any Team objects } } // This specifies the appearance of the League public String toString() { //Creates the toString method to avoid printing class memory address. return ("The " + this.name + " league"); } void showTeams() { //for loop, looping through the teams variable in the Ch6_3Team object for (Ch6_3Team t: this.teams) System.out.println(t); // or System.out.println(t.toString()); } } Lecture 13 Program 2 //The program below implements the TeamTestProgram with ArrayList //Requires the Ch6_3League class to run class Ch6_3TeamArrayList { public static void main(String args[]) { Ch6_3League nhl; //Define a new Ch6_3League object and name it nhl nhl = new Ch6_3League("NHL"); //Add a pile of teams to the league nhl.teams.add(new Ch6_3Team("Ottawa Senators")); nhl.teams.add(new Ch6_3Team("Montreal Canadians")); nhl.teams.add(new Ch6_3Team("Toronto Maple Leafs")); nhl.teams.add(new Ch6_3Team("Vancouver Cannucks")); nhl.teams.add(new Ch6_3Team("Edmonton Oilers")); nhl.teams.add(new Ch6_3Team("Washington Capitals")); nhl.teams.add(new Ch6_3Team("New Jersey Devils")); nhl.teams.add(new Ch6_
More Less

Related notes for COMP 1405

Log In


OR

Join OneClass

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

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit