Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Newbing It Out (Methods)
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
.hack




PostPosted: Mon Nov 01, 2004 9:43 am   Post subject: Newbing It Out (Methods)

Ok, so I hate Java, and my teacher is convinced taht all I do is piss away time in class so he doesn't help me. Apprently everything I need is in this big yellow book written by "holt soft", yet my problems are not fixed. I am hoping that someone here can help me before I go to Holtsoft and blow up their office and destroy every ounce of java and java knowledge in this horrible world. Anyways.

So I'm trying to make methods and I am uber confused. I thought I was doing it right, but I guess I'm not, I read my book and no luck. So heres my code. All I really want at this stage, si for osmone to fix it, and tell me what I did wrong / repost so I can see what I did wrong. heres my code.

Its a Rock paper scissors program. And my teacher didn't help us at all learbing it (he tought us once instance of a method, and he did that incorrectly).

my Gift to you:

UPDATE : I got the METHODS to work! Except their values reset when they return to the main program, can u set a return point?

new code.


code:

// The "RPS" class.
import java.awt.*;
import hsa.Console;

public class RPS
{
    static Console c;           // The output console
   
    public static void main (String[] args)
    {
        c = new Console ();
        int UChoice = 0;
        int CompChoice = 0;
        int Comp; 
        int User;
       
       
       
        RPS.User(UChoice);
        c.println (UChoice);
        RPS.Comp(CompChoice);
        c.println (CompChoice);
       
       
    }   
     
   
       static public int User(int UChoice)
        {
        c.println ("Choose Your Command");
        c.println ("1 - Rock");
        c.println ("2 - Paper");
        c.println ("3 - Scissors");
        UChoice = c.readInt();
        c.println (UChoice);
        return UChoice;
       
        }
       
        static public int Comp (int CompChoice)
        {
       
        CompChoice = (int) (Math.random () * 3);
        CompChoice = CompChoice + 1;
        c.println (CompChoice);
        return CompChoice;
       
        }
       
       
       
     // main method
} // RPS class
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Mon Nov 01, 2004 7:46 pm   Post subject: (No subject)

  • The Console class is evil. Use the standard API. You can find it at http://java.sun.com. The most frequent question is: how do I read from the keyboard?

    code:
    import java.lang.*;
    import java.io.*;

    public class MyProgramName
    {
       private static BufferedReader keyboard = new BufferedReader(
          new InputStreamReader(System.in));

       public static void main(String[] args)
       {
          String input = keyboard.readLine();
       }
    }


    The next most common question is, so how do I read an int or double? The answer is... you don't!

    You're always reading a string. You just convert it to an int or double afterward.

    code:
    import java.lang.*;
    import java.io.*;

    public class MyProgramName
    {
       private static BufferedReader keyboard = new BufferedReader(
          new InputStreamReader(System.in));

       public static void main(String[] args)
       {
          String input = keyboard.readLine();
          int myInt = Integer.parseInt(input);
          double myDouble = Double.parseDouble(input);
       }
    }

  • You're not doing anything that requires the java.awt package to be imported. Don't import stuff you don't need.
  • Java has naming conventions. Learn them. Love them.
    • Class and interface names should be capitalized.

      code:
      class MyClass { ... }

      interface MyInterface { ... }

    • Constants should be all caps, with separate words separated by underscores.

      code:
      final int SOME_CONSTANT = 2;
      final int THE_ANSWER = 42;

    • variable and method names should not be capitalized.

      code:
      int myVariable = 27;

      double functionName() { return 6.3; }

  • You can return from anywhere in a function.
.hack




PostPosted: Tue Nov 02, 2004 8:59 am   Post subject: (No subject)

My teacher won't let me use the sun API, purely because hes newb. Pretty much watever he teaches he read from the book the day brfore, then tells us not to worry about the syntax he puts on the board rarely.

I'm stuck with this holtsoft bullshit, and yea I know about naming classes with 1 capital and such, I just don't really care for it myself lmao
(Not to sound like a lazy bastard, but I do feel that being in this class is a waste of time, as I feel the "Java" were learning in my class is completely useless)

Basically my one question remains.

I have it declaring a variable in the main, which is = 0 so th program will run. THen I have it running a method where the variable number is changes by the user. but when I exit the method and return to the main the variable number gets reset to its value set in declaration in the main (0). This I don;t quit understnad, unless when using methods your supposed to somehow link them together and never go back to the main.

java hurts my head.
wtd




PostPosted: Tue Nov 02, 2004 1:50 pm   Post subject: (No subject)

First lesson: remove extraneous comments. Things like:

code:
// RPS class
public class RPS


Are just comments pointing out the incredibly obvious. Use comments to explain why you're doing certain things, what you're doing.

Second lesson: built-in types get passed to methods by value.

So, in:

code:
static public int User(int UChoice)
{
   // ...
}

int foo = 42;
User(foo);


UChoice is just a copy of the value in the foo variable, not foo itself. Any changes you make to UChoice will not be reflected in foo once the function ends.

Third lesson: variables can be declared anywhere inside a method. They don't all need to be declared at the start.

code:
private static int oid foo()
{
        int a;
        double b;
        boolean f;
       
        a = 5;
        b = 4.3 + a;
        f = true;
}


Can be more succinctly (and correctly) rewritten as:

code:
private static int void foo()
{
        int a = 5;
        double b = 4.3 + a;
        boolean f = true;
}


And lessons get tedious, so the refined code...

code:
import hsa.Console;

public class RPS
{
        // The output Console.
        // It's a static variable, so we can initialize it here.
        // It's explicitly private because it doesn't need to be public.
        // Also gave this one a meaningful name.
        private static Console inputOutputConsole = new Console();         
   
        public static void main(String[] args)
        {
                // Meaningful variable name.
                int userChoice = chooseUser();
                // The chooseUser() function didn't do the output. 
                // So I do it here instead.
                inputOutputConsole.println(userChoice);
               
                // See above.  Same principles at work.
                int computerChoice = chooseComputer();
                inputOutputConsole.println(computerChoice);
        }   
     
        public static int chooseUser()
        {
                inputOutputConsole.println("Choose Your Command");
                inputOutputConsole.println("1 - Rock");
                inputOutputConsole.println("2 - Paper");
                inputOutputConsole.println("3 - Scissors");
               
                // Just read in an int and return it. 
                // This function doesn't need to output
                // that number as well.
                return inputOutputConsole.readInt();
        }

        public static int chooseComputer()
        {
                // All this function has to do it return
                // a valid number between 1 and 3.  So,
                // that's all it does.
                return (int)(Math.random() * 3);
        }   
}
.hack




PostPosted: Wed Nov 03, 2004 8:58 am   Post subject: (No subject)

thanks for all the help, it smuch appreciated, I'll try to take it from there and see how it ends up.

TTYTT I';m not really built for programming -.-

+50bits
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: