CSCI 160 - Problem Solving, Programming and Computers
Spring, 2016
Assignment 5
{File I/O, Sorting }
Q1. [30 points] LineNumbers.java
Write a program that reads a file containing text. Read each line and send it to the output file, preceded by line numbers. If the input
file is:
Mary had a little lamb
Whose fleece was white as snow.
And everywhere that Mary went,
The lamb was sure to go!
then the program produces the output file containing:
/* 1 */ Mary had a little lamb
/* 2 */ Whose fleece was white as snow.
/* 3 */ And everywhere that Mary went,
/* 4 */ The lamb was sure to go!
The line numbers are enclosed in /* */ delimiters so that the program can be used for numbering Java source files.
Prompt the user for the input and output file names.
Q2. [30 points] ReverseLines.java
Write a program that reads each line in a file, reverses its lines, and writes them to another file. For example, if the file input.txt
contains the lines:
Mary had a little lamb
Its fleece was white as snow
And everywhere that Mary went
The lamb was sure to go.
and you run reverse input.txt output.txt, then output.txt contains
The lamb was sure to go.
And everywhere that Mary went
Its fleece was white as snow
Mary had a little lamb
Check following animator: http://www.cs.armstrong.edu/liang/animation/web/BubbleSort.html
Use following code template to get started:
public class ReverseLines
{
/**
This method reads a file line by line and returns the result in an arraylist.
@param filename the file to read
@return an array list with the lines in the file
*/
public static ArrayList<String> readLinesToArray(String filename)
{
//TODO
}
/**
Write all lines in array list to given file in reverse order.
@param filename the name of the file to write to
@param lines the lines to write to the file
*/
public static void writeInReverse(String filename, ArrayList<String> lines)
{
//TODO
}
public static void main(String[] args) // File names are passed as arguments
{
if (args.length < 2)
{
System.err.println("usage: java ReverseLines infile outfile");
}
else
{
ArrayList<String> linesOfFile = readLinesToArray(args[0]);
writeInReverse(args[1], linesOfFile);
}
}
}
Q3. [40 points] BubbleSort.java
Implement the bubble sort algorithm:
While the array is not sorted
For each adjacent pair of elements
If the pair is not sorted
Swap its elements.
Use following code template to get you started:
import java.util.Random;
import java.util.Arrays;
public class BubbleSorter
{
private static Random generator = new Random();
/**
Creates an array filled with random values.
@param length the length of the array
@param n the number of possible random values
@return an array filled with length numbers between 0 and n - 1
*/
public static int[] randomIntArray(int length, int n)
{
int[] a = new int[length];
for (int i = 0; i < a.length; i++)
{
a[i] = generator.nextInt(n);
}
return a;
}
/**
Swaps two entries of an array.
@param a the array
@param i the first position to swap
@param j the second position to swap
*/
public static void swap(int[] a, int i, int j)
{
//TODO: swap a[i] and a[j]
}
/**
Sort an integer array using the bubble sort algorithm.
@param arr array of integers to sort
*/
public static void sort(int[] arr)
{
//TODO: sort using bubble sort
}
public static void main(String[] args)
{
int[] a = randomIntArray(20, 100);
System.out.println(Arrays.toString(a));
BubbleSorter.sort(a);
System.out.println(Arrays.toString(a));
}
}
Note:
Marks will be assigned for proper comments and output. In addition to comments in your program, also include a file header
comment at the top of each program file that includes your name, assignment number, and a description of the program:
//**********************************************************
// AssignmentNo (Enter Assignment number): (Enter ClassName)
// Author(s): (Enter your full name here)
// Description: (Enter Description)
//*********************************************************
Online Submission Instructions:
Copy the folder <<yourname>>Asg5 (e.g. JohnDoeAsg5) containing answer files, into /usr/people/handin/CS160 before midnight of the
date indicated in class website.
© Copyright 2026 Paperzz