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.
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...
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!”.
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();    /**    *...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content)...
Java public class UpperCaseString { //1      protected String content; // 2      public UpperCaseString(String content) { // 3           this.content = content.toUpperCase(); // 4      } // 5      public String toString() { // 6           return content.toUpperCase(); // 7      } // 8      public static void main(String[] args) { // 9           UpperCaseString upperString =              new UpperCaseString("Hello, Cleo!"); // 10           System.out.println(upperString); // 11      } // 12 } // 13 THE FOLLOWING 3 QUESTIONS REFER...
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...
java programing Q: Given the following class: public class Student { private String firstName; private String...
java programing Q: Given the following class: public class Student { private String firstName; private String lastName; private int age; private University university; public Student(String firstName, String lastName, int age, University university) { this.firstName = fisrtName; this.lastName = lastName; this.age = age; this.university = university; } public String getFirstName(){ return firstName; } public String getLastName(){ return lastName; } public int getAge(){ return age; } public University getUniversity(){ return university; } public String toString() { return "\nFirst name:" + firstName +...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface...
******IN JAVA******** I need the following interface implemented accordingly. It is a linked list. The interface can be found below: List.java public interface List<T> extends Iterable<T> { /** * Insert an element at a specified location. * @param index * @param obj * @throws IndexOutOfBoundsException */ public void add(int index, T obj); /** * Append an object to the end of the list. * @param obj */ public boolean add(T obj); public void clear(); public boolean contains(T obj); /** *...
Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+")...
Question for Java String[] tokens = expression.split(" "); for (String token : tokens) { if (token.equals("+") || token.equals("-")) { while (!ops.isEmpty() && (ops.peek() == '+' || ops.peek() == '-' || ops.peek() == '*' || ops.peek() == '/')) {    applyOp(values, ops); } What does the for (String tokens : token) mean? What are the different ways to write this for loop?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT