Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First off, just as a tip, it's useful to add method headers so you know what your method is trying to do. Aka, if you look at your specifications many of your methods require you to "know how much..." so the methods should return a number rather than immediately printing something out (I made the same mistake when I began coding). </p> <p>You are printing out those numbers within your methods (which is useful for debugging, but not what your finished product should do). You can return an int and then print out that integer in your RunSugarBowl (see below).</p> <p>I've given you a general framework of what that entails and added some comments that may help you. You've done well to begin with. If you have more problems, just ask in a comment. </p> <pre><code>public class SugarBowl { private int totalCapacity; private int availableSpace; private int spoonSize; private int occupiedSpace;//starts at 0, because there's nothing in the bowl. /** * Constructor for the sugar bowl. * @param totalCapacity The total capacity of the bowl. */ public SugarBowl (int totalCapacity){ this.totalCapacity = totalCapacity; //set the totalCapacity for the bowl availableSpace=totalCapacity; spoonSize = totalCapacity/20;//arbitrary size occupiedSpace = 0; } /** * Shows how much sugar can fit in a spoon. * @return The size of the spoon */ public int spoon(){ return spoonSize; } /** * Returns amount of sugar in the bowl. * @return The amount of occupied space */ public int occupied(){ return occupiedSpace; } /** * Removes the amount of sugar based on the * number of scoops passed into it. * @param numberOfScoops The number of scoops * @return The amount of sugar removed */ public int scoops (int numberOfScoops){ int possibleAmountTaken = numberOfScoops * spoonSize; int actualAmountTaken = 0; //Think about sugar, even if there is less sugar than the spoon size, //can a spoon still remove that amount? //aka the only time 0 sugar should be taken is when there is 0 sugar in the bowl if (possibleAmountTaken&lt;=occupiedSpace){ actualAmountTaken = possibleAmountTaken; } else{ //there may still be sugar, just not enough for every scoop, you still have to remove it //actualAmountTaken = ??? } occupiedSpace = occupiedSpace - actualAmountTaken; //what about availableSpace? //availableSpace = ??? return actualAmountTaken; } /** * Adds the specified amount of sugar to the bowl. * * @param addedAmount The amount of sugar added to the bowl * @return The overflow amount of sugar or 0 if there was no overflow */ public int addSugar (int addedAmount){ int overflow = 0; if (addedAmount&gt;availableSpace){ overflow = addedAmount-availableSpace; //your bowl is going to be full, what happens to occupiedSpace and availableSpace? //availableSpace = ??? //occupiedSpace = ??? } else{ //overflow is already 0 so you don't have to do anything with it //update availableSpace and occupiedSpace //availableSpace = ??? //occupiedSpace = ??? } return overflow; } } </code></pre> <p>With your above main example:</p> <pre><code>public class RunSugarBowl { public static void main(String[] args) { SugarBowl one = new SugarBowl(200); System.out.println("Sugar overflow: " + Integer.toString(one.addSugar(300))); //once working correctly should print out 100 for the overflow System.out.println("Occupied size is : "+ one.occupied()); } } </code></pre> <p><strong>UPDATE</strong></p> <p>The reason you would use this.totalCapacity is because of the following lines of code:</p> <pre><code>public class SugarBowl { private int totalCapacity; //totalCapacity for this object; aka this.totalCapacity refers to this variable //.. public SugarBowl (int totalCapacity){ // &lt;-- totalCapacity passed in this.totalCapacity = totalCapacity; //this.totalCapacity is setting the totalCapacity for this instance of the object to the value passed in //.. </code></pre> <p>Notice how your constructor is being passed in a variable called "totalCapacity" and yet the class also has its own internal variable called totalCapacity. Comparable code without the "this" keyword is as follows:</p> <pre><code>public class SugarBowl { private int bowlTotalCapacity; //totalCapacity for this object //.. public SugarBowl (int totalCapacity){ bowlTotalCapacity = totalCapacity; //.. </code></pre> <p>You'd just have to make sure that after you initialize that wherever you would have used totalCapacity before, you'd change it to bowlTotalCapacity. It's just a whole lot easier to use this.totalCapacity to refer to the totalCapacity within this class. Take a look here for more information: <a href="http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html" rel="nofollow">http://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html</a>.</p> <p>Technically you don't actually use totalCapacity ever again after you initially construct the object, but if you want to see the weirdness that happens if you don't include the this portion try to understand what happens in the following code:</p> <pre><code>public class ThisExample { private int wrongExample = 0; private int thisExample = 0; public ThisExample (int wrongExample, int thisExample){ wrongExample = wrongExample; this.thisExample = thisExample; } public int getThisExample(){ return thisExample; } public int getWrongExample(){ return wrongExample; } } </code></pre> <p>Running the following may help you understand better:</p> <pre><code>public class ThisExampleMain { public static void main(String[] args) { ThisExample ts = new ThisExample(50, 50); //you want this to be 50 but it ends up being 0: System.out.println("Wrong: " + ts.getWrongExample()); //this returns the correct answer: System.out.println("Right: " + ts.getThisExample()); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload