Results 1 to 5 of 5
  1. #1
    LiquidJava is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default Help with IF ELSE

    I got this question to complete in college recently. I am new to Java so slowly learning, there could be some mistakes

    Calculations and if – else (the sponsored cyclist). A cyclist is sponsored at the rate of 7c for each km up to 10 km, and 10c for each km in excess of 10km. Write a program which will ask for the cyclist’s first name, initial and last name and the distance cycled. Your program should calculate the money due, then display a dialog containing the cylist’s full name (first name + initial + last name), the distance cycled, and the money due. Note here that the problem involves 2 different rates if the number of km exceeds 10 so, for example, if the cyclist is sponsored for 15km then the first 10km are sponsored at 7c and the remaining 5 km are sponsored at 10c giving a total of 70c + 50c i.e. €1.20. Your solution should work correctly no matter what the distance is.
    This is what I have done so far
    Java Code:
    import javax.swing.JOptionPane;
    public class Cyclist{
    	public static void main (String args []){
    
    		String firstName,lastName,initialAsString;
    		char initial;
    		int distanceTravelled;
    
    
    		firstName = JOptionPane.showInputDialog ("Please enter your name");
    		initialAsString = JOptionPane.showInputDialog ("Please enter your initial");
    		initial = initialAsString.charAt(0);
                    lastName = JOptionPane.showInputDialog ("Please enter your last name");
    		distanceTravelled = Integer.parseInt (JOptionPane.showInputDialog ("Please enter the distance travelled"));
    
    		if (distanceTravelled < 10){
    			JOptionPane.showMessageDialog(null, "Your name is: "+firstName+ " ."+initial +
    				" "+lastName+"\nDistance Travelled: "+distanceTravelled+"km\nThe amount " +
    					" of money due is " +distanceTravelled*7);
    		}else{
    
    		if (distanceTravelled > 10){
    			JOptionPane.showMessageDialog(null, "Your name is: "+firstName+ " ."+initial +
    				" "+lastName+"\nDistance Travelled: "+distanceTravelled+"km\nThe amount " +
    					" of money due is " +distanceTravelled*10);
    		}
         
              }
    
        }
    
    }
    I'm having a problem with this part
    if the cyclist is sponsored for 15km then the first 10km are sponsored at 7c and the remaining 5 km are sponsored at 10c giving a total of 70c + 50c i.e. €1.20. Your solution should work correctly no matter what the distance is.
    I would appreciate some help. Thanks
    Last edited by LiquidJava; 09-21-2010 at 02:43 AM.

  2. #2
    jlmp is offline Member
    Join Date
    Sep 2010
    Posts
    21
    Rep Power
    0

    Default

    Your problem is algorithmic...you can make different solutions for this:

    I am editing this: Lets see (cause i am sleepless), think about this, when you take a number which is over 10 all you have to do is take the first 10 and multiply with 7 and the rest and multiply it with 10...so

    if you have 25 you can do something like this 25-10=15!

    so,
    Java Code:
    if (distanceTravelled > 10){
    // multiply 10*7 and put the value in a temp int
    // distanceTravelled=distanceTravelled-10;
    // then mutiply the distanceTravelled with 10 and then add this with temp
    }
    This maybe a solution for your problem but i am saying that u can solve it with different ways!
    Last edited by jlmp; 09-21-2010 at 02:18 AM.

  3. #3
    javaman1 is offline Member
    Join Date
    Sep 2010
    Posts
    56
    Rep Power
    0

    Default

    I agree with Jlmp however please enclose your code in [ code] and [/ code], without the spaces, next time.

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by LiquidJava View Post
    I'm having a problem with this part I would appreciate some help. Thanks
    What you've tried so far, can you show it here?

  5. #5
    LiquidJava is offline Member
    Join Date
    Sep 2010
    Posts
    4
    Rep Power
    0

    Default

    The only code I have done so far is the code I have posted.

Posting Permissions

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