1 / 30

CSC 204 Drill and Practice

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)

Download Presentation

CSC 204 Drill and Practice

An Image/Link below is provided (as is) to download presentation 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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 204 Drill and Practice Creating and Using Classes

  2. Constructors • Given a class named Animal, what will its constructor be named?

  3. 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.

  4. 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)

  5. 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.

  6. 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

  7. 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

  8. Constructors • Show how to call the constructor you just wrote to assign data to the variable thatCreature.

  9. 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

  10. 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.

  11. Accessors • Create accessors to return all three pieces of instance data of an Animal.

  12. Accessors • Show how to call the accessors you just created to print the information about myCat, one piece of data per line.

  13. Accessors • Create a compareTo method that will compare two animals based on their weights (so the heavier one is considered "larger").

  14. Accessors • Create a compareAge method that will compare two animals based on their ages (so the older one is considered "larger").

  15. Accessors • Create a compareBreed method that will compare two animals based on their breeds (so the breed that appears alphabetically later is considered "larger").

  16. Accessors • Using the compare methods you just wrote, compare myCat and thatCreature and tell which is heavier.

  17. Accessors • Using the compare methods you just wrote, compare myCat and thatCreature and tell which is older.

  18. Accessors • Using the compare methods you just wrote, compare myCat and thatCreature and print their breeds in alphabetical order.

  19. Accessors • Create an equals method that will return true if two animals have the same age, weight, and breed.

  20. Mutators • Create a birthday method that will add one to the age of an Animal.

  21. Mutators • Show how to add 1 to the age of myCat and print its age.

  22. 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.

  23. Class variables • Create accessors to return the class variables you just created.

  24. Class variables • Show how to print the values of the class variables you just created, using the accessors.

  25. Class variables • Show the changes needed to the constructor to update the class variables each time a new animal is created.

  26. Mutators • Create a changeWeight method that will add a given amount to the weight of an Animal. • Remember to update the class variable.

  27. 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.

  28. Class variables • Show the changes needed to the constructor to update allAnimals each time a new animal is created.

  29. 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.

  30. Class variables and array lists • Show how to print out all the animals that have been created.

More Related