Results 1 to 6 of 6
  1. #1
    Alpinstang is offline New to Java
    Join Date
    Sep 2012
    Location
    University of Memphis
    Posts
    5
    Rep Power
    0

    Default Classes and Objects with an External Class file - Help requested

    My java class has an assignment to write two class files, one called Vehicle and one called Cars.
    • Declare a class called Vehicle that has the following double type members: fuel_cap, mpg
    • Declare a public class called Cars that will create objects of the Vehicle class for each of the vehicle types shown above, then assign the variables specified to each implementation of the object variable.


    We were never given an explicit example to reference and I'm having trouble finding what is wrong with my syntax. :/
    Sorry for asking but this is going to happen soon

    Here is the code:
    Java Code:
    import java.io.*;
    
    public class Vehicle {
    		
    		public static double fuel_cap;
    		public static double mpg;
    		
    		Vehicle()
    		{
    		}
    
     	        double CalculateToyotaRange(double fuel_cap, double mpg)
    	        {
    		      return(fuel_cap * mpg);
    	        }	
    }
    Java Code:
    public class Cars {
        public static void main(String[] args) 
    	{
                    Vehicle vhcl = new Vehicle();
    		
    		double vhcl.CalculateToyotaRange(15.0,10);
            }
    }
    My tabs got all messed up when I posted this, sorry it was standard.
    Last edited by Alpinstang; 10-06-2012 at 01:59 AM. Reason: code looks crappy

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Classes and Objects with an External Class file - Help requested

    Do you have a java programming question?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Alpinstang is offline New to Java
    Join Date
    Sep 2012
    Location
    University of Memphis
    Posts
    5
    Rep Power
    0

    Default Re: Classes and Objects with an External Class file - Help requested

    Yes, do you know where I could find an example java program using an external class file?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Classes and Objects with an External Class file - Help requested

    As that isn't standard terminology, you need to tell us what you mean by
    an external class file
    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    Alpinstang is offline New to Java
    Join Date
    Sep 2012
    Location
    University of Memphis
    Posts
    5
    Rep Power
    0

    Default Re: Classes and Objects with an External Class file - Help requested

    Here is the working code. Sorry I wasn't describing myself properly.
    Java Code:
    package Cars.Vehicle;
    
    public class Vehicle
    {
    	public double fuel_cap = 0.0;
    	public double mpg = 0.0;
    	public double range = 0.0;
    	
    	public Vehicle() // constructor for class Vehicle...
    	{
    	}
    	public double CalculateRange(double fuel_cap, double mpg)
    	{
    		return(fuel_cap * mpg);
    	}
    }
    Java Code:
    import Cars.Vehicle.*;
    
    public class Cars
    {
    	public static void main(String[] args)
    	{
    		try
    		{
    			HondaRange();
    			
    			System.out.println("Press any key to end the program.");
    			System.in.read();
    		}
    		catch(Exception exc)
    		{
    			System.out.println("Error!\n Details: " + exc);
    		}
    	}
    	private static void HondaRange()
    	{
    		Vehicle vh = new Vehicle();		
    		vh.fuel_cap = 15.0;				
    		vh.mpg = 21.8;
    		try
    		{
    			double range = vh.CalculateRange(vh.fuel_cap, vh.mpg);
    
    			System.out.println("The Honda's average MPG is " + vh.mpg + ".");
    			System.out.println("The Honda's fuel capacity is " + vh.fuel_cap + ".");
    			System.out.println("Based on this data the Honda's max range will be " + range + ".");
    		}
    		catch(Exception exc)
    		{
    			System.out.println("There was an error calculating the Honda Range!\n Details: " + exc);
    		}
    	}
    }
    Last edited by Alpinstang; 10-08-2012 at 02:13 AM.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,882
    Rep Power
    25

    Default Re: Classes and Objects with an External Class file - Help requested

    Here is the working code.
    If you have solved your problem, please mark this thread as Solved.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Open external file with external program
    By Wietse de Vries in forum New To Java
    Replies: 1
    Last Post: 12-31-2011, 05:52 PM
  2. Replies: 9
    Last Post: 07-17-2011, 10:24 AM
  3. Replies: 8
    Last Post: 04-29-2011, 07:24 AM
  4. load external classes
    By gotenks05 in forum New To Java
    Replies: 17
    Last Post: 10-27-2008, 07:09 PM
  5. Replies: 0
    Last Post: 05-13-2008, 07:16 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •