Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A description of each method is also needed. Some additional comments inside of

ID: 3637337 • Letter: A

Question

A description of each method is also needed. Some additional comments inside of methods (especially for the "main" method) to explain codes that are hard to follow should be written. You can look at the Java programs in the text book to see how comments are added to programs.

Skills to be Applied

In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:

Classes
Instance Data
Accessors/Modifiers(Mutators)
Visibility Modifiers (Access specifier)
Encapsulation concept
Aggregation relationship between classes
Wrapper classes -- Integer class
NumberFormat class in java.text package

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:

Pet Name: Chloe
Type: ChihuahuaDog
Birth Information: Date 4/Month 7/Year 2006/Place BeverlyHills


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:

Choice Action
------ ------
A Add Pet
D Display Pet
Q Quit
? Display Help

Next, the following prompt should be displayed:

What action would you like to perform?

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:
Enter a pet name:

Read in the user input and set the pet name on the pet object. Then the following prompt:

Enter its type:

Read in the user input and set the pet type on the pet object. Then the following prompt:

Enter its birth date:

Read in the user input. Then the following prompt:

Enter its birth month:

Read in the user input. Then the following prompt:

Enter its birth year:

Read in the user input. Then the following prompt:

Enter its birth place:

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:

Pet Name: Chloe
Type: ChihuahuaDog
Birth Information: Date 4/Month 7/Year 2006/Place BeverlyHills


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 action

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: ");
System.out.print("Enter a pet name: ");
name = scan.nextLine();
pet1.setPetName(name);

System.out.print("Enter its type: ");
type = scan.nextLine();
pet1.setType(type);

System.out.print("Enter its birth date: ");
date = Integer.parseInt(scan.nextLine());
System.out.print("Enter its birth month: ");
month = Integer.parseInt(scan.nextLine());
System.out.print("Enter its birth year: ");
year = Integer.parseInt(scan.nextLine());
System.out.print("Enter its birth place: ");
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 action ");
break;
}
}
else
{
System.out.print("Unknown action ");
}
} while (input1 != 'Q' || line.length() != 1);
}

/** The method printMenu displays the menu to a user **/
public static void printMenu()
{
System.out.print("Choice Action " +
"------ ------ " +
"A Add Pet " +
"D Display Pet " +
"Q Quit " +
"? Display Help ");
}
}

Explanation / Answer

ase 'A': //Add Pet System.out.print("Please enter the pet information: "); System.out.print("Enter a pet name: "); name = scan.nextLine(); pet1.setPetName(name); System.out.print("Enter its type: "); type = scan.nextLine(); pet1.setType(type); System.out.print("Enter its birth date: "); date = Integer.parseInt(scan.nextLine()); System.out.print("Enter its birth month: "); month = Integer.parseInt(scan.nextLine()); System.out.print("Enter its birth year: "); year = Integer.parseInt(scan.nextLine()); System.out.print("Enter its birth place: "); 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 action "); break; } } else { System.out.print("Unknown action "); } } while (input1 != 'Q' || line.length() != 1); } /** The method printMenu displays the menu to a user **/ public static void printMenu() { System.out.print("Choice Action " + "------ ------ " + "A Add Pet " + "D Display Pet " + "Q Quit " + "? Display Help "); } }

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote