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 very reason we have to
declare the class FarmAnimalBase as an abstract
class. Make three classes, Cat,
Dog, and Cow, that inherit from
FarmAnimalBase. Each of the inherited classes has
a constructor that initializes the value of the instance variable.
Each of the inherited classes also implements a method (prototype:
public String talk();), which contains only one
line of code. For class Cat, return "Meow!
Meow!"; for class Dog, return
"Arf! Arf!"; and for class Cow,
return "Moo! Moo!";
Supply a test program FarmAnimalTest that tests these classes and methods. Specifically, create three objects, one each from the classes Cat, Dog, and Cow using the names - "Missy", "Lassy", and "Nossy". Then put all these three objects into an ArrayList<FarmAnimal> farmAnimals of FarmAnimal objects. And finally, show the farm animal names and sounds they make. The output should look exactly like the following:
Missy: Meow! Meow! Lassy: Arf! Arf! Nossy: Moo! Moo! |
Submit all six source code files: FarmAnimal.java, FarmAnimalBase.java, Cat.java, Dog.java, Cow.java, and FarmAnimalTest.java
All the required files are given below in their respective tables.
FarmAnimal.java |
public interface FarmAnimal { } |
FarmAnimalBase.java |
public abstract class FarmAnimalBase implements
FarmAnimal{ } |
Cat.java |
public class Cat extends FarmAnimalBase { @Override } |
Dog.java |
public class Dog extends FarmAnimalBase{ public Dog(String name) } |
Cow.java |
public class Cow extends FarmAnimalBase{ public Cow(String name) } |
FarmAnimalTest.java |
import java.util.ArrayList; public class FarmAnimalTest { } |
OUTPUT
Define an interface FarmAnimal that contains two methods: getName() and talk(), each returns a string. Declare...
PRG/421 Week One Analyze Assignment – Analyzing a Java™Program 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...
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 | |...
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...
Define an interface named Shape with a single method named area that calculates the area of the geometric shape: public double area(); Next, define a class named Circle that implements Shape. The Circle class should have an instance variable for the radius, a constructor that sets the radius, an accessor and a mutator method for the radius, and an implementation of the area method. Define another class named Rectangle that implements Shape. The Rectangle class should have instance variables...
Suppose we have the following Java Interface: public interface Measurable { double getMeasure(); // An abstract method static double average(Measurable[] objects) { // A static method double sum = 0; for (Measurable obj : objects) { sum = sum + obj.getMeasure(); } if (objects.length > 0) { return sum / objects.length; } else { return 0; } } } Write a class, called Person, that has two instance variables, name (as...
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...
Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings. Create another class HourlyEmployee that inherits from the abstract Employee class. HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...
Create an abstract class Employee. Your Employee class should include the following attributes: First name (string) Last name (string) Employee id (string) Employee home street address (string) Employee home city (string) Employee home state (string) Write a constructor to initialize the above Employee attributes. Create an abstract method called earnings. Create another class HourlyEmployee that inherits from the abstract Employee class. HourEmployee must use the inherited parent class variables and add in attributes HourlyRate and HoursWorked. Your HourEmployee class should...
Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue Access modifier: public Parameters: none Return type: T (parameterized type) Name: enqueue Access modifier: public Parameters: element (data type T, parameterized type) Return type: void Step 2 Develop the following class: Class Name: Queue Node<T> Access Modifier: public Instance variables Name: info Access modifier: private Data type: T (parameterized type) Name: link Access modifier:...
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....