This assignment shpuld be code in java. Thanks
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
This assignment shpuld be code in java. Thanks Overview In this assignment you will write a...
(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 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 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:...
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; // 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 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 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 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 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 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...