Question

* _Example commands for running this file_ * Compilation: javac Assignment1.java * Execution: java Assignment1 <...

* _Example commands for running this file_
* Compilation: javac Assignment1.java
* Execution: java Assignment1 < input.txt
*
* Reads in a text file and for each line verifies whether the word has
* unique characters.
*

* % cat input.txt
* Hello
* World
*
* % java Assignment1 < input.txt
* False
* True

*
******************************************************************************/

import java.util.*;

public class SortInput {

/* a function for checking uniqueness of characters in a word */
private static boolean isUniqueChar(String s){
// Fill this part out
System.out.println(true);
return true;
}
  
/* a function for sorting an input word */
private static String sortWord(String s){
char[] c = s.toCharArray();
insertionSort(c);
return "";
}

/* insertion sort algorithm. It should return a string type */
public static void insertionSort(char[] word) {
if (word == null || word.length == 0)
return; // empty array have nothing to sort

for (int i=0; i < word.length; i++) {
char temp = word[i];
int j = i;
while (j > 0 && word[j-1] > temp) {
word[j] = word[j-1];
j--;
}
word[j] = temp;
}
return;
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

// read in words and determine whether it is composed of unique characters
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
  
isUniqueChar(s);
sortWord(s);
}

scanner.close();
}
}   

THE OUTPUT SHOULD LOOK LIKE THIS:

You can reference the Java Docs or anything on the Internet.

Instructions for usage by command-line can be found in the Example.java.

You may use input.txt as a test case. The contents of the input.txt file:

hello
world
Brooklyn
Queens

The output for that input file should look like:

false   ehllo
true    dlorw
false   bklnoory
false   ceegllo

Format

In each line you have the true/false value followed by a tab space, followed by the word in sorted order (lowercase).

Homework Answers

Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

public class SortInput {

/* a function for checking uniqueness of characters in a word */
private static boolean isUniqueChar(String s){
// Fill this part out
s = sortWord(s);
for(int i=0;i<s.length()-1;i++)
   {
       if(s.charAt(i)==s.charAt(i+1)) {
           return false;
       }
   }
return true;
}
  
/* a function for sorting an input word */
private static String sortWord(String s){
char[] c = s.toCharArray();
return insertionSort(c);
}

/* insertion sort algorithm. It should return a string type */
public static String insertionSort(char[] word) {
if (word == null || word.length == 0)
return "";// empty array have nothing to sort

for (int i=0; i < word.length; i++) {
char temp = word[i];
int j = i;
while (j > 0 && word[j-1] > temp) {
word[j] = word[j-1];
j--;
}
word[j] = temp;
}
String result = new String(word);
return result.toLowerCase();
}

public static void main(String[] args) {
File file = new File("input.txt");
Scanner scanner = null;
try {
   scanner = new Scanner(file);
} catch (FileNotFoundException e) {
   System.out.println("FileNotFound: Check with you input file");
   e.printStackTrace();
}
// read in words and determine whether it is composed of unique characters
while (scanner.hasNextLine()) {
String s = scanner.nextLine();
System.out.print(isUniqueChar(s));
System.out.println("\t"+sortWord(s));
}

scanner.close();
}
}   

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Hello, I am trying to create a Java program that reads a .txt file and outputs...
Hello, I am trying to create a Java program that reads a .txt file and outputs how many times the word "and" is used. I attempted modifying a code I had previously used for counting the total number of tokens, but now it is saying there is an input mismatch. Please help! My code is below: import java.util.*; import java.io.*; public class Hamlet2 { public static void main(String[] args) throws FileNotFoundException { File file = new File("hamlet.txt"); Scanner fileRead =...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The...
THIS IS FOR JAVA I have to write a method for a game of Hangman. The word the user is trying to guess is made up of hashtags like so " ###### " If the user guesses a letter correctly then that letter is revealed on the hashtags like so "##e##e##" If the user guesses incorrectly then it increments an int variable named count " ++count; " String guessWord(String guess,String word, String pound) In this method, you compare the character(letter)...
Here is my java code, I keep getting this error and I do not know how...
Here is my java code, I keep getting this error and I do not know how to fix it: PigLatin.java:3: error: class Main is public, should be declared in a file named Main.java public class Main { ^ import java.io.*; public class Main { private static BufferedReader buf = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { String english = getString(); String translated = translate(english); System.out.println(translated); } private static String translate(String s) { String latin =...
public class PalindromeChecker { /** * Method that checks if a phrase or word is *...
public class PalindromeChecker { /** * Method that checks if a phrase or word is * a Palindrome * * @param str * Represents a string input * * @return true * True if the string is a Palindrome, * false otherwise */ public static boolean isPalindrome(String str) { if (str == null) { return false; } str = str.toLowerCase(); String temp = ""; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ((ch...
Using JAVA For this assignment, you will analyze code that uses a file input stream and...
Using JAVA For this assignment, you will analyze code that uses a file input stream and a file output stream. Read through the linked Java™ code. In a Microsoft® Word document, answer the following questions: Could this program be run as is? If not, what is it lacking? Does this program modify the contents of an input stream? In what way? What are the results of running this code? ********************************************** CODE TO ANALYZE  ******************************************************** /********************************************************************** *   Program:   Datasort *   Purpose:   ...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1...
JAVA -Consider this program: public class Main { public static void main(String[] args) { String s1 = new String("hello"); String s2 = "hello"; String s3 = "hello";    System.out.println(s1 == s3); System.out.println(s1.equals(s3)); System.out.println(s2 == s3); } } When we run the program, the output is: false true true Explain why this is the output, using words and/or pictures.
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class...
1) Consider the following Java program, which one of the following best describes "setFlavor"? public class Food {     static int count;     private String flavor = "sweet";     Food() { count++; }     void setFlavor(String s) { flavor = s; }     String getFlavor() { return flavor; }     static public void main(String[] args) {         Food pepper = new Food();         System.out.println(pepper.getFlavor());     } } a. a class variable b. a constructor c. a local object variable d....
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems...
8.15 *zyLab: Method Library (Required & Human Graded) This code works but there are some problems that need to be corrected. Your task is to complete it to course style and documentation standards CS 200 Style Guide. This project will be human graded. This class contains a set of methods. The main method contains some examples of using the methods. Figure out what each method does and style and document it appropriately. The display method is done for you and...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
[Java] I'm not sure how to implement the code. Please check my code at the bottom....
[Java] I'm not sure how to implement the code. Please check my code at the bottom. In this problem you will write several static methods to work with arrays and ArrayLists. Remember that a static method does not work on the instance variables of the class. All the data needed is provided in the parameters. Call the class Util. Notice how the methods are invoked in UtilTester. public static int min(int[] array) gets the minimum value in the array public...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT