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

Thread: Begineer mistakes, need help (very basic)

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Begineer mistakes, need help (very basic)

    I started java about 3 week ago, I have no experience in computer science at all, except some 3D artist ones
    I work very hard but I can't understand anything about the structure, syntax and all.
    I am overwhelmed by my work and I find it hard to keep up with the group.

    This is my first java code, it's about calculating a car average speed (km per hour) and consumption per minute

    Forgive my english I am not a native speaker at all.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Begineer mistakes, need help (very basic)

    Quote Originally Posted by skeletonbutler View Post
    This is my first java code
    ...Where

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Begineer mistakes, need help (very basic)

    import java.text.DecimalFormat;
     
    import javax.swing.JOptionPane;
     
    public class Cars {
    	public double converthourminutes(int byMinute) {
    		double nbHours = (double) byMinute / 60;
    		return nbHours;
    	}
     
    //this convert minutes into hours
    //I guess it works
     
     
    	public static int askInteger(String byMessage) {
    		int integer= 0;
    		integer = Integer.parseInt(JOptionPane.showInputDialog(null, byMessage,
    				"Saisie", JOptionPane.QUESTION_MESSAGE));
    		return integer;
    	}
     
     
     
     
    	public double calculateaverageconsumption(int byDistance, int byConsommation) {
    		float calculateaverageconsumption = (float) byDistance / byConsommation;
     
    		return averageconsumption;
    	}
     
    	public static void main(String[] args) {
    		int distance, consumption, duration, averageconsumption;
    		double nbHours = 0f, averagespeed = 0f;
    	}
     
    	public float calculateaveragespeed(int byDistance, float byDuration) {
     
    		float averagespeed = byDistance / byDuration;
     
    		return averagespeed;
     
    		consumption = Cars.askInteger(" fuel consumption :");
    		duration = Cars.askInteger(" time of the travel in minute");
    		distance = Cars.askInteger("kilometers travelled");
     
    		nbHours = Cars.converthourminutes(duration);
     
    		averagespeed = Cars.calculateaveragespeed(distance, nbHours);
     
    		averageconsumption = Cars.calculateaverageconsumption(distance,
    				consumption);
     
    		JOptionPane.showMessageDialog(null,
    				" Your speed is : " + speed.format(averagespeed)
    						+ " km per hour" + "Your average consumption is: "
    						+ averageconsumption + " kilometre per liter",
    				JOptionPane.INFORMATION_MESSAGE);
     
    	}
    }


    sorry I thought I had it on attachment

    Where should I put my main function? Eclipse doesn't find it anywhere even though I added public static void main(String[] args) {

  4. #4
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Begineer mistakes, need help (very basic)

    Ok, now repost using code tags. And asking questions might help too.

  5. #5
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Begineer mistakes, need help (very basic)

    Quote Originally Posted by skeletonbutler View Post
    Eclipse doesn't find it anywhere even though I added public static void main(String[] args) {
    What do you mean by this? If there is an error message, copy-paste it here. Are you trying to export or run within the ide?

  6. #6
    Member
    Join Date
    Jul 2013
    Posts
    47
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Begineer mistakes, need help (very basic)

    public double calculateaverageconsumption(int byDistance, int byConsommation) {
    		float calculateaverageconsumption = (float) byDistance / byConsommation;
     
    		return averageconsumption;
    	}

    This is going to give you trouble. You return a non-existent variable. You called the var "calculateaverageconsumption." Then you return "averageconsumption."

    -summit45

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Begineer mistakes, need help (very basic)

    After reading post #6...
    public float calculateaveragespeed(int byDistance, float byDuration) {
     
    		float averagespeed = byDistance / byDuration;
     
    		return averagespeed;//Method returns here
     /*-------------- All code below this line can not be reached --------------*/
    		consumption = Cars.askInteger(" fuel consumption :");
    		duration = Cars.askInteger(" time of the travel in minute");
    		distance = Cars.askInteger("kilometers travelled");
     
    		nbHours = Cars.converthourminutes(duration);
     
    		averagespeed = Cars.calculateaveragespeed(distance, nbHours);
     
    		averageconsumption = Cars.calculateaverageconsumption(distance,
    				consumption);
     
    		JOptionPane.showMessageDialog(null,
    				" Your speed is : " + speed.format(averagespeed)
    						+ " km per hour" + "Your average consumption is: "
    						+ averageconsumption + " kilometre per liter",
    				JOptionPane.INFORMATION_MESSAGE);
     
    	}

Similar Threads

  1. Inheritance problems
    By 93tomh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 15th, 2012, 06:01 AM
  2. Problems with my HW
    By hws in forum Object Oriented Programming
    Replies: 14
    Last Post: December 30th, 2011, 12:18 PM
  3. Problems with ItemEvents
    By bgroenks96 in forum Java Theory & Questions
    Replies: 5
    Last Post: June 24th, 2011, 02:27 PM
  4. [SOLVED] If statement problems
    By SnarkKnuckle in forum Loops & Control Statements
    Replies: 2
    Last Post: January 6th, 2011, 11:51 PM
  5. 2 problems...
    By Day2Day in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 22nd, 2010, 02:51 PM