Find Us On: Facebook Twitter Rss Feed Blog
Need To Register? Sign Up Login

Simple Java question 3


Define a Pet class that allows one to create objects that represent animals one might own as a pet. To keep matters simple, let’s assume every pet has a name, a current age, and a current weight in US pounds. Here are 3 examples of how I might construct Pet objects: (a) Pet myDog = new Pet (“Fido”, 9, 29.5); (b) Pet myParrot = new Pet (“Polly”, 32, 2.85); (c) Pet myCat = new Pet (“Tiger”, 4); Note that in addition to defining the Pet class with three instance variables, you will need to define two constructor methods for the Pet class. One constructor accepts three arguments (the pet’s name, current age and current weight) as shown in the first two examples above; the other constructor accepts just two arguments (the pet’s name and current age) as shown in the third example. You must also implement an instance method public String getName() {...} that returns the name of the pet. In, construct the three examples above and use the getName method to find and print the names of each pet you have just constructed. Attachments
Tags Clarifications


Simple Java question 3 - Solution


Included a two java files containing the solution. Provided also a screenshot of the output. Attachments
Screen Shot 2016-11-26 at 17.46.37.png (30 K) Download Preview

Simple Question (1 K) Download
pet. * @param age The age of the pet. */ public Pet(String name, int age) { = name; this.age = age; } /** * Constructor creating a Pet with a name, age and its weight.

= new Pet("Polly", 32, 2.85); Pet myCat = new Pet("Tiger",

Purchase Answer