CSC 204 Drill and Practice

1 / 30

CSC 204 Drill and Practice - PowerPoint PPT Presentation

CSC 204 Drill and Practice. Creating and Using Classes. Constructors. Given a class named Animal , what will its constructor be named?. Instance variables. The data stored for an Animal is: its weight (a floating point number) its breed (a string) its age (an integer)

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

PowerPoint Slideshow about 'CSC 204 Drill and Practice' - brittany-lancaster

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

CSC 204 Drill and Practice

Creating and Using Classes

Constructors
• Given a class named Animal, what will its constructor be named?
Instance variables
• The data stored for an Animal is:
• its weight (a floating point number)
• its breed (a string)
• its age (an integer)
• Give instance variables for each of these fields.
Constructors
• Create a constructor for Animal that takes values for each of the instance variables.
• The data stored for an Animal is:
• its weight (a floating point number)
• its breed (a string)
• its age (an integer)
Constructors
• Show how to call the constructor you just wrote to assign the data for a 5 year old, 16 pound cat to the variable myCat.
Constructors
• Create a constructor for Animal that uses the default values below for each of the instance variables.
• its weight: 10
• its breed: "Unknown"
• its age : 1
Constructors
• Create a constructor for Animal that uses the default values below for each of the instance variables that calls the 3 parameter constructor you have already created.
• its weight: 10
• its breed: "Unknown"
• its age : 1
Constructors
• Show how to call the constructor you just wrote to assign data to the variable thatCreature.
Accessors
• Create a toString method that returns a string with the data for the animal formatted as shown below:
• Animal: <breed>, <weight> lbs.,<age> years old
• Calling this with myCat should return:
• Animal: cat, 16 lbs.,5 years old
Accessors
• Show how to call toString to print out the information about myCat.
• Give another way to call toString to print out the information about thatCritter.
Accessors
• Create accessors to return all three pieces of instance data of an Animal.
Accessors
• Show how to call the accessors you just created to print the information about myCat, one piece of data per line.
Accessors
• Create a compareTo method that will compare two animals based on their weights (so the heavier one is considered "larger").
Accessors
• Create a compareAge method that will compare two animals based on their ages (so the older one is considered "larger").
Accessors
• Create a compareBreed method that will compare two animals based on their breeds (so the breed that appears alphabetically later is considered "larger").
Accessors
• Using the compare methods you just wrote, compare myCat and thatCreature and tell which is heavier.
Accessors
• Using the compare methods you just wrote, compare myCat and thatCreature and tell which is older.
Accessors
• Using the compare methods you just wrote, compare myCat and thatCreature and print their breeds in alphabetical order.
Accessors
• Create an equals method that will return true if two animals have the same age, weight, and breed.
Mutators
• Create a birthday method that will add one to the age of an Animal.
Mutators
• Show how to add 1 to the age of myCat and print its age.
Class variables
• The class Animal needs to keep track of the animals it has created. In particular, it needs to keep track of:
• the total number of animals that have been created (an integer)
• the total weight of the animals that have been created (a floating point number)
• Give class variables for each of these fields.
Class variables
• Create accessors to return the class variables you just created.
Class variables
• Show how to print the values of the class variables you just created, using the accessors.
Class variables
• Show the changes needed to the constructor to update the class variables each time a new animal is created.
Mutators
• Create a changeWeight method that will add a given amount to the weight of an Animal.
• Remember to update the class variable.
Class variables and array lists
• The class Animal needs to keep track of all the animals it has created.
• Give a class variables that will maintain an array list of all animals.
Class variables
• Show the changes needed to the constructor to update allAnimals each time a new animal is created.
Class variables and array lists
• Create a method to return a string with all animals that have been created, one per line.
• Use toString to get the information for each animal.
Class variables and array lists
• Show how to print out all the animals that have been created.