Know for Quiz 2 - PowerPoint PPT Presentation

About This Presentation
Title:

Know for Quiz 2

Description:

Know for Quiz 2 Everything through Exam 1 Loops: while, for, do-while switch-case Statement – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 16
Provided by: Preferr1561
Learn more at: http://www.cs.iit.edu
Category:
Tags: class | java | know | quiz | scanner

less

Transcript and Presenter's Notes

Title: Know for Quiz 2


1
Know for Quiz 2
  • Everything through Exam 1
  • 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
  • Find a character or substring in String
  • Convert to lower or upper case

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
  • indexOf(char) return int of first occurrence of
    char (index starts at 0)
  • indexOf(String substr) return int of first
    occurrence of String substr
  • length() return int length of String
  • substring(int start, int end) return String
    between start and end
  • toLowerCase(), toUpperCase() convert 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
  • compareString myString.toLowerCase()
  • System.out.println("String is "
    compareString)
  • System.out.println("Char S is at location "
    myString.indexOf('S'))
  • System.out.println("substring og is at location
    " myString.indexOf("og"))
  • System.out.println("Length of string is "
    myString.length())
  • System.out.println("substring between location 3
    and 5 is " myString.substring(3,5))
  • Output
  • String is my dog sandy
  • Char S is at location 7
  • Substring og is at location 4
  • Length of string is 12
  • Substring between location 3 and 5 is Do

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 310-313 in Anderson
  • Check Input for Type Desired
  • If not, Notify User, and Get Rid of Line

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.")
  • System.out.print("Enter Integer ")
  • numE scan.nextInt()
  • System.out.println("You entered " numE)

11
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
12
Input Files
  • import java.io.
  • import java.util.Scanner
  • class FileInput
  • public static void main(String args) throws
    IOException
  • File ifile new File("data.txt")
  • Scanner scan new Scanner(ifile)
  • while (scan.hasNextInt())
  • i scan.nextInt()

13
Output File Streams
  • import java.io.
  • class FileOutput
  • public static void main(String args) throws
    IOException
  • int i
  • FileOutputStream ofile new FileOutputStream("da
    ta2.txt",false) //trueAPP
  • PrintWriter pw new PrintWriter(ofile)
  • for (i0i lt 100 i i 2)
  • pw.println(i)
  • pw.close() // Writes data to file on disk

14
  • 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 lt loops i)
  • integer scan.nextInt()
  • decimal scan.nextFloat()
  • name scan.next()

mydata.txt file 5 8 9.3 Jon 6 14.335 Bill 0
35.67e9 Mary -23 -4.55 Smith -3 -4e3 xyz
Output
8 9.3 Jon 6 14.335 Bill 0 3.567E10 Mary -23
-4.55 Smith -3 4000.0 xyz
15
Know for Quiz 2
  • Everything through Exam 1
  • Loops while, for, do-while
  • switch-case Statement
Write a Comment
User Comments (0)
About PowerShow.com