Home > IB Computer Science 2015 > Conversion program (SL)

Conversion program (SL)

This is some sample code for the conversion program I asked SL to do. Note how the interaction with the user is brought under control by using a numbered menu system.

 

package conversionapp;
import java.util.Scanner;
public class ConversionApp {
    public static void main(String[] args) {
        int choice;
        double val, result=0;
        Scanner in = new Scanner(System.in);
        System.out.println("Welcome to the Conversion Program");
        System.out.println("1. Celsius to Fahrenheit");
        System.out.println("2. Fahrenheit to Celsius");
        System.out.println("3. Km to Miles");
        System.out.println("4. Miles to Km");
        System.out.println("Please enter your choice:");
        choice = in.nextInt();
        System.out.println("Please enter the value you want to convert:");
        val = in.nextDouble();

        if (choice == 1) {
            result = val * 9 / 5 + 32;
        } else if (choice == 2) {
            result = (val - 32) * 5 / 9;
        } else if (choice == 3) {
            result = val / 1.6;
        } else if (choice == 4) {
            result = val * 1.6;
        } else {
            System.out.println("Choice not recognised");
        }
        System.out.println("Conversion: " + result);
    }
}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s