Results 1 to 8 of 8
  1. #1
    MV1
    MV1 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default Return objects called

    Hi, all i am creating a timetable program, which should when a destination is typed in return all instances of that destination. At the moment i can get it to read in a name and return that same name but i cannot get it to return the times associated with it.
    Java Code:
    import java.io.*;
    import java.util.*;
    
    public class Buses
    {
        public Buses()
        {
    		System.out.println("Please enter Destination:");
            ReadIn in = new ReadIn();
            String s = in.readString();
    
    		System.out.println("Your destination is:");
            System.out.println("\n" + s);
            System.out.println("Your departure time:");
            System.out.println("Your arrival time:");
    
        }
    
        public static void main(String args[])
        {
            new Buses();
    
            Journey busJourney[] = new Journey[15];
    
            busJourney[0] = new Journey(9.25,"Thornhill",9.50);
            busJourney[1] = new Journey(9.55,"Newport",10.20);
            busJourney[2] = new Journey(10.00,"Llanishen",10.25);
            busJourney[3] = new Journey(10.10,"Thornhill",10.35);
            busJourney[4] = new Journey(10.25,"Llanishen",10.50);
            busJourney[5] = new Journey(10.35,"Thornhill",11.00);
    		busJourney[6] = new Journey(10.55,"Pembroke",11.15);
    		busJourney[7] = new Journey(11.05,"Thornhill",11.35);
    		busJourney[8] = new Journey(11.25,"Newport",11.55);
            busJourney[9] = new Journey(11.50,"Llanishen",12.10);
            busJourney[10] = new Journey(12.05,"Thornhill",12.25);
    		busJourney[11] = new Journey(12.20,"Llanishen",12.45);
    		busJourney[12] = new Journey(12.35,"Thornhill",13.00);
    		busJourney[13] = new Journey(12.55,"Pembroke",13.10);
            busJourney[14] = new Journey(13.20,"Thornhill",13.35);
    
        }
    }
    
    class ReadIn {
        static InputStreamReader is = new InputStreamReader( System.in );
        static BufferedReader br = new BufferedReader( is );
        StringTokenizer st;
    
        StringTokenizer getToken() throws IOException
        {
           String s = br.readLine();
           return new StringTokenizer(s);
        }
    
        String readString()
        {
           try
           {
             return br.readLine();
           }
           catch (IOException ioe)
           {
              System.err.println("IO Exception in ReadIn.readString");
              return "";
           }
        }
    }
    Java Code:
    public class Journey
    {
    
            Double fdeparture;
            String fdestination;
            Double farrival;
    
    
            public Journey(Double departure, String destination, Double arrival)
            {
                fdeparture = departure;
                fdestination = destination;
                farrival = arrival;
            }
    
    }
    Your help will be very much apprciated and i am eager to learn how to do these tasks.

  2. #2
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    you didn't write any code for reading arrival and departure timings .You just created objects for Journey class .That's it.

  3. #3
    MV1
    MV1 is offline Member
    Join Date
    Mar 2009
    Posts
    2
    Rep Power
    0

    Default

    That is what i would like help with i do not know how to do it.

  4. #4
    angryboy's Avatar
    angryboy is offline Senior Member
    Join Date
    Jan 2009
    Posts
    742
    Rep Power
    6

    Default

    USE CODE TAGS--> [CODE]...[/CODE]
    Get NotePad++ (free)

  5. #5
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    public class Buses
    {


    public static void main(String args[]) throws IOException
    {
    BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter Destination");
    String destination = br.readLine();
    System.out.println("Your Destination is");
    System.out.println(destination);
    Journey[] busJourney = new Journey[15];
    busJourney[0] = new Journey(9.25,"Thornhill",9.50);
    busJourney[1] = new Journey(9.55,"Newport",10.20);
    busJourney[2] = new Journey(10.00,"Llanishen",10.25);
    busJourney[3] = new Journey(10.10,"Thornhill",10.35);
    busJourney[4] = new Journey(10.25,"Llanishen",10.50);
    busJourney[5] = new Journey(10.35,"Thornhill",11.00);
    busJourney[6] = new Journey(10.55,"Pembroke",11.15);
    busJourney[7] = new Journey(11.05,"Thornhill",11.35);
    busJourney[8] = new Journey(11.25,"Newport",11.55);
    busJourney[9] = new Journey(11.50,"Llanishen",12.10);
    busJourney[10] = new Journey(12.05,"Thornhill",12.25);
    busJourney[11] = new Journey(12.20,"Llanishen",12.45);
    busJourney[12] = new Journey(12.35,"Thornhill",13.00);
    busJourney[13] = new Journey(12.55,"Pembroke",13.10);
    busJourney[14] = new Journey(13.20,"Thornhill",13.35);
    busTimings(busJourney,destination);
    }

    public static void busTimings(Journey[] busJourney,String destination){
    for (int i = 0; i < busJourney.length; i++) {
    if(busJourney[i].fdestination.equalsIgnoreCase(destination)){

    System.out.println("Arrival Timings and Departure Timings For "+ destination +"\t"+busJourney[i].farrival +"\t"+busJourney[i].fdeparture);

    }

    }
    }

    }

    public class Journey
    {

    Double fdeparture;
    String fdestination;
    Double farrival;


    public Journey(Double departure, String destination, Double arrival)
    {
    fdeparture = departure;
    fdestination = destination;
    farrival = arrival;
    }

    }

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    So now I look at this and the code works but I cant figure out where I am.
    Unless I'm missing something, that would make a good addition to the program. Also it only works once.

    btw MV1, you dont need the java.util.* import.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    just add one method to print the bus timings using "busTimings" method.It is
    very simple just get the "farrival" and "fdeparture" using busJourney object .
    It is very simple .
    Below code ask the user untill user press "n or N ".


    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;

    public class Buses
    {


    public static void main(String args[]) throws IOException
    {
    BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
    Journey[] busJourney = new Journey[15];
    busJourney[0] = new Journey(9.25,"Thornhill",9.50);
    busJourney[1] = new Journey(9.55,"Newport",10.20);
    busJourney[2] = new Journey(10.00,"Llanishen",10.25);
    busJourney[3] = new Journey(10.10,"Thornhill",10.35);
    busJourney[4] = new Journey(10.25,"Llanishen",10.50);
    busJourney[5] = new Journey(10.35,"Thornhill",11.00);
    busJourney[6] = new Journey(10.55,"Pembroke",11.15);
    busJourney[7] = new Journey(11.05,"Thornhill",11.35);
    busJourney[8] = new Journey(11.25,"Newport",11.55);
    busJourney[9] = new Journey(11.50,"Llanishen",12.10);
    busJourney[10] = new Journey(12.05,"Thornhill",12.25);
    busJourney[11] = new Journey(12.20,"Llanishen",12.45);
    busJourney[12] = new Journey(12.35,"Thornhill",13.00);
    busJourney[13] = new Journey(12.55,"Pembroke",13.10);
    busJourney[14] = new Journey(13.20,"Thornhill",13.35);
    String choice="Y";
    while(choice.equalsIgnoreCase("Y")){
    System.out.println("Enter Destination");
    String destination = br.readLine();
    System.out.println("Your Destination is");
    System.out.println(destination);
    //prints Bustimings
    busTimings(busJourney,destination);
    System.out.println("Are You Sure To Continue: Y/N");
    choice=br.readLine();
    }
    }

    public static void busTimings(Journey[] busJourney,String destination){
    for (int i = 0; i < busJourney.length; i++) {
    if(busJourney[i].fdestination.equalsIgnoreCase(destination)){

    System.out.println("Arrival Timings and Departure Timings For "+ destination +"\t"+busJourney[i].farrival +"\t"+busJourney[i].fdeparture);

    }

    }
    }

    }


    public class Journey
    {

    Double fdeparture;
    String fdestination;
    Double farrival;


    public Journey(Double departure, String destination, Double arrival)
    {
    fdeparture = departure;
    fdestination = destination;
    farrival = arrival;
    }

    }

  8. #8
    Join Date
    Mar 2009
    Posts
    4
    Rep Power
    0

    Default

    look into the toString() method. you could just print an objects attributes like this system.out.print(objectHere) and the objects attributes will be printed only if that object has a toString() method defined in its class.

Similar Threads

  1. Replies: 1
    Last Post: 01-22-2009, 04:25 PM
  2. javascipt function is nt getting called
    By pankaj_salwan in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 12-20-2008, 08:13 AM
  3. Replies: 1
    Last Post: 07-26-2008, 05:26 PM
  4. Replies: 0
    Last Post: 07-03-2008, 08:05 AM
  5. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •