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

Part 1: Hwk2Pet and Hwk2PetTester: Write the Java code for a Hwk2Pet class which

ID: 3886063 • Letter: P

Question

Part 1: Hwk2Pet and Hwk2PetTester: Write the Java code for a Hwk2Pet class which matches the following description.

Each pet will have a name, an animal type (dog, cat, etc.) and the year they were born. This data about each pet should be stored. Identify the instance variables and their types.

Each pet will be created using a no-argument constructor. Default values of your choice can be used to initialize the instance variables.

Each instance variable should have an accessor and mutator method (getters and setters).

In addition a printData() method that displays the values of all the data (instance variables) of a pet is needed.

NOTE: There should not be a main method in the Hwk2Pet class!

Write the Java code for a Hwk2PetTester class which matches the following description: Should contain a main method which does the following

Constructs four different Hwk2Pet objects.

Use the set methods to store a value in the name, animal, and year fields for each object.

Use the printData method on each object to display the contents of the object

IMPORTANT: Upload both Hwk2Pet.java and Hwk2PetTester.java files in the appropriate D2L Assignments folder.

Include comments in the entire code (program header, method header, variables)

Do your own work – do not copy files or code from others!

Part 2: Hwk2Square Class

Create a class called Hwk2Square which has the following instance variable (do NOT add any other instance variables to the class):

                Length of sides (could be decimal values)

Methods

Create the constructor which receives the length of the sides as a parameter

setLength. This method stores a value in the length instance variable

getLength. This method returns the value of the length instance variable

computeArea. This method returns the area of the square

computePerimeter. This method returns the perimeter of the square

printData. This method should display the contents of each the instance variables in a nice format

NOTE: There should not be a main method in the Hwk2Square class!

Hwk2SquareTester Class

Create a class called Hwk2SquareTester which constructs four different Square objects

For each Square object you created:

SOP the square’s length

SOP the square’s area

SOP the square’s perimeter

Notes

Upload to the D2L Dropbox

Hwk2Square.java

Hwk2SquareTester.java

Include comments in the entire code (program header, method header, variables)

Explanation / Answer

public class Hwk2Pet {

private String petName;

private String petType;

private int yearBorn;

public Hwk2Pet() {

// TODO Auto-generated constructor stub

this.petName = "";

this.petType = "";

this.yearBorn = 0;

}

/**

* @return the petName

*/

public String getPetName() {

return petName;

}

/**

* @return the petType

*/

public String getPetType() {

return petType;

}

/**

* @return the yearBorn

*/

public int getYearBorn() {

return yearBorn;

}

/**

* @param petName

* the petName to set

*/

public void setPetName(String petName) {

this.petName = petName;

}

/**

* @param petType

* the petType to set

*/

public void setPetType(String petType) {

this.petType = petType;

}

/**

* @param yearBorn

* the yearBorn to set

*/

public void setYearBorn(int yearBorn) {

this.yearBorn = yearBorn;

}

/**

* to print the instance variables

*/

public void printData() {

System.out.println("Pet Name:" + getPetName());

System.out.println("Pet Type:" + getPetType());

System.out.println("Pet Year Born:" + getYearBorn());

}

}

public class Hwk2PetTester {

public static void main(String[] args) {

// Constructs four different Hwk2Pet objects.

Hwk2Pet pet1 = new Hwk2Pet();

Hwk2Pet pet2 = new Hwk2Pet();

Hwk2Pet pet3 = new Hwk2Pet();

Hwk2Pet pet4 = new Hwk2Pet();

pet2.setPetName("Cat1");

pet3.setPetName("Dog1");

pet4.setPetName("Parrot1");

pet2.setPetType("Cat");

pet3.setPetType("Dog");

pet4.setPetType("Parrot");

pet2.setYearBorn(2014);

pet3.setYearBorn(2016);

pet4.setYearBorn(2013);

System.out.println("Pet 1:");

pet1.printData();

System.out.println("Pet 2:");

pet2.printData();

System.out.println("Pet 3:");

pet3.printData();

System.out.println("Pet 4:");

pet4.printData();

}

}

OUTPUT:

Pet 1:

Pet Name:

Pet Type:

Pet Year Born:0

Pet 2:

Pet Name:Cat1

Pet Type:Cat

Pet Year Born:2014

Pet 3:

Pet Name:Dog1

Pet Type:Dog

Pet Year Born:2016

Pet 4:

Pet Name:Parrot1

Pet Type:Parrot

Pet Year Born:2013

public class Hwk2Square {

private int length;

/**

* @param len

*/

public Hwk2Square(int len) {

// TODO Auto-generated constructor stub

this.length = len;

}

/**

* @return the length

*/

public int getLength() {

return length;

}

/**

* @param length

* the length to set

*/

public void setLength(int length) {

this.length = length;

}

/**

* method returns the area of the square

*

* @return

*/

public double computeArea() {

return getLength() * getLength();

}

/**

* method returns the perimeter of the square

*

* @return

*/

public double computePerimeter() {

return 4 * getLength();

}

/**

* display the contents of each the instance variables in a nice format

*/

public void printData() {

System.out.println("Length of Side:" + getLength());

}

}

public class Hwk2SquareTester {

public static void main(String[] args) {

// constructs four different Square objects

Hwk2Square square1 = new Hwk2Square(3);

Hwk2Square square2 = new Hwk2Square(4);

Hwk2Square square3 = new Hwk2Square(5);

Hwk2Square square4 = new Hwk2Square(6);

// SOP the square’s length

square1.printData();

square2.printData();

square3.printData();

square4.printData();

// SOP the square’s area

System.out.println("Area of Square 1:" + square1.computeArea());

System.out.println("Area of Square 2:" + square2.computeArea());

System.out.println("Area of Square 3:" + square3.computeArea());

System.out.println("Area of Square 4:" + square4.computeArea());

// SOP the square’s perimeter

System.out.println("Perimeter of Square 1:"

+ square1.computePerimeter());

System.out.println("Perimeter of Square 2:"

+ square2.computePerimeter());

System.out.println("Perimeter of Square 3:"

+ square3.computePerimeter());

System.out.println("Perimeter of Square 4:"

+ square4.computePerimeter());

}

}

OUTPUT:

Length of Side:3

Length of Side:4

Length of Side:5

Length of Side:6

Area of Square 1:9.0

Area of Square 2:16.0

Area of Square 3:25.0

Area of Square 4:36.0

Perimeter of Square 1:12.0

Perimeter of Square 2:16.0

Perimeter of Square 3:20.0

Perimeter of Square 4:24.0