The Artima Developer Community
Sponsored Link

Java Answers Forum
urgent excercise help -please :)

3 replies on 1 page. Most recent reply: Nov 24, 2002 9:40 AM by Julia

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
Julia

Posts: 6
Nickname: julia
Registered: Nov, 2002

urgent excercise help -please :) Posted: Nov 24, 2002 3:50 AM
Reply to this message Reply
Advertisement
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

Enter name of author:

The books in publication order are:

1. G?nter Grass: "The Tin Drum", 1959. 804 pages.
2. Jos? Saramago: "Blindness", 1996. 328 pages.
3. Gao Xingjian: "Soul Mountain", 1997. 528 pages.
4. V.S. Naipaul: "Half a Life", 2001. 211 pages.

------------------------------------------------------

/** 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();
}

}


Julia

Posts: 6
Nickname: julia
Registered: Nov, 2002

Re: urgent excercise help -please :) Posted: Nov 24, 2002 4:59 AM
Reply to this message Reply
the problem is perhaps in the do while -loop.
It should stop when I enter empty line after
nameOfAuthor = reader.readString("Enter name of author: ");

but it goes on..

Is there other ways to write that it should stops when I press enter.
while(nameOfAuthor.equals("")) ?

-Julia

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: urgent excercise help -please :) Posted: Nov 24, 2002 7:06 AM
Reply to this message Reply

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;
}

Julia

Posts: 6
Nickname: julia
Registered: Nov, 2002

Re: urgent excercise help -please :) Posted: Nov 24, 2002 9:40 AM
Reply to this message Reply
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.)

http://www.hut.fi/~t106213/english/exercises/unit_5/exercise_4/documents/

Tips needed to the methods of the class TVScheduleReader.
anything? :)


-----------------------------------------------------------
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. 
*/
 
 
  public class 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)) {
    throw new 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
*/
  public void 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
*/
 
  public boolean isFinished() throws IOException {
  }
 
 
 
 
 
/** ----------------------------------------------------
Closes the input stream used by the reader.
Throws:
IOException - if problems arose when handling the file */
 
  public void close() throws IOException { 
    characterStream.close();
 
  } 
 
 
} 
 
 

Flat View: This topic has 3 replies on 1 page
Topic: JDBC Exception: Row update failed Previous Topic   Next Topic Topic: a little help needed.

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use