Java OOP coding question need Help! Please fully answer the question below. publ
ID: 3826722 • Letter: J
Question
Java OOP coding question need Help! Please fully answer the question below.
public class OOPLecture2 {
/*** Note: We are abusing inner classes in this assignment by defining multiple classes inside the
* OOPLecture1 class. In most cases it is better practice to define each class in a separate file ***/
// Q1: Create an interface named "Boilable" containing an abstract method named boil() that takes no parameters and
// has return type void.
public interface Boilable{
public abstract void boil();
}
// Q2: Create a concrete class named "Egg" that implements Boilable. The Egg class should have 1 instance variable
// that is a String named "state" that is initialized to "raw" in the default constructor. Override its boil
// to set the value of state to "hard boiled"
public class Egg implements Boilable{
protected String state;
public Egg(){
this.state = "raw";
}
@Override
public void boil(){
this.state = "hard boiled";
}
@Override
public String toString(){
if(this.state = "raw"){
System.out.println("this egg is raw");
}
}
}
// Q3: Override Egg's toString method to print "this egg is raw" or "this egg is hard boiled" depending on the
// state of the egg.
// Q4: Create a concrete class named "Potato" that implements Boilable. The Potato class should have 1 instance
// variable that is a boolean named "soft" that is initialized to false in the default constructor. Override
// its boil method to set the value of soft to true.
// Q5: Override Potato's toString method to return "this potato is hard" if soft is false and "this potato is soft"
// if soft is true.
public static void main(String[] args) {
// The following code is an example of testing these inner classes
// Uncomment this code to test your classes
// OOPLecture2 outerInstance = new OOPLecture2();
//
// Boilable egg = outerInstance.new Egg();
// Boilable potato = outerInstance.new Potato();
//
// System.out.println(egg);
// System.out.println(potato);
//
// System.out.println(" boiling... ");
// egg.boil();
// potato.boil();
//
// System.out.println(egg);
// System.out.println(potato);
}
}
Note that after the toString() is where I stopped and there was mistake in what to do after that and that's where I need help to finish answering the questions PLEASE HELP.
Explanation / Answer
Ans
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication6;
/**
*
* @author Rashmi Tiwari
*/
public class OOPLecture2 {
class Egg implements Boilable{
String state;
Egg(){
state="raw";
}
@Override
public void boil() {
this.state="Hard-Boiled";
}
public String toString(){
if(this.state.equals("raw")){
return "This Egg is Raw";
}
else{
return "This Egg is Hard Boiled";
}
}
}
class Potato implements Boilable{
Boolean soft;
Potato(){
soft=false;
}
@Override
public void boil() {
soft=true;
}
public String toString(){
if(soft==false){
return "This Potato is hard";
}
else{
return "This Potato is soft";
}
}
}
public static void main(String args[]){
OOPLecture2 outerInstance = new OOPLecture2();
Boilable egg = outerInstance.new Egg();
Boilable potato = outerInstance.new Potato();
System.out.println(egg);
System.out.println(potato);
System.out.println(" boiling... ");
egg.boil();
potato.boil();
System.out.println(egg);
System.out.println(potato);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.