I know its a C forum, but I need help with my java...

This is a discussion on I know its a C forum, but I need help with my java... within the Tech Board forums, part of the Community Boards category; Hi everyone!, I know this a forum dedicated to a C coding, but i really need some help with my ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    I know its a C forum, but I need help with my java...

    Hi everyone!, I know this a forum dedicated to a C coding, but i really need some help with my java... I'm a really big newb at java. You will probably laugh at me, but I really want to understand why my small mini program isn't working.

    Code:
    public class random {
    	//main itself
    	public static void main(String[] args)
    	{
    		int x=6, hold, hold2;
    		;
    		hold = tenten(x);
    		hold2 = evenOrNot(x);
    		System.out.println("METHOD #4: "+hold);
    		System.out.println("METHOD #5: "+hold2);
    	}
    	
    	
    	//4th method
    	public int tenten(int x)
    	{
    		int sum, result;
    		sum = (1+2+3+4+5+6+7+8+9+10);
    		result = sum * x;
    		return result;
    	}
    	//5th method
    	public boolean evenOrNot(int x)
    	{
    		if((x % 2) == 0)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}	
    	}
    }
    It keeps telling me that a static can't go into a non-static method, and I don't really understand that and I just don't know anymore.. :'(. Anyways, any help will be greatly appreciated.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,883
    Moved to Tech Board.

    Quote Originally Posted by dlwlsdn
    It keeps telling me that a static can't go into a non-static method, and I don't really understand that
    You need a random object in main in order to call the tenten and evenOrNot methods.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    A random object in main?

    How do i write that? is it the same as writing like x = rand() ?

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    How do i write that? is it the same as writing like x = rand() ?
    No. You need /litterally/ an object of your class 'random'. You can call non-static methods of an object when you have an instance of this object, you know...OOP.

  5. #5
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You have a main class, then other classes. Also you need to create an instance of your random class. Or, if there is only going to be one instance, use a singleton.
    Code:
    public class Main 
    {
    	public static void main(String[] args)
    	{
    		int x=6, hold, hold2;
                    rand = new random(); //create an instance of random
    		hold = rand.tenten(x);     // call some of its functions
    		hold2 = rand.evenOrNot(x);
    		System.out.println("METHOD #4: "+hold);
    		System.out.println("METHOD #5: "+hold2);
    	}
    }
    
    public class random {	
           
           public random(){}
    
    	//4th method
    	public int tenten(int x)
    	{
    		int sum, result;
    		sum = (1+2+3+4+5+6+7+8+9+10);
    		result = sum * x;
    		return result;
    	}
    	//5th method
    	public boolean evenOrNot(int x)
    	{
    		if((x % 2) == 0)
    		{
    			return true;
    		}
    		else
    		{
    			return false;
    		}	
    	}
    }
    Havent tested it and been a while since i used java so not completely sure if this will work as is, but its at least in the right direction.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    you guys are so smart!

    thank you for responding very fast. My family came down, so we went out to eat and just shopped and stuff. Sorry I couldn't respond until now.

    I get what your saying, thank you for helping. I'll try it out and see what i can do. Thank you ^^

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    mike_g - this is the only thing I see wrong with (missing from) it...
    Code:
    random rand = new random(); //create an instance of random
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    500
    It is a good idea to follow java's naming conventions with class names starting with Uppercase letter.
    ie., to say Random instead of random.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  9. #9
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    Or just declare 4th/5th method to static method.

    Btw, I got bad impression in Java forums, they kinda stupid or something so that I moved here.
    Last edited by audinue; 01-10-2009 at 11:32 PM.
    Just GET it OFF out my mind!!

  10. #10
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by Dino View Post
    mike_g - this is the only thing I see wrong with (missing from) it...
    Code:
    random rand = new random(); //create an instance of random
    Yep, PHP and Python have been rotting my brains lately

  11. #11
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Even so, Random is still a terrible name for a class. It doesn't describe it at all, plus it clashes with the standard Random from java.util. Sure it might be obvious to you that you're not using the standard Random class... but scroll 500 lines down and ask someone else to read the source and it's not going to be so obvious.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    zacs7, you are exactly correct, which is why, as a coding style, it's a good idea (while one is learning) to specify the full path for classes in code.

    Doing this:
    Code:
    java.util.Random rand = new java.util.Random() ;
    would never be confused with the above poor naming choice.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C#, Java, C++
    By incognito in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 10-05-2004, 02:06 PM
  2. The Java language is being expanded
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 26
    Last Post: 06-11-2004, 09:07 PM
  3. Java Forum Here
    By vasanth in forum A Brief History of Cprogramming.com
    Replies: 31
    Last Post: 07-11-2002, 11:32 AM
  4. java forum
    By bigSteve in forum C Programming
    Replies: 1
    Last Post: 02-18-2002, 04:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21