Question

1. Consider the following interface: interface Duty { public String getDuty(); } a. Write a class...

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.

Homework Answers

Answer #1

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.

Know the answer?
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
Similar Questions
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer...
Question: Squares. Write a program class named SquareDisplay that asks the user for a positive integer no greater than 15. The program should then display a square on the screen using the character ‘X’. The number entered by the user will be the length of each side of the square. For example, if the user enters 5, the program should display the following:       XXXXX       XXXXX       XXXXX       XXXXX       XXXXX INPUT and PROMPTS. The program prompts for an integer as follows: "Enter...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
Use the class definition below to answer the following questions. public class TrafficLight { String stopLight...
Use the class definition below to answer the following questions. public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } 1. How many field attributes are there in the TrafficLight class? 2. Name a field attribute for this class. 3. What is the name of the method that is an accessor? 4. What is the name of the method that is...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String...
please fix code to delete by studentname import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String[] students = new String[1];     private int numberOfStudents;     public COurseCom666(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public void addStudent(String student) {         if (numberOfStudents == students.length) {             String[] a = new String[students.length + 1];            ...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while...
4.2.2 Basic while loop with user input. JAVA Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may test code with different test values. This activity will perform three tests, with user input of 9, 5, 2, -1, then with user input of 0, -17, then with user input of 0 1 0 -1. See "How to Use zyBooks". Also note: If the submitted code has an...
For the following class Book: 1- Write a default constructor to give default values (title= Intro...
For the following class Book: 1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables. 2 - Write a parameterized constructor to allow the user to initialize the class variables. 3- Write getter & setter for each variable of this class. 4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and...
Part 1 Given the following code: public class MyClass { private double score; private String studentID;...
Part 1 Given the following code: public class MyClass { private double score; private String studentID; //code of the class ..... } //end of MyClass Code a default constructor and an overloaded constructor of MyClass that will assign values to its two instance fields: Please write in JAVA Part 2 Continue from question about, code an mutator of instance field called studentID:
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the...
JAVA public class Purchase { private String name; private int groupCount; //Part of price, like the 2 in 2 for $1.99. private double groupPrice; //Part of price, like the $1.99 in 2 for $1.99. private int numberBought; //Total number being purchased. public Purchase () { name = "no name"; groupCount = 0; groupPrice = 0; numberBought = 0; } public Purchase (String name, int groupCount, double groupPrice, int numberBought) { this.name = name; this.groupCount = groupCount; this.groupPrice = groupPrice; this.numberBought...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight...
Use the class definition below to answer the following questions. [Total 8 Marks] public class TrafficLight { String stopLight = "red"; String waitLight; String goLight; public void setStopLight(String colour) { stopLight = colour; } public String getGreenLight() { return goLight; } } 1 :How many field attributes are there in the TrafficLight class? 2 :Name a field attribute for this class. 3 :What is the name of the method that is an accessor? 4 :What is the name of the...
Write a program display the following menu: Ohms Law Calculator 1. Calculate Resistance in Ohms 2....
Write a program display the following menu: Ohms Law Calculator 1. Calculate Resistance in Ohms 2. Calculate Current in Amps 3. Calculate Voltage in volts 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for voltage in Volts and the current in Amps. Use the following formula: R= E/i Where: E= voltage in volts I= current in amps R= resistance in ohms If the user enters 2 the program should ask for the voltage...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT