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

Username:   Password: 
 RegisterRegister   
 new Methods
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
rto




PostPosted: Tue Nov 18, 2008 5:40 pm   Post subject: new Methods

Im making a millionaire program that is been updated from my last version and i was wondering:

Instead of putting System.out.println ("Sorry that is incorrect"); i want to make a method that has that automatically in it and than call the method. Could anybody tell me how to do this? I know its sort of a gay question but i'll give you an example of what i want:

import java.io.*
class Millionaire
{
public static void main (String [] args) throws Exception
InputStreamReader input = new InputStreamReader (System.in);
BufferedReader reader = new BufferedReader (input);
{
String reply;
System.out.println ("Enter your answer");
reply = readString (reader.readLine());
}
}

Method = new method Incorrect () {
System.out.println ("Incorrect");
}


Something like that, sorry about the indenting ..
Sponsor
Sponsor
Sponsor
sponsor
HellblazerX




PostPosted: Wed Nov 19, 2008 1:24 am   Post subject: Re: new Methods

You weren't that far off from the mark. To make a new method in a class:
Java:
public void <method name> () {
    //Your code there
}


To call on this method, if you're calling from within the class:
Java:
<method name> ();

or if you're calling from outside the class:
Java:
<object of class>.<method name> ();
DemonWasp




PostPosted: Wed Nov 19, 2008 10:28 am   Post subject: RE:new Methods

To extend on this idea, you may find that this also helps if you (for example) ever want to translate into another language, since you only have to change that one method.

Aside, please refrain from using "gay" as an insult - there is nothing wrong with homosexual people, and using "gay" in this way insinuates that there is. Either way, these forums are here to help complete beginners as well as more experienced programmers, so the only stupid questions are the ones that demand that we do your work for you, not questions like you asked.
S_Grimm




PostPosted: Wed Nov 19, 2008 11:16 am   Post subject: RE:new Methods

Java:

import java.io.*;
public class HelloWorld
{
    public static void main (String [] args)
    {
    HelloWorld Hello = new HelloWorld();
        String Hi = new String();
        Hello.SayHello (Hi);
    }


    public void SayHello (String Hello)
    {
        System.out.println ("Hello");
    }
}


There is an example block of code. Have Fun.
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  [ 4 Posts ]
Jump to:   


Style:  
Search: