For this assignment, you will predict the output of Java code that includes a static (class) variable accessed by a static method. Completing this assignment will help you understand the utility of using static variables and methods, as well as recognize the syntax required to create static variables and methods.

Read through the linked Java code carefully.

Predict the result of running the Java code; specifically, what text you think will appear on the console after running the Java code. Can you think of other uses for static variables and methods? In other words, can you think of other situations in which you might want all instances of a class to be able to access a single class variable?

Below is what is coming from the "Resource Document-Static Variable and Methods"

1.What is the output of the program as it is written? (Program begins on p. 2; play close attention to highlighted material.)

2.Can you think of other uses for static variables and methods? In other words, can you think of other situations in which you might want all instances of a class to be able to access a single class variable?

Linked Java code:

/**********************************************************************
* Program: PRG/421 Week 1 Assignment
* Purpose: Static (class) variables and methods
* Programmer: Iam A. Student
* Class: PRG/421r13, Java Programming II
* Instructor:
* Creation Date: December 13, 2017
*
* Comments:
*
* This code demonstrates using a static variable (and static getter method)
* to keep track of the # of instances created. Static variables are useful
* when you want to store data that is the same for all instances of a class
* (as opposed to data that will be different for each instance).
* **********************************************************************/

package week2recommended;
// Animal is an abstract class because "animal" is conceptual
// for our purposes. We can't declare an instance of the Animal class,
// but we will be able to declare an instance of a concrete class
// that derives from the Animal class.
abstract class Animal {

// All animals have a name, so store that info here in the superclass.
// And make it private so that other programmers have to use the
// getter method to access the name of an animal.
private final String animalName;

// We are making the variable numAnimalsCreated a static
// variable because it relates to all instances of a class. The
// value of this variable will not change from instance to
// instance the way an instance variable would.
private static int numAnimalsCreated = 0;

// One-argument constructor requires a name.
public Animal(String aName) {
numAnimalsCreated++;
animalName = aName;
}

// Return the name of the animal when requested to do so via this
// getter method, getName().
public String getName() {
return animalName;
}

// Public “getter” method returns the number of all animals created.
public static int getNumberAnimals() {
return numAnimalsCreated;
}

// Declare the makeSound() method abstract, as we have no way of knowing
// what sound a generic animal would make (in other words, this
// method MUST be defined differently for each type of animal,
// so we will not define it here--we will just declare a placeholder
// method in the animal superclass so that every class that derives from
// this superclass will need to provide an override method
// for makeSound()).
public abstract String makeSound();
};

// Create a concrete subclass named "Dog" that inherits from Animal.
// Because Dog is a concrete class, we can instantiate it.
class Dog extends Animal {

// This constructor passes the name of the dog to
// the Animal superclass to deal with.
public Dog(String nameOfDog) {
super(nameOfDog);
}

// This method is Dog-specific.
@Override
public String makeSound() {
return ("Woof");
}
}

// Create a concrete subclass named "Cat" that inherits from Animal.
// Because Cat is a concrete class, we can instantiate it.
class Cat extends Animal {

// This constructor passes the name of the cat on to the Animal
// superclass to deal with.
public Cat(String nameOfCat) {
super(nameOfCat);
}

// This method is Cat-specific.
@Override
public String makeSound() {
return ("Meow");
}

}

class Bird extends Animal {

// This constructor passes the name of the bird on to the Animal
// superclass to deal with.
public Bird (String nameOfBird) {
super(nameOfBird);
}

// This method is Bird-specific.
@Override
public String makeSound() {
return ("Squawk");
}

}

public class Week2Recommended {
public static void main(String[] args) {

// Create an instance of the Dog class, passing it the name "Spot."
// The variable aDog that we create is of type Animal.
Animal aDog = new Dog("Spot");

// Create an instance of the Cat class, passing it the name "Fluffy."
// The variable aCat that we create is of type Animal.
Animal aCat = new Cat("Fluffy");

// Create an instance of (instantiate) the Bird class.
Animal aBird = new Bird("Tweety");

// We call getNumberAnimals to return the number of animals created.
// Because this is a static (class) variable,
// there is only one variable for all of the instances of Animal;
// the variable does not vary
// based on instance, the way an instance variable does. So we expect the
// same value to be returned, no matter which instance we use to return the contents
// of the static variable.

System.out.println("nUsing an instance of Bird to get static number of all animals instantiated: " + aBird.getNumberAnimals());
System.out.println("Using an instance of Cat to get static number of all animals instantiated: " + aCat.getNumberAnimals());
System.out.println("Using an instance of Dog to get static number of all animals instantiated: " + aDog.getNumberAnimals());
}
}
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.