Questions
Purpose Purpose is to implement some single linked list methods. Add methods to the List class...

Purpose

Purpose is to implement some single linked list methods.

Add methods to the List class

In the ‘Implementation of linked lists’ lecture, review the ‘Dynamic implementation of single linked list’ section. You will be adding new methods to the List class.

Eight new methods are required:

  1. new constructor – creates a new single linked list from an array of integers

  • e.g.
        int a[] = {1, 2, 3, 4};
        List list = new List(a);
  1. toString() – returns a string representing a list, nicely formatted

  • e.g.
        System.out.println(list.toString());
  • gives a string of comma-separated values, with no comma after the last value. g. for the list above, output would be:
        1, 2, 3, 4
  • hint: use the StringBuilder library class for efficiency, as you build the string from the list
  • returns the null string “” for an empty list
  1. findLast() – returns a reference to the last item in a list, or null if the list is empty

  • e.g.
        Item r = list.findLast();
  1. insertLast() – creates a new item and inserts it at the end of a list

  • e.g.
        list.insertLast(5);
  • must work with an empty list
  1. removeLast() – removes the last item from a list and returns its info

  • e.g.
        int x = list.removeLast();
  • report an error and exit if the list is empty
  • hint: you can use two references:
  • lead – advance this first. Start at the beginning of the list
  • lag – follows one item behind lead
  • stop when lead references the last item in the list, lag references the item before this one
  • be careful to handle the special case of only one item in the list
  1. copy() – makes a copy of a list and returns a reference to the new list

  • e.g.
        List result = list.copy();
  • must not change the list being copied
  1. join() – creates a new list that is the result of joining one list to the end of another

  • e.g.
        List result = list1.join(list2);
  • here, the new result list is the result of joining list2 to the end of list1
  • must not change list1 or list2
  1. intersect() – creates a new list that is the intersection of one list with another

  • e.g.
        List result = list1.intersect(list2);
  • here, the new result list is the result of finding every item in list1 that occurs in list2
  • hint: assume for simplicity that each list does not contain duplicate items
  • must not change list1 or list2

Many classes are given to you

You must write this code in the List class, that implements the single linked list. Other than List, the code provided must not be altered in any way.

Hints

No Java library classes other than StringBuilder are allowed. However you are encouraged to use the List methods as you write your new code, just like in real life.

Your methods should manipulate lists directly. For example, do not convert list to string, use string library methods, then convert string back to list(!)

Run the program to test your completed List class. When correct, save the output as an output.txt text file

Required

Your program must be correct, simple and clear:

  • clear, consistent layout and indentation
  • you must Javadoc comment every class, method and anything that is not simple and clear.
  • clear, meaningful variable and method names

Code Given:

=================================================================================

Tester

public class Tester
{
public static void main()
{
int a[] = {1, 2, 3, 4};
List list1 = new List(a);

int b[] = {6, 5, 4, 3};
List list2 = new List(b);

list1.insertLast(5);
System.out.println("insertLast() gives: " + list1.toString());

list2.removeLast();
System.out.println("removeLast() gives: " + list2.toString());

List result = list2.copy();
System.out.println("copy() gives: " + result.toString());

result = result.join(list2);
System.out.println("join() gives: " + result.toString());

result = list1.intersect(list2);
System.out.println("intersect() gives: " + result.toString());
}
}

=========================================================================================

List

public class List
{
private Item list;

public List()
{
list = null;
}

public void insertFirst(int i)
{
Item r = new Item(i);
r.next = list;
list = r;
}

public int removeFirst()
{
if (isEmpty()) {
System.out.println("Error in removeFirst(): list is empty");
System.err.println("Error in removeFirst(): list is empty");
System.exit(1);
}
Item r = list;
int x = r.info;
list = r.next;
return x;
}

public Boolean isEmpty()
{
return list == null;
}

public int count()
{
int count = 0;
Item r = list;
while (r != null) {
++count;
r = r.next;
}
return count;
}

public void insertAfter(Item p, int i)
{
if (isEmpty() || p == null) {
System.out.println("Error in insertAfter(): list is empty or p not set");
System.err.println("Error in insertAfter(): list is empty or p not set");
System.exit(2);
}
Item r = new Item(i);
r.next = p.next;
p.next = r;
}

public int deleteAfter(Item p)
{
if (p.next == null || p == null) {
System.out.println("Error in deleteAfter(): p not set or is last item");
System.err.println("Error in deleteAfter(): p not set or is last item");
System.exit(3);
}
Item r = p.next;
int x = r.info;
p.next = r.next;
return x;
}

//returns reference to first occurrence of i in list
//returns null if not found
public Item find(int i)
{
if (isEmpty()) {
System.out.println("Error in find(): list is empty");
System.err.println("Error in find(): list is empty");
System.exit(4);
}
Item r = list;
while (r != null && r.info != i)
r = r.next;
return r;
}
}

===========================================================================

Item

public class Item
{
protected int info;
protected Item next;

public Item()
{
info = 0;
next = null;
}

public Item(int i)
{
info = i;
next = null;
}
}

===========================================================================

Also Note that NO JAVA CLASSES OTHER THAN STRINGBUILDER ARE ALLOWED. But, List Methods are allowed.

In: Computer Science

A new bridge is to be constructed over the East River in New York City. Two...

A new bridge is to be constructed over the East River in New York City. Two structural possibilities exist: the support could be in the shape of a parabola or the support could be in the shape of a semi-ellipse.

The space between the supports needs to be 1050 feet.

The height at the center of the arch needs to be 350 feet. Determine the equation of a parabola with these characteristics. (Hint: Place the vertex of the parabola at the origin to simplify calculations).

Part 1

a. What is your equation for the parabola? An empty tanker, 520 feet wide and 260 feet high needs to pass beneath the bridge.

b. Can the tanker pass under this bridge? Justify your answer. Determine the equation of a semi-ellipse with the same characteristics. (Hint: Place the center of the semi-ellipse at the origin to simplify calculations)

Part 2

a. What is the equation for the semi-ellipse? Show the work required to determine the equation.

b. Will the tanker (520 feet wide and 260 feet high) be able to pass under this bridge? Justify your answer.

Part 3

a. If the river were to flood and rise 10 feet, how would the channel widths and the clearances of the two bridges be affected? Explain, using numbers, how the width of the channel would be affected for each bridge.

b. With all things considered, which bridge design would you choose? Explain your reasoning.

In: Civil Engineering

Change the program to modify the output file by making each sentence a new paragraph (inserting...

Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE.

How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be appreciated.

This is the original code that makes it make a new line after every carriage return. I need it to make a new line after every sentence.

Here is the input text file :  

Today we live in an era where information is processed
almost at the speed of light. Through computers, the
technological revolution is drastically changing the way we
live and communicate with one another. Terms such as
“the Internet,” which was unfamiliar just a few years ago, are
very common today. With the help of computers you can send
letters to, and receive letters from, loved ones within
seconds. You no longer need to send a résumé by mail to apply
for a job; in many cases you can simply submit your job
application via the Internet. You can watch how stocks perform
in real time, and instantly buy and sell them. Students
regularly “surf” the Internet and use computers to design
their classroom projects. They also use powerful word
processing software to complete their term papers. Many
people maintain and balance their checkbooks on computers.

//*************************************************************
// Author: D.S. Malik
//
// Program: Line and Letter Count
// This program reads a text, outputs the text as is, and also
// prints the number of lines and the number of times each
// letter appears in the text. An uppercase letter and a
// lowercase letter are treated as being the same; that is,
// they are tallied together.
//*************************************************************

#include
#include
#include

using namespace std;

void initialize(int& lc, int list[]);
void characterCount(char ch, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;

infile.open("textin.txt"); //Step 2

if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}

outfile.open("textout.out"); //Step 4

initialize(lineCount, letterCount); //Step 5

infile.get(ch); //Step 6

while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}

writeTotal(outfile, lineCount, letterCount); //Step 8

infile.close(); //Step 9
outfile.close(); //Step 9
  
return 0;
}

void initialize(int& lc, int list[])
{
int j;
lc = 0;

for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize

void characterCount(char ch, int list[])
{
int index;

ch = toupper(ch); //Step a

index = static_cast(ch)
- static_cast('A'); //Step b

if (0 <= index && index < 26) //Step c
list[index]++;
} //end characterCount

void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText

void writeTotal(ofstream& outtext, int lc, int list[])
{
int index;

outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;

for (index = 0; index < 26; index++)
outtext << static_cast(index + static_cast('A'))
<< " count = " << list[index] << endl;
} //end writeTotal

This is the code I've got so far to make it ask for what the output file should be named.

//*************************************************************
// Author: D.S. Malik
//
// Program: Line and Letter Count
// This program reads a text, outputs the text as is, and also
// prints the number of lines and the number of times each
// letter appears in the text. An uppercase letter and a
// lowercase letter are treated as being the same; that is,
// they are tallied together.
//*************************************************************

#include
#include
#include

using namespace std;

void initialize(int& lc, int list[]);
void characterCount(char ch, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;
string fileName;
  
infile.open("textin.txt"); //Step 2

if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}

cout << "Please enter the name of the output file: "; <----- This is the new code
getline(cin, fileName); <----- This is the new code

outfile.open(fileName.c_str()); //Step 4 <----- This is the new code

initialize(lineCount, letterCount); //Step 5

infile.get(ch); //Step 6

while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}

writeTotal(outfile, lineCount, letterCount); //Step 8

infile.close(); //Step 9
outfile.close(); //Step 9
  
return 0;
}

void initialize(int& lc, int list[])
{
int j;
lc = 0;

for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize

void characterCount(char ch, int list[])
{
int index;

ch = toupper(ch); //Step a

index = static_cast(ch)
- static_cast('A'); //Step b

if (0 <= index && index < 26) //Step c
list[index]++;
} //end characterCount

void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText

void writeTotal(ofstream& outtext, int lc, int list[])
{
int index;

outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;

for (index = 0; index < 26; index++)
outtext << static_cast(index + static_cast('A'))
<< " count = " << list[index] << endl;
} //end writeTotal

In: Computer Science

Change the program to modify the output file by making each sentence a new paragraph (inserting...

Change the program to modify the output file by making each sentence a new paragraph (inserting two carriage returns between every sentence. :) Don't over-think this, but you must have worked through and understand how the program works now in order to modify it. Remember, you want the carriage returns between every SENTENCE, not every LINE.

How would one do this? I'm not to sure how to make it make a new line after a sentence. Any help will be appreciated.

This is the original code that makes it make a new line after every carriage return. I need it to make a new line after every sentence.

Here is the input text file :  

Today we live in an era where information is processed
almost at the speed of light. Through computers, the
technological revolution is drastically changing the way we
live and communicate with one another. Terms such as
“the Internet,” which was unfamiliar just a few years ago, are
very common today. With the help of computers you can send
letters to, and receive letters from, loved ones within
seconds. You no longer need to send a résumé by mail to apply
for a job; in many cases you can simply submit your job
application via the Internet. You can watch how stocks perform
in real time, and instantly buy and sell them. Students
regularly “surf” the Internet and use computers to design
their classroom projects. They also use powerful word
processing software to complete their term papers. Many
people maintain and balance their checkbooks on computers.

Sorry the last question i posted looked very ugly!!

//*************************************************************
// Author: D.S. Malik
//
// Program: Line and Letter Count
// This program reads a text, outputs the text as is, and also
// prints the number of lines and the number of times each
// letter appears in the text. An uppercase letter and a
// lowercase letter are treated as being the same; that is,
// they are tallied together.
//*************************************************************

#include
#include
#include

using namespace std;

void initialize(int& lc, int list[]);
void characterCount(char ch, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;

infile.open("textin.txt"); //Step 2

if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}

outfile.open("textout.out"); //Step 4

initialize(lineCount, letterCount); //Step 5

infile.get(ch); //Step 6

while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}

writeTotal(outfile, lineCount, letterCount); //Step 8

infile.close(); //Step 9
outfile.close(); //Step 9
  
return 0;
}

void initialize(int& lc, int list[])
{
int j;
lc = 0;

for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize

void characterCount(char ch, int list[])
{
int index;

ch = toupper(ch); //Step a

index = static_cast(ch)
- static_cast('A'); //Step b

if (0 <= index && index < 26) //Step c
list[index]++;
} //end characterCount

void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText

void writeTotal(ofstream& outtext, int lc, int list[])
{
int index;

outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;

for (index = 0; index < 26; index++)
outtext << static_cast(index + static_cast('A'))
<< " count = " << list[index] << endl;
} //end writeTotal

This is the code I've got so far to make it ask for what the output file should be named.

//*************************************************************
// Author: D.S. Malik
//
// Program: Line and Letter Count
// This program reads a text, outputs the text as is, and also
// prints the number of lines and the number of times each
// letter appears in the text. An uppercase letter and a
// lowercase letter are treated as being the same; that is,
// they are tallied together.
//*************************************************************

#include
#include
#include

using namespace std;

void initialize(int& lc, int list[]);
void characterCount(char ch, int list[]);
void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[]);
void writeTotal(ofstream& outtext, int lc, int list[]);

int main()
{
//Step 1; Declare variables
int lineCount;
int letterCount[26];
char ch;
ifstream infile;
ofstream outfile;
string fileName;
  
infile.open("textin.txt"); //Step 2

if (!infile) //Step 3
{
cout << "Cannot open the input file."
<< endl;
return 1;
}

cout << "Please enter the name of the output file: "; <----- This is the new code
getline(cin, fileName); <----- This is the new code

outfile.open(fileName.c_str()); //Step 4 <----- This is the new code

initialize(lineCount, letterCount); //Step 5

infile.get(ch); //Step 6

while (infile) //Step 7
{
copyText(infile, outfile, ch, letterCount); //Step 7.1
lineCount++; //Step 7.2
infile.get(ch); //Step 7.3
}

writeTotal(outfile, lineCount, letterCount); //Step 8

infile.close(); //Step 9
outfile.close(); //Step 9
  
return 0;
}

void initialize(int& lc, int list[])
{
int j;
lc = 0;

for (j = 0; j < 26; j++)
list[j] = 0;
} //end initialize

void characterCount(char ch, int list[])
{
int index;

ch = toupper(ch); //Step a

index = static_cast(ch)
- static_cast('A'); //Step b

if (0 <= index && index < 26) //Step c
list[index]++;
} //end characterCount

void copyText(ifstream& intext, ofstream& outtext, char& ch,
int list[])
{
while (ch != '\n') //process the entire line
{
outtext << ch; //output the character
characterCount(ch, list); //call the function
//character count
intext.get(ch); //read the next character
}
outtext << ch; //output the newline character
} //end copyText

void writeTotal(ofstream& outtext, int lc, int list[])
{
int index;

outtext << endl << endl;
outtext << "The number of lines = " << lc << endl;

for (index = 0; index < 26; index++)
outtext << static_cast(index + static_cast('A'))
<< " count = " << list[index] << endl;
} //end writeTotal

In: Computer Science

A survey of college students reported that they spend an average of $9.50 a day on...

A survey of college students reported that they spend an average of $9.50 a day on dinner with a standard deviation of $3. What is the probability that 100 randomly selected college students will spend less than $10.00 on average for dinner? Round your answer to 4 decimal places.

In: Statistics and Probability

The amount of study time a student gets in a week has a mean of 7.25...

The amount of study time a student gets in a week has a mean of 7.25 hours with a standard deviation of 48 minutes. If a sample of 68 students is randomly selected, what is the probability the average amount of study time in a week for these students is less than 7 hours?

In: Statistics and Probability

The grades of 5 randomly selected students in Engineering Statistics are shown below. Construct a 95%...

The grades of 5 randomly selected students in Engineering Statistics are shown below. Construct a 95% two-sided confidence interval for the true mean of student grades?

Students

1

2

3

4

5

Grades

95

85

65

75

80

In: Statistics and Probability

Fifteen out of the 145 randomly selected study-abroad students who were surveyed in a state can...

Fifteen out of the 145 randomly selected study-abroad students who were surveyed in a state can speak the French language. Construct and interpret a 98% confidence interval for the proportion of all study-abroad students in the state who can speak French.

In: Statistics and Probability

Write in detail about the effects of SAD on international students in Canada. Google and read...

Write in detail about the effects of SAD on international students in Canada. Google and read articles on the following topics and use them in your essay: -Effects of SAD on mental health in students, Effects of SAD on physical health, Effects on work and studies and Effects on relationships.

In: Operations Management

The table below shows the number of students absent on the particular day of the week:...

The table below shows the number of students absent on the particular day of the week:

Day M Tu W Th F
Number 111 80 85 94 99

Test if the distribution of students absent is uniform through the week with the significance level of 5%.

In: Statistics and Probability