Main Class Java
luispriscila26 de Junio de 2012
987 Palabras (4 Páginas)627 Visitas
public class Main(){
public static void main(String[] args){
//Body of main method
System.out.println("I just want to say hello!");
}
}
In Java, you need to have a method named main in at least one class. The following is what must appear in a real Java program.
public static void main(String [ ] args)
{
UrRobot Karel = new UrRobot(1, 1, East, 0);
// Deliver the robot to the origin (1,1),
// facing East, with no beepers.
Karel.move();
Karel.move();
Karel.move();
Karel.pickBeeper();
Karel.turnOff();
}
This method must appear within a class, but it can be any class. So here is the above main method enclosed in a class called Temporary. This would normally be in a file named Temporary.java. Your class should implement the Directions interface so that words like North are understood. We also put all of our files into the kareltherobot package so that all features of the UrRobot class are available. So here is a complete version of the file Temporary.java:
package kareltherobot;
public class Temporary implements Directions
{
public static void main(String [] args)
{
UrRobot Karel = new UrRobot(1, 1, East, 0);
// Deliver the robot to the origin (1,1),
// facing East, with no beepers.
Karel.move();
Karel.move();
Karel.move();
Karel.pickBeeper();
Karel.turnOff();
}
}
--------------------------------------------------------------------------------
For those wanting explanation of some of the Java words here we provide the following somewhat simplified definitions.
A Java package is a way to collect different parts of a large program together logically. The Java system (JDK or Java Development Kit) comes with several packages of its own, but you can create your own. The name kareltherobot was created by the authors of the simulator for Karel J. Robot as a convenient place in which to collect robot programming code.
In Java the qualifier public means that something is available across packages. It is also possible to restrict visibility of names like Temporary to a single package or even more, but here we make it public.
The particular form of main is required by Java. While the following explanation of its parts is not needed now, and probably not especially understandable, you can return to it later when you know more.
In Java, main is a static method. This means the method is part of its class and not part of objects. Robots are objects. They are defined by classes, which are like factories for the creation of objects. In terms of our metaphor for robot programming, a static method is like the instructions read to robots by the helicoptor pilot, not the instructions known by the robots themselves. We will see ordinary (instance) methods in the Chapter 3.
Strings in Java are sequence of characters, like the characters in this sentence. The matched brackets indicate that an array of Strings is required. An array is a certain kind of linear collection of things. The name args is just a name for this array. This name is the only part of the declaration of main that can vary.
The declaration (String [] args) is in parentheses as part of the declaration of main to indicate that when the class Temporary is executed by your computer's operating system, an array of strings can be sent to the program to help with program initialization. Since they are not referenced again in the body of main (the part between
...