Register if You want to join the community.
toolbar powered by Conduit
Programming Guides!
April 20, 2024, 02:57:48 am
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Remember - Register to view the Tutorials!
 
  Home   Forum    Forum   Help Search Arcade Site BIO Gallery Scripts Filefront Staff List Login Register  

Tutorial 17 - Using methods with parameters.

Pages: [1]
  Print  
Author Topic: Tutorial 17 - Using methods with parameters.  (Read 172 times)
Hondaman
Administrator
Full Member
*

Karma: +200/-0
Offline Offline

Posts: 98



View Profile WWW
« on: January 27, 2010, 11:33:18 am »

Hey guys and finaly im posting the 17th Java tutorial,
In this tutorial i will teach you how to make a name variable in one class, and set it to a user input in the other class,
All together this should allow you to type your name and then print "Hello <Your name>"

So to start off, make two classes, test1 and test2
In test 2, keep the "class public test2 {" and the other "}"
But delete everything in test1

Then in test1 import the Scanner
Code:
import java.util.Scanner;

and then under that type in the class and the method so it looks like this -
Code:
import java.util.Scanner;

class test1{
public static void main(String args[]){

----------------------------------------------------------------------------------------------------------------------------------------------------------------------

In test two which should look like this -
Code:
public class test2 {

}

Type the method exept with a argument called name, like so -
Code:
public class test2 {
static void simpleMessage(String name){
}

The enter the Hello and the variable "name" (The argument, this also works as a variable for that class)
and to do so start off with the text output for Hello "System.out.println("Hello");"
Then add in the variable for the name by typing " + name" after the hello,
If you dont get it look over this again and look at what it should look like -
Code:
public class test2 {
static void simpleMessage(String name){
System.out.println("Hello " + name);
}
}


----------------------------------------------------------------------------------------------------------------------------------------------------------------------

Now to finish up with the main class "test1"

If you looked at the user input tutorial you will recognise this
Code:
		Scanner input = new Scanner(System.in);
test2 test2Object = new test2();

If you dont recognise this i would recommend you go and have a look at it, or recap your memory
I wont explain this just to shorten the tutorial a bit.

So now we have to make a text output for the person to see so they know what to do, this will print on the screen "Please enter your name: "
So for this just do basic text output "System.out.println("Please enter your name: ");"
And under that type "String name = input.nextLine();"
So that should look like -
Code:
import java.util.Scanner;

class test1{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
test testObject = new test();

System.out.println("Please enter your name: ");
String name = input.nextLine();

}
}

And FINALY you need java to find the class as an object, find the method and fully execute it by typing the following
Code:
import java.util.Scanner;

class fish{
public static void main(String args[]){
Scanner input = new Scanner(System.in);
test testObject = new test();

System.out.println("Please enter your name: ");
String name = input.nextLine();

testObject.simpleMessage(name);
}
}

And that is the finished script!
If you dont understand this look throughout the java tutorials for an answer or PM me

Thanks for viewing this tutorial.
Java tut 17.
Your Admin: Hondaman.
Report Spam   Logged



Share on Facebook Share on Twitter



Pages: [1]
  Print  
 
Jump to:  

Powered by EzPortal
Bookmark this site! | Upgrade This Forum
SMF For Free - Create your own Forum

Powered by SMF | SMF © 2016, Simple Machines
Privacy Policy