1. Consider the following interface:
interface Duty
{
public String getDuty();
}
a. Write a class called Student which implements Duty. Class
Student adds 1 data field, id, and 2
methods, getId and setId, along with a 1-argument constructor. The
duty of a Student is to study
40 hours a week.
b. Write a class called Professor which implements Duty. Class
Professor adds 1 data field, name,
and 2 methods, getName and setName, along with a 1-argument
constructor. The duty of a
Professor is to return homework on time.
c. Complete the following program:
public class DutyApp
{
//Desc: A program which allows the user to create either a Student
or a Professor.
// The program displays a menu asking for 1. Student, 2. Professor,
3. Quit. The
// user enters 1 or 2, followed by the id or name of the person.
The program prints the duty
// of the person, and then asks the user to enter 1, 2, or 3 again.
The program will continue
// until the user selects 3 to quit.
//Input: The user enters 1 or 2 followed by the id or name of the
person via the keyboard.
//Output:For each person entered by the user, the duty of the
person displayed on the screen
public static void main(String[] args)
{
Scanner f=new Scanner(System.in);
int response =0;
Duty person;
while (true)
{
System.out.print("1. Student, 2. Professor, 3. Quit:
");
response =f.nextInt();
f.nextLine(); //WHY????
if (response==3) break;
if (response==1) person =getStudent(f);
else person =getProfessor(f);
System.out.println(person.getDuty());
}
}
//Input: The user enters the id of the Student via the
keyboard.
//Post: One line read from f
//Return: The Student with the input id.
// ** you need to write this method getStudent
//Input: The user enters the name of the Professor via the
keyboard.
//Post: One line read from f
//Return: The Professor with the input name.
// ** you need to write this methof getProfessor
}
Note:
Put all 4 classes in the same file (Duty, Student, Professor, and
DutyApp)
You CANNOT modify the above main method.
Hand in:
DutyApp.java.
import java.util.Scanner;
interface Duty
{
public String getDuty();
}
class Student implements Duty {
String id;
public Student(String id) {
this.id = id;
}
String getId() { return id; }
void setId(String id) { this.id=id; }
public String getDuty() {
return "study 40
hours a week";
}
}
class Professor implements Duty {
private String name;
public Professor(String name){
this.name =
name;
}
public String getName() {
return
this.name;
}
public void setName(String name) {
this.name =
name;
}
public String getDuty() {
return "Return
homework on time";
}
}
public class DutyApp {
//Desc: A program which allows the user to
create either a Student or a Professor.
// The program displays a menu asking for
1. Student, 2. Professor, 3. Quit. The
// user enters 1 or 2, followed by the id
or name of the person. The program prints the duty
// of the person, and then asks the user to
enter 1, 2, or 3 again. The program will continue
// until the user selects 3 to quit.
//Input: The user enters 1 or 2 followed by
the id or name of the person via the keyboard.
//Output:For each person entered by the
user, the duty of the person displayed on the screen
public static void main(String[] args)
{
Scanner f=new
Scanner(System.in);
int response
=0;
Duty person;
while (true)
{
System.out.print("1.
Student, 2. Professor, 3. Quit: ");
response
=f.nextInt();
f.nextLine();
// So that the new line gets cleared from input buffer
if
(response==3) break;
if
(response==1) person = getStudent(f);
else
person = getProfessor(f);
System.out.println(person.getDuty());
}
}
//Input: The user enters the id of the
Student via the keyboard.
//Post: One line read from f
//Return: The Student with the input
id.
// ** you need to write this method
getStudent
public static Student getStudent(Scanner
sc) {
System.out.println("Enter
student id: ");
String id =
sc.nextLine();
return new
Student(id);
}
//Input: The user enters the name of the
Professor via the keyboard.
//Post: One line read from f
//Return: The Professor with the input
name.
// ** you need to write this methof
getProfessor
public static Professor
getProfessor(Scanner sc) {
System.out.println("Enter
Professor name: ");
String name =
sc.nextLine();
return new
Professor(name);
}
}
**************************************************
Thanks for your question. We try our best to help you with detailed
answers, But in any case, if you need any modification or have a
query/issue with respect to above answer, Please ask that in the
comment section. We will surely try to address your query ASAP and
resolve the issue.
Please consider providing a thumbs up to this
question if it helps you. by Doing that, You will help other
students, who are facing similar issue.
Get Answers For Free
Most questions answered within 1 hours.