Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Add another machine to the Cave of programming code and submit a fully functioni

ID: 3787267 • Letter: A

Question

Add another machine to the Cave of programming code and submit a fully functioning program (that has output). Name the program InterfaceExercise instead of App.

Here is what's given:

1:

public class App {

public static void main(String[] args) {

Machine mach1 = new Machine();

mach1.start();

Person person1 = new Person("Bob");

person1.greet();

Info info1 = new Machine();

info1.showInfo();

Info info2 = person1;

info2.showInfo();

System.out.println();

outputInfo(mach1);

outputInfo(person1);

}

private static void outputInfo(Info info) {

info.showInfo();

}

2:

public interface Info {

public void showInfo();

}

3:

public class Machine implements Info {

private int id = 7;

public void start() {

System.out.println("Machine started.");

}

public void showInfo() {

System.out.println("Machine ID is: " + id);

}

}

4:

public class Person implements Info {

private String name;

public Person(String name) {

this.name = name;

}

public void greet() {

System.out.println("Hello there.");

}

@Override

public void showInfo() {

System.out.println("Person name is: " + name);

}

}

5:

public interface IStartable {

public void start();

public void stop();

}

Explanation / Answer

Hi,

PFA Updated classes and interfaces. Please comment for any queries/feedbacks.

Thanks,

Anita Joseph

InterfaceExercise.java

public class InterfaceExercise {

public static void main(String[] args) {

Machine mach1 = new Machine();
mach1.start();

Person person1 = new Person("Bob");
person1.greet();

Info info1 = new Machine();
info1.showInfo();

Info info2 = person1;
info2.showInfo();

//Adding another machine
System.out.println("Adding new machine machine3");
Machine mach3 = new Machine();
//Setting id as 3
mach3.setId(3);
//Starting new machine
mach3.start();

Info info3 = mach3;
info3.showInfo();

System.out.println();

outputInfo(mach1);
outputInfo(person1);
}

private static void outputInfo(Info info) {
info.showInfo();
}
}

Info.java

public interface Info {
public void showInfo();
}

Machine.java

public class Machine implements Info {

private int id = 7;

public int getId() {
   return id;
}

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

public void start() {
System.out.println("Machine started.");
}

public void showInfo() {
System.out.println("Machine ID is: " + id);
}
}

Person.java

public class Person implements Info {

private String name;

public String getName() {
   return name;
}

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

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

public void greet() {
System.out.println("Hello there.");
}

@Override
public void showInfo() {
System.out.println("Person name is: " + name);
}
}

Sample Output:

Machine started.
Hello there.
Machine ID is: 7
Person name is: Bob
Adding new machine machine3
Machine started.
Machine ID is: 3

Machine ID is: 7
Person name is: Bob

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote