Java OOP question need help. There are 4 files Boilable.java, Egg.java, OOPLectu
ID: 3827998 • Letter: J
Question
Java OOP question need help. There are 4 files Boilable.java, Egg.java, OOPLecture3.java, and Patato.java. In a new Java project in eclipse with src containing default package with the 4 .javas. I will provide these code and what I need help with is the questions from the OOPLecture3.java file.
import java.util.ArrayList;
public class OOPLecture3 {
// 2 points
public static ArrayList<Boilable> Q1(){
// Return an ArrayList containing 1 Egg and 3 Potatoes
return null;
}
// 2 points
public static void Q2(ArrayList<Boilable> input){
// Boil each element in the input list
}
// 1 point
public static String Q3(ArrayList input){
// Return a single String containing the output of toString() from every element in the input list separated
// by new line characters ' '
//
// Note: This ArrayList does not specify a type. While this is not something you would want to do outside
// the classroom, we do it here to show a feature of polymorphism in java. Since the type is not
// specified this is not a very useful data structure, but since every class in java extends Object
// and ArrayLists must contain objects (recall we can use primitives like int with an ArrayList)
// we can assume that this ArrayList contains elements of type Object and we can treat it as an
// ArrayList of Objects. As such, we can call the methods associated with the Object class,
// including toString().
return "";
}
public static void main(String[] args) {
// testing
ArrayList<Boilable> breakfast = Q1();
System.out.println(Q3(breakfast));
Q2(breakfast);
System.out.println(Q3(breakfast));
}
}
public class Egg implements Boilable{
private String state;
public Egg(){
this.state = "raw";
}
@Override
public void boil(){
this.state = "hard boiled";
}
@Override
public String toString(){
return "this egg is " + this.state;
}
}
public interface Boilable{
void boil();
}
public class Potato implements Boilable{
private boolean soft;
public Potato(){
this.soft = false;
}
@Override
public void boil(){
this.soft = true;
}
@Override
public String toString(){
return this.soft ? "this potato is soft" : "this potato is hard";
}
}
Explanation / Answer
Hi, Please find my implementation.
import java.util.ArrayList;
public class OOPLecture3 {
// 2 points
public static ArrayList<Boilable> Q1(){
// Return an ArrayList containing 1 Egg and 3 Potatoes
ArrayList<Boilable> list = new ArrayList<>();
list.add(new Egg());
list.add(new Potato());
list.add(new Potato());
return list;
}
// 2 points
public static void Q2(ArrayList<Boilable> input){
// Boil each element in the input list
for(Boilable item : input)
item.boil();
}
// 1 point
public static String Q3(ArrayList input){
// Return a single String containing the output of toString() from every element in the input list separated
// by new line characters ' '
//
// Note: This ArrayList does not specify a type. While this is not something you would want to do outside
// the classroom, we do it here to show a feature of polymorphism in java. Since the type is not
// specified this is not a very useful data structure, but since every class in java extends Object
// and ArrayLists must contain objects (recall we can use primitives like int with an ArrayList)
// we can assume that this ArrayList contains elements of type Object and we can treat it as an
// ArrayList of Objects. As such, we can call the methods associated with the Object class,
// including toString().
String result = "";
for(Object obj : input)
result = result + obj.toString()+" ";
return result;
}
public static void main(String[] args) {
// testing
ArrayList<Boilable> breakfast = Q1();
System.out.println(Q3(breakfast));
Q2(breakfast);
System.out.println(Q3(breakfast));
}
}
/*
Sample run:
this egg is raw
this potato is hard
this potato is hard
this egg is hard boiled
this potato is soft
this potato is soft
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.