Write a project and package that contains definitions for the Pet class. Part 1
ID: 3756484 • Letter: W
Question
Write a project and package that contains definitions for the Pet class.
Part 1 - Class Specifications
Class – Pet
Member Variables (all private)
Data Type
Contains the name of the pet.
Contains the pet’s species.
Contains the pet’s gender.
Member Method Signatures and Descirptions (all public)
Default constructor. Sets the values of each member variable to a default value.
Pet(String name, String Species, String gender)
Constructor. Sets the values of each member variable to the corresponding parameter values.
Get/Set methods
Write get/set methods for all member variables.
void Read(Scanner s)
Read the contents of all member variables from the given instance of Scanner. Assume the following:
Scanner is already open.
Member variable values are on separate lines.
void Write(PrintStream ps)
Write the contents of all member variables to the given instance of PrintStream. Assume the PrintStream is already open and ready to use. Data should be written on to separate lines. DO NOT ADD ANY DESCRIPTIVE TEXT IN THE OUTPUT. JUST PRINT THE VALUES.
IMPORTANT - Whatever data is written out should be readable by the Read method of this class. If descriptive text is added then Read will not work.
This method should return a string using JSON formatting (www.json.org). Here is the format:
{ “variable name” : value, … }
Each variable name should be surrounded by quotes. If the variable data type is String then the value should be surrounded by double quotes. There should be a comma between each pair (no comma after the last pair).
Example (Snickers, dog, male):
{"name" : "Snickers", "species" : "dog", "gender" : "male"}
Example (Princess, cat, female):
{"name" : "Princess", "species" : "cat", "gender" : "female"}
This method should return a String instance (not print on the screen) that contains the values of member variables with descriptive text. Here is an example of a string that gets returned from the method.
Example date string:
Name: Snickers
Species: dog
Gender: male
Class – Main
Member Variables (all private)
Data Type
No member variables
Member Method Signatures and Descirptions (all public)
public static void main(String args[])
Write unit testing and other code in main. See below for details.
Automated Test
Create an automated test in main.
There should be unit tests of ALL get/set methods of both classes. GetJSON is NOT included here.
You need to write code that runs the other methods that are not being unit tested on each class (this includes GetJSON, constructors, Read, Write, toString etc…).
Pet File Format
PetName
PetSpecies
PetGender
Pet Sample Input File (Pet.txt)
Snickers
Dog
Male
Data Type
Contains the name of the pet.
Contains the pet’s species.
Contains the pet’s gender.
Explanation / Answer
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts. If you are satisfied with the solution, please rate the answer. Thanks
// Pet.java
import java.io.PrintStream;
import java.util.Scanner;
public class Pet {
//attributes
private String name;
private String species;
private String gender;
/**
* default constructor
*/
public Pet() {
this.name = "";
this.species = "";
this.gender = "";
}
/**
* parameterized constructor
*/
public Pet(String name, String species, String gender) {
this.name = name;
this.species = species;
this.gender = gender;
}
/**
* getters and setters
*/
public String getName() {
return name;
}
public String getSpecies() {
return species;
}
public String getGender() {
return gender;
}
public void setName(String name) {
this.name = name;
}
public void setSpecies(String species) {
this.species = species;
}
public void setGender(String gender) {
this.gender = gender;
}
public void Read(Scanner scanner) {
name = scanner.nextLine();
species = scanner.nextLine();
gender = scanner.nextLine();
}
public void Write(PrintStream ps) {
ps.println(name);
ps.println(species);
ps.println(gender);
}
/**
* returns pet details in JSON String format
*/
public String GetJSON() {
String data = "{"name" : "" + name + "", "species" : ""
+ species + "", "gender" : "" + gender + ""}";
return data;
}
@Override
public String toString() {
String data = "Name: " + name + " ";
data += "Species: " + species + " ";
data += "Gender: " + gender;
return data;
}
}
// Main.java
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import org.junit.Test;
public class Main {
public static void main(String[] args) {
// creating a Pet object
Pet pet = new Pet();
// ensuring that get methods return default values
assertEquals(pet.getName(), "");
assertEquals(pet.getGender(), "");
assertEquals(pet.getSpecies(), "");
// altering values using set methods
pet.setName("Jimmy");
pet.setSpecies("Cat");
pet.setGender("Male");
// ensuring values are changed
assertEquals(pet.getName(), "Jimmy");
assertEquals(pet.getSpecies(), "Cat");
assertEquals(pet.getGender(), "Male");
System.out.println("Getters and Setters working OK,"
+ " testing parameterized constructor");
// re initializing the object using parameterized constructor
pet = new Pet("Hachi", "Dog", "Male");
//ensuring that all changes are made properly
assertEquals(pet.getName(), "Hachi");
assertEquals(pet.getSpecies(), "Dog");
assertEquals(pet.getGender(), "Male");
// displaying the pet info using toString() method
// should display details in the below format
// Name: Hachi
// Species: Dog
// Gender: Male
System.out.println("Testing toString() method");
System.out.println(pet);
System.out.println("Testing GetJSON() method");
System.out.println(pet.GetJSON());
try {
System.out.println("Opening Pet.txt file");
Scanner scanner = new Scanner(new File("Pet.txt"));
System.out.println("Reading data from file using Read() method");
// reading the contents from file Scanner
pet.Read(scanner);
System.out.println("Writing read data using Write() method");
// writing the read values.
pet.Write(System.out);
} catch (FileNotFoundException e) {
System.out.println("Pet.txt file not found in the same directory!");
}
}
}
/*OUTPUT*/
Getters and Setters working OK, testing parameterized constructor
Testing toString() method
Name: Hachi
Species: Dog
Gender: Male
Testing GetJSON() method
{"name" : "Hachi", "species" : "Dog", "gender" : "Male"}
Opening Pet.txt file
Reading data from file using Read() method
Writing read data using Write() method
Snickers
Dog
Male
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.