Project_04A - COMP 1210 Project 4A: PetOwner and PetDog...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: COMP 1210 Project 4A: PetOwner and PetDog Page 1 of 5 Due: Monday, February 13, 2012 by 11:59 PM Deliverables: The following project files must be submitted to the “graded” assignment in Web-CAT by the due date and time specified above (see the Lab Guidelines for information on submitting project files). You should plan to start on the project not later than Wednesday in lab, and you should make sure that your files are successfully submitted to the “ungraded” assignment in Web-CAT during lab on Wednesday. Projects sent via e-mail past the deadline at 11:59 PM will not be accepted without a universityapproved excuse. Files to submit to Web-CAT: • PetDog.java • PetOwner.java • PetOwnerApp.java Overview: You have been requested to create a project to record pet owners and their pets’ information. For this purpose, you need to design and implement three classes PetDog, PetOwner, and PetOwnerApp: (1) the PetDog class stores pet dog’s basic information and includes methods to set and get individual values; (2) the PetOwner class stores pet owner’s basic information, includes methods to set and get his/her own information also has methods to change his dogs name and estimate the cost of keeping the dog; and (3) the PetOwnerapp class has a main method that creates PetDog and PetOwner objects and generally tests their respective classes. Note: Be sure that all three of your Java files are in the same folder. • PetDog.java Requirements: Create a PetDog class that stores pet dog’s basic information and also includes methods to set and get individual values. Design: The PetDog class has fields, a constructor, and methods as outlined below. (1) Fields (or instance variables): name and breed which are String objects, age which is an int, weight which is a double, and owner which is a String object. These instance variables should be private so that they are not directly accessible outside the PetDog class. (2) Constructor: Your PetDog class must contain a constructor that accepts four parameters representing the pet’s name, breed, age, and weight. Below is an example of how the constructor could be used to create PetDog object: PetDog myPet = new PetDog("Piper", "Pekingese", 2, 12.25); COMP 1210 Project 4A: PetOwner and PetDog Page 2 of 5 (3) Methods: Usually a class provides methods to access (or read) and modify each of its instance variables (known as get and set methods) along with any other required methods. The methods for PetDog are described below. o getName: accepts no parameters and returns a String representing the pet’s name. The pet’s name can be given when the object is created or set up by the method setName. o setName: takes a String parameter and returns a boolean. If the string parameter (the new pet’s name) is null, then the method returns false and the new name is not set. Otherwise, the new name is set to the parameter value and the method returns true. o getBreed: accepts no parameters and returns a String representing the pet’s breed. The pet’s breed is given when the object is created. o getAge: accepts no parameters and returns a int representing the pet’s age, which can be set up by the method setAge. o setAge: takes a int parameter and returns a boolean. If the int parameter (the pet’s age) is negative number, then the method returns false and the age is not set. Otherwise, the age is set to the parameter value and the method returns true. o getWeight: accepts no parameters and returns a double representing the pet’s weight. o setWeight: takes a double parameter and returns a boolean. If the double parameter (the pet’s weight) is negative number, then the method returns false and the age is not set. Otherwise, the age is set to the parameter value and the method returns true. o getOwner: accepts no parameters and returns a String representing the owner’s name. Owner’s name is the setOwner method. o setOwner: takes a String parameter and returns a boolean. If the String paramter (the owner’s name) is null, then the method returns false and the owner’s name is not set. Otherwise, the name is set to the parameter value and the method returns true. o toString: returns a String containing the pet’s name and breed on the first line, followed by the pet’s basic information as shown below: Piper (Pekingese) Age: 2 Weight: 12.25 lbs Owner: Patty Code and Test: As you implement your PetDog class, you should compile and test it using interactions. For example, as soon you have implemented and successfully compiled the constructor, you should create an instance of PetDog in interactions (see the example above). Then after you implement and compile one or more methods, create a PetDog object and invoke each of the methods you added or changed to make sure everything is working as intended. [Hint: When using interactions, you can use the Up-Arrow to avoid retyping previously entered interactions.] COMP 1210 • Project 4A: PetOwner and PetDog Page 3 of 5 PetOwner.java Requirements: Create the PetOwner class that stores pet owner’s basic information, includes methods to set and get his/her own information also has methods to change his dogs name and estimate the cost of keeping the dog. Design: The PetOwner class has fields, a constructor, and methods as outlined below. (1) Fields: name and address which are String objects, and myDog which is a PetDog object. These instance variables should be private so that they are not directly accessible outside the PetOwner class. (2) Constructor: Your PetOwner class must contain a constructor that accepts two String parameters representing the pet owner’s name and address. Below is an example of how the constructor could be used to create PetOwner object: PetOwner myOwner = new PetOwner("Patty Liu", "Auburn, AL"); (3) Methods: Usually a class provides methods to access (or read) and modify each of its instance variables (known as get and set methods) along with any other required methods. The methods for PetOwner are described below. o getName: accepts no parameters and returns a String representing the owner’s name. The owner’s name can be given when the object is created or set up by the method setName. o setName: accepts a String parameter and returns a boolean. If the string parameter (the new pet name) is null, then the method returns false and the new name is not set. Otherwise, the new name is set to the parameter value and the method returns true. o getAddress: accepts no parameters and returns a String representing the owner’s address. Owner’s address can be set using the method setAddress. o setAddress: accepts a String parameter and returns a boolean. If the string parameter is null, then the method returns false and the new address is not set. Otherwise, the new address is set to the parameter value and the method returns true. o getMyDog: returns the PetDog stored in the myDog field in the PetOwner object. o setMyDog: accepts a parameter of type PetDog and sets myDog in the PetOwner object. o changeDogName: accepts a String parameter with no return value. This method changes pet dog’s name using the setName method in PetDog class. o estimatePetCost: returns a double representing the estimated cost of keeping the dog where the cost of keeping a dog depends on the weight and age of the dog. The estimate of pet cost per month = dog food + other expenses. COMP 1210 Project 4A: PetOwner and PetDog Page 4 of 5 The cost of dog food per month is calculated as (weight of dog × 0.7). The other monthly expenses, which include costs for snacks, toys and medical expenses, are calculated based on age: younger than 2 years old average cost is 100 per month; 2 years to 9 years old (but not included 9) average cost is 40 per month; 9 years and older the average cost is 120 per month. o toString: returns a String containing the pet owner’s basic information formatted as shown below: Patty Liu Piper (Pekingese) Age: 2 Weight: 12.25 lbs Owner: Patty Liu Auburn, AL Code and Test: As you implement your PetOwner class, you should compile and test it using interactions. For example, as soon you have implemented and successfully compiled the constructor, you should create an instance of PetOwner in interactions (see the example above). Then after you implement and compile one or more methods, create a PetOwner again and invoke each method on it to make sure it is working as intended. Note that you’ll need to create an instance of PetDog before you invoke the setMyDog method since this method takes a PetDog as a parameter. • PetOwnerApp.java Requirements: Create a PetOwnerapp class with a main method creates PetDog and PetOwner objects and generally tests their respective classes. Design: A class with a main method utilizes the PetDog and PetOwner classes by having the user enter the pet’s name, breed, age and weight, followed by the owner’s name and address. Your program then creates a PetDog object and a PetOwner object, makes the PetDog object the pet of the PetOwner, and then prints out the owner. Your program should now offer the user a chance to change the pet dog’s name and print out a confirmation. The last thing your program needs to do is allow the user to optionally print the monthly cost of owning the dog. See the example of input and output on the following page for formatting details. Replace the prompts in italics with your own words. COMP 1210 Project 4A: PetOwner and PetDog Page 5 of 5 Example: Line # Program input/output 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 • Prompt Prompt Prompt Prompt Prompt Prompt user user user user user user for for for for for for the the the the the the name of his/her pet dog: Piper breed of the dog: Pekingese age of the dog: 2 weight of the dog: 12.25 name of the owner: Patty Liu address of the owner: Auburn, AL Patty Liu Piper (Pekingese) Age: 2 Weight: 12.25 lbs Owner: Patty Liu Auburn, AL Prompt whether the user wants to change the dog name(Y/N): Y Prompt user for the new name: Jenny The name of your dog has been changed to Jenny. Prompt user whether to calculate the estimated cost (Y/N): Y Your dog bills will be approximately $48.58 per month. Code & Test: Your program should declare local variables for a PetDog object, a PetOwner object and all input data. Your program should use the DecimalFormat class to format the estimated cost so that it is rounded to two decimal digits and includes a leading dollar sign ($). jGRASP Project and UML Class diagram – All three of your Java files should be in the same folder. You should also create a jGRASP project in this folder and add your Java files to it. This will facilitate submission to Web-CAT. After you have created the project, you can generate the UML class diagram for your project (as shown at left) by clicking on the UML symbol for project in the Browse tab. After the diagram is generated, rearrange the classes so that class with main is at the top. The purpose of the UML class diagram is to show the dependencies among your classes: PetOwnerApp UML Class Diagram depends on PetOwner and PetDog; PetOwner depends on Petdog; and PetDog does not depend on either of the other classes. These dependencies dictate the order of compilation (e.g., you can only compile PetOwner after you have successfully compiled PetDog). ...
View Full Document

This note was uploaded on 03/02/2012 for the course COMP 1210 taught by Professor Cross during the Winter '07 term at Auburn University.

Ask a homework question - tutors are online