This document describes an assignment to create two new classes - BirthInfo and Pet - and a driver class Assignment4. The BirthInfo class stores information about a pet's birth including date, month, year and place. The Pet class represents a pet with attributes for name, type, and birth information. The Assignment4 driver class displays a menu and allows the user to add a pet by entering its details, display the pet information, or quit the program. It provides test case input and expected output files to test the program.
1. Assignment #4 will be the construction of 2 new classes and a driver program/class (the
class containing a main method).You are required, but not limited, to turn in the following
source files:
Assignment4.java (I have pasted this at the end of the assignment)
Pet.java
BirthInfo.java
BirthInfo class
The BirthInfo class describes information of the birth of a pet. It has following attributes:
Attribute name Attribute type Description
date int The date of the birth
month int The month of the birth
year int The year of the birth
place String The place of departure or arrival
The default value of date, month, and year is 0 and the default for place is "?". Provide a
constructor to set these default values.
public BirthInfo()
The following accessor methods should be provided to get the attributes:
public int getDate()
public int getMonth()
public int getYear()
public String getPlace()
The following modifier(mutator) methods should be provided to set the attributes:
public void setDate(int date1)
public void setMonth(int month1)
public void setYear(int year1)
public void setPlace(String place1)
The following method must be defined:
public String toString()
toString method should return a string of the following format:
Date 4/Month 7/Year 2006/Place BeverlyHills
where "4" is a date, "7" is a month, "2006" is a year, and "BeverlyHills" is a place. So you
need to insert "/", "Data", "Month", "Year", and "Place" in between these variables.
Pet class
2. The Pet class describes a pet that an owner can have. It has the following attributes:
Attribute name Attribute type Description
petName String The name of a pet.
type String The pet type
birth BirthInfo The birth information of a pet
The default value of strings is "?". Provide a constructor to set these default values.
public Pet()
The following accessor methods should be provided to get the attributes:
public String getPetName()
public String getType()
public BirthInfo getBirthInfo()
The following modifier(mutator) methods should be provided to change the attributes:
public void setPetName(String pName)
public void setType(String pType)
public void setBirthInfo(int date, int month, int year, String place)
The following method must be defined:
public String toString()
The toString() method constructs a string of the following format:
nPet Name:ttChloen
Type:tttChihuahuaDogn
Birth Information:tDate 4/Month 7/Year 2006/Place BeverlyHillsnn
Assignment4
(Note that this part is already done in the Assignment4.java file that is given to you. This
explains each functionality of this class.)
In this assignment, download Assignment4.java file by clicking the link, and use it for your
assignment. You do not need to change Assignment4.java file. You only need to write
Pet.java and BirthInfo.java files.
The following is the description of Assignment4 class.
The driver program will allow the user to interact with your other class modules. The
purpose of this module is to handle all user input and screen output. The main method
3. should start by displaying the following menu in this exact format:
ChoicettActionn
------tt------n
AttAdd Petn
DttDisplay Petn
QttQuitn
?ttDisplay Helpnn
Next, the following prompt should be displayed:
What action would you like to perform?n
Read in the user input and execute the appropriate command. After the execution of each
command, re-display the prompt. Commands should be accepted in both lowercase and
uppercase.
Add Pet
Your program should display the following prompt:
Please enter the pet information:n
Enter a pet name:n
Read in the user input and set the pet name on the pet object. Then the following prompt:
Enter its type:n
Read in the user input and set the pet type on the pet object. Then the following prompt:
Enter its birth date:n
Read in the user input. Then the following prompt:
Enter its birth month:n
Read in the user input. Then the following prompt:
Enter its birth year:n
Read in the user input. Then the following prompt:
Enter its birth place:n
4. Read in the user input and set the birth date, month, year, and place on the pet object. Then
the following prompt:
Note that there is only one Pet object in this assignment. Thus when "Add Pet" option is
selected more than once, the new one overwrites the old Pet object information.
Display Pet
Your program should display the pet information in the following format:
nPet Name:ttChloen
Type:tttChihuahuaDogn
Birth Information:tDate 4/Month 7/Year 2006/Place BeverlyHillsnn
Make use of the toString method of the Pet class to display this information. The toString
method is used together with System.out.print method.
(System.out is NOT to be used within the toString method.)
Quit
Your program should stop executing and output nothing.
Display Help
Your program should redisplay the "choice action" menu.
Invalid Command
If an invalid command is entered, display the following line:
Unknown actionn
Input
The following files are the test cases that will be used as input for your program (Right-
click and use "Save As"):
Test Case #1
Test Case #2
Test Case #3
Test Case #4
5. Output
The following files are the expected outputs of the corresponding input files from the
previous section (Right-click and use "Save As"):
Test Case #1
Test Case #2
Test Case #3
Test Case #4
hw4testcases.jar All test case files are in this file. To extract each file:
jar xf hw4testcases.jar
Error Handling
Your program is expected to be robust to handle four test cases.
Here is assignment 4:
// Assignment #: 4
// Name: Your name
// StudentID: Your ID
// Lecture: Your section
// Description: Assignment 4 class displays a menu of choices to a user
// and performs the chosen task. It will keep asking a user to
// enter the next choice until the choice of 'Q' (Quit) is entered.
import java.io.*; //to use InputStreamReader and BufferedReader
import java.util.*;
public class Assignment4
{
public static void main (String[] args)
{
// local variables, can be accessed anywhere from the main method
char input1 = 'Z';
String inputInfo;
String name, type, place;
int date, month, year;
String line = new String();
// instantiate a Pet object
6. Pet pet1 = new Pet();
printMenu();
//Create a Scanner object to read user input
Scanner scan = new Scanner(System.in);
do // will ask for user input
{
System.out.println("What action would you like to perform?");
line = scan.nextLine();
if (line.length() == 1)
{
input1 = line.charAt(0);
input1 = Character.toUpperCase(input1);
// matches one of the case statement
switch (input1)
{
case 'A': //Add Pet
System.out.print("Please enter the pet information:n");
System.out.print("Enter a pet name:n");
name = scan.nextLine();
pet1.setPetName(name);
System.out.print("Enter its type:n");
type = scan.nextLine();
pet1.setType(type);
System.out.print("Enter its birth date:n");
date = Integer.parseInt(scan.nextLine());
System.out.print("Enter its birth month:n");
month = Integer.parseInt(scan.nextLine());
System.out.print("Enter its birth year:n");
year = Integer.parseInt(scan.nextLine());
System.out.print("Enter its birth place:n");
place = scan.nextLine();
pet1.setBirthInfo(date, month, year, place);
break;
case 'D': //Display course
7. System.out.print(pet1);
break;
case 'Q': //Quit
break;
case '?': //Display Menu
printMenu();
break;
default:
System.out.print("Unknown actionn");
break;
}
}
else
{
System.out.print("Unknown actionn");
}
} while (input1 != 'Q' || line.length() != 1);
}
/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("ChoicettActionn" +
"------tt------n" +
"AttAdd Petn" +
"DttDisplay Petn" +
"QttQuitn" +
"?ttDisplay Helpnn");
}
}