Quantcast

Maximum PC

It is currently Fri Apr 18, 2014 4:49 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: How to round up and only up?
PostPosted: Sat Feb 11, 2012 2:32 pm 
8086
8086

Joined: Sat Feb 11, 2012 2:08 pm
Posts: 2
Hello,

I have a project where I need to calculate how many lifeboats I would need on board according to user input of amount of passangers and capacity of lifeboat.

Here is what I have so far.

Code:
/**
*       Program:      K_D_Lifeboats.java
*       Author:       K. Dagenais
*      Student #:       0631794
*      Program:      CPA
*      Course:         INFO1150
*      Date:         11 févr. 2012
*      Description:   Program that calculates how many lifeboats are required on a given ship given the amount of people onboard
*                  capacity of the lifeboats
*/

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.");
      }

   }

}


This is what I get as a result when the program compiles.

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


So far so good, I get the right percentage, and I get the right amount of lifeboats. But when I change to a number that gives decimals after the calculations, here is what I get.

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


As you can see, I still get 13 boats there, even if it should be 14!! Now I tried several methods (Math.round() and Math.ceil(), casting, etc.)... Well, there's just so many things I tried as I hunted on the internet to find help. Nothing worked for me. If my lifeboatsNeeded's decimals are lower than .5, than automatically it will round it down. I just started my course in Programming, so I have just seen the Scanner class. I am at my wits end.

Please can anyone help me? Please?


Top
  Profile  
 
 Post subject: Re: How to round up and only up?
PostPosted: Mon Feb 13, 2012 5:58 am 
Million Club - 2 Plus
Million Club - 2 Plus

Joined: Thu Aug 02, 2007 11:07 pm
Posts: 2622
Location: NC
Welcome to the forum!

First off, I haven't done programming in years, but I think this is probably the most likely problem:
Quote:
double lifeboatsNeeded = passangersNumber / maxPassangersLifeboat;

You define the variable lifeboatsneeded as a double instead of an integer. It should work if you keep it as an integer, keeping the values full whole numbers. If I remember correctly, it will round up even if you are below the .5 threshold for math rounding. So 4.1 would display the value of 5.


Top
  Profile  
 
 Post subject: Re: How to round up and only up?
PostPosted: Tue Feb 14, 2012 7:08 pm 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Jbaz has made two small mistakes .. not his fault, he is new to this section and hasn't programmed in a while.

First mistake:
We don't fix homework assignments. We do talk you through them, though.

You need to examine the logic of your program. At the very least, you need to identify where the problem lies.

Code:
double lifeboatsNeeded = passangersNumber / maxPassangersLifeboat;
...
System.out.println("A minimum of " + (int)lifeboatsNeeded + " lifeboats are required to rescue everyone on board of " + shipName + ".\n");


The first line determines the number of lifeboats needed. Easy enough.
The second line casts the double as an integer. This, I assume, is your attempt to round the answer. This is also the solution that Jbaz offered.

So ... when java casts a double to an int ... what does it do? If you don't know the answer .. why are you doing it? Obviously, it isn't doing what you want. When you cast a double as an int, java simply discards everything after the decimal.

Code:
double smallNine = 9.1
int nine = (int) smallNine


Here, the value of nine is 9

Code:
double bigNine = 9.9
int nine = (int) bigNine


Here, the value of nine is .. also 9.

So, if you wanted to always round DOWN, you'd have your answer.

What you want to do, though, is always round up. The only time you don't round up is when you have a integer.
9.1 ... round up
9.5 ... round up
9.0 ... do not round

Thus, what you need to do is identify whether your value is an integer and, if so, leave it alone. If not, add one and discard everything after the decimal. I've already explained how to get rid of everything after the integer. What you need to do is figure out how to determine if your number is an integer. Hint: modulus.


Top
  Profile  
 
 Post subject: Re: How to round up and only up?
PostPosted: Tue Feb 14, 2012 9:14 pm 
8086
8086

Joined: Sat Feb 11, 2012 2:08 pm
Posts: 2
Hello,

Thank you for kindly responding to my post. I must say though that whilst I was waiting for it to be approved for showing by the moderators, I was able to find the answer to my problem on my own. :) I came up with an answer that allowed me to keep the integer value, as I didn't enjoy seeing the .0 in my output.

I am very grateful to both of you for taking the time to analyse my program and giving me your ideas. It is very appreciated.


Top
  Profile  
 
 Post subject: Re: How to round up and only up?
PostPosted: Mon Mar 26, 2012 10:40 am 
Thoroughbred
Thoroughbred
User avatar

Joined: Sat May 07, 2011 12:30 pm
Posts: 1922
Location: A place not actively occupied by something else.
Jipstyle wrote:
Jbaz has made two small mistakes .. not his fault, he is new to this section and hasn't programmed in a while.

First mistake:
We don't fix homework assignments. We do talk you through them, though.

You need to examine the logic of your program. At the very least, you need to identify where the problem lies.

Code:
double lifeboatsNeeded = passangersNumber / maxPassangersLifeboat;
...
System.out.println("A minimum of " + (int)lifeboatsNeeded + " lifeboats are required to rescue everyone on board of " + shipName + ".\n");


The first line determines the number of lifeboats needed. Easy enough.
The second line casts the double as an integer. This, I assume, is your attempt to round the answer. This is also the solution that Jbaz offered.

So ... when java casts a double to an int ... what does it do? If you don't know the answer .. why are you doing it? Obviously, it isn't doing what you want. When you cast a double as an int, java simply discards everything after the decimal.

Code:
double smallNine = 9.1
int nine = (int) smallNine


Here, the value of nine is 9

Code:
double bigNine = 9.9
int nine = (int) bigNine


Here, the value of nine is .. also 9.

So, if you wanted to always round DOWN, you'd have your answer.

What you want to do, though, is always round up. The only time you don't round up is when you have a integer.
9.1 ... round up
9.5 ... round up
9.0 ... do not round

Thus, what you need to do is identify whether your value is an integer and, if so, leave it alone. If not, add one and discard everything after the decimal. I've already explained how to get rid of everything after the integer. What you need to do is figure out how to determine if your number is an integer. Hint: modulus.

It helped me with my rounding up problem (Health and division and non-integer problems in version 2.0 of my laser tag Arduino program). I never thought about it this way... Cool!


Top
  Profile  
 
 Post subject: Re: How to round up and only up?
PostPosted: Tue Mar 27, 2012 11:54 am 
Java Junkie
Java Junkie
User avatar

Joined: Mon Jun 14, 2004 10:23 am
Posts: 24218
Location: Granite Heaven
Glad I could help. :)


Top
  Profile  
 
 Post subject: Re: How to round up and only up?
PostPosted: Mon Apr 23, 2012 2:34 pm 
Bitchin' Fast 3D Z8000*
Bitchin' Fast 3D Z8000*
User avatar

Joined: Tue Jun 29, 2004 11:32 pm
Posts: 2555
Location: Somewhere between compilation and linking
OlivierDeSillegue wrote:
As you can see, I still get 13 boats there, even if it should be 14!! Now I tried several methods (Math.round() and Math.ceil(), casting, etc.)... Well, there's just so many things I tried as I hunted on the internet to find help. Nothing worked for me. If my lifeboatsNeeded's decimals are lower than .5, than automatically it will round it down. I just started my course in Programming, so I have just seen the Scanner class. I am at my wits end.

Please can anyone help me? Please?


Since the homework issue isn't a concern anymore, I'll provide a short terse answer. Whenever we want to "always round down" or "always round up", what we're actually attempting to accomplish is known as a floor or ceiling function. Also, we're not really attempting to "round" a number either. The definition for these functions is pretty straight-forward:

Code:
floor(x) = max(m in Z such that m <= x) and ceiling(x) = min(m in Z such that m >= x) ... where x is a real number and Z is the set of integers.

Thus, the floor(1) == ceiling(1) == 1 (for all x in Z).

Most programming languages have built-in ceiling() and floor() functions, but let's assume that we're working in an environment that doesn't provide these functions. How do we write them? Notice at the bottom of the Wikipedia article that older versions of Excel compute ceiling() incorrectly for negative values - ceiling(-4.5) is -4, not -5. Given the definition, the function argument is going to be a real number (ie a double floating-point value in most programming languages) and return an integer. I'll leave the implementation as an exercise for interested readers, but highly recommend that everyone spend at least a little time writing these functions in a few different styles of programming languages. They're a surprisingly good gauge of programming maturity and pretty typical of the type of programming problem that you can expect to see when interviewing at a Google or Microsoft.


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group