Part I There Can Be Only One 5 points Filenames Homework5Met

Part I: There Can Be Only One (5 points) Filename(s): Homework5Methods. java Write a method public static String eliminateDuplicates (String str) that returns a copy of the argument str after eliminating all duplicate characters. The first occurrence of each character must be pre- served and must stay in order relative to the other letters of the string. For example, if str began \"cdabba...\" then the return value would begin \"cdab\" and not, say, \"cdba\" or \"abcd\". The str parameter could contain y printable ASCII characters and spaces, but not tabs or newlines Examples: Method Cal eliminateDuplicates (\"abracadabra\") eliminateDuplicates (\"What\'s a Seawolf? I\'m a Seawolf!\"What\' s Sewolf?Im!\" eliminateDuplicates (\"Stony Brook University\") eliminateDuplicates (\"AaBbCcDd\") eliminat eDuplicates ( \" \" ) empty string as argument Return Value \"abrcd\" \"Stony BrkUives\" \"AaBbCcDd\" \" \" empty string Hint: Although we have not studied the StringBuffer class yet, you are welcome to read about it in the textbook (pp. 392-397) and use it. Here are are few other online resources that may be of help: . Basic tutorial: http://tutorialspointexamples.com/stringbuilder-in-java/ More examples: www.java-examples.com/java-stringbuffer-examples This website showcases the StringBuffer class instead of StringBuilder, but the two are very similar. Just replace the class name StringBuffer with StringBuilder and you\'re good to go! » From Oracle: docs.oracle.com/javase/tutorial/java/data/buffers.html The official docs: docs.oracle.com/javase/8/docs/api/java/1ang/StringBuilder.html

Solution

import java.util.*;
import java.lang.*;
import java.io.*;

class Homework5Methods
{
public static String eliminateDuplicates(String str)
{
boolean checked[] = new boolean[150]; //take booleans for each character of the string to mark it checked
    StringBuilder sb = new StringBuilder(checked.length); // stringbuiler object of length checked

    for (int i = 0; i < str.length(); i++)
    {
        char ch = str.charAt(i);    //extract character one by one
        if (!checked[ch])           // check if checked[] is true ie it is already traversed
        {
            checked[ch] = true;    // check char for first time only
            sb.append(ch);         // put checked characters in string builder object
        }
    }

    return sb.toString();
}


}

class homework5Driver
{
public static void main (String[] args)
{
  System.out.println(\"Part 1: \");
  String result;
  
  result=Homework5Methods.eliminateDuplicates(\"abracadabra\");
  System.out.println(result);
  result=Homework5Methods.eliminateDuplicates(\"What\'s a Seawolf? I\'m a Seawolf!\");
  System.out.println(result);
  result=Homework5Methods.eliminateDuplicates(\"Stony Brook University\");
  System.out.println(result);
  result=Homework5Methods.eliminateDuplicates(\"AaBbCcDd\");
  System.out.println(result);
  result=Homework5Methods.eliminateDuplicates(\"\");
  System.out.println(result);    //displays empty string
  
}
}

Output:

Success time: 0.04 memory: 711168 signal:0

 Part I: There Can Be Only One (5 points) Filename(s): Homework5Methods. java Write a method public static String eliminateDuplicates (String str) that returns
 Part I: There Can Be Only One (5 points) Filename(s): Homework5Methods. java Write a method public static String eliminateDuplicates (String str) that returns

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site