CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Oct 2010
    Posts
    5

    Question Problem with -negative output

    Hey everybody, I am writing a java program that asks the user for a ship name that they are on, the amount of people on it, the amount of people that will fit into life boats and the amount of lifeboats on the ship.

    I have it nearly complete but after testing it I am getting a negative output.

    For example here is the output:

    ______________________________________________________

    Person-per-LifeBoat Calculation Program

    ______________________________________________________

    Please enter the name of your ship: Princeton
    How many people are on board the Princeton?800
    What is the maximum number of people that can be carried in one lifeboat? 100
    How many lifeboats are avaiable on the ship? 9

    The results are...

    A minimum of 8 lifeboats are required to rescue everybody on board the Princeton.
    900 people (112.5%) would be rescued.
    -100 people (-12.5) would be likely to drown.



    I am lost on how the program calculates 900 people @ 112.5%. I know the 100 extra it see's is from the -100 people that would drown, but how can I correct it so it would display:


    A minimum of 8 lifeboats are required to rescue everybody on board the Princeton.
    800 people (100%) would be rescued.
    0 people (0%) would be likely to drown.



    Any suggestions to help this novice?


    Code:
    import java.util.Scanner;
    
    
    public class LifeBoats
    
    {
    	public static void main(String[] args)
    	{
    		// Display a title
    		System.out.println("          ______________________________________________________\n");
    		System.out.println("                 Person-per-LifeBoat Calculation Program\n");
    		System.out.println("          ______________________________________________________\n");
    		
    		// Create a Scanner object for receiving user input
    		Scanner input = new Scanner(System.in);
    		
    		// Ask the user for the name of the ship - can include spaces
    		System.out.print("Please enter the name of your ship: ");
    		String shipName = input.nextLine();
    		
    		// Ask the user for the number of people on board the ship
    		System.out.print("How many people are on board the " + shipName + "?");
    		int numPeople = input.nextInt();
    				
    		// Ask the user the maximum number of people that can be carried by one lifeboat
    		System.out.print("What is the maximum number of people that can be carried in one lifeboat? ");
    		int peopleBoat = input.nextInt();
    		
    		// Ask the user how many actual lifeboats are available on the ship
    		System.out.print("How many lifeboats are avaiable on the ship? ");
    		int boatsAvail = input.nextInt();
    		
    		// Calculate the min number of lifeboats required to carry all the people
    		int totalBoats = numPeople / peopleBoat;
    		
    		// Display results		
    		System.out.println("\nThe results are...\n");
    		System.out.println("A minimum of " + totalBoats + " lifeboats are required to rescue everybody on board the " + shipName + ".");
    		
    		// Calculate the number of people to be rescued
    		int peopleRescu = boatsAvail * peopleBoat;
    		double rescuPerc = (double)100 * peopleRescu / numPeople;	
    		
    		// Calculate the number of people likely to drown.
    		int peopleDrown = numPeople - peopleRescu;
    		double drownPerc = 100.0 * peopleDrown / numPeople;
    		
    		// Display rescued calculation with percentage
    	
    	
    			System.out.println(peopleRescu + " people ("+ rescuPerc +"%) would be rescued.");
    			System.out.println(peopleDrown + " people ("+ drownPerc +") would be likely to drown.");
    		
    		
    		
    	} // end main
    
    } // end class

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with -negative output

    I am lost on how the program calculates 900 people @ 112.5%.
    Your calculation computes the number of lifeboats (9) times the number of people per boat (100) which equals 900 - where's the difficulty in working this out?

    I know the 100 extra it see's is from the -100 people that would drown
    No, it's the other way around. The -100 people drowning is because you have more people in lifeboats than you have on the ship.

    What you need to do is test the result of the number of people rescued computation and if it is greater than the number of people on the ship set the result to the number of people on the ship - after all you can't rescue more people than you have.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Oct 2010
    Posts
    5

    Re: Problem with -negative output

    Quote Originally Posted by keang View Post
    Your calculation computes the number of lifeboats (9) times the number of people per boat (100) which equals 900 - where's the difficulty in working this out?

    No, it's the other way around. The -100 people drowning is because you have more people in lifeboats than you have on the ship.

    What you need to do is test the result of the number of people rescued computation and if it is greater than the number of people on the ship set the result to the number of people on the ship - after all you can't rescue more people than you have.

    Thank you for the post.
    I should have reworded my thread, I do understand why the -100 shows up and how 100x9 =900, but I am pretty lost on how I would correct this by having the the max amount of people on this ship all in lifeboats.

    I do realized 800 people are on the ship and 100x9 is 900, but maybe i've been trying to figure this out too long but how could I make a line of code that would display 800 people instead of 900.

    Thank you for all the help

  4. #4
    Join Date
    Oct 2010
    Posts
    5

    Re: Problem with -negative output

    Edit: I might have figured it out

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with -negative output

    I've already told you what you need to do.

    for example

    Code:
    x = a * b;
    
    if ( x > c )
        x = c;
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Oct 2010
    Posts
    5

    Re: Problem with -negative output

    Quote Originally Posted by keang View Post
    I've already told you what you need to do.

    for example

    Code:
    x = a * b;
    
    if ( x > c )
        x = c;


    Thank you, I gave it a break yesturday as I did what I believe I was supposed to do and it corrected a part of it but made it wrong again lol. I've been referencing my other java programs for clarity but this one is something different then I've done before so excuse my ignorance.

    here is what I have
    Code:
    import java.util.Scanner;
    
    
    public class LifeBoats
    
    {
    	public static void main(String[] args)
    	{
    		// Display a title
    		System.out.println("          ______________________________________________________\n");
    		System.out.println("                 Person-per-LifeBoat Calculation Program\n");
    		System.out.println("          ______________________________________________________\n");
    		
    		// Create a Scanner object for receiving user input
    		Scanner input = new Scanner(System.in);
    		
    		// Ask the user for the name of the ship - can include spaces
    		System.out.print("Please enter the name of your ship: ");
    		String shipName = input.nextLine();
    		
    		// Ask the user for the number of people on board the ship
    		System.out.print("How many people are on board the " + shipName + "?");
    		int numPeople = input.nextInt();
    				
    		// Ask the user the maximum number of people that can be carried by one lifeboat
    		System.out.print("What is the maximum number of people that can be carried in one lifeboat? ");
    		int maxPerLifeBoat = input.nextInt();
    		
    		// Ask the user how many actual lifeboats are available on the ship
    		System.out.print("How many lifeboats are avaiable on the ship? ");
    		int numLifeBoatsAvail = input.nextInt();
    		
    		// Calculate the minimum number of lifeboats required to carry all the people
    		int numLifeBoatsRequired = numPeople / maxPerLifeBoat;
    		numLifeBoatsRequired++;
    		
    		// Display results		
    		System.out.println("\nThe results are...\n");
    		System.out.println("A priminimum of " + numLifeBoatsRequired + " lifeboats are required to rescue everybody on board the " + shipName + ".");
    		
    
    		int numPeopleRescued = maxPerLifeBoat * numLifeBoatsAvail;
    		
    		
    		if (numPeopleRescued > numPeople);
    		numPeopleRescued = numPeople;
    		{
    			System.out.println(numPeopleRescued + " people would be rescued.");
    		}
    	
    					
    		int numPeopleDrowned = numPeople - numPeopleRescued;
    		System.out.println(numPeopleDrowned + " people  would be likely to drown.");
    			
    	}
    	
    }

    I did cut out a few parts to make it easier for me to work with and so it's not to confusing, but I believe I followed your suggestion Keang and I'm still doing something wrong.

    Now I constantly get the same answer of:
    A priminimum of 9 lifeboats are required to rescue everybody on board the prince.
    800 people would be rescued.
    0 people would be likely to drown.


    instead of the answer which should be:
    A priminimum of 9 lifeboats are required to rescue everybody on board the prince.
    630 people would be rescued.
    170 people would be likely to drown.

  7. #7
    Join Date
    Oct 2010
    Posts
    6

    Re: Problem with -negative output

    Ah, someone else who's doing this project, most likely from the same class as me >: )
    If im correct you trying to get it to do this?


    **The Titanic Lifeboat Program**
    Enter the name of the ship: The Eagle
    Enter the number of people on board the The Eagle:800
    Enter the maximum number of people per lifeboat:90
    Enter the number of lifeboats that are available on the The Eagle:7
    Here are the results...
    A minimum of 8 lifeboats are required to rescue everyone on board.
    630 people (78.75&#37 would be rescued.
    170 people (21.25%) would likely drown.

    and

    **The Titanic Lifeboat Program**
    Enter the name of the ship: The Eagle
    Enter the number of people on board the The Eagle:800
    Enter the maximum number of people per lifeboat:120
    Enter the number of lifeboats that are available on the The Eagle:7
    Here are the results...
    A minimum of 7 lifeboats are required to rescue everyone on board.
    800 people (100.0%) would be rescued.
    0 people (0.0%) would likely drown.
    There would be room for an extra 40 people in the lifeboats

    I am basically done this code except I cannot get it to dispay the minimum lifeboats properly
    for example 800/90 = 8.88 therefore the program will display it as 8, because I made them integer's you can't display as doubles becasue you cannot have 8.88 lifeboats, I need it to say 9, i noticed yours does say 9 but that is due to your numLifeBoatsRequired++;
    I tried this idea, and even though it worked... it is a failure, becasue if you input 800/100 you'd think you would get 8 minimum lifeboats, but you get 9 actually becasue numLifeBoatsRequied++ always adds 1....

  8. #8
    Join Date
    Oct 2010
    Posts
    5

    Re: Problem with -negative output

    Quote Originally Posted by Jack2727 View Post
    Ah, someone else who's doing this project, most likely from the same class as me >: )
    If im correct you trying to get it to do this?


    **The Titanic Lifeboat Program**
    Enter the name of the ship: The Eagle
    Enter the number of people on board the The Eagle:800
    Enter the maximum number of people per lifeboat:90
    Enter the number of lifeboats that are available on the The Eagle:7
    Here are the results...
    A minimum of 8 lifeboats are required to rescue everyone on board.
    630 people (78.75%) would be rescued.
    170 people (21.25%) would likely drown.

    and

    **The Titanic Lifeboat Program**
    Enter the name of the ship: The Eagle
    Enter the number of people on board the The Eagle:800
    Enter the maximum number of people per lifeboat:120
    Enter the number of lifeboats that are available on the The Eagle:7
    Here are the results...
    A minimum of 7 lifeboats are required to rescue everyone on board.
    800 people (100.0%) would be rescued.
    0 people (0.0%) would likely drown.
    There would be room for an extra 40 people in the lifeboats

    I am basically done this code except I cannot get it to dispay the minimum lifeboats properly
    for example 800/90 = 8.88 therefore the program will display it as 8, because I made them integer's you can't display as doubles becasue you cannot have 8.88 lifeboats, I need it to say 9, i noticed yours does say 9 but that is due to your numLifeBoatsRequired++;
    I tried this idea, and even though it worked... it is a failure, becasue if you input 800/100 you'd think you would get 8 minimum lifeboats, but you get 9 actually becasue numLifeBoatsRequied++ always adds 1....
    Hey, yea I'm in CTY haha
    I only did the ++ as that's what we discussed in class.

    here is what I jotted down in notes:

    numlifeboatsneeded = numPeople / maxPerLifeBoat

    if there is a remainter to the above calculation, add one to numLifeBoatsNeeded


    That's why I added on the ++ at the end of numLifeBoatsNeeded, as you can't have 8.88 life boats but you have to round up to 9.

    am I wrong? probably lol

  9. #9
    Join Date
    Oct 2010
    Posts
    6

    Re: Problem with -negative output

    Its correct, but wrong because if you end up putting in that theres 800 passengers and 100 passengers per lifeboat, then your suppose to get a minimum of 8.0 lifeboats but because you have the ++ it becomes 9...

    It works perfect for something like 800 passengers and 90 per lifeboat becasue that equals into a decimal 8.88 meaning you have to add one because java always rounds down...

    Overall the ++ won't work for a non decimal answer like jus plain 8...

  10. #10
    Join Date
    Oct 2010
    Posts
    6

    Re: Problem with -negative output

    I've addressed the teacher about this issue but all he said was use Modulus Division
    BUt i don't understand how modulus works or where to even begin putting it in, I might just hand my project in the way it is and accept the lost mark or 2

  11. #11
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with -negative output

    If you think about what you need to achieve there are several ways of doing this.

    The modulus operator basically computes the remainder of a division operation, so the result of a modulus division by 100 will give a result from 0 to 99. If the result is 0 you know the there was no remainder ie (800 % 100 = 0) and the result of the division will give the exact number of lifeboats required whereas if there is a remainder (ie 805 % 100 = 5) you need an extra lifeboat.

    Another way of doing this is to add to the dividend the divisor-1 before doing the division ie (800 + (100-1)) / 100.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  12. #12
    Join Date
    Oct 2010
    Posts
    6

    Re: Problem with -negative output

    Quote Originally Posted by keang View Post
    If you think about what you need to achieve there are several ways of doing this.

    The modulus operator basically computes the remainder of a division operation, so the result of a modulus division by 100 will give a result from 0 to 99. If the result is 0 you know the there was no remainder ie (800 % 100 = 0) and the result of the division will give the exact number of lifeboats required whereas if there is a remainder (ie 805 % 100 = 5) you need an extra lifeboat.

    Another way of doing this is to add to the dividend the divisor-1 before doing the division ie (800 + (100-1)) / 100.
    DUde your a LIFE SAVIOR!!, I finally get how to apply modulus, due to your explanation so basically all I need to do is create an if statement to add 1 if there is a remainder or to leave it alone if its 0

  13. #13
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with -negative output

    so basically all I need to do is create an if statement to add 1 if there is a remainder or to leave it alone if its 0
    Yep.

    Personally I prefer the second approach as it guarantees the correct answer without any further processing but if your teacher has suggested using the modulus operator then use it.

    As I said before there are many ways of doing this for example you could also use floating point math and then use the Math.ceil() method to get the result you require.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  14. #14
    Join Date
    Oct 2010
    Posts
    6

    Re: Problem with -negative output

    Quote Originally Posted by keang View Post
    Yep.

    Personally I prefer the second approach as it guarantees the correct answer without any further processing but if your teacher has suggested using the modulus operator then use it.

    As I said before there are many ways of doing this for example you could also use floating point math and then use the Math.ceil() method to get the result you require.
    ONly problem with that way, is we haven't learned it lol

    Hmm I can't get the if statement to work right
    Code:
    int remainder = pplNumber % pplLifeboat;
    		System.out.println(remainder);
    	 if (remainder > 0);
    	 {
    		 minLifeboats++;
    	 }
    		System.out.println(minLifeboats);
    Its always adding 1 even if the remainder is over 0

  15. #15
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Problem with -negative output

    That's because you have a spurious semi-colon at the end of the if statement line.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured