SlideShare a Scribd company logo
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
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
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
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
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
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
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");
}
}
Assignment #4 will be the construction of 2 new classes and a driver program/class (the  class conta

More Related Content

PPT
Class and object in c++
PDF
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
PPT
Python session 7 by Shan
PPTX
Computer programming 2 Lesson 3
PDF
Python unit 3 m.sc cs
PDF
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
PPTX
oop lecture 3
PPTX
class c++
Class and object in c++
اسلاید جلسه ۹ کلاس پایتون برای هکر های قانونی
Python session 7 by Shan
Computer programming 2 Lesson 3
Python unit 3 m.sc cs
جلسه هفتم پایتون برای هکر های قانونی دوره مقدماتی پاییز ۹۲
oop lecture 3
class c++

What's hot (13)

PDF
Lecture 4 - Object Interaction and Collections
PDF
PHP OOP
PPTX
Basics of Object Oriented Programming in Python
PPTX
Python oop third class
PPT
Booa8 Slide 02
PPT
1 1 5 Clases
 
PPT
Inheritance in C++
PPT
inheritance
PDF
Doctrator Symfony Live 2011 Paris
PPTX
Object Oriented Programming in Python
PPTX
Java New Features
PPTX
Chap4 oop class (php) part 1
Lecture 4 - Object Interaction and Collections
PHP OOP
Basics of Object Oriented Programming in Python
Python oop third class
Booa8 Slide 02
1 1 5 Clases
 
Inheritance in C++
inheritance
Doctrator Symfony Live 2011 Paris
Object Oriented Programming in Python
Java New Features
Chap4 oop class (php) part 1
Ad

Similar to Assignment #4 will be the construction of 2 new classes and a driver program/class (the class conta (20)

DOCX
CSE 110 - Lab 6 What this Lab Is About  Working wi.docx
PDF
Overview You are tasked with writing a program called Social Security.pdf
PDF
Code to copy Person.java .pdf
PDF
Lect 1-java object-classes
PDF
Hello. Im currently working on the last section to my assignment a.pdf
DOCX
Lab 8 User-CF Recommender System – Part I 50 points .docx
PDF
Below is my program, I just have some issues when I want to check ou.pdf
PPT
Defining classes-and-objects-1.0
PDF
First compile all the classes.But to run the program we have to run .pdf
PDF
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
PDF
Implementation Your program shall contain at least the follo.pdf
PDF
Main issues with the following code-Im not sure if its reading the fil.pdf
PPSX
Java session4
PPTX
3.Syntax.pptx for oops programing language
PPT
Java căn bản - Chapter7
PPT
Chapter 7 - Defining Your Own Classes - Part II
DOCX
1 INVALID & VALID PARTY & FAVOR CHOICES P.docx
PPTX
Pj01 x-classes and objects
DOCX
INTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docx
PPTX
Numerical data.
CSE 110 - Lab 6 What this Lab Is About  Working wi.docx
Overview You are tasked with writing a program called Social Security.pdf
Code to copy Person.java .pdf
Lect 1-java object-classes
Hello. Im currently working on the last section to my assignment a.pdf
Lab 8 User-CF Recommender System – Part I 50 points .docx
Below is my program, I just have some issues when I want to check ou.pdf
Defining classes-and-objects-1.0
First compile all the classes.But to run the program we have to run .pdf
Repeat Programming Project 2 in Chapter 5. This time, add the follow.pdf
Implementation Your program shall contain at least the follo.pdf
Main issues with the following code-Im not sure if its reading the fil.pdf
Java session4
3.Syntax.pptx for oops programing language
Java căn bản - Chapter7
Chapter 7 - Defining Your Own Classes - Part II
1 INVALID & VALID PARTY & FAVOR CHOICES P.docx
Pj01 x-classes and objects
INTERMIDIATE PROGRAMMINGCMPSC 122LAB 9 INHERITANCE AND POLYMO.docx
Numerical data.
Ad

More from hwbloom3 (7)

PDF
Do not use arrays or any library sorting method. Declare 3String variables: s...
PDF
I accidentally installed Ask Toolbar, and it changed my Homepage,Search engin...
PDF
Please show a screenshot of the code. Thank you. Solve the function F= 25x^2...
PDF
9.4 ?Use Warshall
PDF
Your company has assigned you the task of evaluating its computer networks. Y...
PDF
There are three sections of a class. There are 12 students in section 1. Ther...
PDF
Please follow the requiremen
Do not use arrays or any library sorting method. Declare 3String variables: s...
I accidentally installed Ask Toolbar, and it changed my Homepage,Search engin...
Please show a screenshot of the code. Thank you. Solve the function F= 25x^2...
9.4 ?Use Warshall
Your company has assigned you the task of evaluating its computer networks. Y...
There are three sections of a class. There are 12 students in section 1. Ther...
Please follow the requiremen

Recently uploaded (20)

PPTX
Cell Types and Its function , kingdom of life
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
Institutional Correction lecture only . . .
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
Presentation on HIE in infants and its manifestations
PPTX
Cell Structure & Organelles in detailed.
Cell Types and Its function , kingdom of life
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
01-Introduction-to-Information-Management.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
STATICS OF THE RIGID BODIES Hibbelers.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Institutional Correction lecture only . . .
O7-L3 Supply Chain Operations - ICLT Program
Microbial diseases, their pathogenesis and prophylaxis
GDM (1) (1).pptx small presentation for students
Supply Chain Operations Speaking Notes -ICLT Program
VCE English Exam - Section C Student Revision Booklet
Abdominal Access Techniques with Prof. Dr. R K Mishra
Presentation on HIE in infants and its manifestations
Cell Structure & Organelles in detailed.

Assignment #4 will be the construction of 2 new classes and a driver program/class (the class conta

  • 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"); } }