1 / 75

Review of CSE115

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

rianna
Download Presentation

Review of CSE115

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Review of CSE115

  2. 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 );

  3. 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

  4. 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.

  5. 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

  6. 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

  7. 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

  8. 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.

  9. 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

  10. Use instead of System.out.println int x = 10; JOptionPane.showMessageDialog(null,"X = " + x );

  11. 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

  12. 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

  13. // header • Name, Lab, Assumptions • Comments : /* Block Comments */ // end-line comments

  14. 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.

  15. public variables • variables that are directly accessible from outside the class • regarded as “unsafe” • always initialize; public static String userInput = “ ”;

  16. 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;

  17. 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

  18. 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

  19. 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

  20. 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 = “%/ >”

  21. 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 >

  22. Work Areas on the main computer disk mkdir Lab1 cd Lab1 ~/ Lab1/ ~/Lab1 Work Area is the location of “edited” files

  23. 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

  24. 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

  25. Logging on from “other” computers ssh -X username@timberlake.cse.buffalo.edu ssh -X username@styx.cse.buffalo.edu

  26. Display System.out.println( “anything in quotes” ); System.out.println( anyVariable ); e.g. String greeting = “Hello”; System.out.println( greeting );

  27. 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

  28. 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

  29. 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

  30. Two System methods System.out.println( x +" factorial: " + Product ); - concatenate Strings System.exit( 0 );- stop the program

  31. 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 ); }

  32. 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

  33. String Helper Methods String userInput, Str1, Str2, Str3; int Int1, Int2; Str2 = userInput.toLowerCase( );

  34. 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( );

  35. 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

  36. String Helper Methods Str2 = Str1.toUpperCase( );

  37. String helper methods Str1 = "x"; Int1 = Str1.compareTo("x"); 0 Int1 = Str1.compareTo("a"); 23 Int 1 = Str1.compareTo("z"); - 2

  38. String Helper Methods Int2 = Str1.indexOf("X"); • The place of X in the string • - 1 if X not there

  39. the word “Flag” • not a keyword, just a standard • commonly a boolean • a boolean variable that reflects status • e.g. quitFlag, continueFlag, etc.

  40. Setting up a do…while boolean quitFlag = false; do { } while (quitFlag == false);

  41. Setting up a while boolean quitFlag = false; while (quitFlag == false) { }

  42. 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

  43. do { // program runs forever } while( true ); while ( true ) { } The Outermost Loop

  44. loops within loops do { continue; do { continue; } while ( quitFlag = = false ); } while ( true );

  45. Get / Set Methods • Private Variables and the Protection of Data • Methods with Input Parameters • Sending Arguments to Methods • Get and Set Methods

  46. 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;

  47. 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

  48. 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.

  49. “Set” method – protects accountBalance public class accountClass { private static double accountBalance; public static void setBalance(double inputBalance) { accountBalance = inputBalance; } }

More Related