Question
PF2 lab6 This lab will provide additional practice working with Strings by having you to parse a string and extract all of the tokens in
PF2 lab6 This lab will provide additional practice working with Strings by having you to parse a string and extract all of the tokens in the string, given a source string and (optionally) a list of delimeters. You are to implement your own version of the StringTokenizer class. Your program should function identically to the StringTokenizer class, but you are only required to implement the methods/constructors that are called from the test program (below). One other change I made to my ST class is to put a line or two of code in the constructor(s) so that it will print out the String that it is tokenizing, the list of delimeters, and the number of tokens. See my output below. Make your program do the same. Tell you what, I'll steal an idea that I know that other instructors have done here at AC in the past. I'll give you a couple options on this lab which will provide different levels of difficulty and as a result, different maximum grade values. A) If your program will tokenize Strings such as #1 only, your program will be worth a maximum grade of 70. B) If your program will tokenize Strings that are delimeted by the default delimeter (space character) as in #1 and #2 below, your program will be worth a maximum grade of 80. C) If your program will allow you to pass in a different SINGLE delimeter (problem #3 below) to override the default, your program will be worth a maximum grade of 90. D) If your program will allow you to pass in a list of delimeters (problem #4 below), it will be worth a maximum grade of 100. Your program should use the test program shown below, but should work for other sets of data as well. Do not modify my main method except for commenting out the sections of code which you have not (yet) implemented. NOTE: Your program can not use the StringTokenizer class or any other class or method that we have not covered in class. One simplifying assumption I made when I wrote my program is to assume that the Strings that are passed in will have no more than 50 tokens. Depending on how you write your program, this will make your code a tiny bit simpler. Put your program in the file: ~/PF2/lab6/Lab6.java
= TEST PROGRAM ========================================================
public class Lab6 { public static void main(String argv[]) {
String str;
//1)
str = "Hello world";
ST stok = new ST(str); System.out.println("[" + str + "]");
while (stok.hasMoreTokens()) {
System.out.println("#tokens = " + stok.countTokens());
System.out.println("token: [" + stok.nextToken() + "]"); }
System.out.println("#tokens = " + stok.countTokens());
//System.out.println("token: " + stok.nextToken());
System.out.println(" ");
//2)
str = " Hello world ";
stok = new ST(str);
System.out.println("[" + str + "]");
while (stok.hasMoreTokens()) {
System.out.println("#tokens = " + stok.countTokens());
System.out.println("token: [" + stok.nextToken() + "]"); }
System.out.println("#tokens = " + stok.countTokens());
System.out.println(" ");
//3)
str = "root:x:0:0:root:/root:/bin/bash";
stok = new ST(str, ":");
System.out.println("[" + str + "]");
int n = stok.countTokens();
System.out.println("#tokens = " + n);
for (int i=0; i System.out.println("token: [" + stok.nextToken() + "]"); } System.out.println(" "); //4) str = "Hello-world.It is!a nice day,today"; stok = new ST(str,"-.!, "); System.out.println("[" + str + "]"); while (stok.hasMoreTokens()) { System.out.println("#tokens = " + stok.countTokens()); System.out.println("token: [" + stok.nextToken() + "]"); } System.out.println("#tokens = " + stok.countTokens()); } } = OUTPUT ============================================================== [Hello world] #tokens = 2 token: [Hello] #tokens = 1 token: [world] #tokens = 0 [ Hello world ] #tokens = 2 token: [Hello] #tokens = 1 token: [world] #tokens = 0 [root:x:0:0:root:/root:/bin/bash] #tokens = 7 token: [root] token: [x] token: [0] token: [0] token: [root] token: [/root] token: [/bin/bash] [Hello-world.It is!a nice day,today] #tokens = 8 token: [Hello] #tokens = 7 token: [world] #tokens = 6 token: [It] #tokens = 5 token: [is] #tokens = 4 token: [a] #tokens = 3 token: [nice] #tokens = 2 token: [day] #tokens = 1 token: [today] #tokens = 0 This is what I have so far: class ST { private String star[]; private int numtokens; public int index; public ST(String s, String delim) { star = new String[50]; index =0; for (int i = 0; i { ??? } } private boolean isDelim(String c, String d) { if(d.indexOf(c)!=-1) { return true; } else { return false; } } public ST(String s) { this(s, " "); } public String nextToken() { String returnString; returnString = star[index]; index++; return returnString; } public int countTokens() { return(numtokens - index); } public boolean hasMoreTokens() { return(numtokens>index); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started