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

Define a public toString() method on class Ground that returns a String in the f

ID: 3596751 • Letter: D

Question

Define a public toString() method on class Ground that returns a String in the following format: Jack[](0) [Quartz#4](1) [Pearl#3](2) [Opal#2](3) /6(4) Programming Fundamentals (48023) Spring 2017 Assignment (Released September 19) Page 11 This string describes the player “Jack” with an empty jar at position 0, three jars at positions 1, 2 and 3 containing rocks named “Quartz”, “Pearl” and “Opal” with weights of 4, 3 and 2 respectively, and the chest with combination 6 at position 4. The numbers and names may of course differ depending on what data is entered by the user when the constructors are executed. Thus, your string should always incorporate data directly from the fields. If the chest is unlocked, it should be rendered as /(4) instead of /6(4). This task must be implemented compositionally with a toString() method in each class. The String returned by this method can embed variables this way: return "string1” + “string2” + variable + “another string” + … You can also use the StringBuffer class described in the text book. You must not, however, use the sprintf method (since it uses the untaught varargs feature).

Explanation / Answer

import java.util.ArrayList;

public class Ground {

   /* Fields of Ground Class */
   String playerName;
   ArrayList<Jar> jars = new ArrayList<Jar>();
   Chest chest=null;
          
   public Ground() {
       /* Initialize values of the fields in constructor */
       playerName = "Jack";
       jars.add(new Jar("",0,0));
       jars.add(new Jar("Quartz",4,1));
       jars.add(new Jar("Pearl",3,2));
       jars.add(new Jar("Opal",2,3));
       chest = new Chest(6, 4, true);
   }
  
   @Override
   public String toString() {
       StringBuffer sb = new StringBuffer(playerName);
       for(Jar j : jars){
           sb.append(j);
       }
       sb.append(chest);
       return sb.toString();      
   }
  
   public static void main(String[] args) {
       Ground g = new Ground();
       System.out.println(g);

   }

}

class Jar{
   String rock="";
   int weight;
   int position;
      
   public Jar(String rock, int weight, int position) {
       this.rock = rock;
       this.weight = weight;
       this.position = position;
   }


   @Override
   public String toString() {
       if(rock.equals("")){
           return "[]("+position+")";
       }else{
           return "["+rock+"#"+weight+"]("+position+")";
       }
   }
}

class Chest{
   int combination;
   int position;
   boolean locked;
  
   public Chest(int combination, int position, boolean locked) {
       this.combination = combination;
       this.position = position;
       this.locked = locked;
   }

   @Override
   public String toString() {
       if(locked){
           return "/"+combination+"\("+position+")";
       }else{
           return "\"+combination+"/("+position+")";
       }
   }
}

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