Results 1 to 5 of 5

# Thread: Help with IF ELSE

- 09-21-2010, 01:44 AM #1Member
- Join Date
- Sep 2010
- Posts
- 4

- Rep Power
- 0

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

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

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.Last edited by LiquidJava; 09-21-2010 at 02:43 AM.

- 09-21-2010, 01:59 AM #2Member
- Join Date
- Sep 2010
- Posts
- 21

- Rep Power
- 0

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 }

Last edited by jlmp; 09-21-2010 at 02:18 AM.

- 09-21-2010, 02:13 AM #3Member
- Join Date
- Sep 2010
- Posts
- 56

- Rep Power
- 0

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

- 09-21-2010, 02:30 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1

- Rep Power
- 25

- 09-29-2010, 04:32 PM #5Member
- Join Date
- Sep 2010
- Posts
- 4

- Rep Power
- 0

## Bookmarks