Please use java language and comment as much as you can. I am learning. Create a
ID: 3604712 • Letter: P
Question
Please use java language and comment as much as you can. I am learning.
Create an interface called Speakable that contains a void speak() method. Define 2 classes, Man and Woman, which are subclasses of the Human class, and implement the interface Speakable. The speak() method should be abstract at the superclass level, and defined at the subclass levels.
Define only 1 attribute at the Human class level: name. At the Woman subclass, add the attribute favoriteStore, and at the Man subclass, add the attribute favoriteSport. In the speak() method of the Woman subclass, print out “Hi, my name is “ and the name attribute, and say “my favorite store is “ and the name of the store. In the speak() method of the Man subclass, print out “Hello, my name is “ and the name attribute, and say “my favorite sport is “ and the name of the sport.
Don’t forget to define a constructor at the superclass level and at the subclass levels. Also, don’t forget the toString() method at the superclass and at each subclass.
In the driver class called Conversations, create an ArrayList called people, of type Speakable. In a method called createPeople(), which is called from main, add 3 Woman and 3 Man objects to the people arrayList. Ask the user to enter all the attributes of each object before creating each Woman or Man object. Place each object in the people arrayList.
In another method called processConversations(), also called from main, use a for-loop to iterate over Speakable objects, and invoke the speak( ) method.
Hint: To create an arrayList of Speakable objects, code the following:
ArrayList<Speakable> people = new ArrayList<Speakable>( );
Explanation / Answer
import java.util.*;
interface Speakable //interface
{
public void speak();
}
abstract class Human //base abstract class
{
private String name;
public Human(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
public String toString()
{
return " Human : name :"+name;
}
}
class Man extends Human implements Speakable //derived class
{
private String favoriteSport;
public Man(String name,String favoriteSport)
{
super(name); //sending argument to base class constructor
this.favoriteSport = favoriteSport;
}
public void speak()
{
System.out.println("Hello, my name is "+getName() + ", my favorite sport is " +favoriteSport);
}
public String toString()
{
return " Man :"+super.toString()+ "Favourite Sport : "+favoriteSport;
}
}
class Woman extends Human implements Speakable
{
public String favoriteStore;
public Woman(String name,String favoriteStore)
{
super(name);
this.favoriteStore = favoriteStore;
}
public void speak()
{
System.out.println("Hi, my name is "+getName() + ", my favorite store is " +favoriteStore);
}
public String toString()
{
return " Woman :"+super.toString()+ "Favourite Store : "+favoriteStore;
}
}
class Conversations
{
public static void main (String[] args)
{
ArrayList<Speakable> people = new ArrayList<Speakable>( );
createPeople(people);
processConversations(people);
}
public static void createPeople(ArrayList<Speakable> people)
{
Scanner sc = new Scanner(System.in);
String name,favoriteSport,favoriteStore;
System.out.println(" Enter name of 3 men and their favorite sport : ");
for(int i = 0;i<3;i++)
{
name = sc.next();
favoriteSport = sc.next();
Man m = new Man(name,favoriteSport);
people.add(m);
}
System.out.println(" Enter name of 3 women and their favorite store : ");
for(int i = 0;i<3;i++)
{
name = sc.next();
favoriteStore = sc.next();
Woman w = new Woman(name,favoriteStore);
people.add(w);
}
}
public static void processConversations(ArrayList<Speakable> people)
{
for(Speakable s: people)
{
s.speak();
}
}
}
output: