Results 1 to 4 of 4
  1. #1
    paulieb is offline Member
    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Default Error adding a method to a class

    Hi,

    im new to Java and learning how to program through a book. Im working on adding a method to a class but I am getting an error when compiling and im not sure what i need to do to the method to allow it to be read.

    Any help is much appreciated :o)

    Here is the problem.

    im using eclipse to compile.
    The class is saved as AddMeth.java.
    I had no problems previously adding class vehicle to an existing class before i prepared AddMeth.

    Error message:

    Minivan can carry 7.Exception in thread "main" java.lang.NoSuchMethodError: vehicle.range()V
    at AddMeth.main(AddMeth.java:28)

    code

    class vehicle{
    int passengers;
    int fuelcap;
    int mpg;

    void range()
    {
    System.out.println("The Range is " + fuelcap * mpg);
    }
    }
    public class AddMeth {
    public static void main(String args []){
    vehicle minivan = new vehicle();
    vehicle sportscar = new vehicle();

    int range1, range2;

    minivan.passengers = 7;
    minivan.fuelcap = 16;
    minivan.mpg = 21;

    sportscar.passengers = 3;
    sportscar.fuelcap = 14;
    sportscar.mpg = 12;

    System.out.print(" Minivan can carry " + minivan.passengers + ".");
    minivan.range();

    System.out.println(" Sportscar can carry" + sportscar.passengers + " .");
    sportscar.range();





    }

    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,618
    Rep Power
    5

    Default Re: Error adding a method to a class

    Please enclose your code in code tags [CODE][/CODE]for proper formatting. Your code works for me. Are you getting any compiler errors?

    Regards,
    Jim
    Last edited by jim829; 06-11-2013 at 09:50 PM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: Error adding a method to a class

    Java Code:
    class vehicle{
    int passengers;
    int fuelcap;
    int mpg;
    
    void range()
    {
    System.out.println("The Range is " + fuelcap * mpg);
    }
    }
    public class AddMeth {
    public static void main(String args []){
    vehicle minivan = new vehicle();
    vehicle sportscar = new vehicle();
    
    int range1, range2;
    
    minivan.passengers = 7;
    minivan.fuelcap = 16;
    minivan.mpg = 21;
    
    sportscar.passengers = 3;
    sportscar.fuelcap = 14;
    sportscar.mpg = 12;
    
    System.out.print(" Minivan can carry " + minivan.passengers + ".");
    minivan.range();
    
    System.out.println(" Sportscar can carry" + sportscar.passengers + " .");
    sportscar.range();
    Now let's see what we can do with this.

  4. #4
    bigsonny is offline Senior Member
    Join Date
    Nov 2010
    Posts
    164
    Rep Power
    4

    Default Re: Error adding a method to a class

    Okay, first of all, let's clean up the sequence of statements in your code:

    Typically, import would be at the stop, followed by your class declaration and your main method, then you can add the other things. Also, as jim829 already stated, add the code tags. I would split them in two to differentiate your two classes:

    Java Code:
    class vehicle{
    int passengers;
    int fuelcap;
    int mpg;
    
    void range()
    {
    System.out.println("The Range is " + fuelcap * mpg);
    }
    }
    Main class
    Java Code:
    public class AddMeth {
    public static void main(String args []){
    
    vehicle minivan = new vehicle();
    vehicle sportscar = new vehicle();
    
    int range1, range2;
    
    minivan.passengers = 7;
    minivan.fuelcap = 16;
    minivan.mpg = 21;
    
    sportscar.passengers = 3;
    sportscar.fuelcap = 14;
    sportscar.mpg = 12;
    
    System.out.print(" Minivan can carry " + minivan.passengers + ".");
    minivan.range();
    
    System.out.println(" Sportscar can carry" + sportscar.passengers + " .");
    sportscar.range();
    • Here are a few things that concern me, but don't seem to keep JIm from compiling it. Your vehicle class has no constructor and yet you build two objects minivan and sportscar from it.
    • You declared the variables range1 and range2 but you don't use them. Is there a point in doing that?
    • Though you don't have to, you could simplify your code by adding a constructor and passing the variables passenngers, fuelcap and mpg directly to it.

Similar Threads

  1. Error when adding an array into main method
    By abeidson in forum New To Java
    Replies: 7
    Last Post: 03-12-2013, 04:49 PM
  2. Replies: 8
    Last Post: 11-22-2012, 07:16 AM
  3. main method not found error in class . wat s the error?
    By kirankumaragnihotram in forum New To Java
    Replies: 10
    Last Post: 06-29-2012, 11:26 AM
  4. Replies: 1
    Last Post: 12-17-2010, 09:24 AM

Posting Permissions

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