Results 1 to 11 of 11
  1. #1
    OlivierSillegue is offline Student in programming
    Join Date
    Feb 2012
    Location
    London, Ontario
    Posts
    6
    Rep Power
    0

    Default Problem with rounding up in a school project

    Hello,

    I have a project for my Programming's Fundamental's. In this project, I have to determine how many lifeboats are required on a ship according to the amount of passangers and the capacity of the lifeboats typed in by a user.

    I have only seen the Scanner class yet, but I have an example of a class I don't know yet, I might be able to use it.

    Here is my program as I have coded it, using Eclipse.

    Java Code:
    import java.util.Scanner;
    
    public class K_D_Lifeboats
    {
    
    	public static void main(String[] args)
    	{
    
    		// Create a Scanner object to read-in user inputs
    		Scanner input = new Scanner(System.in);
    		
    		// Display a title
    		System.out.println("Lifeboats Calculator\n");
    				
    		// 2. Ask the user to enter the following values:
    		// a. The name of the ship (possibly including spaces like HMS Endurance)
    		// b. The number of people on board the ship
    		// c. The maximum number of people that can be carried by one lifeboat, assuming that all
    		// the lifeboats on the ship are the same size
    		// d. The actual number of lifeboats that are available on board the ship
    		System.out.print("Enter the name of the ship: ");
    		String shipName = input.nextLine();
    		System.out.print("Enter the number of passangers on board of the ship: ");
    		int passangersNumber = input.nextInt();
    		System.out.print("Enter the maximum number of passangers that can fit in one lifeboat (same size boats): ");
    		int maxPassangersLifeboat = input.nextInt();
    		System.out.print("Enter the number of lifeboats that are available on board of the ship: ");
    		int numberLifeboats = input.nextInt();
    		
    		double lifeboatsNeeded = passangersNumber / maxPassangersLifeboat;
    		
    		System.out.println("Here are the results...\n");
    		
    		if(passangersNumber > (numberLifeboats * maxPassangersLifeboat))
    		{
    			System.out.println("A minimum of " + (int)lifeboatsNeeded + " lifeboats are required to rescue everyone on board of " + shipName + ".\n");
    			double survivors = numberLifeboats * maxPassangersLifeboat;
    			double casualties = passangersNumber - survivors;
    			double percentageSurvivors = (100 * survivors) / passangersNumber;
    			double tempPercentageSurvivors = percentageSurvivors * 100;
    			int tempPercentageSurvivors2 = (int)tempPercentageSurvivors;
    			percentageSurvivors = tempPercentageSurvivors2 / 100.00;
    			double percentageCasualties = (100 * casualties) / passangersNumber;
    			double tempPercentageCasualties = percentageCasualties * 100;
    			int tempPercentageCasualties2 = (int)tempPercentageCasualties;
    			percentageCasualties = tempPercentageCasualties2 / 100.00;
    			System.out.println((int)survivors + " people (" + percentageSurvivors + "%) would be rescued.");
    			System.out.println((int)casualties + " people (" + percentageCasualties + "%) would likely drown.");
    		}
    		else
    		{
    			System.out.println("A minimum of " + (int)lifeboatsNeeded + " lifeboats are required to rescue everyone on board of " + shipName + ".\n");
    			int extraPassangers = (numberLifeboats * maxPassangersLifeboat) - passangersNumber;
    			System.out.println(passangersNumber + " people (100.00%) would be rescued.");
    			System.out.println("0 people (0.0%) would likely drown.");
    			System.out.println("There would be room for an extra " + extraPassangers + " people in the lifeboats.");
    		}
    
    	}
    
    }
    When I compile it, the percentages are fine, but there is a definite problem with the amounts of lifeboats needed.

    Lifeboats Calculator

    Enter the name of the ship: the Admiral
    Enter the number of passangers on board of the ship: 650
    Enter the maximum number of passangers that can fit in one lifeboat (same size boats): 65
    Enter the number of lifeboats that are available on board of the ship: 10
    Here are the results...

    A minimum of 10 lifeboats are required to rescue everyone on board of the Admiral.

    650 people (100.00%) would be rescued.
    0 people (0.0%) would likely drown.
    There would be room for an extra 0 people in the lifeboats.
    Up until now, there is no issue. the calculation is correct and so is the amount of lifeboats.

    Lifeboats Calculator

    Enter the name of the ship: the Admiral
    Enter the number of passangers on board of the ship: 650
    Enter the maximum number of passangers that can fit in one lifeboat (same size boats): 50
    Enter the number of lifeboats that are available on board of the ship: 10
    Here are the results...

    A minimum of 13 lifeboats are required to rescue everyone on board of the Admiral.

    500 people (76.92%) would be rescued.
    150 people (23.07%) would likely drown.
    I also don't see a problem with it when the number of passangers on the ship divided by the maximum of passangers that can fit on a lifeboat turns out an integer. But when there are decimals, this is what it gives me:

    Lifeboats Calculator

    Enter the name of the ship: the Admiral
    Enter the number of passangers on board of the ship: 655
    Enter the maximum number of passangers that can fit in one lifeboat (same size boats): 50
    Enter the number of lifeboats that are available on board of the ship: 10
    Here are the results...

    A minimum of 13 lifeboats are required to rescue everyone on board of the Admiral.

    500 people (76.33%) would be rescued.
    155 people (23.66%) would likely drown.
    The minimum of lifeboats required should be 14 here, so accomodate the 5 other persons, but no matter what I try (Math.round(), Math.ceil(), multiply but 100, divide and then cast, etc), I can't get that value to round up to the next integer, if the double lifeboatsNeeded has decimals lower than .50. So I come here to ask for some guidance, and perhaps a few tips on how I could successfully resolve this issue.

    Can you help me? Please? I would really appreciate it!

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem with rounding up in a school project

    System.out.println("A minimum of " + (int)lifeboatsNeeded + " lifeboats are required etc
    That (int) cast is going to truncate, ie round down. ceil() should be good here. You said you had tried that. What code did you use? And what were the results?

    [Edit]

    Java Code:
    double lifeboatsNeeded = passangersNumber / maxPassangersLifeboat;
    Also note that this line performs integer division. That is, it also truncates. To get a standard division you would say:

    Java Code:
    double lifeboatsNeeded = (double)passangersNumber / maxPassangersLifeboat;
    Last edited by pbrockway2; 02-12-2012 at 06:51 AM.

  3. #3
    OlivierSillegue is offline Student in programming
    Join Date
    Feb 2012
    Location
    London, Ontario
    Posts
    6
    Rep Power
    0

    Default Re: Problem with rounding up in a school project

    This is how I had written it with ceil...

    Java Code:
    System.out.println("A minimum of " + Math.ceil(lifeboatsNeeded) + " lifeboats are required to rescue everyone on board of " + shipName + ".\n");
    And this is what I obtain as a result, unfortunately.

    Lifeboats Calculator

    Enter the name of the ship: the Admiral
    Enter the number of passangers on board of the ship: 655
    Enter the maximum number of passangers that can fit in one lifeboat (same size boats): 50
    Enter the number of lifeboats that are available on board of the ship: 10
    Here are the results...

    A minimum of 13.0 lifeboats are required to rescue everyone on board of the Admiral.

    500 people (76.33%) would be rescued.
    155 people (23.66%) would likely drown.
    The decimal place is not so much of a problem, but I thought that ceil would automatically round this up for me. Unfortunately, it rounds it down. Do you have any other ideas? Thank you so much for your help, by the way!

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem with rounding up in a school project

    Did you see my point about integer division (7/2 = 3 etc)?

    Java Code:
    public class RoundEg {
        public static void main(String[] args) {
            System.out.println("7/3=" + 7/3);
            System.out.println("Math.ceil(7/3)=" + Math.ceil(7/3));
            System.out.println("(double)7/3=" + (double)7/3);
            System.out.println("Math.ceil((double)7/3)=" + Math.ceil((double)7/3));
            
                // if the following line makes sense to you, use it.  But Math.ceil()
                // is the most straight forward since you want to round up.  The
                // argument to Math.ceil() must be cast to double to avoid losing
                // the fraction.
            System.out.println("(7+(3-1))/3=" + (7+(3-1))/3);
        }
    }

  5. #5
    OlivierSillegue is offline Student in programming
    Join Date
    Feb 2012
    Location
    London, Ontario
    Posts
    6
    Rep Power
    0

    Default Re: Problem with rounding up in a school project

    Good morning!

    I hadn't seen your edit, but now that I did, I cast the integer to a double. It's funny though, I was taught that when you write something like

    double example = int1 + int2

    since the double is bigger than the int, it would automatically turn the answer into a double, and that casting was only required when trying to put a bigger variable into a smaller one (the professor gave me the example of putting the container of a small bucket into a bigger one, versus the contrary).

    I hadn't put that public class RoundEg on top of my project since my professor didn't teach us that yet, but I'm going to try to apply your example.

    Oh, wait, that was a goof on my part. Your RoundEg was the name of your class. D'uh. Is there anything I have to import in order to do this calculation?
    Last edited by OlivierSillegue; 02-12-2012 at 03:47 PM.

  6. #6
    OlivierSillegue is offline Student in programming
    Join Date
    Feb 2012
    Location
    London, Ontario
    Posts
    6
    Rep Power
    0

    Default Re: Problem with rounding up in a school project

    Well! It worked! Once I cast the Math.ceil(lifeboatsNeeded) to a double in my System.out.println, I was able to obtain the right answer! Hurray!!!

    Lifeboats Calculator

    Enter the name of the ship: the Admiral
    Enter the number of passangers on board of the ship: 655
    Enter the maximum number of passangers that can fit in one lifeboat (same size boats): 50
    Enter the number of lifeboats that are available on board of the ship: 10
    Here are the results...

    A minimum of 14.0 lifeboats are required to rescue everyone on board of the Admiral.

    500 people (76.33%) would be rescued.
    155 people (23.66%) would likely drown.
    Now, I tried to be finnicky and round up the variable before recasting it to an int, but alas, I seem to encounter the same snag as before when I do that. It looks like I will have to content myself with having that decimal place there. I hope that my professor will not dock me any points for that.

    But on the overall, I got it! Thank you so much for your wonderful help!!! :D :D :D

  7. #7
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem with rounding up in a school project

    You don't have to import anything. (But you already know that now...)

    It's rather instructive that declaring the answer to be double on the left hand side does not affect the integer calculation on the right hand side. The assignment of the answer to the double variable is a second operation that comes *after* evaluating what the answer is. So, all together, the order of things is:

    - evaluate integer expression on the right hand side
    - cast the result to double
    - assign the double version to the variable

    If you "lose" the fraction in the first step you will never get it back.

    -----

    As regards the horrible .0 you get for the number of lifeboats... Thinking about this I feel your logical instinct (or aesthetic predisposition or whatever) is correct. People and lifeboats are countable things in the sense that their multitude can be represented using Natural numbers. Lifeboat capacities are likewise Natural. So it really is a diversion for the calculation to involve floating point arithmetic.

    Try

    Java Code:
    int lifeboatsNeeded = passangersNumber + ((maxPassangersLifeboat - 1)) / maxPassangersLifeboat;
    This is what the last line of the code I posted was hinting at. Note that lifeboatsNeeded is now an int.

    Stare at that expression until it makes sense: it's such a useful piece of natural number arithmetic that it deserves to be mentally stored away somewhere.

  8. #8
    OlivierSillegue is offline Student in programming
    Join Date
    Feb 2012
    Location
    London, Ontario
    Posts
    6
    Rep Power
    0

    Default Re: Problem with rounding up in a school project

    I think I might have put the line of code at the wrong place... Here is what I obtained with this new calculation

    Enter the name of the ship: the Admiral
    Enter the number of passangers on board of the ship: 655
    Enter the maximum number of passangers that can fit in one lifeboat (same size boats): 50
    Enter the number of lifeboats that are available on board of the Admiral: 10

    Here are the results...

    A minimum of 655.0 lifeboats are required to rescue everyone on board of the Admiral.

    500 people (76.33%) would be rescued.
    155 people (23.66%) would likely drown.
    I replaced this line of code with the one you suggested to me

    Java Code:
    double lifeboatsNeeded = (double)passengersNumber / maxPassengersLifeboat;
    Where should I apply this suggestion?

  9. #9
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem with rounding up in a school project

    Sorry - I got the braces in the wrong place.

    Java Code:
    int lifeboatsNeeded = (passangersNumber + maxPassangersLifeboat - 1) / maxPassangersLifeboat;
    The point is you add almost a complete lifeboat full to the passengers and then you can use the truncating integer division.

  10. #10
    OlivierSillegue is offline Student in programming
    Join Date
    Feb 2012
    Location
    London, Ontario
    Posts
    6
    Rep Power
    0

    Default Re: Problem with rounding up in a school project

    Thank you so much! With your help, I was able to make my program exactly how I wanted. I really appreciate it.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Problem with rounding up in a school project

    You're welcome.

Similar Threads

  1. Need help with school project
    By camaro01 in forum New To Java
    Replies: 16
    Last Post: 02-20-2011, 06:34 PM
  2. School project help!
    By Addez in forum Advanced Java
    Replies: 5
    Last Post: 09-17-2010, 10:37 PM
  3. Need help with school project!
    By jaxxx in forum New To Java
    Replies: 5
    Last Post: 03-25-2010, 08:55 PM
  4. so very lost on school project....
    By Moltisanti in forum New To Java
    Replies: 19
    Last Post: 09-23-2009, 08:39 AM
  5. school project help
    By justin8790 in forum New To Java
    Replies: 5
    Last Post: 03-22-2009, 09:54 AM

Posting Permissions

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