Click to See Complete Forum and Search --> : JAVA ADT...Help needed!!!!
raggy_man
Feb 14th, 2007, 06:59 AM
Guyz I really need ur help in developin thiz code coz I can understand wat my Lecturer is expecting but I don't know how 2 implement the coding.....He say itz quite simple but I'm breakin my head here.....Can some 1 help me 2 come up with the code or algorithm for thiz question??? I appreciate ur help....Plzz
Consider there is train track connecting various stations in a circular path and train can go onward as well as downward.
1. Create a class Station with name, onward train timing and downward train timing, distance, onward fare to next station and downward fare to previous station.
2. Create a class Track that can accommodate details of trains and able to provide services-
- method to identify target from current position onward and downward and display distance and fare.
- method to choose the cheapest direction and display the platform bound for next station with time of the train.
3. Use suitable data structures and exceptions to handle the details and show the evidence of handling any abnormal situations. Additional features will attract bonus marks.
raggy_man
Feb 14th, 2007, 08:37 PM
I just don't know how 2 implement the queue in the Track class....Help me out n here r my codez...I'm quite sure my Track class iz wrong...
Station Class:
public class Station implements java.io.Serializable {
private String name ;
private Time onwardTime, downwardTime ;
private double onwardFare, downwardFare ;
private int distance ;
public Station() {
setStation( null, null, null, 0, 0.0, 0.0 ) ;
}
public Station( String n, Time on, Time down, int dis, double oFare, double dFare ) {
setStation( n, on, down, dis, oFare, dFare ) ;
}
private void setStation( String n, Time on, Time down, int dis, double oFare,
double dFare ) {
name = n ;
onwardTime = on ;
downwardTime = down ;
distance = dis ;
onwardFare = oFare ;
downwardFare = dFare ;
}
public String getName() {
return name ;
}
public Time getOnwardTime() {
return onwardTime ;
}
public Time getDownwardTime() {
return downwardTime ;
}
public int getDistance() {
return distance ;
}
public double getOnwardFare() {
return onwardFare ;
}
public double getDownwardFare() {
return downwardFare ;
}
public double getFare( double fare ) {
return distance * fare ;
}
public String toString() {
return name + "\t" + onwardTime + "\t" + downwardTime + "\t" + distance + "\t" +
onwardFare + "\t" + downwardFare ;
}
}
Queue Class:
public class Queue {
protected int front, back, count, size;
protected Object q[];
/** Creates a new instance of Queue */
public Queue(int n) {
size = n;
q = new Object[size];
front = count = 0;
back = size - 1;
}
public boolean isEmpty() {
return count == 0;
}
public boolean isFull() {
return count == size;
}
public void enqueue(Object item) throws QException {
if(!isFull()){
back = (back + 1) % size;
q[back] = item;
count++;
}
else throw new QException("Q FULL");
}
public String toString() {
String out = "";
for(int k = 0; k < q.length; k++)
out += q[k].toString() + "\n";
return out;
}
}
raggy_man
Feb 14th, 2007, 08:41 PM
Time Class:
public class Time implements java.io.Serializable
{
private int hour;
private int minute;
private int second;
/**
* Contructor for objects of Time class.
* This Empty constructor responsible for initialising
* all instance variables.
*/
public Time() {
//initialise instance variables with setData method
setData(0,0,0);
}
/**
* Second constructor for objects of Time class.
*
* @param hour start hour or close hour
* @param minute start minute or close minute
* @param second start second or close second
*/
public Time(int hour, int minute, int second) {
setData(hour, minute, second);
}
/**
* A private setData method, so that the constructors
* can make use of setData method.
*
* @param h indicates hour
* @param m indicates minute
* @param s indicates second
*/
private void setData(int h, int m, int s) {
hour = h;
minute = m;
second = s;
}
/**
* Retrieve either the start hour or close hour
* for a subject.
*
* @return start hour or close hour
*/
public int getHour() {
return hour;
}
/**
* Retrieve either the start minute or close minute
* for a subject.
*
* @return start minute or close minute
*/
public int getMinute() {
return minute;
}
/**
* Retrieve either the start second or close second
* for a subject.
*
* @return start second or close second
*/
public int getSecond() {
return second;
}
/**
* Receives and compare another set of Time in order
* to find out whether it is equal to the stored Time.
*
* @param Time another set of Time
* @return true if both of the Time is equal
*/
public boolean isEquals(Time t) {
return (hour == t.hour && minute == t.minute && second == t.second);
}
/**
* Receives and compare another set of Time, to find out
* whether the Time given is the equal to the stored Time,
* greater (later) than the stored Time, or less (earlier) than
* the stored Time.
*
* @param Time another set of Time
* @return 0 if both of the Time is equal;
* -1 if Time given is greater (later) than the stored Time;
* 1 if Time given is less (earlier) than the stored Time.
*/
public int compareTo(Time t) {
if (t.getHour() == hour && t.getMinute() == minute && t.getSecond() == second) {
return 0;
}
else if ((t.getSecond() > second && t.getMinute() > minute && t.getHour() > hour) ||
(t.getMinute() > minute && t.getHour() > hour) || (t.getHour() > hour)) {
return -1;
}
else return 1;
}
/**
* A toString() method, to verify all the input is
* passed to Time class correctly.
*
* @return Time in a format of HH:MM:SS
*/
public String toString() {
return hour + ":" + minute + ":" + second;
}
}
Track Class:
import java.io.* ;
import java.util.* ;
import javax.swing.* ;
import static javax.swing.JOptionPane.* ;
public class Track implements java.io.Serializable {
private Station stat, station[] ;
private String StatName[] = { "Nilai" , "Serdang", "Tasek Selatan", "Jln Bdr" } ;
private int size, count ;
private ArrayList list ;
public Track() {
list = new ArrayList() ;
stat = new Station() ;
size = 0 ; count = 0;
}
public int isNext( int num, int size ) {
return num + 1 ;
}
public int isPrevious( int num , int size ) {
return num - 1 ;
}
public void createStation() {
int more = 0;
do{
int option = Integer.parseInt( showInputDialog( "Select a Current Station:\n" +
"1-Nilai\n" + "2-Serdang\n" + "3-Tasek Selatan\n" + "4-Jln Bdr\n" ) ) ;
int option1 = Integer.parseInt( showInputDialog( "Select a Destination:\n" +
"1-Nilai\n" + "2-Serdang\n" + "3-Tasek Selatan\n" + "4-Jln Bdr\n" ) ) ;
String statName = StatName[option - 1] ;
String destination = StatName[option1 -1] ;
int hour = Integer.parseInt( showInputDialog( "Onward: Hour" ) ) ;
int minute = Integer.parseInt( showInputDialog( "Onward: Minute" ) ) ;
int second = Integer.parseInt( showInputDialog( "Onward: Second" ) ) ;
Time onwardTime = new Time( hour, minute, second ) ;
int hour1 = Integer.parseInt( showInputDialog( "Downward: Hour" ) ) ;
int minute1 = Integer.parseInt( showInputDialog( "Downard: Minute" ) ) ;
int second1 = Integer.parseInt( showInputDialog( "Downward: Second" ) ) ;
Time downwardTime = new Time( hour1, minute1, second1 ) ;
double onwardFare = Double.parseDouble( showInputDialog( "Enter onward fare" ) ) ;
double downwardFare = Double.parseDouble( showInputDialog( "Enter downward fare" ) ) ;
int distance = ((option1-1) - (option-1)) * 10 ;
Station s = new Station( statName, onwardTime, downwardTime, distance, onwardFare, downwardFare ) ;
list.add( s ) ; size++ ;
more = Integer.parseInt( showInputDialog( "1-Continue " + " 0-Exit" ) ) ;
}while( more != 0 ) ;
Iterator iter = list.iterator() ;
station = new Station[size] ;
for( int j = 0; j < size; j++ )
station[j] = (Station)iter.next() ;
sortRecord() ; writeRecord() ;
}
public void sortRecord() {
for( int i =0; i < size; i++ )
for( int k = 0; k < size - 1; k++ )
if ( station[k].getOnwardTime().compareTo( station[i].getOnwardTime() ) > 0 ) {
Station temp = station[k] ;
station[k] = station[i] ;
station[i] = temp ;
}
for( int i =0; i < size; i++ )
for( int k = 0; k < size - 1; k++ )
if ( station[k].getDownwardTime().compareTo( station[i].getDownwardTime() ) > 0 ) {
Station temp = station[k] ;
station[k] = station[i] ;
station[i] = temp ;
}
for( int i =0; i < size; i++ )
for( int k = 0; k < size - 1; k++ )
if ( station[k].getName().compareTo( station[i].getName() ) > 0 ) {
Station temp = station[k] ;
station[k] = station[i] ;
station[i] = temp ;
}
}
private void writeRecord() {
try{
FileOutputStream fos = new FileOutputStream( "Transaction", false ) ;
ObjectOutputStream oos = new ObjectOutputStream( fos ) ;
for( Station s : station )
oos.writeObject( s ) ;
oos.close() ;
}catch( FileNotFoundException fe ) {
fe.printStackTrace() ;
}catch( IOException ioe ) {
ioe.printStackTrace() ;
}
}
public String displayRecord() {
String put = " " ;
try{
FileInputStream fis = new FileInputStream( "Transaction" ) ;
ObjectInputStream ois = new ObjectInputStream( fis ) ;
do{
stat= ( Station )ois.readObject() ;
put += stat + "\n" ;
}while( stat != null ) ;
ois.close() ;
}catch( EOFException eofe ) {
}catch( ClassNotFoundException ce ) {
ce.printStackTrace() ;
}catch( FileNotFoundException fe ) {
fe.printStackTrace() ;
}catch( IOException ioe ) {
ioe.printStackTrace() ;
}
return put ;
}
}
raggy_man
Feb 15th, 2007, 12:38 AM
- A train that moves on a circular path where it can go onward n downward
For eg:
These r the destinations in the train
1 > 2> 3 > 4 > 5
So, I wish 2 travel 2 destination 5 from destination 3...So, I have 2 obtain user input 2 know the current position n find the cheapest n shortest route....There will be 2 wayz to achieve the destination
Path1: 3>4>5
Path2: 3>2>1>5
So, thiz iz the part where I don't know how 2 implement the code// Plz help me out
CornedBee
Feb 15th, 2007, 09:56 AM
Please write in proper English.
raggy_man
Feb 15th, 2007, 11:05 AM
It'z like thiz bro....
- A train that moves on a circular path where it can go onward n downward
For eg:
These r the destinations in the train
1 > 2> 3 > 4 > 5
So, I wish 2 travel 2 destination 5 from destination 3...So, I have 2 obtain user input 2 know the current position n find the cheapest n shortest route....There will be 2 wayz to achieve the destination
Path1: 3>4>5
Path2: 3>2>1>5
I have try 2 solve with by testing onward n downward methodz in a client class...My onward method iz working well but my downward loop iz not functioning as expected...
import javax.swing.* ;
public class Testing {
public static void main( String args[] ) {
int distance[] = { 0, 4, 5, 3 };
String location[] = { "Nilai" , "Labu", "Tiroi", "Seremban" } ;
int current = Integer.parseInt( JOptionPane.showInputDialog("Select current location\n\n" +
"1-Nilai\n" + "2-Labu\n" + "3-Tiroi\n" + "4-Seremban\n" ) ) ;
int destination = Integer.parseInt( JOptionPane.showInputDialog("Select Destination\n" +
"1-Nilai\n" + "2-Labu\n" + "3-Tiroi\n" + "4-Seremban\n" ) ) ;
current = current - 1 ;
int size = location.length ;
int on = 0;
int down = 0;
int journey = 0;
while( on != destination ){
on = current % size ;
System.out.println( location[on] ) ;
on++ ;
current++ ;
}
System.out.println( journey ) ;
do{
down = (size + (current)) % size ;
System.out.println( distance[current] ) ;
journey = journey + distance[current] ;
--current ;
}
while( down != destination );
System.out.println( journey ) ;
}
}
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.