Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: A better way to do things?

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    // 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


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default 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
    		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
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

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

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: A better way to do things?

    Here is just a small way for reducing main method size for ya

    // 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
    Last edited by newbie; January 17th, 2011 at 06:03 PM.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    14
    My Mood
    Cheerful
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: A better way to do things?

    Thanks for the replies

Similar Threads

  1. Something wrong with the import things
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 25th, 2010, 07:30 AM
  2. Displaying things in gui
    By KrisTheSavage in forum AWT / Java Swing
    Replies: 2
    Last Post: March 29th, 2010, 12:21 PM
  3. XML, and other things.
    By Tortex in forum The Cafe
    Replies: 0
    Last Post: March 27th, 2010, 03:33 PM
  4. How would i do these things??
    By Curious in forum Java Theory & Questions
    Replies: 4
    Last Post: February 21st, 2010, 08:33 PM
  5. [SOLVED] Prime number generator program in Java
    By big_c in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 27th, 2009, 12:08 PM