-
June 30th, 2007, 08:17 AM
#1
Problems in running client class
I have a class, Terminus. I also have the class TerminusTest to test run the compilation of the whole Bus program which includes the ADT Bus class, Terminus, TerminusTest and Time ADT.
But when i try to run the TerminusTest which is the main program(client class), I do not get a sorted output, and the number of inputs that appears is not the number of inputs I input. Let's say the number of inputs given is 20, it does not happen that way. There is also an input box that pops up asking for a number of places required. The number I enter does not coincide with what comes out either.
There is also a problem in the edit method in the Terminus class.Please help me correct this error.
Besides can u help me correct my client class(TerminusTest) in case it has any errors. This class should be able to allow the user to addBus, sort the records, edit , delete, filter the records. But I am not sure how to do this.
Below is the code for the Terminus class, and below that is TerminusTest.
Please help me debug these codes.
Terminus.java
/**
* Terminus is to accept input from the user, using vector to store the information gathered.
* It has methods like readFile() to output data inserted into the vector,
* the writeFile() method to write input into the vector
* addBus() method to add data about the bus
* sort() method to sort data required
* filter() methods to filter results wanted only
* edit() and delete() methods to edit data and delete data in the vector
* display() method to display the final output
* @author (your AIGINI)
* @version (a version number or a date)
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
import static javax.swing.JOptionPane.*;
public class Terminus
{
private Vector buses;
int choice = 0;
private int records;
public Terminus(){
buses = new Vector();
records = 0;
readFile();
}
/**method to get input from the user to add data into the vector
*@param b : bus number
* @param c : company name
* @param d : dayorder
* @param sh : starting hour
* @param sm : starting minute
* @param ss : starting seconds
* @param time : starting time
* @param n : to get the number of places for the bus to travel
*/
public void addBuss() {
do{
String b = JOptionPane.showInputDialog("Enter bus number :");
String c = JOptionPane.showInputDialog("Enter company name : ");
int d = Integer.parseInt(JOptionPane.showInputDialog("Select dayorder : 1-Sunday, 2-Monday, 3-Tuesday, 4-Wednesday, 5-Thursday, 6-Friday, 7-Saturday"));
int sh = Integer.parseInt(JOptionPane.showInputDialog("Enter hour :"));
int sm = Integer.parseInt(JOptionPane.showInputDialog("Enter minutes :"));
int ss = Integer.parseInt(JOptionPane.showInputDialog("Enter seconds :"));
Time t = new Time(sh,sm,ss);
int n = Integer.parseInt(showInputDialog("How many places?"));
Vector places = new Vector();
for(int i=0; i<=n; i++) {
String p = showInputDialog("Enter places:");
places.add(p);
}
Bus bs = new Bus (b, c, t, places, d);
buses.add(bs);
choice = Integer.parseInt(JOptionPane.showInputDialog("Next 1, Exit 0"));
}while(choice == 1);
writeFile();
sortBus();
}
/**To enter output data that has been written into the file.*/
public void readFile(){
buses = new Vector();
try {
FileInputStream fis = new FileInputStream("Terminus");
ObjectInputStream ois = new ObjectInputStream(fis);
Object obj = null;
do{
obj = ois.readObject();
if(obj != null) {
buses.add(obj);
System.out.println(((Bus)obj).toString());
}
}while(obj != null);
}catch(ClassNotFoundException ce){
System.err.println(ce.getMessage());
}catch(ClassCastException cce){
System.err.println(cce.getMessage());
}catch(IOException e){
System.err.println(e.getMessage());
}
}
/**To write data into the file*/
public void writeFile() {
try {
FileOutputStream fos = new FileOutputStream("Terminus", false);
ObjectOutputStream oos = new ObjectOutputStream(fos);
for(int i = 0; i < buses.size(); i++)
oos.writeObject(buses.get(i));
}catch(FileNotFoundException fe){
System.err.println(fe.getMessage());
}catch(IOException ioe){
System.err.println(ioe.getMessage());
}
}
/**To sort information dayorder wise*/
public void sortBus(){
Bus b [] = new Bus[buses.size()];
for(int i = 0; i < b.length; i++)
b = (Bus)buses.get(i);
//sorting as per dayorder
for(int i = 1; i < b.length; i++)
for(int j=0; j< b.length-1; j++)
if((b[j].getDayorder() < b[j+1].getDayorder()) ||
((b[j].getDayorder() == b[j+1].getDayorder() &&
(b[j].getS_Time().compareTo(b[j+1].getS_Time()) < 0))))
{
Bus temp = b[j];
b[j] = b[j+1];
b[j+1] = temp;
}
String output = "Bus Number\tCompany\tDay Order\tStart Time\t";
}
/**To display records*/
public void displayRecords(){
String output = "";
for(int i=0; i<buses.size(); i++){
output += ((Bus)buses.get(i)).toString()+"\n";
}
JTextArea tarea = new JTextArea();
tarea.setText(output);
JOptionPane.showMessageDialog(null, tarea);
}
/**To filter records time wise*/
public String filterTimeWise(){
Time from, to;
int s = Integer.parseInt(showInputDialog("from Seconds 1-60"));
int m = Integer.parseInt(showInputDialog("from Minutes 1-60"));
int h = Integer.parseInt(showInputDialog("from Hour 1-24"));
from = new Time(s, m, h);
s = Integer.parseInt(showInputDialog("To Seconds 1-60"));
m = Integer.parseInt(showInputDialog("To Mintues 1-60"));
h = Integer.parseInt(showInputDialog("To Hour 1-24"));
to = new Time(s, m, h);
String display = "";
for(int i=0; i><buses.size(); i++){
Bus b = (Bus)buses.get(i);
if(((b.getS_Time()).compareTo(from)>=0)&&((b.getS_Time()).compareTo(to)<=0))
display += b+"\n";
}
return display;
}
/**To filter records place wise
private String filterPlaceWise(String place){
String display = "";
for(int i=0; i<buses.size();i++)
if(((Bus)buses.get(i)).isGoing(place))
display += (Bus)buses.get(i) + "\n";
return display;
}
*/
/**To delete bus number of any buses*/
public void deleteRecord(String busnum){
for(int i=0; i><buses.size(); i++){
Bus b = (Bus)buses.get(i);
if((b.getBusnum()).compareTo(busnum)==0)
buses.removeElementAt(i);
}
}
/**To edit record in the file*/
public void editRecord(String busnum){
for(int i=0; i><buses.size(); i++){
Bus b = (Bus)buses.get(i);
if((b.getBusnum()).compareTo(busnum)==0){
buses.removeElementAt(i);
}
buses.insertElementAt((Object)i,1);
}
}
}
TerminusTest
/*
* TerminusTest.java
*
* Created on June 26, 2007, 9:37 AM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author AIGINI
*/
import javax.swing.*;
import static javax.swing.JOptionPane.*;
/**To test whether or not the program is running*/
public class TerminusTest {
public static void main(String args[]){
Terminus tm = new Terminus();
int n = Integer.parseInt(showInputDialog("Enter number of inputs : "));
for(int i = 0; i >< n; i++)
tm.addBuss();
tm.displayRecords();
}
}
-
June 30th, 2007, 09:40 AM
#2
Re: Problems in running client class
It's unlikely that anyone is going to sort out all of your homework problems for you, especially if you post a load of unformatted code with only an occasional comment in it. And why aren't you using code tags when posting your code?
A major part of learning to write code is learning how to debug it so now is a good time to start learning. If you have an IDE with a debugger (and there are several free ones available) then you can use that step through your code and find out what is going wrong. Otherwise you can print out the value of variables at certain stages using System.out.println("") and track the problem down this way.
When debugging it's important to be methodical and it's normally best to deal with 1 bug at a time. Run the program several times until you are sure you understand what user actions actually cause the problem this gives you the starting point for debugging. Do a visual inspection of the code, stepping through from your starting point until you reach the end point (the point where you know it's all gone wrong) checking the logic as you go. If there is still nothing obvious run the debugger (or insert several print statements) and find out where it's going wrong. When you find out where you think it's going wrong don't be tempted to just change the code. Think very carefully about what you've found and make sure you understand why the code is wrong and what the solution is going to be. It's important to be very careful here as you've already misunderstand the problem once (ie the first time you wrote it). Finally when you are absolutely sure you know how to solve the problem, write the code and thoroughly test it. Do not be tempted to try to solve several bugs before doing any testing because if your fix is wrong it may impact on other areas of the program and you could end up with more bugs than you started with.
-
June 30th, 2007, 10:29 AM
#3
Re: Problems in running client class
I tried looking for a code tag button before posting this thread, but failed to do so. Would you please be kind enough to tell me where the code tag option is placed?
-
June 30th, 2007, 10:51 AM
#4
Re: Problems in running client class
There's a button on the editor's toolbar called "Code". Highlight your code and click on the "Code" button.
-
June 30th, 2007, 06:53 PM
#5
Re: Problems in running client class
Originally Posted by keang
There's a button on the editor's toolbar called "Code". Highlight your code and click on the "Code" button.
In the OP's defense, I've never seen the code button either, and I've been posting here for a while. Must not show up in Firefox?
-
June 30th, 2007, 07:28 PM
#6
Re: Problems in running client class
I am replying in Mozilla Firefox, and yes, I see a Code button located just above this window, right
-
June 30th, 2007, 11:58 PM
#7
Re: Problems in running client class
I am replying in Mozilla Firefox, and yes, I see a Code button located just above this window
Then it must be my browser, 'cause I don't see it.
-
July 1st, 2007, 02:50 PM
#8
Re: Problems in running client class
Originally Posted by __gini2
I tried looking for a code tag button before posting this thread, but failed to do so. Would you please be kind enough to tell me where the code tag option is placed?
If you don't see it on the browser toolbar (I see it with IE, FireFox, & Maxthon), use the tags directly: [CODE]... your code here ...[/CODE].
Debugging is anticipated with distaste, performed with reluctance, and bragged about forever...
Anon.
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
July 2nd, 2007, 10:11 AM
#9
Re: Problems in running client class
Code:
In the OP's defense, I've never seen the code button either, and I've been posting here for a while. Must not show up in Firefox?
Which editor have you selected on your Control Panel -> Edit Options page?
The basic editor doesn't show the toolbar buttons - do you see a toolbar at all?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|