Page 1 of 2 12 LastLast
Results 1 to 20 of 29
Like Tree1Likes

Thread: Custom method logic help

  1. #1
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Custom method logic help

    I have a custom class name swimmingPool

    here my constructor that allows each instance variable to be initialized to the user input at instantiation.
    Java Code:
    public swimmingPool(double l, double w, double d, double f, double h)
    	{
    		setPool(l, w, d, f, h);
    	}//end of setPool constructor
    l is for the length, w is the width, d is the depth, f is the fill rate, h is and the percent its already filled.

    here is my object:
    Java Code:
    swimmingPool tenPercentFullPool = new swimmingPool(55, 34, 6, 165, 10);
    here is my time to fill method:
    Java Code:
    	public double timeToFill()
    	{
    		double time;
    		
    		time = waterNeededToFill() / (fillRate - DRAIN_RATE);
    		return time;
    	}//end timeToFill
    and the water needed to fill method:
    Java Code:
    public double waterNeededToFill()
    	{	
    		double waterNeeded, percent;
    		percent = (howFull % 100) * (getVolume());
    		waterNeeded = getVolume() - percent; 
    		return waterNeeded;
    		
    	}//end of
    then my get volume method:
    Java Code:
    public double getVolume()
    	{
    		double volume;
    		
    		volume = length * width * depth;
    		volume = volume * GALLONS_PER_CUBIC_FOOT;
    		return volume;
    	}//end of getVolume
    }
    I think I am missing something obvious. If I set the howFull instance variable(h in my constructor) to 0, it gives me the correct time to fill. When how full is
    set to 10 it gives me the same answer.

    When I debug, and step into the water needed to fill method, after the line "percent = (howFull % 100) * (getVolume());" executes its says percent is equal to 0.00. I just cannot figure out how? howFull is 10...so 10 %100 = .1 right? so .1 * get volume()
    .1 * 83925.6 = 8392.56 right?
    so how is it coming bank 0.00??????????

    Thanks in advance.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Custom method logic help

    Check to see if you're doing int division somewhere, that is an int divided by an int. Since this always returns an int, it may not be what you want. In that situation, cast one of the numbers to double.

  3. #3
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Custom method logic help

    I tried casting it to a double the result fro percent and it still comes back as 0.00 and it coming back 0.00 tells me its doing double arithmetic right if it was int the debugger would say its equal to just 0 right?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Custom method logic help

    If the result of a division is 0 then casting to a double will just result in 0.0. What you need to do is cast one or more of the operands to a double before the division is performed.

  6. #6
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Custom method logic help

    all the variables are doubles, I don;t even have any int types

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Custom method logic help

    You will need to do some more debugging. Make sure the getVolume method is return a valid value. What about howFull? What value does that hold? If it is a multiple of 100 then howFull % 100 will be zero.

  8. #8
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Custom method logic help

    I did a print line and the get volume is fine see what is happening is percent is coming back 0 so getVolume is coming back as its empty, so the the time to fill method is always what it takes the pool to fill from empty. I am stumped why percent is 0.00??

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Custom method logic help

    Post your full code or a SSCCE and the input values you are using.

  10. #10
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Custom method logic help

    I instanciated the objects with the input values.
    Thanks in advance!
    Here is my class:
    Java Code:
    public class swimmingPool
    {
    	static final double DRAIN_RATE = 1.1; //gallons per hour
    	static final double GALLONS_PER_CUBIC_FOOT = 7.48;
    	
    	//instance variables
    	static double length, width, depth, fillRate, howFull;
    	
    	//Default constructor
    	//intializes all instance variables to 0
    	//post condition: length = 0, width = 0, depth = 0. fillRate = 0
    	public swimmingPool()
    	{
    		setPool(0, 0, 0, 0, 0);
    	
    	}//end of default constructor
    	
    	//constructor that sets properties to user input at time of instantiation
    	//post conditions: length = l, width = w, depth = d, fillrate = f
    	public swimmingPool(double l, double w, double d, double f, double h)
    	{
    		setPool(l, w, d, f, h);
    	}//end of setPool constructor
    	
    	//constructor that copies instance of swimmingPool class at instantiation
    	//post conditions: mySwimmingPool = new swimmingPool(firstSwimmingPool)
    	public swimmingPool(swimmingPool otherSwimmingPool)
    	{
    		length = otherSwimmingPool.length;
    		width = otherSwimmingPool.width;
    		depth = otherSwimmingPool.depth;
    		fillRate = otherSwimmingPool.fillRate;
    		howFull = otherSwimmingPool.howFull;
    	}//end of copy constructor
    	
    	//Method to set Pool
    	//post condition length = l, width = w, depth = d, fillrate = f
    	public void setPool(double l, double w, double d, double f, double h)
    	{
    		if (l >= 0)
    		{
    			length = l;
    		}
    		else
    			length = 0;
    			
    		if(w >= 0)
    		{
    			width = w;
    		}
    		else
    			width = 0;
    			
    		if(d >= 0)
    		{
    			depth = d;
    		}
    		else
    			depth = 0;
    			
    		if (f >= 0)
    		{
    			fillRate = f;
    		}
    		else
    			fillRate = 0;
    			
    		if (h >= 0)
    		{
    			howFull = h;
    		}
    		else
    			howFull = 0;
    	}//end of setPool
    	
    	public void setLength(double l)
    	{
    		length = l;
    		
    	}
    	
    	public void setWidth(double w)
    	{
    		width = w;
    	}
    	
    	public void setDepth(double d)
    	{
    		depth = d;
    	}
    	
    	public void setFillRate(double f)
    	{
    		fillRate = f;
    	}
    	
    	public void setHowFull(double h)
    	{
    		howFull = h;
    	}
    	
    	//method to get Length
    	//post condition length is returned
    	public double getLength()
    	{
    		return length;
    	}//end of getLength
    	
    	//method to get width
    	//post condition width is returned
    	public double getWidth()
    	{
    		return width;
    	}//end of getWidth
    	
    	//method to get depth
    	//post condition depth is returned
    	public double getDepth()
    	{
    		return depth;
    	}//end of getDepth
    	
    	//method to get fillRate
    	//post condition fillRate is returned
    	public double getFillRate()
    	{
    		return fillRate;
    	}//end of getFillRate
    	
    	//method to return how full the pool is
    	//post condition: how Full the pool is, is returned
    	public double getHowFull()
    	{
    		return howFull;
    	}//end of getHowFull
    	
    	//method to get DRAIN_RATE
    	//post conditions DRAIN_RATE is returned
    	public double getDrainRate()
    	{
    		return DRAIN_RATE;
    	}//end of getDrainRate
    	
    	//method to get volume of the pool in gallons
    	//volume = l * w * d, is returned
    	public double getVolume()
    	{
    		double volume;
    		
    		volume = length * width * depth;
    		volume = volume * GALLONS_PER_CUBIC_FOOT;
    		return volume;
    	}//end of getVolume
    	
    	//method to figure amount of water needed to fill the pool from how much is in it
    	//post condition number of gallons needed to fill is returned.
    	public double waterNeededToFill()
    	{	
    		double waterNeeded, percent;
    		percent = (howFull % 100.0) * getVolume();
    		waterNeeded = getVolume() - percent; 
    		return waterNeeded;
    		
    	}//end of waterNeededToFill
    	
    	//method to find the time needed to fill the pool from the point at which it is at
    	//postconditions: returns a double time to fill.
    	public double timeToFill()
    	{
    		double time;
    		
    		time = waterNeededToFill() / (fillRate - DRAIN_RATE);
    		return time;
    	}//end timeToFill
    	
    	//method to find the time needed to empty the pool
    	//postcondition double time needed returned
    	public double timeToEmpty()
    	{
    		double time;
    		
    		if(howFull == 0)
    		{
    			time = getVolume() / DRAIN_RATE;
    		}
    		else
    		{
    			time = howFull / DRAIN_RATE;
    		}
    		return time;
    	}//end of time to empty
    	
    	//method to add water to the pool
    	//post condition: howFull =  (fillRate - DRAIN_RATE) * time
    	public void addWater(double time)
    	{
    		howFull = (fillRate - DRAIN_RATE) * time;
    	}//end addWater
    	
    	//method to drain water over a given period of time
    	//post condition howFill = DRAIN_RATE * time
    	public void drainWater(double time)
    	{
    		howFull = DRAIN_RATE * time;
    	}
    		
    }//end of class swimmingPool

    here's my client:
    Java Code:
    import java.util.*;
    
    public class testPool
    {
    	static Scanner console = new Scanner(System.in);
    	
    	public static void main(String[] args)
    	{
    		double tenToEighty;
    		
    		swimmingPool bigPool = new swimmingPool();
    		swimmingPool emptyPool = new swimmingPool(55, 34, 6, 165, 0);
    		swimmingPool tenPercentFullPool = new swimmingPool(55, 34, 6, 165, 10);
    		swimmingPool eightyPercentFullPool = new swimmingPool(55, 34, 6, 165, 80);
    		swimmingPool fullPool = new swimmingPool(55, 34, 6, 165, 100);
    		
    		System.out.println("Please enter the length, width, depth, and fill rate; each separated by a space, then press enter.");
    		bigPool.setLength(console.nextDouble()); 
    		bigPool.setWidth(console.nextDouble());
    		bigPool.setDepth(console.nextDouble());
    		bigPool.setFillRate(console.nextDouble());
    		
    		System.out.println("The Length of the pool is: " + String.format("%.2f", bigPool.getLength()) + "ft");
    		System.out.println("The Width of the pool is:  " + String.format("%.2f", bigPool.getWidth()) + "ft");
    		System.out.println("The Depth of the pool is:  " + String.format("%.2f", bigPool.getDepth()) + "ft");
    		System.out.println("The Fill rate is:          " + String.format("%.2f", bigPool.getFillRate()) + "gal");
    		System.out.println("The Drain rate is:         " + String.format("%.2f", bigPool.getDrainRate()) + "gal");
    		System.out.println("The Volume of the pool is: " + String.format("%.2f", bigPool.getVolume()) + "gal");
    		
    		
    		
    		System.out.println("The time to fill the pool from empty is: " + String.format("%.2f", emptyPool.timeToFill()) + " hours");
    		tenToEighty = tenPercentFullPool.timeToFill() - eightyPercentFullPool.timeToFill();
    		
    		System.out.println(tenPercentFullPool.timeToFill());
    		System.out.println(eightyPercentFullPool.timeToFill());
    		
    		System.out.println("The time to fill the pool from 10% to 80% is: " + String .format("%.2f", tenToEighty) + " hours");
    	
    	}
    }

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Custom method logic help

    The only pool that is used in your code is bigPool and it gets its values from used input. However you do not get the user to enter a value for howFull therefore it will have the default value of 0. Guess what the result of 0 % 100 is?

  12. #12
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Custom method logic help

    Rule #1: DON'T USE STATIC VARIABLES!

    Also that's not how you calculate precentages.

  13. #13
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Custom method logic help

    no I use others here the bottom of the client:

    Java Code:
    System.out.println(tenPercentFullPool.timeToFill());
    		System.out.println(eightyPercentFullPool.timeToFill());
    		
    		System.out.println("The time to fill the pool from 10% to 80% is: " + String .format("%.2f", tenToEighty) + " hours");

  14. #14
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Custom method logic help

    Please read and re-read my post above.

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Custom method logic help

    Do you understand my concerns? If not please say something, because I'm going to bed very soon.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Custom method logic help

    /me throws sand into Pete's eyes.

  17. #17
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Custom method logic help

    I have been round and round and this is crazy. I rewrote part just to segregate what is going on.

    so first I did this:



    Java Code:
    public double waterNeededToFill()
    	{	
    		double waterNeeded, percent;
    		double volumeGal = getVolume();
    		percent = volumeGal * (howFull / 100.00);
    		waterNeeded = getVolume() - percent; 
    		return waterNeeded;
    		
    	}//end of waterNeededToFill
    I added the double variable volumeGal = getVolume() it returns to the caller correct.

    then percent executes equally the same value as volumeGal that tells me (howfull / 100.00) is coming out to be = 1

    if how full is 0.00 how is 0.00 /100.00 coming out to be 1?

    here is the screen from the compiler.

    Custom method logic help-swim.png

  18. #18
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Custom method logic help

    Have you made your static variables non-static? Are you even listening to anything I post? You're not responding to it so I can only guess not and fear that I'm just wasting time. Please prove this wrong.

  19. #19
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Re: Custom method logic help

    The assignment parameters state to use static instance variables and static named constants. I wasn't ignoring you. I guess I should have stated that. sorry

  20. #20
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Custom method logic help

    Quote Originally Posted by mwr1976 View Post
    The assignment parameters state to use static instance variables and static named constants. I wasn't ignoring you. I guess I should have stated that. sorry
    Yes for constants, but not for those variables. Please post your assignment instructions verbatim.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 0
    Last Post: 09-12-2011, 01:06 PM
  2. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 06:44 PM
  3. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 11:06 AM
  4. [SOLVED] Writing an Iterator method in a custom list
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 02-12-2009, 05:22 AM
  5. Replies: 3
    Last Post: 10-06-2008, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •