csc 204 drill and practice n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
CSC 204 Drill and Practice PowerPoint Presentation
Download Presentation
CSC 204 Drill and Practice

Loading in 2 Seconds...

play fullscreen
1 / 30

CSC 204 Drill and Practice - PowerPoint PPT Presentation


  • 131 Views
  • Uploaded on

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)

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

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


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

CSC 204 Drill and Practice

Creating and Using Classes

constructors
Constructors
  • Given a class named Animal, what will its constructor be named?
instance variables
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.
constructors1
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)
constructors2
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.
constructors3
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
constructors4
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
constructors5
Constructors
  • Show how to call the constructor you just wrote to assign data to the variable thatCreature.
accessors
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
accessors1
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.
accessors2
Accessors
  • Create accessors to return all three pieces of instance data of an Animal.
accessors3
Accessors
  • Show how to call the accessors you just created to print the information about myCat, one piece of data per line.
accessors4
Accessors
  • Create a compareTo method that will compare two animals based on their weights (so the heavier one is considered "larger").
accessors5
Accessors
  • Create a compareAge method that will compare two animals based on their ages (so the older one is considered "larger").
accessors6
Accessors
  • Create a compareBreed method that will compare two animals based on their breeds (so the breed that appears alphabetically later is considered "larger").
accessors7
Accessors
  • Using the compare methods you just wrote, compare myCat and thatCreature and tell which is heavier.
accessors8
Accessors
  • Using the compare methods you just wrote, compare myCat and thatCreature and tell which is older.
accessors9
Accessors
  • Using the compare methods you just wrote, compare myCat and thatCreature and print their breeds in alphabetical order.
accessors10
Accessors
  • Create an equals method that will return true if two animals have the same age, weight, and breed.
mutators
Mutators
  • Create a birthday method that will add one to the age of an Animal.
mutators1
Mutators
  • Show how to add 1 to the age of myCat and print its age.
class variables
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 variables1
Class variables
  • Create accessors to return the class variables you just created.
class variables2
Class variables
  • Show how to print the values of the class variables you just created, using the accessors.
class variables3
Class variables
  • Show the changes needed to the constructor to update the class variables each time a new animal is created.
mutators2
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
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 variables4
Class variables
  • Show the changes needed to the constructor to update allAnimals each time a new animal is created.
class variables and array lists1
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 lists2
Class variables and array lists
  • Show how to print out all the animals that have been created.