Question

PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...

PRG/421 Week One Analyze Assignment –

Analyzing a JavaProgram Containing Abstract and Derived Classes

1.    What is the output of the program as it is written? (Program begins on p. 2)

2. Why would a programmer choose to define a method in an abstract class (such as the Animal constructor method or the getName()method in the code example) vs. defining a method as abstract (such as the makeSound()method in the example)?

/**********************************************************************

*           Program:          PRG/421 Week 1 Analyze Assignment

*           Purpose:         Analyze the coding for an abstract class

*                                   and two derived classes, including overriding methods

*           Programmer:    Iam A. Student

*           Class:               PRG/421r13, Java Programming II

* Comments:

* Notice that in the abstract Animal class shown here, one method is

* concrete (the one that returns an animal's name) because all animals can

* be presumed to have a name. But one method, makeSound(), is declared as

* abstract, because each concrete animal must define/override the makeSound() method

* for itself--there is no generic sound that all animals make.

/**********************************************************************

package mytest;

// 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 any 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;

// One-argument constructor requires a name.

public Animal(String aName) {

animalName = aName;

}

// Return the name of the animal when requested to do so via this

// getter method, getName().

public String getName() {

return animalName;

}

// 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 MyTest {

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

//Exercise two different methods of the aDog instance:

// 1) getName() (which was defined in the abstract Animal class)

// 2) makeSound() (which was defined in the concrete Dog class)

System.out.println("The dog named " + aDog.getName() + " will make this sound: " + aDog.makeSound());

//Exercise two different methods of the aCat instance:

// 1) getName() (which was defined in the abstract Animal class)

// 2) makeSound() (which was defined in the concrete Cat class)

System.out.println("The cat named " + aCat.getName() + " will make this sound: " + aCat.makeSound());

System.out.println("The bird named " + aBird.getName() + " will make this sound: " + aBird.makeSound());

}

}

0 0
Add a comment Improve this question Transcribed image text
Know the answer?
Add Answer to:
PRG/421 Week One Analyze Assignment – Analyzing a Java™Program Containing Abstract and Derived Classes 1.    What is...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • This is the question about object-oriend programming(java) please show the detail comment and prefect code of...

    This is the question about object-oriend programming(java) please show the detail comment and prefect code of each class, Thank you! This question contain 7 parts(questions) Question 1 Create a class a class Cat with the following UML diagram: (the "-" means private , "+" means public) +-----------------------------------+ | Cat | +-----------------------------------+ | - name: String | | - weight: double | +-----------------------------------+ | + Cat(String name, double weight) | | + getName(): String | | + getWeight(): double | |...

  • Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare...

    Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare an abstract class FarmAnimalBase that implements the interface FarmAnimal. In the class FarmAnimalBase, define a private final instance variable name (type: String), a public constructor that initializes the value of the instance variable name, and a public method getName() that returns the value of the instance variable name. Note that we do not implement the method talk() declared in the interface FarmAnimal, that’s the...

  • Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and...

    Java file Name Dog Classes and Methods Create a constructor that incorporates the type, breed, and name variables (do not include topTrick). Note: The type refers to what the breed typically does; for example, a corgi would be a “cattle herding dog.” A Shiba Inu would be a “hunting dog.” Create the setTopTrick() mutator method Dog is parent class Corgi and Driver are subclasses Complete the Corgi class: Using the UML Class diagram, declare the instance variables. Create the two...

  • Using your Dog class from earlier this week, complete the following: Create a new class called...

    Using your Dog class from earlier this week, complete the following: Create a new class called DogKennel with the following: Instance field(s) - array of Dogs + any others you may want Contructor - default (no parameters) - will make a DogKennel with no dogs in it Methods: public void addDog(Dog d) - which adds a dog to the array public int currentNumDogs() - returns number of dogs currently in kennel public double averageAge() - which returns the average age...

  • A Java Program Purpose:         Work with Abstract classes. Purpose:         Abstract classes are import because they ensure...

    A Java Program Purpose:         Work with Abstract classes. Purpose:         Abstract classes are import because they ensure than any children which inherit from it must implement certain methods. This is done to enforce consistency across many different classes. Problem:        Implement a class called Student. This class needs to be abstract and should contain a String for the name, and a String for the major (and they should be private). The class must have a constructor to set the name and major....

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will...

    Create an abstract class “Appointment” that represents an event on a calendar. The “Appointment” class will have four instance variables: • An instance variable called “year” which will be of type int • An instance variable called “month” which will be of type int • An instance variable called “day” which will be of type int • An instance variable called “description” which will be of type String The “Appointment” class must also implement the following methods: • A getter...

  • In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program...

    In Java Code Needed is Below the Question Using Program 3: Encapsulating Dogs, modify this program to create a database of dogs of your own. You will also include File I/O with this program. You will create 3 classes for this assignment. The first class: Create a Class representing the information that is associated with one item (one dog) of your database. Name this class Dog. Using Lab 3, this will include the information in regard to one dog. The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT