Download printer-friendly

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
Creating True Executables
• In order for the JVM to run your program, it
needs to know where to start.
main and Scanner
• Put public static void
in one of your classes.
main(String[] args){ }
• Implement this method to create the objects
and call the methods in order to run the
program.
NSCC
CSC 142 Notes
Winter 2005
l-1
NSCC
CSC 142 Notes
Winter 2005
Running your program
l-2
Simple Text Input
• Via the GUI interface (e.g. Windows)
• Scanner class
– Create a .jar file
– Specify which class has the main method to use as the
starting point
– double click on the .jar icon
– Used to read in data to a program
– Either from the keyboard (System.in), a
file, or other stream
– Easily parses the stream into primitives
and String data
– Part of the java.util package
• From the command line
– Start the JVM and specify the name of the class that has the
main method
EX: I:\> java PointProgram
• For help in running executables
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/ind
ex.html
NSCC
CSC 142 Notes
Winter 2005
l-3
NSCC
What is a package?
CSC 142 Notes
Winter 2005
l-4
import
To use a class that is in a package, you must use its
complete name (e.g. java.util.Scanner)
The import statement allows you to refer to a class by its
simple name. (e.g. Scanner)
• Because the Java language has so
many classes (> 2500) they are
organized into packages.
import java.util.*;
• Helps to avoid name collisions (2
classes with the same name). They
reside in separate packages.
public class Test{
public class Test{
public Test(){
public Test( ){
java.util.Scanner c = newjava.util.Scanner(…);
Scanner c = new Scanner(…);
}
}
}
}
NSCC
CSC 142 Notes
Winter 2005
l-5
NSCC
CSC 142 Notes
Winter 2005
l-6
1
import (cont.)
Back to the Scanner class
You can either import a single class
import java.util.Scanner;
• To create the Scanner object for keyboard
input
Scanner in = new Scanner(System.in);
Or the entire package
import java.util.*;
• Class contains methods for reading in data
– nextXxx() where Xxx is a numeric type.
– next()
to read in the next String.
– See the class API to learn about others.
Careful when importing entire packages.
May still have naming collisions.
NSCC
CSC 142 Notes
Winter 2005
l-7
NSCC
CSC 142 Notes
Winter 2005
l-8
2