If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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

Re: Problem with -negative output

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.

Re: Problem with -negative output

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.

Re: Problem with -negative output

Originally Posted by keang

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.

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....

Re: Problem with -negative output

Originally Posted by Jack2727

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.

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...

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

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.

Re: Problem with -negative output

Originally Posted by keang

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