Question: Lex.java import java.io.*; import java.util.Scanner; class Lex { public static void main (String[] args) throws IOException { Scanner in = null; PrintWriter out = null;

![main (String[] args) throws IOException { Scanner in = null; PrintWriter out](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f95f38c8cbf_87266f95f3841b7e.jpg)
![= null; String[] token = null; int i, n =0; int lineNumber](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f95f3999a1f_87366f95f3923028.jpg)
Lex.java
import java.io.*;
import java.util.Scanner;
class Lex {
public static void main (String[] args) throws IOException {
Scanner in = null;
PrintWriter out = null;
String[] token = null;
int i, n =0;
int lineNumber =0;
int line = -1;
//makes sure two arguements are inputted
if(args.length != 2) {
System.err.println("Usage: FileIO infile outfile");
System.exit(1);
}
in = new Scanner(new File(args[0]));
out = new PrintWriter(new FileWriter(args[1]));
//counts number of lines
while(in.hasNextLine()) {
lineNumber++;
in.nextLine();
}
in.close();
in = null;
List list = new List();
token = new String[lineNumber];
in = new Scanner(new File(args[0]));
out = new PrintWriter(new FileWriter(args[1]));
//inputs values to token array
while(in.hasNextLine()){
token[++line] = in.nextLine();
}
list.append(0);
//based off of insertion sort
for(int j = 1; j
i = j - 1;
String tmp = token[j];
list.moveBack();
while(i >= 0 && tmp.compareTo(token[list.get()])
list.movePrev();
i--;
}
if(list.index() >= 0) list.insertAfter(j);
else list.prepend(j);
}
list.moveFront();
while(list.index() >= 0) {
out.println(token[list.get()]);
list.moveNext();
}
in.close();
out.close();
}
}
List.java
public class List{
private class Node{
//Fields
int data;
Node next;
Node prev;
//Constructors
Node(int data){
this.data = data;
next = null;
prev = null;
}
// Returns String representation of the data property.
public String toString() {
return String.valueOf(data);
}
// Returns true if two Nodes data properties are equal.
public boolean equals(Object x) {
boolean eq = false;
Node that;
if(x instanceof Node) {
that = (Node) x;
eq = (this.data == that.data);
}
return eq;
}
}
//Fields
private int length;
private int index = -1;
private Node front;
private Node back;
private Node cursor;
//Constructor
//creates a new empty list
List(){
front = back = cursor = null;
length = 0;
}
// Access functions
//returns the number of elements in this List
int length(){
return length;
}
//if cursor is defined, returns the index of the cursor element,
//otherwise returns -1.
int index(){
return index;
}
//Returns front element. pre:length()>0
int front(){
if(length
throw new RuntimeException("List Error: front() called on empty list!");
}
return front.data;
}
//Retrusn back element. Pre: length() >0
int back(){
if(length
throw new RuntimeException("List Error: back() called on empty list!");
}
return back.data;
}
//Returns cursor element. Pre: length()> 0, index >= 0
int get(){
if(index
throw new RuntimeException("List Error: get() called on empty list!");
}
return cursor.data;
}
//Returns true if this List and L are the same integer
//sequence. The cursor is ignored in both lists.
boolean equals(List L) {
if(L.length() != length) {
return false;
}
Node cfront = L.front;
Node tmp = front;
while(cfront.next != null && tmp.next != null) {
if(!cfront.equals(tmp))
return false;
cfront = cfront.next;
tmp = tmp.next;
}
return true;
}
//Manipulation procedures
//Resets this List to its original empty state
void clear(){
front = back = cursor = null;
length = 0;
index = -1;
}
//If List is non-empty, place the cursor under the front element,
//otherwise does nothing
void moveFront(){
if(length > 0) {
cursor = front;
index = 0;
}
}
void moveBack() {
if(length > 0) {
cursor = back;
index = length - 1;
}
}
// If cursor is defined and not at front, moves cursor one step
// toward front of this List, if cursor is defined and at front,
// cursor becomes undefined, if cursor is undefined does nothing.
void movePrev() {
if(cursor != null && index != 0) {
cursor = cursor.prev;
index--;
}
else if(cursor != null && index == 0) {
cursor = null;
index = -1;
}
}
// If cursor is defined and not at back, moves cursor one step
// toward back of this List, if cursor is defined and at back,
// cursor becomes undefined, if cursor is undefined does nothing.
void moveNext() {
if(cursor != null && index != length - 1) {
cursor = cursor.next;
index++;
}
else if(cursor != null && index == length - 1) {
cursor = null;
index = -1;
}
}
//Insert new element into this List. if List is non-empty,
//insertion takes place before front element
void prepend(int data){
Node temp = new Node(data);
if(front == null){
front = back = temp;
}
else{
front.prev = temp;
temp.next = front;
front = temp;
index++;
}
length++;
}
//insert new element into this List. If list is non-empty,
//insertion takes place after back element
void append(int data){
Node temp = new Node(data);
if(front == null){
front = back = temp;
}
else{
back.next = temp;
temp.prev = back;
back = temp;
}
length++;
}
// Inserts new element before cursor element in this List. Pre: length()>0, getIndex()>=0
void insertBefore(int data){
Node node = new Node(data);
if(length ==0){
throw new RuntimeException("List Error: insertBefore() called on empty list!");
}
else{
node.next = cursor;
if(cursor.prev != null){
node.prev = cursor.prev;
cursor.prev.next = node;
}
cursor.prev = node;
if (node.prev == null)
front = node;
}
index++;
length++;
}
// Inserts new element after cursor element in this
// List. Pre: length()>0, getIndex()>=0
void insertAfter(int data) {
if(length()
throw new RuntimeException("List error: insertAfter() called on empty List");
if(cursor == back) {
append(data);
}
else {
Node temp = new Node(data);
cursor.next.prev = temp;
temp.next = cursor.next;
temp.prev = cursor;
cursor.next = temp;
length++;
}
}
// Deletes the front element.
// Pre: length() > 0
void deleteFront() {
if(length
throw new RuntimeException("List Error: deleteFront() called on an empty List");
if(cursor == front) {
cursor = null;
index = -1;
}
front = front.next;
front.prev = null;
length--;
}
// Deletes the back element.
// Pre: length() > 0
void deleteBack() {
if(length
throw new RuntimeException("List Error: deleteBack() called on an empty List");
if(cursor == back) {
cursor = null;
index = -1;
}
back = back.prev;
back.next = null;
length--;
}
// Deletes cursor element, making cursor undefined.
// Pre: length() > 0, index() >= 0
void delete() {
if(index
throw new RuntimeException("List Error: delete() called with an undefined index on List");
if(length
throw new RuntimeException("List Error: delete() called on an empty List");
if(cursor == back)
deleteBack();
else if(cursor == front)
deleteFront();
else {
cursor.prev.next = cursor.next;
cursor.next.prev = cursor.prev;
cursor = null;
index = -1;
length--;
}
}
//Other methods
public String toString() {
String str = "";
for(Node temp = front; temp != null; temp = temp.next){
str+= temp.toString() + " ";
}
return str;
}
List copy(){
List L = new List();
Node N = this.front;
while( N!=null ){
L.append(N.data);
N = N.next;
}
return L;
}
}
The code I implemented is perfectly fine, but I got "incomplete name definition" and "premature EOF" when I compile lex with my test file
Our goal in this project is to build an Integer List ADT and use it to alphabetize the lines in a file. This ADT module will also be used (with some modifications) in future programming assignments, so you should test it thoroughly, even though not all of its features will be used here. Begin by reading the handout ADT.pdf posted on the class webpage for a thorough explanation of the programming practices and conventions required in this class for implementing ADTs in Java and C. Program Operation The main program for this project will be called Lex.java. Your List ADT module will be contained in a file called List.java, and will export its services to the client module Lex.java. Each file will define one top level class, List and Lex respectively. The required List operations are specified in detail below. Lex.java will take two command line arguments giving the names of an input file and an output file. The input can be any text file The output file will contain the same lines as the input arranged in lexicographic (i.e. alphabetical) order. For example Output file: one two three four four one three two Lex.java will follow the sketch given below 1. 2. 3. Create a List whose elements are the indices of the above String array. These indices should be arranged irn Check that there are two command line arguments. Quit with a usage message to stderr if more than or less than two strings are Count the number of lines n in the file named by args [0]. Create a String array of length n and read in the lines of the file as Strings, placing them into the array given on the command line. an order that effectively sorts the array. Using the above input file as an example we would have Indices: 0 Array: one two tree four five 1 List Our goal in this project is to build an Integer List ADT and use it to alphabetize the lines in a file. This ADT module will also be used (with some modifications) in future programming assignments, so you should test it thoroughly, even though not all of its features will be used here. Begin by reading the handout ADT.pdf posted on the class webpage for a thorough explanation of the programming practices and conventions required in this class for implementing ADTs in Java and C. Program Operation The main program for this project will be called Lex.java. Your List ADT module will be contained in a file called List.java, and will export its services to the client module Lex.java. Each file will define one top level class, List and Lex respectively. The required List operations are specified in detail below. Lex.java will take two command line arguments giving the names of an input file and an output file. The input can be any text file The output file will contain the same lines as the input arranged in lexicographic (i.e. alphabetical) order. For example Output file: one two three four four one three two Lex.java will follow the sketch given below 1. 2. 3. Create a List whose elements are the indices of the above String array. These indices should be arranged irn Check that there are two command line arguments. Quit with a usage message to stderr if more than or less than two strings are Count the number of lines n in the file named by args [0]. Create a String array of length n and read in the lines of the file as Strings, placing them into the array given on the command line. an order that effectively sorts the array. Using the above input file as an example we would have Indices: 0 Array: one two tree four five 1 List
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
