Question

In: Computer Science

Finish the following java question: Consider the following interface: interface Duty { public String getDuty(); }...

  1. Finish the following java question: Consider the following interface:

interface Duty

{ public String getDuty();

}

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

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

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

}

Solutions

Expert Solution

Here i write code according to your requirement, if you any issue or doubt in this code feel free to ask me or post the issue, i will update the code according to your need.

Note: here, you dont ask for print Student id or professor name, if you want post it.

Interface Duty


public interface Duty {
        
        public String getDuty();
}

Professor.java


public class Professor implements Duty {

        String name;
        
        public Professor(String name) {
                this.name = name;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        @Override
        public String getDuty() {
                return "The duty of a Professor is to return homework on time.";
        }

}

Student.java


public class Student implements Duty {

        int id;
        
        public int getId() {
                return id;
        }

        public void setId(int id) {
                this.id = id;
        }

        public Student(int id) {
                this.id = id;
        }

        @Override
        public String getDuty() {
                return "The duty of a Student is to study 40 hours a week.";
        }

}

DutyApp.java

import java.util.Scanner;

public class DutyApp {

        public static Duty getStudent(int id) {
                return new Student(id);
        }
        
        private static Duty getProfessor(String name) {
                return new Professor(name);
        }

        public static void main(String[] args) {
                Duty person = null;
                Scanner f = new Scanner(System.in);
                int response = 0;

                while (true) {
                        System.out.print("1. Student, 2. Professor, 3. Quit: \n");
                        response = f.nextInt();

                        if (response == 3)
                                break;

                        if (response == 1) {
                                System.out.println("Enter Student id : ");
                                int id = f.nextInt();
                                person = getStudent(id);
                                System.out.println(person.getDuty());
                        }
                        else if(response == 2) {
                            System.out.println("Enter Professor name : ");
                                String name = f.next();
                                person = getProfessor(name);
                                System.out.println(person.getDuty());
                        }
                        else
                                System.out.println("Wrong choice entered. Again try");
                        
                        System.out.println("");
                }
                f.close();
        }
}

Related Solutions

Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk();...
Consider the following interface: public interface Car{ public String getMake(); public void setMake(); public void honk(); public void crash(); public void drive(); } public interface Boat{ public String getMake (); public void setMake (); public void blast_horn(); public void sink(); public void move(); } 1. Create a concrete FamilyCar class from the Car interface.
from Big Java Early Objects 7th edition Question Declare an interface Filter as follows: public interface...
from Big Java Early Objects 7th edition Question Declare an interface Filter as follows: public interface Filter { boolean accept(Object x); } Modify the implementation of the Data class in Section 10.4 to use both a Measurer and a Filter object. Only objects that the filter accepts should be processed. Demonstrate your modification by processing a collection of bank accounts, filtering out all accounts with balances less than $1,000. Solve Exercise •• P10.6, using a lambda expression for the filter....
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** *...
JAVA PROGRAM: FINISH THE FOLLOWING METHOD IN THE CLASS BasicBioinformatics. public class BasicBioinformatics { /** * Calculates and returns the reverse complement of a DNA sequence. In DNA sequences, 'A' and 'T' * are complements of each other, as are 'C' and 'G'. The reverse complement is formed by * reversing the symbols of a sequence, then taking the complement of each symbol (e.g., the * reverse complement of "GTCA" is "TGAC"). * * @param dna a char array representing...
IN JAVA Step 1 Develop the following interface: Interface Name: ImprovedStackInterface Access Modifier: public Methods Name:...
IN JAVA Step 1 Develop the following interface: Interface Name: ImprovedStackInterface Access Modifier: public Methods Name: push Access modifier: public Parameters: item (data type T, parameterized type) Return type: void Throws: StackFullException Name: push Access modifier: public Parameters: item1 (data type T, parameterized type), item2 (data type T, parameterized type) Return type: void Throws: StackFullException Name: pop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Name: doublePop Access modifier: public Parameters: none Return type: void Throws: StackEmptyException Name:...
JAVA coding language: Consider the method getAllCharAsString: public statis String getAllCharAsString(String inputStr, char target) { }...
JAVA coding language: Consider the method getAllCharAsString: public statis String getAllCharAsString(String inputStr, char target) { } The method accepts a String parameter, inputStr, and a char parameter, target. It returns the result string consisting of all the characters in inputStr which match the target character (matching is case sensitive, for example, 'c' does not match "C"). If the characters match, the character is appended to the result String. After all the characters in inputStr have been compared, the method returns...
How would you write the following recursively? This is in Java.    public static String allTrim(String...
How would you write the following recursively? This is in Java.    public static String allTrim(String str) { int j = 0; int count = 0; // Number of extra spaces int lspaces = 0;// Number of left spaces char ch[] = str.toCharArray(); int len = str.length(); StringBuffer bchar = new StringBuffer(); if (ch[0] == ' ') { while (ch[j] == ' ') { lspaces++; j++; } } for (int i = lspaces; i < len; i++) { if (ch[i]...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField {...
java: Given the definitions: public interface ActionListener { public void actionPerformed(ActionEvent e); } public JTextField { public JTextField(){} public void setText(String text) {} public String getText() {} } Write the code to create a JButton on the South of the window and a JTextField on the North. The first time you click on the button, it should print out “hello!” on the JTextField. For the second time, should show “hello!hello!”. For the third time, should show “hello!hello!hello!”.
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int...
Consider the following java class: class Student {    private int student_Number;    private String student_Name;    public Student(int stNo,String name) {         student_Number=stNo;         student_Name=name;      }     public String getName() {       return student_Name;     }      public int getNumber() {       return student_Number;      }     public void setName(String st_name) {       student_Name = st_name;     } } Write a Tester class named StudentTester which contains the following instruction: Use the contractor to create a student object where student_Number =12567, student_Name = “Ali”. Use the setName method to change the name of...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm:...
Finish the following java question:  Modify a Encryption program so that it uses the following encryption algorithm: Every letter (both uppercase and lowercase) converted to its successor except z and Z, which are converted to 'a' and 'A' respectively (i.e., a to b, b to c, …, y to z, z to a, A to B, B to C, …, Y to Z, Z to A) Every digit converted to its predecessor except 0, which is converted to 9 (i.e., 9...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    *...
Using Java The given class SetInterface.java is : public interface SetInterface<T> {    /**    * Gets the current number of entries in this set.    *    * @return The integer number of entries currently in the set.    */    public int getSize();    /**    * Sees whether this set is empty.    *    * @return True if the set is empty, or false if not.    */    public boolean isEmpty();    /**    *...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT