Code Shell – DogVer2.java /*****************************************************
ID: 3938192 • Letter: C
Question
Code Shell – DogVer2.java
/*********************************************************
// DogVer2.java
//
// This class contains data and methods related to the
// names and ages of dogs.
//*********************************************************
public class DogVer2
{
// same variables to store the name/age of the dog
// as Lab 8
// IMPORTANT: Declare static variable here
//Methods are below. They should be public
//------------------------------------------------------
// Constructor: Sets up Dog object with specified data.
//------------------------------------------------------
public DogVer2(String name) // Complete constructor
//header
{
// IMPORTANT: Use this reference
// IMPORTANT: increment static variable here
}
//------------------------------------------------------
// Create second constructor
//------------------------------------------------------
public DogVer2() // Yup, this is the header
{
// IMPORTANT: Use this reference
// IMPORTANT: increment static variable here
}
// DON’T make changes to name accessor, age accessor,
// name mutator age mutator, personYears or toString.
// Include methods as shown in Lab 8 shell
//------------------------------------------------------
// Returns the number of instances created
//------------------------------------------------------
// public static int // name method and complete header
{
}
}
Code Shell – KennelVer2.java
//*********************************************************
// KennelVer2.java
//
// This is the driver program for the DogVer2 class
//*********************************************************
// import Scanner class
public class KennelVer2
{
//------------------------------------------------------
// Creates and exercises some Dog objects.
//------------------------------------------------------
public static void main (String[] args)
{
// Create objects - test first constructor
DogVer2 d1 = new DogVer2("Cooper", 1);
// create more objects using this constructor
// create an object using 2nd constructor
//Scanner scan = new Scanner(System.in);
String dogname;
int dogage;
// Input information for the new dog
// Update name and age for the new dog
// Test toString
System.out.println(d1);
// create corresponding statements
System.out.println();
// Retrieve a name: call getName from an output
// statement
System.out.print(/*call get name + */
" also answers to the name ");
// Update a name
String newname = "Kruger";
// call setName here
// Print updated name
// Retreive and update an age
int newage;
newage = /* call getAge*/ + 1;
// call setAge here
System.out.println();
// Print new age for the dog. Call personYears here
// Print number of dogs
}
}
Sample Output (new output in red)
Enter the name of the new dog: Sir Fluffington
Enter the age of the new dog: 4
Dog: Cooper Age: 1 Person-Years: 7
Dog: Pico Age: 2 Person-Years: 14
Dog: Remi Age: 7 Person-Years: 49
Dog: Kroogs Age: 3 Person-Years: 21
Dog: Sir Fluffington Age: 4 Person-Years: 28
Kroogs also answers to the name Kruger.
Happy Birthday, Remi!
You are now 8, which is 56 years old in person-years.
This code provided info on 5 dogs.
Explanation / Answer
MODIFIED DOG CLASS:
public class DogVer2
{
string name;
int age;
static int counter;
public DogVer2(String name, int age)
{
this.name=name;
this.age=age;
counter++;
}
public DogVer2() // Yup, this is the header
{
this.name=' ';
this.age= 0;
counter++;
}
public String getName()
{
return Name;
}
public int getage()
{
return Age;
}
public static int getNumOfInstances()
{
return counter;
}
}
MODIFIED KENNEL CLASS
//*********************************************************
// KennelVer2.java
//
// This is the driver program for the DogVer2 class
//*********************************************************
// import Scanner class
import java.util.Scanner;
public class KennelVer2
{
//------------------------------------------------------
// Creates and exercises some Dog objects.
//------------------------------------------------------
public static void main (String[] args)
{
// Create objects - test first constructor
DogVer2 d1 = new DogVer2("Cooper", 1);
DogVer2 d2 = new DogVer2("Pico", 2);
DogVer2 d3 = new DogVer2("Remi ", 7);
DogVer2 d4 = new DogVer2("Kroogs", 3);
// create more objects using this constructor
DogVer2 d5 = new DogVer2();
// create an object using 2nd constructor
Scanner scan = new Scanner(System.in);
System.out.println("Enter the name of the new dog");
String dogname=scan.next();
System.out.println(dogname);
System.out.println("Enter the age of the new dog")
int dogage=scan.next();
System.out.println(dogage);
scan.close();
// Input information for the new dog
// Update name and age for the new dog
// Test toString
System.out.println(d1);
// create corresponding statements
System.out.println(d2);
System.out.println(d3);
System.out.println(d4);
System.out.println(d5);
// Retrieve a name: call getName from an output
// statement
System.out.print(d4.getName() +
" also answers to the name ");
// Update a name
String newname = "Kruger";
System.out.print(d4.setName(newname));
// call setName here
// Print updated name
// Retreive and update an age
int newage;
newage = d3.getAge() + 1;
// call setAge here
d3.setAge(newage);
System.out.println("Happy Birthday, Remi!"/n);
// Print new age for the dog. Call personYears here
System.out.println("You are now" +newage + ", which is" + d3.getpersonyears() + "years old in person-years.");
// Print number of dogs
int numdogs;
numdogs=DogVer2.getNumOfInstances();
System.out.println("This code provided info on" + numdogs + "days");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.