Results 1 to 10 of 10
- 04-29-2009, 01:39 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 20
- Rep Power
- 0
Class problem: getting incorrect output
The following is the code for calculating the range of a vehicle and it works perfect.
And then, the code is modified for two vehicles and I get the output as 0 range for both vehicles.Java Code:class Vehicle{ int passengers; // No of passengers int fuelCap; //Fuel capacity int mpg; //Miles per gallon } class VehicleOne{ public static void main(String args []){ Vehicle MiniVan = new Vehicle(); //create minivan object int range; //Assign values to the fields in minivan MiniVan.passengers = 20; /* These are the MiniVan.fuelCap = 16; variables, called members, of MiniVan.mpg = 21; object MiniVan and are created by using dot(.) operators and are called object.member */ range = MiniVan.fuelCap * MiniVan.mpg; System.out.println("Minivan can carry " + MiniVan.passengers + " with a range of " + range + "\n"); } }
Where am I going wrong ??Java Code:class Vehicle{ int passengers, fuelCap, mpg; //Declare fuel capacity, miles per gallon etc } //The following class declares an OBJECT of the class vehicle class VehicleTwo{ public static void main(String args []){ Vehicle Van = new Vehicle(); Vehicle Bus = new Vehicle(); int range1, range2; //Assign values to the fields of the two vehicles Van.passengers = 20; Van.fuelCap = 30; Van.mpg = 15; Bus.passengers = 40; Bus.fuelCap = 60; Bus.mpg = 10; //Calculate range assuming full tank range1 = Van.fuelCap * Van.mpg; range2 = Bus.fuelCap * Bus.mpg; System.out.println("The van can carry " + Van.passengers + "with a range of " + range1); System.out.println("The bus can carry " + Bus.passengers + "with a range of " + range2); } }
- 04-29-2009, 01:48 PM #2
Iam getting the below output for the above code
The van can carry 20with a range of 450
The bus can carry 40with a range of 600
- 04-29-2009, 01:54 PM #3
Member
- Join Date
- Apr 2009
- Posts
- 20
- Rep Power
- 0
Your code is perfect. It will not print range as 0
Last edited by kbindumadhavi; 04-29-2009 at 02:30 PM.
-
It calculates fine for me, but I would strongly recommend that you place the range calculation within the Vehicle class rather than reproduce the code each time you need it.
Something like:
then just call calcRange() on any vehicle object when you want to know its range.Java Code:class Vehicle { int passengers; int fuelCap; int mpg; public Vehicle(int passengers, int fuelCap, int mgp) { this.passengers = passengers; this.fuelCap = fuelCap; this.mpg = mgp; } public int calcRange() { return fuelCap * mpg; } }
- 04-29-2009, 02:27 PM #5
Member
- Join Date
- Apr 2009
- Location
- Brisbane
- Posts
- 86
- Rep Power
- 0
Spot the problem now?

Putz!
Don't know what the (competent) posters above where talking about. Brain-farts all round, IMHO.
Cheers. Keith.Last edited by corlettk; 04-29-2009 at 02:28 PM. Reason: Dratz!
- 04-29-2009, 03:57 PM #6
Member
- Join Date
- Feb 2009
- Posts
- 20
- Rep Power
- 0
I was using JCreator and it is still giving '0' output. On Eclipse, however, I am getting the correct output. Perhaps, there is some bug in JCreator.
2. Is there any free and reliable IDE other than Eclipse ??(Let me study the 'sticky thread' and I shall come back for advice.Last edited by mgm2010; 04-29-2009 at 04:57 PM.
- 04-29-2009, 04:08 PM #7
A big comment...
I think Keith wanted you to see this:
http://image74.webshots.com/174/2/74...2BTTEzo_ph.jpg
Don't know if you've corrected the range of the comment (/* */) in your code
Luck,
CJSLChris S.
Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.
-
I didn't looked carefully at the first code tagged segment, just the second one as that was the code he was having problems with. I now see the problem in the first section, but I don't see how that will effect the code in the second section that he states is not calculating well. It still should calculate fine.
- 04-29-2009, 07:34 PM #9
- 04-30-2009, 07:17 AM #10
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
well... hate to break it to u all, but IDE's save noobs more than they condemn them. (at least Eclipse does, saved me more than once) As for IDE's other than Eclipse, there is NetBeans... but I've never looked at it, so can't tell you how good it is. It is Sun's IDE for Java, so it can't be too bad.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
Similar Threads
-
Incorrect Package? Help!
By chaits86 in forum NetBeansReplies: 10Last Post: 12-17-2008, 03:08 AM -
The addition program is providing incorrect sum '0'
By tabrez_k81 in forum New To JavaReplies: 4Last Post: 12-16-2008, 01:43 PM -
Output Problem
By jazz2k8 in forum New To JavaReplies: 4Last Post: 05-26-2008, 10:59 AM -
Java Swing class capturing output to the console
By Java Tip in forum Java TipReplies: 0Last Post: 03-12-2008, 11:24 AM -
Output problem
By jvasilj1 in forum New To JavaReplies: 0Last Post: 01-31-2008, 06:39 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks