Question

In: Computer Science

simple Java project// All variables have descriptive names or comments describing their purpose. Thank you Read...

simple Java project//  All variables have descriptive names or comments describing their purpose. Thank you

Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. 

The input file Each 
line of the input file will contain a sentence with words separated by one space. Read a line from the file and use a StringTokenizer to extract the words from the line. An example of the input file would be: 
Mary had a little lamb
whose fl33ce was white as sn0w
And everywhere that @Mary went
the 1amb was sure to go. 

You should have two files to submit for this project: 
Project1.java 
WordGUI.java

Solutions

Expert Solution

SOLUTION:

OUTPUT:

input.txt

Mary had a little lamb
whose fl33ce was white as sn0w
And everywhere that @Mary went
the 1amb was sure to go.
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Project.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.StringTokenizer;

class Project1 {

public static void main(String args[]) {
File file = null;
FileReader fr = null;
BufferedReader br = null;
String line;
StringTokenizer st = null;

int numberOfWordsInFile;

String words[];
int index;

String validWords[];
int validWordsIndex;

String invalidWords[];
int invalidWordsIndex;
try {
file = new File("input.txt");
fr = new FileReader(file);
br = new BufferedReader(fr);
numberOfWordsInFile = 22;
words = new String[numberOfWordsInFile];
index = 0;
while ((line = br.readLine()) != null) {
st = new StringTokenizer(line);
while (st.hasMoreElements()) {
words[index] = (String) st.nextElement();
index++;
}
}
fr.close();
validWords = new String[numberOfWordsInFile];
validWordsIndex = 0;
invalidWords = new String[numberOfWordsInFile];
invalidWordsIndex = 0;
for (int i = 0; i < words.length; i++) if (
(!words[i].equals("")) &&
(words[i] != null) &&
(words[i].matches("^[a-zA-Z]*$"))
) {
validWords[validWordsIndex] = words[i];
validWordsIndex++;
} else {
invalidWords[invalidWordsIndex] = words[i];
invalidWordsIndex++;
}
selectionSort(validWords, validWordsIndex);
WordGUI wg = new WordGUI();
wg.display(
words,
index,
validWords,
validWordsIndex,
invalidWords,
invalidWordsIndex
);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

static String[] selectionSort(String[] validWords, int validWordsIndex) {
for (int i = 0; i < validWordsIndex - 1; i++) {
int min_index = i;
String minStr = validWords[i];
for (int j = i + 1; j < validWordsIndex; j++) {
if (validWords[j].compareTo(minStr) < 0) {
minStr = validWords[j];
min_index = j;
}
}
if (min_index != i) {
String temp = validWords[min_index];
validWords[min_index] = validWords[i];
validWords[i] = temp;
}
}
return validWords;
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

WordGUI.java

import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;

class WordGUI {

public void display(
String[] words,
int index,
String[] validWords,
int validWordsIndex,
String[] invalidWords,
int invalidWordsIndex
) {
JFrame f = new JFrame();
f.setLayout(new GridLayout(1, 3));

String label = "";

label = convertArrayToLabel(words, index);
f.add(new JLabel(label));

label = convertArrayToLabel(validWords, validWordsIndex);
f.add(new JLabel(label));

label = convertArrayToLabel(invalidWords, invalidWordsIndex);
f.add(new JLabel(label));

f.setSize(1000, 1000);
f.setVisible(true);
}

public String convertArrayToLabel(String[] array, int maxIndex) {
String label = "<html>";
for (int i = 0; i < maxIndex; i++) {
label = label + array[i] + "<br/>";
}
label = label + "</html>";
return label;
}
}


PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU


Related Solutions

simple Java project// please explain every statement with reasoning. Thank you Read from a file that...
simple Java project// please explain every statement with reasoning. Thank you Read from a file that contains a paragraph of words. Put all the words in an array, put the valid words (words that have only letters) in a second array, and put the invalid words in a third array. Sort the array of valid words using Selection Sort. Create a GUI to display the arrays using a GridLayout with one row and three columns. The input file Each line...
Below is my c program code you have to change all the functions names variables name...
Below is my c program code you have to change all the functions names variables name , every thing so it wont look same but runs the same and make sure you run the program and share the output so i get to know that the program is running thank you. #define _CRT_SECURE_NO_DEPRECATE #include #include #include #include #include struct patient { int id; int age; bool annualClaim; int plan; char name[30]; char contactNum[15]; char address[50]; }; #define MAX_LENGTH 500 struct...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you...
JAVA CODE, BEGINERS; Please use comments to explain For all of the following words, if you move the first letter to the end of the word, and then spell the result backwards, you will get the original word: banana dresser grammar potato revive uneven assess Write a program that reads a word and determines whether it has this property. Continue reading and testing words until you encounter the word quit. Treat uppercase letters as lowercase letters.
//JAVA //basic please for learning programming 2. Thank you. Write the Class Variables Class Box is...
//JAVA //basic please for learning programming 2. Thank you. Write the Class Variables Class Box is to have the following private data members: height of type double width of type double length of type double Write the following Overridden Method Class Box is to have an overridden equals() method that does the following:​​​​ tests to see if the parameter represents an object (null test) tests to see if the parameter object is of the same class type as the calling...
Programmed In Java In this assignment you will create a simple game. You will put all...
Programmed In Java In this assignment you will create a simple game. You will put all of your code in a class called “Game”. You may put all of your code in the main method. An example of the “game” running is provided below. Y ou will start by welcoming the user. 1. You should print "Welcome! Your starting coordinates are (0, 0).” 2. On the next line, you will tell the user the acceptable list of commands. This should...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a...
**Please write in Java, in a very simple/beginner coding style/language - thank you!** Directions: Given a factorial n!. Find the sum of its digits, and the number of trailing zeros (ie: the number of zeros at the end of the factorial). Input: integer nn, a non-negative integer where n≤20n≤20 Output: integer xyxy, the concatenation of x and y, where x is the sum of the digits in n! and y is the number of the zeros in n!) Note, 0≤x,y0≤x,y....
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive...
JAVA make sure file name is FirstnameLastname_02_CS1Calculator. Thank you! Overview This program implements a simple, interactive calculator. Major topics writing a program from scratch multiple methods testing In this document program logic & program structure (class, methods, variables) input / output assumptions and limitations documentation & style suggested schedule cover letter discussion questions grading, submission, and due date additional notes challenges sample output test plan Program Logic The program prompts the user to select a type of math problem (addition,...
PLEASE SHOW ALL WORK. THANK YOU Holly is considering a new project. The project will require...
PLEASE SHOW ALL WORK. THANK YOU Holly is considering a new project. The project will require $500,000 for new fixed assets, $208,000 for additional inventory, and $36,000 for additional accounts receivable. Short-term debt is expected to increase by $165,000. The project has a 6-year life. The fixed assets will be depreciated straight-line to a zero book value over the life of the project. At the end of the project, the fixed assets can be sold for 20 percent of their...
Using java you have to create a simple program that will allow for the storage of...
Using java you have to create a simple program that will allow for the storage of requests for money. Each request will consist of a person's name and dollar amount (US dollars). The business rules are straight forward. Each name should be a first and last name. The first letter of each element in the name should be capitalized. The dollar amount should be between 1 and 1000 dollars and include cents (2 decimals). You should include validations, but it...
As a programmer in a java project, you have been asked by your project manager to...
As a programmer in a java project, you have been asked by your project manager to describe the most efficient way to store the following assigned numbers 10,20,30,1000,200,350 for an operation which involves on calculation such as sum and average.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT