Java program compiles, but hangs when run

Discussion in 'Software' started by Anon-5805b4582a, Feb 13, 2006.

  1. Anon-5805b4582a

    Anon-5805b4582a Anonymized

    Hi, I need a little help with a java program i'm writeing.

    My PC
    Kingston ValueRAM 1GB (2 x 512MB) 184-Pin DDR SDRAM DDR 400 (PC 3200)
    Intel Celeron D 335 Prescott 533MHz FSB Socket 478 Processor
    ASUS P4P800-E DELUXE Socket 478 Intel 865PE
    JetWay 96XT-AD-256C Radeon 9600XT 256MB DDR AGP 4X/8X Video Card
    COOLMAX "EZ Wire" CU-400T ATX 400W Power Supply
    2 x Seagate 160 GB SATA in RAID 0
    Windows XP Pro SP2

    Well her is my code.

    Code:
    //import JOptionPane
    import javax.swing.JOptionPane;
    
    public class FindSalesAmount 
    {
      // The commission sought constant
      final static double COMMISSION_SOUGHT = 25000;
      
      //main method
      public static void main(String[] args) 
      {
        double salesamount = 0;
        
        do
        {
          double high = .01;
          double low = 1000000;
          //binary search
          while (high >= low) 
          {
            double mid = (low + high) / 2;
            if (getCommission(mid) == COMMISSION_SOUGHT)
              mid = salesamount;
            else if (getCommission(mid) < COMMISSION_SOUGHT)
              low = mid + 1;
            else
              high = mid - 1;
          }
          
        }
        while (salesamount != COMMISSION_SOUGHT);
        
        
        
        // Display the sales amount
        JOptionPane.showMessageDialog(null, "the sales amount needed is " + salesamount , "Sales Amount", JOptionPane.INFORMATION_MESSAGE);
      }
      public static double getCommission (double sales_amount)
      {
        if (sales_amount > 10000)
          return 900 + (sales_amount - 10000) * .12;
        
        else if (sales_amount > 5000)
          return 400 + (sales_amount - 5000) * .1;
        
        else return sales_amount * .08;
      }
    }
    The program is supposed to finde the sales amount needed to make a commission of $25000 a year. The commission is calculated based on how much you sell if you get 8% commission on the first 5000, 10% from 5000.01 - 10000, and 12% on 10000.01 and up. Also you get 5000 a year no matter how much you sell. Also i'm trying to use a binary search to figure it out so the problem proabably hase something to do with the loop.

    The first question I have is why the compiler wants me to put a simicolon after my while (while (salesamount != COMMISSION_SOUGHT)) statement?

    second why dose the program never show an output message? Did i forget to put something in my loop? Is it just going to run forever?

    Thank you for any help.
     
  2. QuickSilver

    QuickSilver Corporal

    Yes that is correct. You have both a do-while loop and a while loop implemented in the code, both correctly. A while loop doesn't have a semi-colon on the end of its line, but a do-while does. A while loop is not guarunteed to run once. When the code hits it it says "Ok, I need to execute this next block of code until this condition is met. Is the condition met?" If the condition is met then it doesn't get executed. If it isn't hten it does. Ad infinitum.
    A do-while is slightly different. The code says "Ok I need to do this set of code here..." which it does, and then says "Have i met this condition?" It will always run the block of code once, subsequently the actual loop condition is at teh end of the block of code, and is terminated properly with a ';' as you've done. Don't worry its fine.


    The reason you're not showing an output is simply because your do-while loop (the outer one) will run forever and the code will never break out of it. Look at what you're evaluating.
    Code:
    while (salesamount != COMMISSION_SOUGHT);
    You're saying run this until salesamount is equal to COMMISSION_SOUGHT. Fair enough, except that in your code you never alter the value of salesamount from when its initialised, and so it is always not-equal to COMMISSION_SOUGHT and the code just keeps on running...

    Does that make sense?

    Your binary search implementation looks alright at a glance but I haven't had a chance to run it - I don't have java set up on this machine...

    Heres something to try though.

    When you're stuck like this put loads of print statements in the code. Print the values of each loop variable out as each loop iteration runs and add an input at the start of each loop so the program waits for you to hit return (and doesn't overwhelm you with an infinite stream of output) so you can check the values of each variable step by step. This will give you a huge picture of whats going on, allow you to break out the code and change things to your liking and then re-run and tweak it until you understand why the program is doing something unexpected.

    This looks like a great start to solving the problem though! You're definitely on the right track and wiht a bit of tweaking I think you'll find you're there.

    Hope this helps!
     
  3. Anon-5805b4582a

    Anon-5805b4582a Anonymized

    Hey, thanks for the help. I finaly got it to work the way i wanted it to. I had to make a method for the binary search so that i could return the amount. the major problem was that the loop was inside the main method which was void.

    The advice about the print statements helped, I never thought about that. Anyway here is the final code if anyone is interested is seeing how it turned out.

    Code:
    //import JOptionPane
    import javax.swing.JOptionPane;
    
    public class FindSalesAmount 
    {
      // The commission sought constant
      final static double COMMISSION_SOUGHT = 25000;
      
      //main method
      public static void main(String[] args) 
      {
    
        
        // Display the sales amount
        String output =
          "The sales amount $" + (int)((binarySearch(10000000, .01)) * 100) / 100.0 +
          "\nis needed to make a commission of $" + COMMISSION_SOUGHT;
        JOptionPane.showMessageDialog(null, output,
          "Example 3.8 Output", JOptionPane.INFORMATION_MESSAGE);
        
      }
      public static double getCommission (double sales_amount)
      {
        if (sales_amount > 10000)
          return 900 + (sales_amount - 10000) * .12;
        
        else if (sales_amount > 5000)
          return 400 + (sales_amount - 5000) * .1;
        
        else return sales_amount * .08;
      }
      
      public static double binarySearch (double high, double low)
      {
        double salesamount = 0;
        
        do
          
        {
           
          //binary search
          while (high >= low)
          {
            double mid = (low + high) / 2;
            if (getCommission(mid) == COMMISSION_SOUGHT)
              
              return mid;
            
            else if (getCommission(mid) < COMMISSION_SOUGHT)
              low = mid + 1;
            else
              high = mid - 1;
          }
          return low;
        }
      while (salesamount != COMMISSION_SOUGHT);
    
      }
    }
    Thanks agian for the help.
     

MajorGeeks.Com Menu

Downloads All In One Tweaks \ Android \ Anti-Malware \ Anti-Virus \ Appearance \ Backup \ Browsers \ CD\DVD\Blu-Ray \ Covert Ops \ Drive Utilities \ Drivers \ Graphics \ Internet Tools \ Multimedia \ Networking \ Office Tools \ PC Games \ System Tools \ Mac/Apple/Ipad Downloads

Other News: Top Downloads \ News (Tech) \ Off Base (Other Websites News) \ Way Off Base (Offbeat Stories and Pics)

Social: Facebook \ YouTube \ Twitter \ Tumblr \ Pintrest \ RSS Feeds