I have deadline tomorrow morning. And I dont know what to do :( I should do a sorting program. do-while loop is not working right, and there is something else wrong with run-method. May someone kind person help and save me! :)
---------------------------------------------------- Create a class Toolkit which contains an implementation for sorting lists. Also create a class SortingProgram, which provides a simple user interface for sorting book objects. Use the class Book provided with exercise. You may also wish to use the class KeyboardReader from the first exercise in this unit. The public interfaces of the classes are described in the attached Javadoc documentation. http://www.hut.fi/~t106213/english/exercises/unit_5/exercise_2/documents/ ------------------------------------------------------
Example Run When you have correctly solved the exercise, the output of your sorting program should look like this:
Please give some book data. Input an empty author name to finish.
Enter name of author: Gao Xingjian Enter book title: Soul Mountain Enter publication year: 1997 Enter number of pages: 528
Enter name of author: Jos? Saramago Enter book title: Blindness Enter publication year: 1996 Enter number of pages: 328
Enter name of author: G?nter Grass Enter book title: The Tin Drum Enter publication year: 1959 Enter number of pages: 804
Enter name of author: V.S. Naipaul Enter book title: Half a Life Enter publication year: 2001 Enter number of pages: 211
/** Class SortingProgram provides a simple, text-based user interface for testing the sorting method from class Toolkit using Book objects as list elements. */
public class SortingProgram extends Object {
private KeyboardReader reader = new KeyboardReader(); private String informationToString; private String nameOfAuthor; private int publicationYear; private int numberOfPages; private String nameOfBook; private Vector allBooks = new Vector();
public SortingProgram() { }
public void run() {
try { do {
nameOfAuthor = reader.readString("Enter name of author: "); nameOfBook = reader.readString("Enter name of the book: "); publicationYear = reader.readInt("Enter publication year: "); numberOfPages = reader.readInt("Enter number of pages: ");
Book informationOfBook = new Book (nameOfAuthor, nameOfBook, publicationYear, numberOfPages);
informationOfBook.toString(); allBooks.add(informationOfBook); } while (nameOfAuthor.length() > 0);
} catch (InvalidWorkParameters wrong) { }
Toolkit tlk = new Toolkit(); tlk.sort(allBooks);
for (Enumeration e = allBooks.elements() ; e.hasMoreElements(); ) { Book b = (Book) e.nextElement(); System.out.println(b.toString); }
}
/** The main method creates a new sorting program and runs it.Parameters: commandLineParameters - (not in use) */
public static void main(String[] komentoriviparametrit) {
SortingProgram sortingman = new SortingProgram(); sortingman.run(); }
boolean done = false; while (!done){ //......... all the lines of code to do .... nameOfAuthor = reader.readString("Enter name of author: "); /* last line in while loop */ done = nameOfAuthor.length() == 0; }
Thanks Charles Bell! :) ----------------------------------------------
I have problems also with another program (last excercise!). I should create a class TVScheduleFileReader which can be used to read TV schedules from text files.
(Also a class TVScheduleBrowser, which provides a simple user interface for using the file reader. I should use the classes TVSchedule, TVProgramInfo, and InvalidScheduleData provided with this exercise.
The public interfaces of the classes are described in the attached Javadoc documentation.)
import java.io.*;
/**
A TVScheduleReader is an object that provides its user
with an interface for reading TVSchedule data from a
text file. The reader reads schedule objects from
a file sequentially, starting from the beginning.
In the text files read by the reader, each "program broadcasting unit" (e.g. a day) is represented by
a number of lines. The first two lines contain the name
and broadcasting time of the program schedule, and the following lines contain the titles and durations of the individual TV programs,
in broadcasting order.
There may be an arbitrary number of programs in each broadcasting unit; the end of a unit is marked
in the file by a line consisting of three hyphenation marks.
*/
publicclass TVScheduleFileReader
extends Object {
static String SEPARATOR_LINE;
private Reader characterStream;
private BufferedReader bufferedStream;
private String filePath;
private TVSchedule reader;
/**---------------------------------------------------------
Creates a new reader to read the given file.
The reader starts at the beginning of the file.
Parameters:
filePath - the path to the file to be read
Throws:
IOException - if problems arose when handling the file
*/
public TVScheduleReader(String filepath)
throws IOException {
if(filepath.equals(null)) {
thrownew IOException("wrong");
}
Reader characterStream = new FileReader(filepath);
BufferedReader bufferedStream = new
BufferedReader(characterStream);
this.SEPARATOR_LINE = "---";
this.filepath = filepath;
}
/**---------------------------------------------
Reads the next TV schedule from the file.
Returns:
the next TV schedule from the file or null,
if there is no more unread data in the file
Throws:
InvalidScheduleData - if there is an error in the formatting of the file (e.g. the file ends abnormally
or the duration of a program is not a positive integer).
IOException - if problems arose when handling the file
*/
public TVSchedule read()
throws InvalidScheduleData, IOException {
String lineRead = bufferedStream.readLine();
while (lineRead != null) {
System.out.print(lineRead);
lineRead = bufferedStream.readLine();
}
// return something
}
/**-------------------------------------------------------
Returns the reader to the beginning of the file. That is, the next call of the method read will return the first schedule in the file.
Throws:
IOException - if problems arose when handling the file
*/
publicvoid reset() throws IOException {
String beginning = bufferedStream.readLine();
}
/**--------------------------------------------------------
Tells if the reader has already read all the data in the file.
Returns:
a boolean value indicating if all the data has been read (i.e. the rest of the file contains empty lines at most)
Throws:
IOException - if problems arose when handling the file
*/
publicboolean isFinished() throws IOException {
}
/** ----------------------------------------------------
Closes the input stream used by the reader.
Throws:
IOException - if problems arose when handling the file */
publicvoid close() throws IOException {
characterStream.close();
}
}