Question

This assignment shpuld be code in java. Thanks
Overview In this assignment you will write a program that will model a pet store. The program will have a Pet class to model individual pets and the Assignment5 class will contain the main and act as the pet store. Users will be able to view the pets, make them age a year at a time, add a new pet, and adopt any of the pets. Note: From this point on, your assignments will require multiple files. If you want to keep things easily organized, you may want to create a new project within your IDE to keep these files separate from all the others. Requirements Your program must do the following in order to receive full credit on this assignment. 1. Create a new public class called Pet in its own file. a. Remember that class and file name must match. 2. Create three private instance variables. a. b. c. The name of the pet. The age of the pet. Whether the pet is adopted or not. 3. Create a default constructor for the Pet class. Remember that a default constructor takes no arguments and assigns meaningless values to the instance variables. a. 4. Create a second constructor for Pet that takes a name and age as arguments. a. Assign these values to the instance variables. b. Pets always start off un-adopted. 5. 6. Provide a get and set for the name instance variable. Provide a get for age, but instead of a regular set create a method that increases age by 1 a. Keep in mind that this age increase method will need to be public. 7. Provide a get for the adopted variable and instead of a set provide a public method which sets this adoption variable to true. Note: This is the last thing you will do in Pet. The rest of these steps are done in your main program file. a. 8. Create a Scanner object that will be visible to all methods in Assignment5. a. This object will need a particular modifier. What is it?

media%2F745%2F7458ad18-1449-4d3a-9181-8c

media%2F0ec%2F0ec2d949-6ad3-4d60-8e90-81

media%2F567%2F56747c7f-f2bb-4c96-9e89-46

media%2F17f%2F17fd16f6-9b3b-4694-9060-af

media%2Ffec%2Ffeccca38-8854-440d-9cf2-0e

media%2F1d4%2F1d4673c5-98ec-4fbb-9438-38

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pet.java

public class Pet {

//private instance variables

private String petName;

private int age;

private boolean isAdopted;

//default constructor

public Pet()

{

petName = "";

age = 0;

isAdopted = false;

}

//parameterised constructor

public Pet(String pName, int pAge)

{

petName = pName;

age = pAge;

isAdopted = false;

}

//set method for name

public void setName(String pName)

{

petName = pName;

}

//get method for name

public String getName()

{

return petName;

}

//method that sets age

public void ageUp()

{

age = age + 1;

}

//method that gets the age

public int getAge()

{

return age;

}

//method to that adoption

public boolean getIsAdopted()

{

return isAdopted;

}

//method to adopt pet

public void adoptPet()

{

isAdopted = true;

}

}

Assignment5.java

import java.util.Scanner;

public class Assignment5 {

static Scanner input = new Scanner(System.in);

public static void main(String[] args) {

//create two local pet objects

Pet pet1 = new Pet("Fido", 3);

Pet pet2 = new Pet("Furball", 1);

//create a third local pet object

Pet pet3 = null;

String userChoice;

//display the welcome message

System.out.println("Welcome to the Pet store!");

//display menu and get the user choice

userChoice = displayMenu();

//run a loop until E is entered

while(!(userChoice.equals("E")))

{

//run a switch case based on user choice

switch(userChoice)

{

case "A":

//print the details of pet1

System.out.print(pet1.getName() + " is " + pet1.getAge());

if(pet1.getAge() > 1)

System.out.print(" years and is ");

else

System.out.print(" year and is ");

if(pet1.getIsAdopted())

System.out.println("adopted.");

else

System.out.println("not adopted.");

//print the details of pet2

System.out.print(pet2.getName() + " is " + pet2.getAge());

if(pet2.getAge() > 1)

System.out.print(" years and is ");

else

System.out.print(" year and is ");

if(pet2.getIsAdopted())

System.out.println("adopted.");

else

System.out.println("not adopted.");

//print the details of pet3 if not null

if(pet3 != null)

{

System.out.print(pet3.getName() + " is " + pet3.getAge());

if(pet3.getAge() > 1)

System.out.print(" years and is ");

else

System.out.print(" year and is ");

if(pet3.getIsAdopted())

System.out.println("adopted.");

else

System.out.println("not adopted.");

}

break;

case "B":

//increase the age of all pets

pet1.ageUp();

pet2.ageUp();

//if pet3 is not null then increase its age

if(pet3 != null)

pet3.ageUp();

System.out.println("Everyone just got a little older.");

break;

case "C":

//check if pet3 doesn't exist

if(pet3 == null)

{

String name;

int age;

//prompt and read the name and age

System.out.println("Please type in a name.");

name = input.nextLine();

System.out.println("Type in an age");

age = Integer.parseInt(input.nextLine());

//instantiate pet3

pet3 = new Pet(name, age);

System.out.println(pet3.getName() + " has arrived in the store.");

}

//if pet3 exists then print the store is full

else

{

System.out.println("Sorry, the store is full.");

}

break;

case "D":

int number;

//read the number of the pet to be adopted

System.out.println("Type the number of the pet you would like to adopt.");

System.out.println("The number is the order they appear in the list.");

number = Integer.parseInt(input.nextLine());

//if number is 1 the adopt pet1

if(number == 1)

{

pet1.adoptPet();

System.out.println(pet1.getName() + " was adopted!");

}

//if number is 2 the adopt pet2

else if(number == 2)

{

pet2.adoptPet();

System.out.println(pet2.getName() + " was adopted!");

}

//if number is 3 the adopt pet3 only if it is not null

else if(number == 3)

{

if(pet3 != null)

{

pet3.adoptPet();

System.out.println(pet3.getName() + " was adopted!");

}

//else print error message

else

{

System.out.println("That is not a valid pet.");

}

}

//else print error message

else

System.out.println("That is not a valid pet.");

break;

}

//display the menu again

userChoice = displayMenu();

}

//display good bye message

System.out.println("Have a good day!");

input.close();

}

//method that displays menu and reads the user choice

private static String displayMenu()

{

String choice;

//print the menu

System.out.println("Type the letter to make your selection.");

System.out.println("A. List the pets in the store.");

System.out.println("B. Age up the pets.");

System.out.println("C. Add a new pet.");

System.out.println("D. Adopt a pet.");

System.out.println("E. Quit.");

//read the choice

choice = input.nextLine();

//accept input until a valid choice is entered

while(!(choice.equals("A") || choice.equals("B") || choice.equals("C")

|| choice.equals("C") || choice.equals("E")))

{

System.out.println("Invalid choice.");

choice = input.nextLine();

}

return choice;

}

}

Outputs

貝Console X «terminated> AssignmentS Java Application C:\Program Files Java rel·80151\binyavav exe (Oct 10, 2018, 11:23:29 AM Welcome to the Pet store! Type the letter to make your selection a. List the pets in the store. b. Age up the pets c. Add a new pet d. Adopt a pet e. Quit Fido is 3 years and is not adopted Furball is 1 year and is not adopted Type the letter to make your selection a. List the pets in the store. b. Age up the pets c. Add a new pet d. Adopt a pet e. Quit Have a good day!

Add a comment
Know the answer?
Add Answer to:
This assignment shpuld be code in java. Thanks Overview In this assignment you will write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text...

    (JAVA) Use the Pet.java program from the original problem (down below) Compile it. Create a text file named pets10.txt with 10 pets (or use the one below). Store this file in the same folder as your “class” file(s). The format is the same format used in the original homework problem. Each line in the file should contain a pet name (String), a comma, a pet’s age (int) in years, another comma, and a pet’s weight (double) in pounds. Perform the...

  • Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the...

    Hello. I need help writing the following Java Program. Thank you Develop a class encapsulating the concept of a college course, assuming that a course has following attributers: code (for instance COSC1337), a description, and a number of credits (for instance 3). Include a constructor, the accessors, mutators and methods ‘toString’, ‘equals’, and ‘finalize’. Write a client class to test the behavior of the class and its methods. The outline of the class is given as follows: public class Course...

  • Create a UML diagram to help design the class described in exercise 3 below. Do this...

    Create a UML diagram to help design the class described in exercise 3 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Baby class object; should they be private or public? Determine what class methods are required; should they be private or public? Write Java code for a Baby class. A Baby has a name of type String and an age of type integer. Supply two constructors:...

  • Java Object Array With 2 Elements In 1 Object

    1. Create a UML diagram to help design the class described in Q2 below. Do this exercise before you attempt to code the solution. Think about what instance variables will be required to describe a Person class object; should they be private or public? Determine what class methods are required; should they be private or public?2. Write Java code for a Person class. A Person has a name of type String and an age of type integer.Supply two constructors: one will be...

  • Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; //...

    Code in JAVA UML //TEST HARNESS //DO NOT CHANGE CODE FOR TEST HARNESS import java.util.Scanner; // Scanner class to support user input public class TestPetHierarchy { /* * All the 'work' of the process takes place in the main method. This is actually poor design/structure, * but we will use this (very simple) form to begin the semester... */ public static void main( String[] args ) { /* * Variables required for processing */ Scanner input = new Scanner( System.in...

  • ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class...

    ****Here is the assignment **** Purpose: To write an Object-Oriented application that creates a Java class with several instance variables, a constructor to initialize the instance variables, several methods to access and update the instance variables’ values, along with other methods to perform calculations. Also, write a test class that instantiates the first class and tests the class’s constructor and methods. Details: Create a class called Rectangle containing the following: Two instance variables, An instance variable of type double used...

  • Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super...

    Java Program (PLEASE PROVIDE SCREENSHOT OF THE CODE) Create a class Person which is a super class. The class includes four private String instance variables: first name, last name, social security number, and state. Write a constructor and get methods for each instance variable. Also, add a toString method to print the details of the person. After that create a class Teacher which extends the class, Person. Add a private instance variable to store number of courses. Write a constructor...

  • JAVA QUESTION 16 Write and submit the java source for the following class specification: The name...

    JAVA QUESTION 16 Write and submit the java source for the following class specification: The name of the class is Question_16 Class Question_16 has 3 instance variables: name of type String, age of type int and height of type double. Class Question_16 has the following methods: print - outputs the data values stored in the instance variables with the appropriate label setName - method to set the name setAge - method to set the age setHeight - method to set...

  • Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables...

    Java Eclipse Coding question: Use data abstraction (real-world modeling) to come up with 3 instance variables and 2 effectors for a new data type – the class HospitalPatient. You have the freedom to design the instance variables and effectors. instance variables effectors 1. 1. 2. 2. 3. Write the code for class HospitalPatient, according to the requirement above. Include the default constructors with no argument, and the constructor with all arguments. Provide a getter and setter for each individual instance...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT