Question
Abstract Classes and Interfaces.
Write the code for all the necessary classes and/or interfaces for a solution to the problem below. Focus on class structure and interaction. You may implement your solution however you wish, but you will be graded on the appropriateness of your solution to the requirements. Note the use of capit and bold for clarification in the problem. You may use whatever constructors or additional methods you wish. - Define a structure that can represent Animals. - A general Animal s hould not be able to be instantiated and has a permanent name for its lifetime. Animals have two behaviors; they can speak() and they can move ). By default, when an animal moves, the message This animal moves forward is displayed. By default, when an animal speaks, the message This animal speaks is displayed. - Define also two classes, Goose and Lynx, that are Animals. Both Goose and Lynx behave such that where animal is displayed in speak( ) or move(), goose or lynx is displayed by the appropriate classes. Define the default constructors to accomplish this Finally, any instance of Goose can fly(), just as any Flying object can - An Airplane is also a Flying object. Define the Airplane class such that it is Flying and make sure that any instance of Goose is also Flying. The specific behaviors when instances of either class fly() should display in the message: the class name and name of object soars, wings flapping for a Goose or th message the class name and name of object soars, engines running for an Airplane.. You may use getName).get.Class() Instances of either Goose or Airplane should be able to be stored in a variable of type Flying All classes should include default constructors and if needed overloaded constructors. - Hand in all abstract classes, classe different behaviors for a Lynx, Goo output. s and interfaces along with a client class that demonstrates the se and Airplane. You also must provide a screen shot of your
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// ------ Animal.java --------------

// This is Animal abstract class which defines default behavior of methods.

public abstract class Animal {   

   public void speak() {

       System.out.println("This animal speaks.");

   }   

   public void move() {

       System.out.println("This animal moves forward.");

   }

}

// ----------- Flying.java -------------

// This is declaration of flying behavior.

public interface Flying {

   void fly();

}

// -------- Lynx.java -----------

// This Lynx extends behaviors of animal and override them.

public class Lynx extends Animal {  

   public Lynx() {

   }

   public void speak() {

       System.out.println("Lynx speaks.");

   }  

   public void move() {

       System.out.println("Lynx moves forward.");

   }

}

//------------ Goose.java --------------

// This Goose is extended version of animal and it implements Flying behavior.

public class Goose extends Animal implements Flying {

   public Goose() {

   }

   public void speak() {

       System.out.println("Goose speaks.");

   }  

   public void move() {

       System.out.println("Goose moves forward.");

   }  

   @Override

   public void fly() {

       System.out.println(getClass().getSimpleName() + " wings flapping.");  

   }

}

// --------- Airplane.java ---------

// This implements flying behavior.

public class Airplane implements Flying {

   public Airplane() {  

   }

   @Override

   public void fly() {

       System.out.println(getClass().getSimpleName() + " engines running.");

   }

}

//---------- DemoApplication.java --------------

// This class demonstrates the object creation and assignment.

public class DemoApplication {

   public static void main(String[] args) {

      

       Animal animal1 = new Goose();

       animal1.speak();

      

       Animal animal2 = new Lynx();

       animal2.speak();

      

       Flying flyingObject1 = new Goose();

       flyingObject1.fly();

      

       Flying flyingObject2 = new Airplane();

       flyingObject2.fly();

      

   }

}

// -------------- Output -------------------

Goose speaks.
Lynx speaks.
Goose wings flapping.
Airplane engines running.

Add a comment
Know the answer?
Add Answer to:
Abstract Classes and Interfaces. Write the code for all the necessary classes and/or interfaces for a...
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
  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • I need some help i need to do this in C# Objectives: • Create an application...

    I need some help i need to do this in C# Objectives: • Create an application that uses a dictionary collection to store information about an object. • Understanding of abstract classes and how to use them • Utilize override with an abstract class • Understanding of Interfaces and how to use them • Implement an Interface to create a “contract” between classes. • Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the...

  • IN C# Objectives: Create an application that uses a dictionary collection to store information ...

    IN C# Objectives: Create an application that uses a dictionary collection to store information about an object. Understanding of abstract classes and how to use them Utilize override with an abstract class Understanding of Interfaces and how to use them Implement an Interface to create a “contract” between classes. Compare and contrast inheritance and interfaces. Instructions: Interface: Create an interface called ITrainable which contains the following: Dictionary<string, string> Behaviors{ get; set; } string Perform(String signal); string Train(String signal, string behavior);...

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment:...

    MUST WRITE IN C++ Objective: Learn how to design classes using abstract classes and inheritance Assignment: In cryptography, encryption is the process of encoding a message or information in such a way that only authorized parties can access it. In this lab you will write a program to decode a message that has been encrypted using two different encryption algorithms. Detailed specifications: Define an abstract class that will be the base class for other two classes. It should have: A...

  • Abstract classes and Interfaces problems 10. Explain one similarity and one difference between ab...

    Abstract classes and Interfaces problems 10. Explain one similarity and one difference between abstract classes and interfaces. 11. Consider the following declarations. public interface Shape{ int someFunction(Shape other); //other functions not shown } public class Square implements Shape {/*implementation not shown*/} Which of the following function headings of someFunction must be added to the declaration of the Square class such that it will satisfy the Shape interface? public int someFunction (Shape other) public int someFunction (Square other) public boolean someFunction(Object...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • JAVA CODING: (14 points) Abstract Class exercise You are given two abstract classes with no abstract...

    JAVA CODING: (14 points) Abstract Class exercise You are given two abstract classes with no abstract method in the NoGo.java file. The purpose of this exercise is to get familiar with abstract class and its object creation 4. Follow the steps below: (be sure common on each to score points) 1) 2) 3 points) Create a subclass Go1 with Nogo1 as its super class and create Go1() constructor 3) (2 points) Now inside the NoGo class create an instance of...

  • 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 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...

  • C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polym...

    C++ Programming Assignment S Mammal Lab This lab's goal is to give you some practice using inheritance, virtual functions, pointers, dynamic memory allocation, random numbers, and polymorphism. To complete the lab implement the following steps: Create a class called Mammal. All mammals have a weight and a name, so its data should be the mammal's weight and name. Provide a default constructor that sets the mammal's weight to 0 and name to null, and another constructor that allows the weight...

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