Answer:
Check the explanation
Step-by-step explanation:
Based on the above information, this the following resulting code.
The solution has namely one interface named Speaker, one class called Student and one Main class called StudentMain.
The StudentMain class just initialize various instances of the Student class with various classRank values and then subsequently calling in the speak() methods of all.
announce() method is called on one such instance of Student class to demonstrate its functioning.
Comments are added at required place to better help in understanding the logic.
Speaker.java
public interface Speaker {
abstract void speak();
abstract void announce(String str);
}
Student.java
public class Student implements Speaker {
/*
* Write the Student class so that it implements Speaker as follows.
The speak method will output "I am a newbie here" if the Student is a "Freshman",
"I like my school" if the Student is either a "Sophomore" or a "Junior",
or "I can not wait to graduate" if the student is a "Senior".
The announce method will output "I am a Student, here is what I have to say" followed by the String parameter on a separate line.
*/
String classRank;
// Based on the requirement taking in classRank String as a constructor parameter
public Student(String classRank) {
super();
this.classRank = classRank;
}
// Using the switch-case to code the above requirment
public void speak() {
switch(classRank) {
case "Freshman":
System.out.println("I am a newbie here");
break;
// This case would output the same result for the 2 case conditions
case "Sophomore":
case "Junior":
System.out.println("I like my school");
break;
case "Senior":
System.out.println("I can not wait to graduate");
break;
default:
System.out.println("Unknown classRank inputted");
}
}
//Based on the requirement, first line is printed first after second which is
// the inputted String parameter
public void announce(String str) {
System.out.println("I am a Student, here is what I have to say");
System.out.println(str);
}
}
StudentMain.java
public class StudentMain {
public static void main(String[] args) {
// Initializing all the Objects with required classRank
Student stu1=new Student("Freshman");
Student stu2=new Student("Sophomore");
Student stu3=new Student("Junior");
Student stu4=new Student("Senior");
Student stu5=new Student("Freshman");
//Calling the speak methods of the all the above create objects
stu1.speak();
stu2.speak();
stu3.speak();
stu4.speak();
stu4.speak();
stu4.announce("Wish u all the best");
}
}
Following is the output generated from the code run.