Results 1 to 6 of 6
- 10-06-2012, 12:58 AM #1
New to Java
- Join Date
- Sep 2012
- Location
- University of Memphis
- Posts
- 5
- Rep Power
- 0
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); } }My tabs got all messed up when I posted this, sorry it was standard.Java Code:public class Cars { public static void main(String[] args) { Vehicle vhcl = new Vehicle(); double vhcl.CalculateToyotaRange(15.0,10); } }Last edited by Alpinstang; 10-06-2012 at 12:59 AM. Reason: code looks crappy
- 10-06-2012, 04:06 AM #2
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.
- 10-06-2012, 11:11 AM #3
New to Java
- Join Date
- Sep 2012
- Location
- University of Memphis
- Posts
- 5
- Rep Power
- 0
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?
- 10-06-2012, 11:21 AM #4
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
dban external class fileWhy do they call it rush hour when nothing moves? - Robin Williams
- 10-08-2012, 01:11 AM #5
New to Java
- Join Date
- Sep 2012
- Location
- University of Memphis
- Posts
- 5
- Rep Power
- 0
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 01:13 AM.
- 10-08-2012, 02:12 AM #6
Similar Threads
-
Open external file with external program
By Wietse de Vries in forum New To JavaReplies: 1Last Post: 12-31-2011, 04:52 PM -
Class names are only accepted if annotation processing is explicitly requested
By jon80 in forum New To JavaReplies: 9Last Post: 07-17-2011, 09:24 AM -
Is it possible to use an external program to run a .class file?
By ReclaimerGold in forum New To JavaReplies: 8Last Post: 04-29-2011, 06:24 AM -
load external classes
By gotenks05 in forum New To JavaReplies: 17Last Post: 10-27-2008, 06:09 PM -
Able to find class file in WEB-INF/classes but not after add sub folders in class dir
By vitalstrike82 in forum Web FrameworksReplies: 0Last Post: 05-13-2008, 06:16 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks