some Unix advice
• • • • at the command prompt not in the editor not related to Java code note: the proper name for a “workspace “ or “folder”, is directory
Some Unix help
man command - lists the user manual pages for the command man –k keyword – search for manual page containing keyword
Running jobs in “background”
Sometimes a unix task takes a long time. If you start the command with “&”, you get the unix prompt right back: > netscape & will run netscape in another window
1
Running jobs in background
jobs – displays all background tasks, by number kill %n - stops background job number n
If a command gets “stuck”
If you type the command, and you get no response: -c or -z - hit Ctrl key and z together jobs - lists the jobs running, find the one that’s stuck kill %x
Ending a java program so it doesn’t get stuck
public static void main(String[] args) { . . . System.exit( 0 ); // last line }
2
File commands
ls –l or ls –F - list files cp old new – copy file to new name mv old new – move file (same as rename) rm filename - erase file rmdir directoryname - erases empty directory
Directories
pwd – list present working directory cd directory – change to directory cd ~ – go to home directory mkdir name – make a new directory
Creating a lab directory
cd ~ mkdir Lab2 cd Lab2
3
Time Stamp directory
cd ~ mkdir TimeStamp cp Lab2/*.* TimeStamp
Submitting labs
cd ~ cd Lab2 ls –l submit_cse113 Lab2.java for each file
Editing files
pico filename emacs filename nedit filename – will ask to create a new file vi filename
4
Lab grades
• if you have a concern….
– write to TAs and ask why – copy me: mikeb@cse.buffalo.edu – if you're still unhappy, send the TA's response to me – we'll fix it – we have 3 years
A few Java items
Adding Labels to System.out.println
• Use the + sign to combine printable text and values x = 12; System.out.println(“x value: ” + x); x value: 12
5
Initializing at the type definition
int x = 0; String Greeting = “Hello”; boolean Status = true;
The "if..else" statement
if ( a == b ) { // do this } else { // do that }
comparison
"logical" comparisons
if ( a == b ) if ( a != b ) if ( a < b ) if ( a > b ) if ( a <= b ) if ( a >= b )
6
User Input
Java Class Libraries
• Methods (in Classes), written by other people for you to use in your programs. • Over 130,000 Classes exist, each with many methods. • Some come with Java, others must be imported when you write your program.
– import javax.swing.JOptionPane;
• Swing Class library: user input and output • Java Beans Class library: on-screen graphics
Integer Class library
• • • • convert to and from integers used to convert a String to an int. Parse – to “extract” Parse to Integer – find an integer in a string (e.g. xxx100xxx), and extract it (e.g. 100).
int x; String UserInput = “14”; // readable 14 x = Integer.parseInt( UserInput );
7
Calling a library method
4 1 2
return
= libraryClass.method ( send );
3
1 - a collection of classes 2 - a particular method in that class 3 - a value “sent” to the method 4 - the result of the method’s work in the example below a string is sent, and an integer is returned
x = Integer.parseInt( UserInput );
JOptionPane
• Pane, not pain – like a “window pane” • Puts a standard message box on-screen import javax.swing.JOptionPane;
UserInput = JOptionPane.showInputDialog("Enter x: "); An on-screen message is sent. A string is returned.
Taking user input from the keyboard
1. import javax.swing.JOptionPane; 2. declare a string variable UserInput, and an integer variable x
3. UserInput = JOptionPane.showInputDialog(“Enter x: “);
4. x = Integer.parseInt( UserInput ); //convert String to int
8
int to String
• Convert an int to a String (the other way) String y; int x; y = Integer.toString( x ); // x is an int, y is a String remember: return = library.method ( send )
int to String
• Convert an int to a String (the other way) String y; int x; y = String.valueOf( x ); // x is an int, y is a String remember: return = library.method ( send )
Why is String capitalized, and “int” not?
• • • • • • String is a class Library int is not a class Library Integer.parseInt - Integer is a class Library JOptionPane is a class Library boolean is not double is not
String.valueOf - class Libraries have methods
9
String Greeting = “ ”; ????
How can Greeting be defined as a String, if String is a class library? The String class library makes Greeting a member, takes over all control of Greeting, and manages it, just like “int” or “char” or “boolean” are managed by Java. Greeting is an object of the String class.
the Java keyword “null”
• It means, literally, “nothing” • Has no value • Used as a placeholder before something is given a legitimate value. • Most uninitialized variables have a null value.
A simple Message Box
JOptionPane.showMessageDialog (null, "Welcome to the CSE 113 demo"); • null is a necessary placeholder for showMessageDialog • will be used later to specify where to place Message • if null, then it shows up on your screen
10
Use instead of System.out.println
int x = 10; JOptionPane.showMessageDialog(null,"X = " + x );
A simple Yes/No/Cancel box
int Int2; Int2 = JOptionPane.showConfirmDialog (null, "Do you really want to Exit?"); Returns an int – Int2 = 0 means Yes Int2 = 1 means No Int2 = 2 means Cancel
11
Iteration
Iteration
• Repeatedly execute the same, bracketed section of Java code • Execute until certain conditions are met • while loops • for loops
Iteration - the “do-while” loop do { // repeated code here } while (wantToQuit == false);
12
while loop
int LoopCounter = 0; while (LoopCounter < 6) { LoopCounter = LoopCounter + 1; } System.out.println(“LoopCounter is: ” + LoopCounter); then, on screen… LoopCounter is: 6
for loop
for (x = start; condition for x valid; increment ) { // repeated code here }
for loops use an “auto-increment” variable
for ( x = 0; { }
x < = 100; x=x+1 )
// x goes from 0 to 100
int Counter = 0; for (Counter = 4; Counter < 10; Counter = Counter + 2) {
}
Counter is 4, 6, 8
13
for loop
int x=0; int total = 0; for (x = 0; x < 10; x = x+1) { total = total + x; } System.out.println(“x value: ” + x); System.out.println(“total value: ” + total);
on screen… x value: 9 total value: 45
Flow of Control within a method
public void method1 ( ) { 1 int x; for (x = 0; x<5; x=x+1) {
2 loop 3 x = 0, 1, 2, 3, 4
} }
shorthand 1
• x++ means x = x+1
14
the word “Flag”
• • • • not a keyword, just a standard commonly a boolean a boolean variable that reflects status e.g. quitFlag, continueFlag, etc.
Setting up a do…while boolean quitFlag = false; do {
} while (quitFlag == false);
The Outermost Loop
do { // program runs forever
} while( true );
15
"a Pattern"
boolean wantToQuit = false; do { // set wantToQuit here // …. if (wantToQuit == true) { System.exit(0); } // all of your main code here } while( true );
16