Download Set up a for-loop and print out all values of the args

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Roskilde edb-skole
Prog1:\OpgaverAlm\Main.doc
2003-04-02 /Michael Claudius
COMPUTER SUBJECT:
BASIC JAVA PROGRAMMING
TYPE:
EXERCISE/DISCUSSION
IDENTIFICATION:
MAIN/MC
COPYRIGHT:
Michael Claudius
LEVEL:
EASY
DURATION:
15 - 40 minutes
SIZE:
10-15 lines!!
OBJECTIVE:
Use of TextPad and understanding the composition of a
Java-program.
REQUIREMENTS:
Ken Arnold & James Gosling Ch. 1 OR
Koffman Ch. 5.1
COMMANDS:
Roskilde edb-skole
Prog1:\OpgaverAlm\Main.doc
2003-04-02 /Michael Claudius
IDENTIFICATION: MAIN/MC
The problem
What is behind the mysterious args-array in the public void main(String[] args) method ?
We will take a closer look at this and what the args stands for and what it can be used for.
Assignment 1
Start to type in the HelloWorld program:
import java.util.*
class HelloWorld {
public static void main (String[] args) {
System.out.println("Hello world");
System.out.println(args[0]);
}
}
Compile the source-code as usual.
(Select ”Tools” from the menu and choose ”Compile Java” OR press <Ctrl>+<1>)
From the ”Tools”-menu, choose ”Run Java Application” OR press <Ctrl>+<2>)
Then you will get an IndexOutOfBoundsException. Why?
Assignment 2.
From the ”Tools”-menu, choose ”Run…" instead.
Then a special dialogwindow pops up. Fill out the textfields:
command with "java"
parameter must be the name of the class: "HelloWorld"
and mark it as a DOS command as shown below:
Click on OK.
You are now running it from a DOS-prompt and you still have the exception !
Try to run again, but this time fill the textfield "parameter" with "HelloWorld Hi my name is xx ".
Look at the print out. Explain what happened !
Roskilde edb-skole
Prog1:\OpgaverAlm\Main.doc
2003-04-02 /Michael Claudius
Assignment 3
Set up a for-loop and print out all values of the args-array.
Explain what args is!
Assignment 4
Try to sort the args by using the method sort in Arrays class in package java.util:
Arrays.sort(args)
Compile and run !