750 likes | 882 Views
Review of CSE115. 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
E N D
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 );
Calling a library method 4 1 2 return = libraryClass.method ( send ); 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 ); 3
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 • import javax.swing.JOptionPane; • declare a string variable UserInput, and an integer variable x • UserInput = JOptionPane.showInputDialog(“Enter x: “); • x = Integer.parseInt( UserInput ); //convert String to int
String libraries • 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 ) 10000s of such methods
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
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 116 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 Welcome to the CSE 116 demo
Use instead of System.out.println int x = 10; JOptionPane.showMessageDialog(null,"X = " + x );
A simple Yes/No/Cancel box 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
comment libraries class definition shared variables main method not-shared support method note end brackets // header imports; // explain why if possible public class Lab1MainClass { public variables; public static void main(String args [ ]) { int x; } // end main method public static supportMethod( ) { }// end supportMethod } // end Lab1MainClass
// header • Name, Lab, Assumptions • Comments : /* Block Comments */ // end-line comments
imports import javax.swing.JOptionPane; better than import javax.swing.*; No restrictions on import libraries in lab. The alternate to importing libraries explicitly is editing an editor’s CLASSPATH.
public variables • variables that are directly accessible from outside the class • regarded as “unsafe” • always initialize; public static String userInput = “ ”;
private variables • only accessible by methods within the class • considered “safe”, because methods must be written in the class, to present these values to objects outside the class (Get/Set methods) • always initialize private int tempGrade = 0;
local variables • Known only within the method in which they are defined. Sometimes called “trash” variables. public static void main(String args [ ]) { int x; } // end main method
Variable “domain” public class myMainClass { public static int x; private int y; public static void main(String args [ ]) { int z; ……….. code here ……………… } // end main method public void supportMethod( ) { ……can x, y, or z be used here? ….. } } // end myMainClass
public static void main (String args [ ]) { int choice do {choice = JOptionPane.showConfirmDialog (null,“Exit?"); if ( choice = = 0 ) // only exit if = = 0, or yes { System.exit(0); } // program work done here } while( true ); } // end main method
A “Terminal Session” Log In: Username and Password A window appears (???) Check the prompt: some computer name {other stuff } > styx.cse.buffalo.edu is the computer, there are many. “{ }” can be anything “>” is where you type 4. set prompt = “%/ >”
Creating Folders (or Subdirectories, or Work Areas) You type… set prompt=“%/ >” eng/home/mikeb> mkdir Lab1 eng/home/mikeb > cd Lab1 eng/home/mikeb /Lab1 > What just happened? eng/home/mikeb /Lab1 > cd ~ eng/home/mikeb >
Work Areas on the main computer disk mkdir Lab1 cd Lab1 ~/ Lab1/ ~/Lab1 Work Area is the location of “edited” files
What is a file? Dear John, I think we should just be friends… Created and Saved in an “editor”: eng {~}> /util/bin/jgrasp Dearjohn.txt Could be a Java program: eng {~/Lab1} > /util/bin/jgrasp myProgram.java
Unix vs. Java • Unix: • - At the terminal prompt • Conversations with the computer to build files • clear • ls • set prompt = “% >” • mkdir • cd ~ • cd directoryName • /util/bin jgrasp • rm filename • Java: • A program that runs • In a file • not Unix • public class name • public static void main… • - brackets
Logging on from “other” computers ssh -X username@timberlake.cse.buffalo.edu ssh -X username@styx.cse.buffalo.edu
Display System.out.println( “anything in quotes” ); System.out.println( anyVariable ); e.g. String greeting = “Hello”; System.out.println( greeting );
if…else statements int x; String displayText; if (x = = 0) // note comparison equal { displayText = “X is zero”; } else { displayText = “X is not zero”; } // end if x is zero System.out.println( displayText ); optional
public class Factorial{public static void main( String args [] ) { int Product = 1; // holds the running factorial int FactNum = 25; // number to find the factorial of int x = 0; // increment variablewhile ( x < FactNum ) { x = x+1; Product = Product * x; System.out.println( x + " factorial: " + Product ); if (Product < 0) { System.out.println("Error! Number too large."); System.exit( 0 ); } }// end while loop } // end main} // end class
public static void main( String args [] ) { int oldProduct=0, newProduct = 1; // holds the running factorial int FactNum = 25; // number to find the factorial of int x = 0; // increment variable while ( x < FactNum ) { x = x+1; oldProduct = newProduct; newProduct = newProduct * x; if ( (newProduct / oldProduct) < x ) { System.out.println("Error! "); System.exit( 0 ); } } // end while loop } // end main
Two System methods System.out.println( x +" factorial: " + Product ); - concatenate Strings System.exit( 0 );- stop the program
Methods in main, the CALL b = addNumbers( q, r ); the METHOD public static int addNumbers( int x, int y) { int z = 0; z = x + y ; return( z ); }
String libraries • 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 ) 10000s of such methods
String Helper Methods String userInput, Str1, Str2, Str3; int Int1, Int2; Str2 = userInput.toLowerCase( );
Note the DIFFERENT way (i.e. ACTOR) of calling methods… String userInput, lowerStr; int x; Integer Y =10; x = Integer.parsInt( userInput ); lowerStr = userInput.toLowerCase( ); x = Y.intValue( );
The ACTOR is • What class or object supplies the method • What it requires • What it provides • Every CLASS or OBJECT provides services • Services are methods
String Helper Methods Str2 = Str1.toUpperCase( );
String helper methods Str1 = "x"; Int1 = Str1.compareTo("x"); 0 Int1 = Str1.compareTo("a"); 23 Int 1 = Str1.compareTo("z"); - 2
String Helper Methods Int2 = Str1.indexOf("X"); • The place of X in the string • - 1 if X not there
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);
Setting up a while boolean quitFlag = false; while (quitFlag == false) { }
Continuing a do…while do { Str1 =JOptionPane.showInputDialog(“G to GoOn, or x to eXit"); Str2 = Str1.toUpperCase( ); Int1 = Str2.indexOf("X"); if (Int1 != -1) // there's an x somewhere { quitFlag = true; continue;// go to the end of THIS loop } } while (quitFlag = = false); jumps directly to while
do { // program runs forever } while( true ); while ( true ) { } The Outermost Loop
loops within loops do { continue; do { continue; } while ( quitFlag = = false ); } while ( true );
Get / Set Methods • Private Variables and the Protection of Data • Methods with Input Parameters • Sending Arguments to Methods • Get and Set Methods
Public Variables • Can be accessed directly, outside of the class public class testClass { public static int myInteger; } // in another class testClass testObject = new TestClass( ); testObject.myInteger = 10;
Private Variables • Cannot not be accessed outside of the class public class testClass { private static int myInteger; } testClass testObject = new testClass( ); testObject.myInteger = 10; // ILLEGAL will not compile
Therefore, methods can “hide” variables • A Class declares variables to be “Private”. • Not accessible (directly) outside of the Class • The Class provides a “Set” method to change the variable internally. • The Class provides a “Get” method to get the variable’s values.
“Set” method – protects accountBalance public class accountClass { private static double accountBalance; public static void setBalance(double inputBalance) { accountBalance = inputBalance; } }