Skip to content

Instantly share code, notes, and snippets.

@Caleb2501
Created May 19, 2013 00:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Caleb2501/5606177 to your computer and use it in GitHub Desktop.
Save Caleb2501/5606177 to your computer and use it in GitHub Desktop.
This is my second effort in Java. I modified my original Python program to function in Java. I think t turned out pretty well. The process allowed me to write the code without having to think too hard about what exactly I was making and how I could make it calculate correctly . I will probably do the same with most of the other challenges.
// Create a calculator application that has use in your life.
// It might be an interest calculator, or it might be something
// that you can use in the classroom.
// I created this originally in Python, and this is a conversion to Java.
import java.util.Scanner;
public class Challenge2{
public static void main(String[] agrs){
int milesPerGallon, miles;
float gasPrice, mileCost;
Scanner input = new Scanner(System.in);
//receive data
System.out.print("What is the average MPG of your car? ");
milesPerGallon = input.nextInt();
System.out.print("How many miles do you drive to work? ");
miles = input.nextInt();
System.out.print("What is the price per gallon for gas? ");
gasPrice = input.nextFloat();
// give results
miles = miles * 2;
System.out.println("We will double the miles for the round trip.");
mileCost = (miles/milesPerGallon) * gasPrice;
System.out.println("Here are the stats for your commute cost.");
System.out.println("Round trip: " + mileCost);
System.out.println("Five days a week: " + (mileCost * 5) + "per week.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment