Results 1 to 4 of 4
  1. #1
    ShermW0829 is offline Member
    Join Date
    Apr 2016
    Posts
    4
    Rep Power
    0

    Default 70 Retired and First program failed

    Where did I go wrong? Thank you in advance.

    Error message: debug:
    Error: Main method not found in class bicycle.Bicycle, please define the main method as:
    public static void main(String[] args)
    C:\Users\ShermW0829\AppData\Local\NetBeans\Cache\8 .1\executor-snippets\debug.xml:83: Java returned: 1
    BUILD FAILED (total time: 5 seconds)

    Java Code:
    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    package bicycle;
    
    /**
     *
     * @author Sherman Willden
     */
    public class Bicycle {
    
        /**
         * @param args the command line arguments
         */
            
            int cadence = 0;
            int speed = 0;
            int gear = 1;
         
            void changeCadence( int newValue ) {
                cadence = newValue;
            }
            
            void changeGear( int newValue ) {
                gear = newValue;
            }
            
            void speedUp( int increment ){
                speed = speed + increment;
            }
            
            void applyBrakes( int decrement ){
                speed = speed - decrement;
            }
            
            void printStates() {
                System.out.println("cadence:" +
                        cadence + "speed:" +
                        speed + "gear:" + gear
                );
            }
        }
        
        
    class BicycleDemo {
        public static void main(String[] args) {
            // Create two different Bicycle objects
            Bicycle bike1 = new Bicycle();
            Bicycle bike2 = new Bicycle();
                
            // Invoke methods on those objects
            bike1.changeCadence(50);
            bike1.speedUp(10);
            bike1.changeGear(2);
            bike1.printStates();
                
            bike2.changeCadence(50);
            bike2.speedUp(10);
            bike2.changeCadence(40);
            bike2.speedUp(10);
            bike2.changeGear(3);
            bike2.printStates();
        }
        
    }
    Last edited by JosAH; 04-18-2016 at 01:21 PM. Reason: fixed the [code] ... [/code] tags

  2. #2
    ShermW0829 is offline Member
    Join Date
    Apr 2016
    Posts
    4
    Rep Power
    0

    Default Re: 70 Retired and First program failed

    I was on WIn7 with netbeans. Now I am on Linux where I built the program using VIM and performing javac Bicycle in a command prompt. The javac produced Bicycle.java and BicycleDemo.java. So javac builds a class file for each class in one program? When I ran java Bicycle I received the same error message as from Win7. When I ran java BicycleDemo the program functioned. What is Netbeans on Win7 trying to run?

    Thank you;

    Sherman

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: 70 Retired and First program failed

    Quote Originally Posted by ShermW0829 View Post
    So javac builds a class file for each class in one program?
    Not one program but one file. Multiple classes defined in a single file will result in many .class files.

    When I ran java Bicycle I received the same error message as from Win7. When I ran java
    BicycleDemo the program functioned. What is Netbeans on Win7 trying to run?
    Not certain for NetBeans but it may default to running the public class (or the one in its own file by
    the same name). The static main is the entry point for execution to commence and in this case it is
    in the other class. So in Netbeans you may need to specify the appropriate class to run.

    Note that every class in your file may have a static main entry point. And they may all be used to
    begin execution (if your program logic can handle it). However, your overall program organization may not be
    designed to handle multiple entry points (all programs I have every written only have a single static main
    method).

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: 70 Retired and First program failed

    Quote Originally Posted by ShermW0829 View Post
    I was on WIn7 with netbeans. Now I am on Linux where I built the program using VIM and performing javac Bicycle in a command prompt. The javac produced Bicycle.java and BicycleDemo.java. So javac builds a class file for each class in one program? When I ran java Bicycle I received the same error message as from Win7. When I ran java BicycleDemo the program functioned. What is Netbeans on Win7 trying to run?
    Maybe you instructed NetBeans to run Bicycle.class? It indeed doesn't have a static main( ... ) method; the other class has such a method though ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. 70 Retired Trying to learn OOP
    By ShermW0829 in forum Introductions
    Replies: 0
    Last Post: 04-17-2016, 06:58 PM
  2. Simple Program that displays "PASSED/FAILED VOLTAGE TEST"
    By DPdieciocho in forum New To Java
    Replies: 2
    Last Post: 07-02-2013, 12:25 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
  •