Results 1 to 14 of 14
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By Vinx

Thread: Brand new to programming completely, attempting object oriented. Error.

  1. #1
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Brand new to programming completely, attempting object oriented. Error.

    IDE: Eclipse
    Error:
    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	The method StartProgram() is undefined for the type StartMain
    
    	at StartMain.main(StartMain.java:9)
    StartMain.java
    Java Code:
    public class StartMain {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		StartProgram();
    				
    
    	}
    } // end StartMain
    main.java
    Java Code:
    public class main {
    
    	/**
    	 * @param args
    	 */
    		// TODO Auto-generated method stub
    	public static void StartProgram() {
    		String bytelength = "127";
    		String shortlength = "32,767";
    		String intlength = "2,147,483,647";
    		String longlength = "huge";
    		String welcome = "Welcome to StateTheLength! ";
    		String printingmessage = "Stating values via console...";
    		
    			System.out.println(welcome + printingmessage);
    			System.out.println(""); // Blank Space
    			
    				System.out.println("The max legth of byte is: " + bytelength);
    				System.out.println("The max length of short is: " + shortlength);
    				System.out.println("The max length of int is: " + intlength);
    				System.out.println("The max length of long is: " + longlength);
    			
    		
    	
    
    } }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    The full name of that (static) method is main.StartProgram().

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    That is a wonky setup. Why not just put the StartProgram method (which should be called startProgram) in the StartMain class?

    Since you have the method in your main class (which should be called Main), you can't just call it from your StartMain class.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    Quote Originally Posted by JosAH View Post
    The full name of that (static) method is main.StartProgram().

    kind regards,

    Jos
    Fix worked, thank you!

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

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    StartProgram is a static method in the main class, and to call it, you must call it off of the main class:

    Java Code:
        public static void main(String[] args) {
            main.StartProgram();
        }
    Note that this is not yet object-oriented programming, not til you start using non-static methods and variables.

    Also, please learn about and use Java naming conventions. Class names should start with a capital letter, and variable and method names with a lower-case letter. This way others will more easily and more quickly understand your code.


    edit: too darn slow!!!!!!!!!!!!

  6. #6
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    Quote Originally Posted by KevinWorkman View Post
    That is a wonky setup. Why not just put the StartProgram method (which should be called startProgram) in the StartMain class?

    Since you have the method in your main class (which should be called Main), you can't just call it from your StartMain class.
    Because I wanted to try (Whoops, apparently this wasn't object oriented) through 2+ different files. It was for the learning experience, not the beauty of the code.

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    Quote Originally Posted by Vinx View Post
    Because I wanted to try (Whoops, apparently this wasn't object oriented) through 2+ different files. It was for the learning experience, not the beauty of the code.
    Fair enough. But if you want to try out an OO approach, nothing except the main method should be static. See what happens then.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    Quote Originally Posted by KevinWorkman View Post
    Fair enough. But if you want to try out an OO approach, nothing except the main method should be static. See what happens then.
    This happened when I removed static from main.java .

    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Cannot make a static reference to the non-static method StartProgram() from the type main
    
    	at StartMain.main(StartMain.java:9)

  9. #9
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    Quote Originally Posted by Vinx View Post
    This happened when I removed static from main.java .

    Java Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Cannot make a static reference to the non-static method StartProgram() from the type main
    
    	at StartMain.main(StartMain.java:9)
    Right, exactly. So to make that work, you have to have a reference to an instance of main (which should be Main). That's the very beginning of OO.
    Fubarable likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    The compiler generated an error for your source code but still you're trying to run that faulty code; if the compiler still had generated code (which is beyond me) you get those "unresolved compilation errors", always. Read what your compiler had to say and don't hope for the best by trying to run that (defective) code.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    So, I decided to take a new approach to OO, and make a entirely different console application.

    Please tell me this is OO, I spent quite some time in my book making sure.

    Java Code:
    class Object { 
    	int order;
    	String name;
    	String type;
    	int weight;
    	int bouyancy;
    	
    	void announceCreation(){
    		System.out.println("Object " + order + " has been created."); 
    		} // end announceCreation()
    	
    	void stateProperties(){
    	System.out.println("Name: " + name);
    	System.out.println("Type: " + type);
    	System.out.println("Weight: " + weight);
    	System.out.println("Bouyancy: " + bouyancy);
    	} // end stateProperties()
    } // end Object
    
    class TestIt {
    	public static void main (String[] args) {
    	Object a = new Object();
    	a.order = 1;
    	a.announceCreation();
    	a.name = "Wooden Ball";
    	a.type = "Solid";
    	a.weight = 5;
    	a.bouyancy = 19;
    	a.stateProperties();
    } // end static void whatever it is
    } // end TestIt

    Output:
    Java Code:
    Object 1 has been created.
    Name: Wooden Ball
    Type: Solid
    Weight: 5
    Bouyancy: 19

  12. #12
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,453
    Blog Entries
    7
    Rep Power
    20

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    Just a tip: don't call your class 'Object'; the Object class is the mother of all classes in Java; it might even confuse the compiler.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  13. #13
    Vinx is offline Member
    Join Date
    May 2012
    Location
    USA
    Posts
    38
    Rep Power
    0

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    Quote Originally Posted by JosAH View Post
    Just a tip: don't call your class 'Object'; the Object class is the mother of all classes in Java; it might even confuse the compiler.

    kind regards,

    Jos
    So would this be a better alternative?

    Java Code:
    class Matter { 
    	int order;
    	String name;
    	String type;
    	int weight;
    	int bouyancy;
    	
    	void announceCreation(){
    		System.out.println("Matter " + order + " has been created."); 
    		} // end announceCreation()
    	
    	void stateProperties(){
    	System.out.println("Name: " + name);
    	System.out.println("Type: " + type);
    	System.out.println("Weight: " + weight);
    	System.out.println("Bouyancy: " + bouyancy);
    	} // end stateProperties()
    } // end Object
    
    class TestIt {
    	public static void main (String[] args) {
    	Matter a = new Matter();
    	a.order = 1;
    	a.announceCreation();
    	a.name = "Wooden Ball";
    	a.type = "Solid";
    	a.weight = 5;
    	a.bouyancy = 19;
    	a.stateProperties();
    } // end static void whatever it is
    } // end TestIt
    Output:
    Java Code:
    Matter 1 has been created.
    Name: Wooden Ball
    Type: Solid
    Weight: 5
    Bouyancy: 19
    Also, I'm working on converting my old application to object oriented.



    EDIT: Finished :D

    Java Code:
    class StateTheLength {
    	String name;
    	String length;
    	   void printData() {
    	   		System.out.println("The length of " + name + " is " + length);
    	    } // end printData()
    }
    
    class main {
    	public static void main (String[] args) {
    
    	StateTheLength byteLengthObject = new StateTheLength();
    	byteLengthObject.name = "byte";
    	byteLengthObject.length = "127";
    	byteLengthObject.printData(); // State the length of byte
    
    	StateTheLength shortLengthObject = new StateTheLength();
    	shortLengthObject.name = "short";
    	shortLengthObject.length = "32,767";
    	shortLengthObject.printData(); // State the length of short
    
    	StateTheLength intLengthObject = new StateTheLength();
    	intLengthObject.name = "int";
    	intLengthObject.length = "2,147,483,657";
    	intLengthObject.printData(); // State the length of int
    
    	StateTheLength longLengthObject = new StateTheLength();
    	longLengthObject.name = "long";
    	longLengthObject.length = "9,223,372,036,854,775,807";
    	longLengthObject.printData();
    } // end main
    } // end mainProgram
    Output:
    Java Code:
    The length of byte is 127
    The length of short is 32,767
    The length of int is 2,147,483,657
    The length of long is 9,223,372,036,854,775,807
    Last edited by Vinx; 05-23-2012 at 09:55 PM.
    Zigster likes this.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Brand new to programming completely, attempting object oriented. Error.

    Sort of.
    Your first class is probably closer to the ideal.
    It's more a thing, though lacking functionality.
    In general, the attributes should be hidden and accessible (only if needed) by getters and setters.

    Your second example, StateTheLength, is a bit wrong. For starters the name implies a method rather than a Thing.
    You could call it a PrimitiveLength, which would have a method stateTheLength().
    Even better simply override the toString method of Object.
    This might seem picky, but it does help to get the idea that a class represents to blueprint for a Thing, and Things have properties and do stuff (even if that stuff is simply print out the parameters).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. new to object oriented programming and arrays
    By maximumammo in forum New To Java
    Replies: 11
    Last Post: 04-22-2012, 08:25 PM
  2. help for Object Oriented Programming RMI
    By clapton in forum New To Java
    Replies: 0
    Last Post: 03-20-2012, 06:42 PM
  3. Simple Problem with Object-Oriented Programming...
    By CuppaCoffee in forum New To Java
    Replies: 12
    Last Post: 01-04-2012, 10:12 PM
  4. Help in object oriented programming .
    By renu in forum New To Java
    Replies: 18
    Last Post: 10-26-2010, 07:57 PM
  5. Is JAVA completely object-oriented???
    By venkateshk in forum New To Java
    Replies: 2
    Last Post: 11-17-2008, 09:17 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
  •