Code Shell DogVer2java DogVer2java This class contains

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.

DogiKennel This exercise is based on the class definitions that were submitted in the previous lab (Lab 8) Modify the Dog class exercise as follows (and save as DogVer2): 1. Modify the class constructor using the this Make sure the constructor matches the new class name! 2. Create a static variable to keep track of the number of instance created The static variable should be incremented in here, as shown in the Slogan example 3. Create an additional accessor method to get the number of instances created This method should be static 4. Create an overloaded constructor that takes no parameters and assigns a blank space to name, and 0 to age. e The static variable should be incremented in here as well. Modify the Kennel class exercise as follows (and save as KenneVer2): 1. Create 4 objects of the DogVer2 class You can simply update the class used and retain the same information 2. Create a new object that uses the overloaded constructor. In order words, create a new dog object, but don\'t provide any values for name and age. 3. Since your new object doesn\'t have any useful information, we should ask the user to input the information for the new dog Don\'t forget to import the Scanner class! 4. Use the name and age mutator methods to update the name of age of that object. 5. Use the toString method to verify that the information for the new dog, as well as the info for the previous dogs are stored 6. Invoke the getcount method to verify the number of dogs\' info (objects) created

Solution

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\");
}
}

Code Shell – DogVer2.java /********************************************************* // DogVer2.java // // This class contains data and methods related to the /
Code Shell – DogVer2.java /********************************************************* // DogVer2.java // // This class contains data and methods related to the /
Code Shell – DogVer2.java /********************************************************* // DogVer2.java // // This class contains data and methods related to the /
Code Shell – DogVer2.java /********************************************************* // DogVer2.java // // This class contains data and methods related to the /
Code Shell – DogVer2.java /********************************************************* // DogVer2.java // // This class contains data and methods related to the /

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site