cosc 1020 the stringtokenizer class yves lesp erance
play

COSC 1020 The StringTokenizer Class Yves Lesp erance Allows you to - PDF document

COSC 1020 The StringTokenizer Class Yves Lesp erance Allows you to easily extract tokens/components from a String. e.g. import type.lang.*; import java.util.StringTokenizer; public class TokenizerEg { public static void main(String[]


  1. COSC 1020 The StringTokenizer Class Yves Lesp´ erance Allows you to easily extract tokens/components from a String. e.g. import type.lang.*; import java.util.StringTokenizer; public class TokenizerEg { public static void main(String[] args) Lecture Notes { String s = "Today is October 23."; StringTokenizer t = new StringTokenizer(s); Week 7 — Testing & File I/O while(t.hasMoreTokens()) { IO.println(t.nextToken()); } } } zebra 312 % java TokenizerEg Today Recommended Readings: is Horstmann: Ch. 8 Sec. 1 to 3 & Sec. 6.4.2 October 23. Lewis & Loftus: Ch. 10 Sec. 1 & Ch. 4 Sec. 6 zebra 313 % 1 You can also specify what characters you consider de- limiters and whether you want these to be returned, You can sometimes chose delimiters so that it is easy e.g. to extract components of the string, e.g. import type.lang.*; import type.lang.*; import java.util.StringTokenizer; import java.util.StringTokenizer; public class TokenizerEg2 public class TokenizerEg3 { public static void main(String[] args) { public static void main(String[] args) { String s = "Today is October 23."; { String s = "Doe, John T.;203203203;cs232323"; StringTokenizer t = StringTokenizer t = new StringTokenizer(s,";"); new StringTokenizer(s," .",true); String name = t.nextToken(); while(t.hasMoreTokens()) IO.println("name: " + name); { IO.println(t.nextToken()); long number = Long.parseLong(t.nextToken()); } IO.println("number: " + number); } String account = t.nextToken(); } IO.println("account: " + account); } zebra 317 % java TokenizerEg2 } Today zebra 324 % java TokenizerEg3 is name: Doe, John T. number: 203203203 October account: cs232323 zebra 325 % 23 . zebra 318 % 2 3

  2. import type.lang.*; import java.util.StringTokenizer; public class CapJava { public static void main(String[] args) { final String INFILENAME = "infile.txt"; final String OUTFILENAME = "outfile.txt"; UniReader myReader = new UniReader(INFILENAME); UniWriter myWriter = E.g. Capitalizing “java” in a text file new UniWriter(OUTFILENAME); while(true) zebra 365 % more infile.txt { String inline = myReader.readLine(); The java programming language was developped in the if(myReader.eof()) ’90s; it is good for programming web applications. break; You must learn java! String outline = ""; zebra 366 % java CapJava StringTokenizer st = zebra 367 % more outfile.txt new StringTokenizer(inline," .,;:?!",true); The Java programming language was developped in the while(st.hasMoreTokens()) ’90s;it is good for programming web applications. { String tok = st.nextToken(); You must learn Java! if(tok.equals("java")) zebra 368 % { tok = "Java"; } outline = outline + tok; } myWriter.println(outline); } myReader.close(); myWriter.close(); } } 4 5 The switch Statement Problem: Given a letter grade letGrade , assign the Java provides another control structure for selecting numerical grade equivalent to numGrade . among alternatives depending on the value of an ex- pression of an ordinal type such as int or char , the char letGrade; switch statement. int numGrade; ... // determine numerical grade switch ( expression ) switch (letGrade) {case value1 : {case ’A’ : numGrade = 9; statements1 break; break; case ’B’ : case value2 : numGrade = 7; break; statements2 case ’C’ : break; numGrade = 6; . . . break; case ’D’ : case valueN : numGrade = 5; statementsN break; break; case ’F’ : default : // optional numGrade = 4; break; statementsOtherwise default : } // end switch IO.println("Error: bad letter grade"); numGrade = 0; statementsK are executed when expression has } valueK . statementsOtherwise are performed when expression has a value different from all of the cases. 6 7

  3. Note also that cases requiring the same actions can Another Loop E.g. grouped together. Problem: write a program that reads the marks from Problem: Print store hours depending on weekday . a class and produces a histogram showing the distri- final int SUNDAY = 0; bution of the marks; the marks are integers and are final int MONDAY = 1; final int TUESDAY = 2; out of 100; the end of input is indicated by a sentinel, final int WEDNESDAY = 3; a negative integer. E.g. final int THURSDAY = 4; final int FRIDAY = 5; tiger 62 % more marks.txt final int SATURDAY = 6; 72 int weekday; 89 ... // assign weekday 76 switch (weekday) 65 {case MONDAY : case TUESDAY : 75 case WEDNESDAY : case SATURDAY : 34 IO.println("Hours are 10am - 6pm"); 95 break; -1 case THURSDAY : case FRIDAY : tiger 62 % java MarksHistogram < marks.txt IO.println("Hours are 10am - 9pm"); Marks Histogram break; case SUNDAY : A: ** IO.println("Hours are 12am - 5pm"); B: *** break; C: * default : D: IO.println("Error: bad weekday"); F: * } 8 9 Second subtask, printing the histogram: Two main subtasks: print header read marks and calculate distribution print line for As print histogram print line for Bs First subtask: To store the distribution, can use a counter . . . print line for Fs for each marks category. Reading the marks is a repetitive task. Don’t know in advance how many rep- Printing lines is repetitive, but must use different counter etitions, so use conditional loop. Exit loop when input each time; so can’t use a loop. is negative. Algorithm: Subtask “print line for category c with count n ”: initialize counters noAs , noBs , noCs , noDs , and noFs to 0 print label c : loop print n stars read mark { if mark < 0 skip to next line exit loop if mark >= 80 Subtask “print n stars”: increment noAs else if mark >= 70 Number of stars varies, so must repeatedly print one increment noBs star n times. Know how many repetitions at beginning else if mark >= 60 increment noCs of loop, so use for loop. Loop counter should go from else if mark >= 50 1 to n . So: increment noDs else for i from 1 to n incrementing by 1 increment noFs print a star { } } 10 11

  4. Code import york.*; IO.println("Marks Histogram\n"); IO.print("A: "); public class MarksHistogram for (int i = 1; i <= noAs; i++) { public static void main(String[] args) IO.print("*"); { int mark; IO.println(""); int noAs = 0; IO.print("B: "); int noBs = 0; for (int i = 1; i <= noBs; i++) int noCs = 0; IO.print("*"); int noDs = 0; IO.println(""); int noFs = 0; IO.print("C: "); while(true) for (int i = 1; i <= noCs; i++) { mark = IO.readInt(); IO.print("*"); if (mark < 0) IO.println(""); break; IO.print("D: "); if (mark >= 80) for (int i = 1; i <= noDs; i++) noAs++; IO.print("*"); else if (mark >= 70) IO.println(""); noBs++; IO.print("F: "); else if (mark >= 60) for (int i = 1; i <= noFs; i++) noCs++; IO.print("*"); else if (mark >= 50) IO.println(""); noDs++; } else } noFs++; } 12 13 Black-Box Testing Testing Generate test cases based on the specification the Testing is the process of discovering errors/bugs by class/method/app you are checking. No access to im- executing a method in a class or an app with some plementation code. test data. We try to break the method or app. Check boundary conditions. Should test each unit (method) individually. White-Box Testing Have access to implementation code. Generate test Design test suite; data can be hand picked and/or ran- cases to check that unit works for every possible code domly generated; can keep in a file. flow or branch. The more branches are tested, the better the test coverage. Write harness to feed test data to method, get results, and produce report. Regression Testing Use oracle to check results; sometimes there is a sim- Fixing one bug can introduce addditional errors. Need ple way to check, e.g. Equation; in other cases, have to ensure that tests done earlier can still be passed. file of known results. Best to rerun all tests after each modification. 14 15

  5. Limits of Testing Testing can only ensure correctness when there is a finite number of possible inputs and all are checked. Use design-by-contract to ensure you have a precise specification of what unit should do. Also helps in gen- eration of test cases. Extreme programming: write test cases first. Can try to do formal verification. It ensures correct- ness, assuming that proof is correct. But often cost is prohibitive. 16

Recommend


More recommend