Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Until today we have used plain arrays to store arrays of numbers and Strings. La

ID: 3755795 • Letter: U

Question

Until today we have used plain arrays to store arrays of numbers and Strings. Lab#4 will review the syntax and semantics of plain arrays as well as the use of the Arrays library to sort and search plain arrays.

Lab#4 will also introduce you to the ArrayList class - a self resizing, self managing, object that contains a plain array but takes takes away your ability to directly index [] into the underlying plain array and instead gives you several methods to call that allow you to add and retrieve values into and from the underlying array. An ArrayList is thus just a plain array bundled together with a bunch of methods that allow you to add/get values to/from the array plus many other operations. This underlying array and methods are wrapped up in an object called an ArrayList. ArrayLists keep track of the count which you can retrieve cia a call to .size(). ArrayLists also enforce the array discipline for you keeping all values packed tight to the front.

Plain arrays and the Arrays library that contains sorting and searching methods to sort or search your plain array.

PlainArraySortSearch.java

Plain arrays have a library of search and sort methods, Arrays library

ArrayList: a smart, self-managing array which you will find very convenient to solve the jumbles problem.

ArrayListSortSearch.java

ArrayLists have a library of search and sort methods too: Collections library

Java programmers consider ArrayList superior to plain arrays for several reasons.

Avoid declaring and using ArrayList in a undisciplined manner. Always specify the <type> of object that will be stored in the list when you define it:

ToCharArray.java

Illustrates a method .toCharArray() built into every String.

See Java 9 API for String. This built in String method extracts the letters of the String and puts them into a plain array of char. This plain array of char is full - i.e. the array is just long enough to hold the exact number of letters from the String. There is no need for a count value. The .length of the array is the count of chars in the array.


Y O U R     L A B # 4    A S S I G N M E N T

Write a Java program that does the following. No start file given. (Remember to put throws Exception after main signature since you are opening a File )

Your output must look EXACTLY like this below:

Here is your input file: jumbles.txt

Expect the user must to put a filename on the comamnd line like this: C:>java Lab4 jumbles.txt

Use that args[0] value as the name of the input file and open it with a BufferedReader

Load eveyy line/word of that input file (one word per line) into an ArrayList of String

Sort that ArrayList of words

With every word of the ArrayList output a single line of output containing the word, a space, the canonical form of the word

CORRECT FORMS OF DECLARATION UNDISCIPLINED FORMS OF DECLARATION ArrayList<String> words;
ArrayList<String> words = null;
ArrayList<String> words =new ArrayList<String>(); ArrayList words;
ArrayList words = null;
ArrayList words =new ArrayList(); 1 import java.util.*; import java . 10 . * ; 3 4 public class PlainArraySortSearch public static void main (String [1 args) throws Exception Random rand - new Random( 17 ; // any number will do to make program produce same numbers each execution intll plainArr - new intl 15 ]; int count; 10 12 13 14 15 16 17 18 19 20 21 for ( count-0 count

Explanation / Answer

Please fine below the code:

//save the below code as Lab4.java

import java.io.BufferedReader;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Scanner;

class Lab4 {

/**

* method to return canonical form of word

* @param word

* @return

*/

public static String canonical(String word) {

char[] letters = word.toCharArray();

Arrays.sort(letters);

return new String(letters);

}

public static void main(String[] args) throws IOException {

Scanner sc = new Scanner(System.in);

// array list type of string for adding words

ArrayList<String> words = new ArrayList<>();

String filename = "";

// if file name passed via command line args then

// i will not ask on console

if (args.length > 0) {

filename = args[0];

} else {

System.out.print("Here is your input file : ");

filename = sc.next();

}

// using bufferreader opening the file

BufferedReader reader = new BufferedReader(new FileReader(filename));

String line;

// reading line by line and adding to arraylist

while ((line = reader.readLine()) != null) {

words.add(line);

}

System.out.println();

//printing each word in canonical way

// word |space| canonical

for (String s : words)

System.out.println(s + " " + canonical(s));

// closing the resources

reader.close();

sc.close();

}

}

//i have tested a below list of words please feel free to add yours

//save the below text file as jumbles.txt

swarm
soul
work
voot
fruit
tuple

//COnsole output

javac Lab4.java

java Lab4
Here is your input file : jumbles.txt

swarm amrsw
soul losu
work korw
voot ootv
fruit firtu
tuple elptu

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote