Presentation is loading. Please wait.

Presentation is loading. Please wait.

Know for Quiz Everything through Last Week and Lab 7

Similar presentations


Presentation on theme: "Know for Quiz Everything through Last Week and Lab 7"— Presentation transcript:

1 Know for Quiz Everything through Last Week and Lab 7
Loops: while, for, do-while switch-case Statement

2 Strings Revisited String Is a Type (Object) in JAVA
Automatically imported in java.lang May Initialize or Assign with Double Quoted Values Can Add String to String with ‘+’ Operator CANNOT Compare using Equality Operators (uses .equals() method instead) Methods Available to Access single characters Get length of String Compare Strings

3 Objects Objects: Types of Variables Made from Built-In Types
Variables Can be Declared as an Object Type Objects Usually Have Functions Tied to Them Member functions Public functions Interface functions Methods String Is an Example of an Object Type StringVariable.length() Returns the Number of Characters in the String

4 Methods Also called Procedures, Functions
Have Parenthesis (e.g., method() ) May be Passed Arguments, or Parameters Parameters are values to initialize new variables in method Parameters used to pass information to method

5 String Methods charAt(int index): return char at named index (starting at 0) equals(String str): return boolean length(): return int length of String

6 String myString = "My Dog Sandy",compareString;
System.out.println("The fourth character is: " + myString.charAt(3)); if (myString.equals("my dog sandy")) System.out.println("Strings are equal"); else System.out.println("Strings are not equal"); compareString = myString + myString; System.out.println("Added strings are: " + compareString); Output: The fourth character is: D Strings are not equal Added strings are: My Dog SandyMy Dog Sandy

7 System.out.println("Length of string is: " + myString.length());
Output: Length of string is: 12

8 import java.util.Scanner;
public class MyProgram { public static void main( String [ ] args) Scanner scan = new Scanner(System.in); String lastname; boolean instructor = false; System.out.print(“Enter last name:”); lastname = scan.next(); if(lastname.equals(“hanrath”)) instructor = true; // privileged else instructor = false; // not privileged }

9 More on Input Input Failure Common *MUST* Be Part of Test Plan
Pages in Anderson Check Input for Type Desired hasNextInt(), hasNextDouble() If not, Notify User, and Get Rid of Line Once hasNotXXX() returns true, safe to read in value

10 Input Failure Example import java.util.*; class InputFailureExample {
public static void main(String[] args) int numE; String garbage; Scanner scan = new Scanner(System.in); System.out.print("Enter Integer: "); while (!scan.hasNextInt()) garbage = scan.nextLine(); System.out.println("Please try again."); } numE = scan.nextInt(); System.out.println("You entered: " + numE);

11 Arrays Syntax: type variableName[] = new type [size];
Memory Is Set Aside for size Items of type Each Variable Location in Array Is Accessed by Offsetting into the Array with an Integer Expression Legitimate Offsets Are 0 to size-1

12 Array Example int [] values = new int[15]; values[0] = 150;
System.out.println( values[0]); values[3] = values[0] + 6;

13 Array Example final int ARRAY_SIZE = 100; int offset;
int [] numArray = new int [ARRAY_SIZE]; for(offset = 0; offset < ARRAY_SIZE; offset++) { numArray[offset] = 0; } for(offset = 0; offset < numArray.length; offset++) numArray[offset] = offset;

14 Files Data in Main Memory is “volatile”
File: Place for “permanent” data storage C: drive, A: drive, Flash drive, etc. Main Memory File main() int num; string firstname; Disk

15 Output File Streams import java.io.*; class FileOutput {
public static void main(String [ ] args) throws IOException int i; FileOutputStream ofile = new FileOutputStream("data2.txt",false); //true:APP PrintWriter pw = new PrintWriter(ofile); for (i=0;i < 100; i = i + 2) pw.println(i); } pw.close(); // Writes data to file on disk

16 Input Files import java.io.*; import java.util.Scanner;
class FileInput { public static void main(String [ ] args) throws IOException File ifile = new File("data2.txt"); Scanner scan = new Scanner(ifile); while (scan.hasNextInt()) i = scan.nextInt(); }

17 mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0 35.67e9 Mary
import java.util.Scanner; import java.io.*; class FormatFileData { public static void main(String [ ] args) throws IOException int loops, integer, i; float decimal; String name; File ifile = new File("mydata.txt"); Scanner scan = new Scanner(ifile); loops = scan.nextInt(); for(i= 0 ; i < loops; i++) integer = scan.nextInt(); decimal = scan.nextFloat(); name= scan.next(); System.out.print(integer + " "); System.out.print(decimal + " "); System.out.print(name + " "); System.out.println(); } mydata.txt file 5 8 9.3 Jon Bill e9 Mary Smith -3 -4e3 xyz Output: 8 9.3 Jon Bill E10 Mary Smith -3 – xyz

18 Know for Quiz 2 Everything through Last Week and Lab 7
Loops: while, for, do-while switch-case Statement


Download ppt "Know for Quiz Everything through Last Week and Lab 7"

Similar presentations


Ads by Google