Results 1 to 10 of 10
  1. #1
    mgm2010 is offline Member
    Join Date
    Feb 2009
    Posts
    20
    Rep Power
    0

    Default Class problem: getting incorrect output

    The following is the code for calculating the range of a vehicle and it works perfect.
    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");
        
      }
    }
    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, 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);                 
    		}
    	}
    Where am I going wrong ??

  2. #2
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    836
    Rep Power
    6

    Default

    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

  3. #3
    kbindumadhavi is offline Member
    Join Date
    Apr 2009
    Posts
    20
    Rep Power
    0

    Default

    Your code is perfect. It will not print range as 0
    Last edited by kbindumadhavi; 04-29-2009 at 02:30 PM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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:

    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;
      }
    }
    then just call calcRange() on any vehicle object when you want to know its range.

  5. #5
    corlettk is offline Member
    Join Date
    Apr 2009
    Location
    Brisbane
    Posts
    86
    Rep Power
    0

    Default

    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!

  6. #6
    mgm2010 is offline Member
    Join Date
    Feb 2009
    Posts
    20
    Rep Power
    0

    Default

    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.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default 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,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  9. #9
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    Quote Originally Posted by mgm2010 View Post
    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.
    You prob don't have JCreator setup to run the second file. So the errors are from the first file. (which are cause by incorrect comments).

    More reasons why beginners shouldn't use IDE's huh keith.
    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  10. #10
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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

  1. Incorrect Package? Help!
    By chaits86 in forum NetBeans
    Replies: 10
    Last Post: 12-17-2008, 03:08 AM
  2. The addition program is providing incorrect sum '0'
    By tabrez_k81 in forum New To Java
    Replies: 4
    Last Post: 12-16-2008, 01:43 PM
  3. Output Problem
    By jazz2k8 in forum New To Java
    Replies: 4
    Last Post: 05-26-2008, 10:59 AM
  4. Java Swing class capturing output to the console
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-12-2008, 11:24 AM
  5. Output problem
    By jvasilj1 in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 06:39 PM

Posting Permissions

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