Results 1 to 9 of 9

Thread: Java Help - Objects and Multiple Classes

  1. #1
    Join Date
    Oct 2012
    Location
    Porto, Portugal
    Posts
    218
    Mentioned
    0 Post(s)
    Quoted
    42 Post(s)

    Default Java Help - Objects and Multiple Classes

    So, I am having a bad time here. I watched the newboston video tutorials on objects and multiple classes and now I'm trying to do myself just to see if I got it.


    My classes:

    Java Code:
    package bucky;



    public class bacon {
        private String sport;
       
        public void setSport(String sportname)  {
            sport=sportname;
        }
       
       
        public String getSport()    {
            return sport;
        }
       
        public void SaySport()  {
            System.out.printf("Really?? %S is my favourite sport too!", getSport());
           
           
        }

    }


    Java Code:
    package bucky;


    import java.util.Scanner;

    public class banana {
       
        public void main(){
            Scanner choco= new Scanner(System.in);
            System.out.println("What is your favourite sport?");
            String temp = choco.nextLine();
            bacon object = new bacon();
            object.setSport(temp);
            object.SaySport();
        }

    }


    And this is the error I got:

    Code:
    Error: Could not find or load main class bucky.apples



    I happen to have a class named apples which is the class that I copied from the tutorial but I don't see how I get this error.
    Anyone?

    Thanks everybody in advance

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    should be:

    java Code:
    package bucky;

    public class apples {
        public static main(String Args[]) {
        }
    }
    I am Ggzz..
    Hackintosher

  3. #3
    Join Date
    Oct 2012
    Location
    Porto, Portugal
    Posts
    218
    Mentioned
    0 Post(s)
    Quoted
    42 Post(s)

    Default

    But why should the class be named apples. Can't I name it bacon? That's what I'm not understanding.

    Thanks again for your help Brandon

  4. #4
    Join Date
    Dec 2011
    Location
    The Netherlands
    Posts
    1,631
    Mentioned
    47 Post(s)
    Quoted
    254 Post(s)

    Default

    Quote Originally Posted by Nirvana View Post
    But why should the class be named apples. Can't I name it bacon? That's what I'm not understanding.

    Thanks again for your help Brandon
    It happens somewhere in the mainloop. I think Bucky's way of naming the classes is extremely confusing.

    It would have been better to have

    House
    -> University extends House
    Person
    -> Student extends Person
    -> Teacher extends Person
    -> -> Professor extends Teacher

    Then have an Object Subject
    Subject(String name)

    And a Student has a Teacher which gives a Subject, etc. but that stuff is more for future tutorials. But other names would've been better, they confused me aswell.

    Script source code available here: Github

  5. #5
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Nirvana View Post
    But why should the class be named apples. Can't I name it bacon? That's what I'm not understanding.

    Thanks again for your help Brandon

    I did not say that the bacon class should be renamed (I didn't have time to elaborate so I thought you'd get the hint). Look at the left-side of your picture. It says "apples" is a class in the package "bucky".
    Your apples class is missing the function:

    Java Code:
    static void main(String args[]) {
        //main..
    }

    That is what your error is saying. It says that apples does not contain a main and that it cannot find main within bucky.apples. You must have told it that apples would be the main class when you created the project. Hopefully I didn't reply too late but yeah.
    I am Ggzz..
    Hackintosher

  6. #6
    Join Date
    Oct 2012
    Location
    Porto, Portugal
    Posts
    218
    Mentioned
    0 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by Brandon View Post
    I did not say that the bacon class should be renamed (I didn't have time to elaborate so I thought you'd get the hint). Look at the left-side of your picture. It says "apples" is a class in the package "bucky".
    Your apples class is missing the function:

    Java Code:
    static void main(String args[]) {
        //main..
    }

    That is what your error is saying. It says that apples does not contain a main and that it cannot find main within bucky.apples. You must have told it that apples would be the main class when you created the project. Hopefully I didn't reply too late but yeah.
    Ok I understand but that's not really my problem, perhaps I didn't explain well, sorry about that.

    The problem is that it executes the apples class and I don't want. I just want to execute the banana and bacon classes. I've tried to removed apples and tuna classes from the package but the program still asks for them.

    I understand that the apples class is the main one and that's why it asks for it. But how can I make the main class be the "banana"? I have a public void main method in that class, shouldn't it start there?

    Thank you for your patience.

  7. #7
    Join Date
    Oct 2006
    Location
    Netherlands
    Posts
    3,285
    Mentioned
    105 Post(s)
    Quoted
    494 Post(s)

    Default

    Some project option. I have no experience with that ide. But usually it is right clicking on the class in the class tree and sellecting 'main class' or something like that.
    Working on: Tithe Farmer

  8. #8
    Join Date
    Dec 2006
    Location
    Sydney, New South Wales, Australia
    Posts
    4,603
    Mentioned
    15 Post(s)
    Quoted
    42 Post(s)

    Default

    Quote Originally Posted by Nirvana View Post
    Ok I understand but that's not really my problem, perhaps I didn't explain well, sorry about that.

    The problem is that it executes the apples class and I don't want. I just want to execute the banana and bacon classes. I've tried to removed apples and tuna classes from the package but the program still asks for them.

    I understand that the apples class is the main one and that's why it asks for it. But how can I make the main class be the "banana"? I have a public void main method in that class, shouldn't it start there?

    Thank you for your patience.
    There should only be one public static void main(Stringp[] args) in your project. This method is the "main event loop", i.e. the very first method that is called when you run your program.

    It must have a main event loop. The class, SimpleCalculator, seems like an appropriate place to put this main method. Try adding it there.

    Quote Originally Posted by masterBB View Post
    I have no experience with that ide.
    It's Eclipse.
    You may contact me with any concerns you have.
    Are you a victim of harassment? Please notify me or any other staff member.

    | SRL Community Rules | SRL Live Help & Chat | Setting up Simba | F.A.Q's |

  9. #9
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    Quote Originally Posted by Nirvana View Post
    Ok I understand but that's not really my problem, perhaps I didn't explain well, sorry about that.

    The problem is that it executes the apples class and I don't want. I just want to execute the banana and bacon classes. I've tried to removed apples and tuna classes from the package but the program still asks for them.

    I understand that the apples class is the main one and that's why it asks for it. But how can I make the main class be the "banana"? I have a public void main method in that class, shouldn't it start there?

    Thank you for your patience.

    Just add the main to the banana class. Then right click your banana class and click properties then "Run/Debug Settings" on the side. Set the main class as bucky.bacon. Save and exit.
    Delete the other two classes that you don't want.
    Select the banana class then go to the run menu and run-as Java-Application
    I am Ggzz..
    Hackintosher

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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