# A better way to do things?

• January 17th, 2011, 02:11 PM
SnarkKnuckle
A better way to do things?
As I was coding this and finally got it working, yes I am new with Java programming, and programming in general, I was wondering if theres not a better way of coding this, especially with the variables? Maybe not, just curious.

Code :

```// 2.35: Daily Driving Cost Calculator // Calculates your daily driving cost import java.util.Scanner; // Imports the class Scanner   public class DrivingCost { public static void main( String[] args ) { //Creating scanner to accept user input Scanner input = new Scanner( System.in );   double miles; //Obtains users total miles driven double gasprice; //Obtains users cost per gallon of gas double milespergal; //Obtains users miles per gallon double parkingfees; //Obtains users daily parking fees double tolls; //Obtains users daily driving tolls double gallonsused; // Holds gallons used double gasmoney; // Holds Gas Money amt double total; // Holds total of all sums   System.out.print( "Enter total miles driven daily: "); //Prompt miles = input.nextDouble(); //Captures User Input   System.out.print( "Enter cost per gallon of gas: "); //Prompt gasprice = input.nextDouble(); //Captures User Input   System.out.print( "Enter your average miles per gallon: "); //Prompt milespergal = input.nextDouble(); //Captures User Input   System.out.print( "Enter daily parking fees: "); //Prompt parkingfees = input.nextDouble(); //Captures User Input   System.out.print( "Enter any tolls per day: "); //Prompt tolls = input.nextDouble(); //Captures User Input   gallonsused = miles / milespergal; //Calculates total gallons of gas used gasmoney = gallonsused * gasprice;   total = gasmoney + parkingfees + tolls;   System.out.printf( "Your total daily driving cost is %.2f\n", total ); //Displays total amt   } //End main method } //End class DrivingCost```
• January 17th, 2011, 03:12 PM
newbie
Re: A better way to do things?
I'm no Java expert, but for the type of program that it is, I'd say its absolutely fine.

Only thing you could consider is
Code java:

``` double miles,gasprice,milespergal, parkingfees,tolls,gallonsused,gasmoney,total;```
instead of listing them.. but it does not say its better!
The way you've done it provides good information via comment, where you wouldn't really be able to using that format. I assume it's all preference.

For GUI's, I tend to use above format for a lot of components, but use your approach when variables have more meaning.

A more experienced programmer might correct me on this though :P
• January 17th, 2011, 03:25 PM
copeg
Re: A better way to do things?
I'll start off with an idiom: If it ain't broke don't fix it. ;) Now that that's out of the way...java is an object-oriented language. Not writing programs in an object oriented fashion means you loose the power (and fun) associated with OOP. For something this simple it may seem overkill, but what if you want to build this into something more...placing everything in the main method can get ugly real quick.
• January 17th, 2011, 03:39 PM
newbie
Re: A better way to do things?
Here is just a small way for reducing main method size for ya :P

Code java:

```// 2.35: Daily Driving Cost Calculator // Calculates your daily driving cost import java.util.Scanner; // Imports the class Scanner   public class DrivingCost {   public static void main(String[] args) { //Creating scanner to accept user input Scanner input = new Scanner(System.in); double miles; //Obtains users total miles driven double gasprice; //Obtains users cost per gallon of gas double milespergal; //Obtains users miles per gallon double parkingfees; //Obtains users daily parking fees double tolls; //Obtains users daily driving tolls double gallonsused; // Holds gallons used double gasmoney; // Holds Gas Money amt double total; // Holds total of all sums   miles = requestInput("Enter total miles driven daily: ", input); gasprice = requestInput("Enter cost per gallon of gas: ", input); milespergal = requestInput("Enter your average miles per gallon: ", input); parkingfees = requestInput("Enter daily parking fees: ", input); tolls = requestInput("Enter any tolls per day: ", input);   gallonsused = miles / milespergal; //Calculates total gallons of gas used gasmoney = gallonsused * gasprice; total = gasmoney + parkingfees + tolls;   System.out.printf("Your total daily driving cost is %.2f\n", total); //Displays total amt   } //End main method   public static double requestInput(String s, Scanner input) { System.out.println(s); return input.nextDouble(); } } //End class DrivingCost```
• January 18th, 2011, 12:52 PM
SnarkKnuckle
Re: A better way to do things?
Thanks for the replies ;)