I am at the end of my quarter and have two programs to write and need help. I dont even know where to begin. I have decided to change my major to something else, but I dont want to fail this class and hurt my GPA. I have a programming diploma but it is 13 years old and I was trying to get an associates, but I am to old for all that mess now and programming is so much differant than when I took it before. I changed my major to Paralegal because that is my next thing I am veery interested in, and am going to double major in it.
But I must finish this quarter. I am not able to get in touch with the instructor for help and he never goes to the school. I have complained to the supervisor with no help there. Now I am stuck either getting this done or failing the course. So here goes what I need:

to Create a class called Flight that represents a commercial flight. It should contain strings to represent the source city, destination city, and airline. It should also contain GregorianCalendar objects to represent departure and arrival times. Finally, it should implement the Comparable interface. Two flights should be considered equal if they have the same source and destination cities. If they are not equal, the comparison should be made based on the source cities alphabetically.
Make sure to write a toString() method to output the flight information. To output the strings, simply use their own toString() methods. The GregorianCalendar toString() method returns far more information that you will need to sure the getDate() method instead. This method returns a Date object that has a toString() method that is much nicer than GregorianCalendar’s.

I have 2 days to get this done and 1 more program repaired and another written. Like I said I cant get the teachers help and I am for some reason not comprehending these last 2chapters. But I am trying hard.

Unfortunately no one here is going to do it for you. But this problem is rather simple when you think about it logically. I assume your teacher has gone over class structure and all that wonderfull stuff.

So to start create a new class called fight. Assign some attributes to it; for your requirements you need a source and destination cities (probably data type of String) as well as departure and arrival times (as data type of GregorianCalendar). I would personally include something like a flight number to make your toString a little easier to read. Next build some getters and setters (or accessors/mutators) depending on what you instructor called them.

Next I would write your toString method, as this is the next easiest thing to do.

public class Flight
{
   //attributes
   ...

   //constructors
   ...

   //getters and setters
   ...

   public String toString()
   {
      return "Flight Number: "+ getFlightNumber() + "\n" +
             "Departure City: " + getDepartureCity();
      ...
      //go ahead an implement the rest. I also assumed you added a flight number in
   }
}

That code snippet should get you started.

After that you need to implement the interface comparable, which will give you a method stub of:

public int compareTo(Object obj)
{
}

Now I would for safety reasons insure the Object is a flight, but your instructor did not specify this, so go ahead and cast the passed in object (obj) to a Flight. Now in the compareTo method implement the logic your instuctor mentioned to compare the two flights.

If you get stuck post the code that is important to have people help you, and I or others can assist you.

Edited 5 Years Ago by cale.macdonald: Removing missed quote

Thanks so very much. I have started on a program but am getting lots of error messages.
I have gotten more from you than I have ever gotten from my instructor. He is neveer available and does not come to the school and there is not tuto to help either. Kinda suxs! I asked to be dropped at the beginning of the quarter and did not succed in getting it. He refused to drop me and supervisor agreed. So here I am at the end of the quarter with 3 programs to get done & no help. My Java 1 and 2 instructor was okay but still not helpful. Anyways I will take your advice. Thanks. I will prob be back on here with issues involving my errors. I didnt want anyone to write for me I got from you what will start me rolling. I believe I am on the right track. I am having issues getting th gregorian calender going, and not finding much help online either. Have not done any assignments where we had to insert the date or this calender thing either.

My main issue now is the toString method. I can not seem to get how to put the string togeter without looking like garbage on the screen

Also I have not worked with the date at all. I do not know how to accomplish this at all.

Here is my code I have written so far: ( Still a work in progress)

//********************************************************************
//  Searching.java       Java Foundations
//
//  Contains various search algorithms that operate on an array of
//  Comparable objects.
//********************************************************************
import java.lang.*;
public class SearchingFlights
{
   //-----------------------------------------------------------------
   //  Searches the specified array of objects using a linear search
   //  algorithm. Returns null if the target is not found.
   //-----------------------------------------------------------------
   public static Comparable linearSearch (Comparable[] data, Comparable target)
   {
      Comparable result = null;
      int index = 0;

      while (result == null && index < data.length)
      {
         if (data[index].compareTo(target) == 0)
            result = data[index];
         index++;
      }

      return result;
   }
}

//********************************************************************
//  FlightListMain.java---Linda Schwalbauch---Chapter 13 & 14 Test
//
//       Demonstrates a linear search of Comparable objects.
//********************************************************************
import java.util.*;
import java.lang.*;
public class FlightListMain
{
   //-----------------------------------------------------------------
   //  Creates an array of Flight objects, then searches for a
   //  particular flight.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      Flight[] planes = new Flight[7];

      planes[0] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", "9:30 am", "11:00am");
      planes[1] = new Flight ("Charleston, SC", "New York, NY", "Con Air", "12:30 pm", "4:30pm");
      planes[2] = new Flight ("Indianapolis, IN", "Columbia, SC", "Pan Am", "1:30pm", "5:30pm");
      planes[3] = new Flight ("Indianapolis, IN", "Columbus, OH", "Delta", "4:30pm", "5:30pm");
      planes[4] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", "7:30pm", "9:00pm");
      planes[5] = new Flight ("Detroit, MI", "Miami, FL", "Pan Am", "10:30am" , "6:30pm");
      planes[6] = new Flight ("Philidelphia, PA", "Pittsburg, PA", "Con Air", "12:00pm", "1:00am");

      Sorting.selectionSort(planes);

      for (Comparable plane : planes)
         System.out.println (plane);

      Flight target = new Flight ("Detroit, MI", "Miami, FL", "", "");

      Flight found = (Flight)SearchingFlights.linearSearch(planes, target);
      if (found == null)
         System.out.println ("Flight was not found.");
      else
         System.out.println ("Found:");
         System.out.println(found);
     }
}

//********************************************************************
//  Contact.java       Java Foundations
//
//  Represents a Commercial Flight List that implements Comparable.
//********************************************************************
import java.util.*;

public class Flight implements Comparable
{
   private String String1, String2;
   private String sourceCity, destinationCity, airline;
   private String departTime, arrivalTime, timeA, timeD;

   //-----------------------------------------------------------------
    public LinkedBag ()

   //-----------------------------------------------------------------
   //  Returns true if this bag is empty and false otherwise.
   //-----------------------------------------------------------------
    public boolean isEmpty()
    {
        return (size() == 0);
    }

   //-----------------------------------------------------------------
   //  Returns the number of elements currently in this bag.
   //-----------------------------------------------------------------
    public int size()
    {
        return count;
    }

   //-----------------------------------------------------------------
   //  Sets up this flight with the specified information.
   //-----------------------------------------------------------------
   public Flight(String sourceCity, String destinationCity, String airline,
                    String departTime, String arrivalTime)
   {
      this.sourceCity = sourceCity;
      this.destinationCity = destinationCity;
      this.airline = airline;
      this.departTime = departTime;
      this.arrivalTime = arrivalTime;
    }

   //-----------------------------------------------------------------
   //        Gets the flight time departure time
   //-----------------------------------------------------------------
    public DepartTime getNext()
    {
       return timeD;
    }

   //-----------------------------------------------------------------
   //        Gets the flight time departure time
   //-----------------------------------------------------------------
    public ArrivalTime getNext()
    {
       return timeA;
    }

   //---------------------------------------------------------
   //  Sets the node that follows this one.
   //---------------------------------------------------------
    public void setNext (Flight arriveTime)
    {
       arrivalTime = arriveTime;
    }

   //-----------------------------------------------------------------
   //  Returns a string representation of this flight.
   //-----------------------------------------------------------------
    public String toString ()
    {
       return ("Flight is leaving: " + sourceCity + " Going to: " +
              destinationCity + ". On " + airline  + " Airlines.");
    }
   //-----------------------------------------------------------------
   //  Returns a string representation of this bag.
   //-----------------------------------------------------------------
   public String toString()
   {
      String result = "";

      LinearNode current = contents;

      while (current != null)
      {
         result += current.getElement().toString() + "\n";
         current = current.getNext();
       }

      return result;

   //-----------------------------------------------------------------
   //  Uses both destinationCity and sourceCity names to determine
   //  lexical ordering.
   //-----------------------------------------------------------------
   public int compareTo (Object other)
   {
      int result;

      if (destinationCity.equals(((Flight)other).destinationCity))
         result = sourceCity.compareTo(((Flight)other).sourceCity);
      else
         result = destinationCity.compareTo(((Flight)other).destinationCity);

      return result;
   }

   //-----------------------------------------------------------------
   //  Returns true if this bag contains the specified target
   //  element.
   //-----------------------------------------------------------------
    public boolean contains (Object target)
    {
        boolean found = false;
        LinearNode current = contents;
        for (int look=0; look < count && !found; look++)
        if (current.getElement().equals(target))
           found = true;
        else
        current = current.getNext();
        return found;
     }

    //-----------------------------------------------------------------
    //  Calculates midnight of the day in which date lies with respect
    //  to a time zone.
    //-----------------------------------------------------------------
     public Date midnight(Date date, TimeZone tz)
     {
        Calendar cal = new GregorianCalendar(tz);
        cal.setTime(date);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        return cal.getTime();
     }
}

I am still working on this one! I got one more after these two I have posted and it is just to turn an old program into a program that uses the array bag.

Edited 3 Years Ago by mike_2000_17: Fixed formatting

OK first lets get the Flight class working before we carry on.

First I would change the departTime and arrivalTime to be type of either Date or Calendar (Calendar is the super class [parent class] of GregorianCalendar). I would also drop the timeA, timeD, String1 and String2 attributes because you really don't need them (timeA and timeD are duplicates and you are not using String1 and String2).

So just the attribute portion and the getters and setters would look like:

import java.util.Calendar;

public class Flight
{
        //Attributes
        private String sourceCity, destinationCity, airline;
        private Calendar departTime, arrivalTime;

        //Getters and Setters
	public String getSourceCity() {
		return sourceCity;
	}
	public void setSourceCity(String sourceCity) {
		this.sourceCity = sourceCity;
	}
	public String getDestinationCity() {
		return destinationCity;
	}
	public void setDestinationCity(String destinationCity) {
		this.destinationCity = destinationCity;
	}
	public String getAirline() {
		return airline;
	}
	public void setAirline(String airline) {
		this.airline = airline;
	}
	public Calendar getDepartTime() {
		return departTime;
	}
	public void setDepartTime(Calendar departTime) {
		this.departTime = departTime;
	}
	public Calendar getArrivalTime() {
		return arrivalTime;
	}
	public void setArrivalTime(Calendar arrivalTime) {
		this.arrivalTime = arrivalTime;
	}
}

Now we can focus on the toString method. All we need to do here is print out the flight information. I also added 2 new methods to help with the display

public String getArrivalTimeString()
	{
		Calendar cal = getArrivalTime(); //get the calendar object
		
		//transform calendar to a nice readable date
		//calendar uses 0 as January so we need to add one to it
		return (cal.get(Calendar.MONTH) + 1) + "\\" + cal.get(Calendar.DAY_OF_MONTH) + 
			"\\" + cal.get(Calendar.YEAR) + "  " + cal.get(Calendar.HOUR_OF_DAY) +
			":"+cal.get(Calendar.MINUTE);
	}
	
	public String getDepartureTimeString()
	{
		Calendar cal = getDepartTime(); //get the calendar object
				
		//transform calendar to a nice readable date
		return (cal.get(Calendar.MONTH) + 1) + "\\" + cal.get(Calendar.DAY_OF_MONTH) + 
			"\\" + cal.get(Calendar.YEAR) + "  " + cal.get(Calendar.HOUR_OF_DAY) +
			":"+cal.get(Calendar.MINUTE);
	}
	
        public String toString()
        {
    	    return "Airline: "+getAirline()+"\n"+
    		"Departure "+getSourceCity()+" @ " + getDepartureTimeString() +"\n"+
    		"Arrival " + getDestinationCity() + " @ " + getArrivalTimeString()+ "\n";
        }

This should output something like

Airline: West Jet
Departure Calgary @ 12\19\2010 19:30
Arrival Toronto @ 12\19\2010 23:30

Next I just modified you compareTo method to store the Flight instead of casting it each time.

public class Flight
{
    ...
    public int compareTo (Object other)
    {
        
        Flight f = (Flight)other;
        
        int result = getDestinationCity().compareTo(f.getDestinationCity());
        
        if(result == 0) //they are the same
        {
        	result = getSourceCity().compareTo(f.getSourceCity());
        }
        
        return result;
    }
    ...
}

You have to remember that a Java class is a representation of a particular real world object. You also want to try and keep your class as simple as possible, the more complicated it gets the harder it is to find problems with.

Having said that I would remove all the container stuff to another class (if you need it) or use a built in java class, such as ArrayList. You can type ArrayList to hold your flight objects like

ArrayList<Flight> flights = new ArrayList<Flight>();

Now you can use the methods inside ArrayList to see if a flight has already been added, get the number of flights, add / remove a flight.

Also remember for next time to post your code inside code blocks. Makes it far easier for someone to read.

Edited 5 Years Ago by cale.macdonald: n/a

I dont know what youmean by code blocks. But I will try to put the program back out in a differant way so you can read it easier.

Here is the new error messages I am getting now. I can not seem to find out what it wants me to do.

:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\Flight.java:135: <identifier> expected
"\\" + cal.get(Calendar.YEAR) + cal.get(Calendar.(HOUR_OF_DAY) +
^
1 error

Tool completed with exit code 1

I got it! didnt read your last post before I entered the last thread. Again Thanks for your help. I think this one is almost done. If I can get this one done and my next one I will be good to go. To heck with the Programming exercise in the book. It does not affect my grade much. I will work on it if I have time. I only got till 7 pm tomorrow night. Then alls done that can be done.

anyways here is the new code revised with your suggestions whih are quite simle once I seen them. I moght be able to do my next one a little easier now with this help.

Here is the code:(still has errors in the messages too)Nowhere near worried about them)

//********************************************************************
//  Flight.java       Java Foundations
//
//  Represents a Commercial Flight List that implements Comparable.
//********************************************************************
import java.util.*;

public class Flight implements Comparable
{
   private String sourceCity, destinationCity, airline;
   private Calendar departTime, arrivalTime;

   //------------------------------------------------------------------
   //  Sets up this flight with the specified information.
   //------------------------------------------------------------------
   public Flight(String sourceCity, String destinationCity, String airline,
      					String departTime, String arrivalTime)
   {
	   this.sourceCity = sourceCity;
       this.destinationCity = destinationCity;
       this.airline = airline;
       this.departTime = departTime;
       this.arrivalTime = arrivalTime;
	}
   //-----------------------------------------------------------------
   //       Gets the source city, where flight will be leaving
   //-----------------------------------------------------------------
   public String getSourceCity()
   {
	   return sourceCity;
   }

   //-----------------------------------------------------------------
   //                 Sets the source city.
   //-----------------------------------------------------------------
   public void setSourceCity(String sourceCity)
   {
	   this.sourceCity = sourceCity;
   }

   //-----------------------------------------------------------------
   // Gets the Destination City, where flights final landing will be
   //-----------------------------------------------------------------
   public DestinCity getDestinationCity()
   {
	   return destinationCity;
   }

   //-----------------------------------------------------------------
   //              Gets the name of the AirLine.
   //-----------------------------------------------------------------
   public String getAirline()
   {
	   return airline;
   }

   //-----------------------------------------------------------------
   //            Sets up the name of the airline.
   //-----------------------------------------------------------------
   public void setAirline()
   {
	   return airline;
   }
   //-----------------------------------------------------------------
   // Gets the time the flight will be leaving Source city, the
   // Departure time.
   //-----------------------------------------------------------------
   public Calendar getDepartTime()
   {
	   return departTime;
   }

   //------------------------------------------------------------------
   //  Sets the Departure time, when flight will arrive at the
   //  destination city
   //------------------------------------------------------------------
   public void setArrivalTime(Calendar arrivalTime)
   {
	   this.departTime = departTime;
   }

   //------------------------------------------------------------------
   //  Get Arrival Time, when the flight arrives at destination city
   //------------------------------------------------------------------
   public Calendar getArrivalTime(Calendar arrivalTime)
   {
	   return arrivalTime;
   }

   //------------------------------------------------------------------
   // Sets the arrival time, when flight will reach the destination city
   //------------------------------------------------------------------
   public void setArrivalTime(Calendar arrivalTime)
   {
	   this.arrivalTime = arrivalTime;
   }

    //------------------------------------------------------------------
    //  Uses both last and first names to determine lexical ordering.
    //------------------------------------------------------------------
    public int compareTo (Object other)
    {
	   Flight f = (Flight)other;
       int result = getDestinationCity().compareTo(f.getDestinationCity());

	   if (result == 0)  //they are the same
	       result = getSourceCity().compareTo(f.getSourceCity());
	   return result;
   }
   //--------------------------------------------------------------------
   // Gets the calendar information to print the time and date in a
   // nice readable form and sets index for the month index + 1,
   // because the month of January is indexed at 0 so need to add
   // 1 to it to set index to 1 instead of 0.
   //--------------------------------------------------------------------
   public String getArrivalTimeString()
   {
	   Calendar cal = getArrivalTime();  // gets the calender object

	   // Transform calendar to a nice readable date
	   // Calender uses 0 as January so we need to add 1 to it
	   return (cal.get(Calender.MONTH) + 1) + "\\" + cal.get 
                    Calendar.DAY_OF_MONTH) +
	           "\\" + cal.get(Calendar.YEAR) + " " + cal.get 
                    (Calendar.HOUR_OF_DAY) +
	           ":" + cal.get(Calendar.MINUTE);
	}

	//--------------------------------------------------------------------
	//            Sets the departure time up a a String.
	//--------------------------------------------------------------------
	public String getDepartureTimeString()
	{
	  Calendar cal = getDepartTime();         // gets the calendar object

	return (cal.get(Calendar.MONTH) + 1) + "\\" + cal.get  
                        (Calendar.DAY_OF_MONTH)  +
	       	      "\\" + cal.get(Calendar.YEAR) + cal.get
                        (Calendar.HOUR_OF_DAY) +
		      ":" + cal.get(Calander.MINUTE));
	}

   //----------------------------------------------------------------------
   //  Returns a string representation of this bag.
   //----------------------------------------------------------------------
   public String toString()
   {
	   return ("Airline: "+ getAirline()+"\n"+
   		 "Departure: "+ getSourceCity()+" @ " + getDepartureTimeString()
                   + "\n" +
   		 "Arrival " + getDestinationCity() + " @ " + getArrivalTimeString()
                   + "\n");
    }
}
//********************************************************************
//  SortingFlight.java       Java Foundations
//
//  Contains various sort algorithms that operate on an array of
//  Comparable objects.
//********************************************************************
import java.lang.*;
public class SortingFlight
{
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the selection
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void selectionSort (Comparable[] data)
   {
      int min;

      for (int index = 0; index < data.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < data.length; scan++)
            if (data[scan].compareTo(data[min]) < 0)
               min = scan;

         swap (data, min, index);
      }
   }

   //-----------------------------------------------------------------
   //  Swaps two elements in the specified array.
   //-----------------------------------------------------------------
   private static void swap (Comparable[] data, int index1, int index2)
   {
       Comparable temp = data[index1];
       data[index1] = data[index2];
       data[index2] = temp;
   }

}
//********************************************************************
//  Searching.java       Java Foundations
//
//  Contains various search algorithms that operate on an array of
//  Comparable objects.
//********************************************************************
import java.lang.*;
public class SearchingFlights
{
   //-----------------------------------------------------------------
   //  Searches the specified array of objects using a linear search
   //  algorithm. Returns null if the target is not found.
   //-----------------------------------------------------------------
   public static Comparable linearSearch (Comparable[] data, Comparable target)
   {
      Comparable result = null;
      int index = 0;

      while (result == null && index < data.length)
      {
         if (data[index].compareTo(target) == 0)
            result = data[index];
         index++;
      }

      return result;
   }
}
//********************************************************************
//  FlightListMain.java---Linda Schwalbauch---Chapter 13 & 14 Test
//
//       Demonstrates a linear search of Comparable objects.
//********************************************************************
import java.util.*;
import java.lang.*;
public class FlightListMain
{
   //-----------------------------------------------------------------
   //  Creates an array of Flight objects, then searches for a
   //  particular flight.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      Flight[] planes = new Flight[7];

      planes[0] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", "9:30 am", "11:00am");
	  planes[1] = new Flight ("Charleston, SC", "New York, NY", "Con Air", "12:30 pm", "4:30pm");
	  planes[2] = new Flight ("Indianapolis, IN", "Columbia, SC", "Pan Am", "1:30pm", "5:30pm");
	  planes[3] = new Flight ("Indianapolis, IN", "Columbus, OH", "Delta", "4:30pm", "5:30pm");
	  planes[4] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", "7:30pm", "9:00pm");
	  planes[5] = new Flight ("Detroit, MI", "Miami, FL", "Pan Am", "10:30am" , "6:30pm");
planes[6] = new Flight ("Philidelphia, PA", "Pittsburg, PA", "Con Air", "12:00pm", "1:00am");

      Sorting.selectionSort(planes);

	  for (Comparable plane : planes)
                System.out.println (plane);

      Flight target = new Flight("Detroit, MI", "Miami, FL", " ", " ");

      Flight found = (Flight)SearchingFlights.linearSearch(planes, target);
      if (found == null)
         System.out.println ("Flight was not found.");
      else
         System.out.println ("Found: ");
 	System.out.println(found);
	 }
}

Edited 5 Years Ago by nickelsunshine: used code block option wrong

I did not get to finish my comments on the last post. My classes used for sorting and searching are giving me no errors. But here is the errors for my FlightListMain:

[error messages]

.\Flight.java:135: <identifier> expected
"\\" + cal.get(Calendar.YEAR) + cal.get(Calendar.(HOUR_OF_DAY) +
^
.\Flight.java:44: cannot find symbol
symbol : class DestinCity
location: class Flight
public DestinCity getDestinationCity()
^
.\Flight.java:93: setArrivalTime(java.util.Calendar) is already defined in Flight
public void setArrivalTime(Calendar arrivalTime)
^
F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:26: cannot find symbol
symbol : variable Sorting
location: class FlightListMain
Sorting.selectionSort(planes);
^
F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:31: cannot find symbol
symbol : constructor Flight(java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Flight
Flight target = new Flight("Detroit, MI", "Miami, FL", " ", " ");
^
.\Flight.java:22: incompatible types
found : java.lang.String
required: java.util.Calendar
this.departTime = departTime;
^
.\Flight.java:23: incompatible types
found : java.lang.String
required: java.util.Calendar
this.arrivalTime = arrivalTime;
^
.\Flight.java:62: cannot return a value from method whose result type is void
return airline;
^
.\Flight.java:118: getArrivalTime(java.util.Calendar) in Flight cannot be applied to ()
Calendar cal = getArrivalTime(); // gets the calender object
^
.\Flight.java:122: cannot find symbol
symbol : variable Calender
location: class Flight
return (cal.get(Calender.MONTH) + 1) + "\\" + cal.get(Calendar.DAY_OF_MONTH) +
^
.\Flight.java:122: operator + cannot be applied to <any>,int
return (cal.get(Calender.MONTH) + 1) + "\\" + cal.get(Calendar.DAY_OF_MONTH) +
^
.\Flight.java:135: cannot find symbol
symbol : variable HOUR_OF_DAY
location: class Flight
"\\" + cal.get(Calendar.YEAR) + cal.get(Calendar.(HOUR_OF_DAY) +
^
.\Flight.java:136: cannot find symbol
symbol : variable Calander
location: class Flight
":" + cal.get(Calander.MINUTE));
^
.\Flight.java:135: get(int) in java.util.Calendar cannot be applied to (java.lang.String)
"\\" + cal.get(Calendar.YEAR) + cal.get(Calendar.(HOUR_OF_DAY) +
^
14 errors

[end error messages]

Tool completed with exit code 1

I put my error messages on a post earlier. Am strating to finally getting my hopes up this can be accomplished! Again Thank You sooooooooooo Verry Much!

Edited 5 Years Ago by nickelsunshine: did not use the code blocks correctly

The attached image shows where the button is to put in code.

It will put in the editor what you see in the second image. Put your code between for nice formatting.

Please post the code that you have for your flight class between them so I can see where you are getting the errors. Line 122 & 135 does not match up with the code that you posted.

Attachments Code_Blocks.PNG 18.44 KB Code_Blocks_Result.PNG 0.55 KB

Okay here it is. I changed the compareTo method back to what I had it at. cause it would not work the way you showed me.

I am not worried about any ways to make it smaller or anything like that as long as it runs and I can get it turned in, he wont be grading me on the possible ways to do the stuff just that the program runs, and includes the methods and classes required as stated in the lab assignment outline. But I will keep your suggestions in mind to finish the other lab assignment due with this one.
The next program is a rewrite of a program from our book that he wants us to change it to where instead of just an array that it already uses I am to redo the program so it runs using an array bag. But I will be working on that later. Rate now I got to get this one going first. I still have a math final at 9:30 am tomorrow to cram for also. But I am pretty sure I dont have to do aything but review alittle, math is one of my better subjects.

Soooo! Here goes:

//********************************************************************
//  FlightListMain.java---Linda Schwalbauch---Chapter 13 & 14 Test
//
//       Demonstrates a linear search of Comparable objects.
//********************************************************************
import java.util.*;
import java.lang.*;
public class FlightListMain
{
//-----------------------------------------------------------------
//  Creates an array of Flight objects, then searches for a
//  particular flight.
//-----------------------------------------------------------------
public static void main (String[] args)
{
  Flight[] planes = new Flight[7];
  planes[0] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", "9:30 am", " 
         "11:00am");
  planes[1] = new Flight ("Charleston, SC", "New York, NY", "Con Air", "12:30 pm", 
          "4:30pm");
  planes[2] = new Flight ("Indianapolis, IN", "Columbia, SC", "Pan Am", "1:30pm", 
          "5:30pm");
  planes[3] = new Flight ("Indianapolis, IN", "Columbus, OH", "Delta", "4:30pm", "
          "5:30pm");
  planes[4] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", "7:30pm", 
          "9:00pm");
  planes[5] = new Flight ("Detroit, MI", "Miami, FL", "Pan Am", "10:30am" , "6:30
           pm");
  planes[6] = new Flight ("Philidelphia, PA", "Pittsburg, PA", "Con Air", "12:00
           pm", "1:00am");

  SortingFlight.selectionSort(planes);

  for (Comparable plane : planes)
         System.out.println (plane);

 Flight target = new Flight("Detroit, MI", "Miami, FL", " ");

 Flight found = (Flight)SearchingFlights.linearSearch(planes, target);
 
 if (found == null)
     System.out.println ("Flight was not found.");
 else
     System.out.println ("Found: ");
     System.out.println(found);
}
}

That was the FlightListMain class. I have changed some of it but I am getting these errors now. Alot of them have to do with the Flight class (but am getting no errors on it. Here are the errors:

[error messages start]

F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:18: cannot find symbol
symbol : constructor Flight (java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Flight
planes[0] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", "9:30am", "11:00am");
^

F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:19: cannot find symbol
symbol : constructor Flight (java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Flight
planes[1] = new Flight ("Charleston, SC", "New York, NY", "Con Air", "
12:30 pm", "4:30pm");
^


F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:20: cannot find symbol
symbol : constructor Flight (java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Flight
planes[2] = new Flight ("Indianapolis, IN", "Columbia, SC", "Pan Am", "1:30pm", "5:30pm");
^
F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:21: cannot find symbol
symbol : constructor Flight(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Flight
planes[3] = new Flight ("Indianapolis, IN", "Columbus, OH", "Delta", "4:30pm", "5:30pm");
^
F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:22: cannot find symbol
symbol : constructor Flight(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Flight
planes[4] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", "7:30pm", "9:00pm");
^
F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:23: cannot find symbol
symbol : constructor Flight(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Flight
planes[5] = new Flight ("Detroit, MI", "Miami, FL", "Pan Am", "10:30am" , "6:30pm");
^
F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:24: cannot find symbol
symbol : constructor Flight(java.lang.String,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Flight
planes[6] = new Flight ("Philidelphia, PA", "Pittsburg, PA", "Con Air", "12:00pm", "1:00am");
^
F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:31: cannot find symbol
symbol : constructor Flight(java.lang.String,java.lang.String,java.lang.String)
location: class Flight
Flight target = new Flight("Detroit, MI", "Miami, FL", " ");
^
8 errors

Tool completed with exit code 1

[End]

Here is code for Flight Class, does not have any errors except what was generated from FlightListMain class:

//********************************************************************
//  Flight.java       Java Foundations
//
//  Represents a Commercial Flight List that implements Comparable.
//********************************************************************
import java.util.*;

public class Flight implements Comparable
{
   private String sourceCity, destinationCity, airline;
   private Calendar departTime, arrivalTime;

   //------------------------------------------------------------------
   //  Sets up this flight with the specified information.
   //------------------------------------------------------------------
   public Flight(String sourceCity, String destinationCity, String airline,
      					Calendar departTime, Calendar arrivalTime)
   {
	   this.sourceCity = sourceCity;
       this.destinationCity = destinationCity;
       this.airline = airline;
       this.departTime = departTime;
       this.arrivalTime = arrivalTime;
	}
   //-----------------------------------------------------------------
   //       Gets the source city, where flight will be leaving
   //-----------------------------------------------------------------
   public String getSourceCity()
   {
	   return sourceCity;
   }

   //-----------------------------------------------------------------
   //                 Sets the source city.
   //-----------------------------------------------------------------
   public void setSourceCity(String sourceCity)
   {
	   this.sourceCity = sourceCity;
   }

   //-----------------------------------------------------------------
   // Gets the Destination City, where this flights final landing
   // will be.
   //-----------------------------------------------------------------
   public String getDestinationCity()
   {
	   return destinationCity;
   }

   //-----------------------------------------------------------------
   // Sets The destination city, where this flights final landing
   // will be.
   //-----------------------------------------------------------------
   public void setDestinationCity(String destinationCity)
   {
	   this.destinationCity = destinationCity;
   }

   //-----------------------------------------------------------------
   //              Gets the name of the AirLine.
   //-----------------------------------------------------------------
   public String getAirline()
   {
	   return airline;
   }

   //-----------------------------------------------------------------
   //            Sets up the name of the airline.
   //-----------------------------------------------------------------
   public void setAirline(String airline)
   {
	   this.airline = airline;
   }
   //-----------------------------------------------------------------
   // Gets the time the flight will be leaving Source city, the
   // Departure time.
   //-----------------------------------------------------------------
   public Calendar getDepartTime()
   {
	   return departTime;
   }

   //------------------------------------------------------------------
   //  Sets the Departure time, when flight will arrive at the
   //  destination city
   //------------------------------------------------------------------
   public void setDepartTime(Calendar departTime)
   {
	   this.departTime = departTime;
   }

   //------------------------------------------------------------------
   //  Get Arrival Time, when the flight arrives at destination city
   //------------------------------------------------------------------
   public Calendar getArrivalTime()
   {
	   return arrivalTime;
   }

   //------------------------------------------------------------------
   // Sets the arrival time, when flight will reach the destination city
   //------------------------------------------------------------------
   public void setArrivalTime(Calendar arrivalTime)
   {
	   this.arrivalTime = arrivalTime;
   }

    //------------------------------------------------------------------
    //  Uses the destination city and source city, or departure city
    // to determine the alphabetical order of the elements.
    //------------------------------------------------------------------
    public int compareTo (Object other)
    {
		int result;

		if (destinationCity.equals(((Flight)other).destinationCity))
		   result = sourceCity.compareTo(((Flight)other).sourceCity);
		else
		   result = destinationCity.compareTo(((Flight)other).destinationCity);

        return result;

    }
   //--------------------------------------------------------------------
   // Gets the calendar information to print the time and date in a
   // nice readable form and sets index for the month index + 1,
   // because the month of January is indexed at 0 so need to add
   // 1 to it to set index to 1 instead of 0.
   //--------------------------------------------------------------------
   public String getArrivalTimeString()
   {
	   Calendar cal = getArrivalTime();

	   return (cal.get(Calendar.MONTH) + 1) + "\\" + cal.get(Calendar.DAY_OF_MONTH) +
	           "\\" + cal.get(Calendar.YEAR) + " " + cal.get(Calendar.HOUR_OF_DAY) +
	           ":" + cal.get(Calendar.MINUTE);
	}

	//--------------------------------------------------------------------
	//            Sets the departure time up a a String.
	//--------------------------------------------------------------------
	public String getDepartureTimeString()
	{
		Calendar cal = getDepartTime();

		return (cal.get(Calendar.MONTH) + 1) + "\\" + cal.get(Calendar.DAY_OF_MONTH) +
		        "\\" + cal.get(Calendar.YEAR) + cal.get(Calendar.HOUR_OF_DAY) +
       		    ":" + cal.get(Calendar.MINUTE);
	}

   //----------------------------------------------------------------------
   //  Returns a string representation of this bag.
   //----------------------------------------------------------------------
   public String toString()
   {
	   return ("Airline: " + getAirline() + "\n" +
   			   "Departure " + getSourceCity() + " @ " + getDepartureTimeString() + "\n" +
   			   "Arrival " + getDestinationCity() + " @ " + getArrivalTimeString() + "\n");
    }
}

Here is the code for Sorting the flight list into alphabetical order called SortingFlight:

//********************************************************************
//  SortingFlight.java       Java Foundations
//
//  Contains various sort algorithms that operate on an array of
//  Comparable objects.
//********************************************************************
import java.lang.*;
public class SortingFlight
{
   //-----------------------------------------------------------------
   //  Sorts the specified array of integers using the selection
   //  sort algorithm.
   //-----------------------------------------------------------------
   public static void selectionSort (Comparable[] data)
   {
      int min;

      for (int index = 0; index < data.length-1; index++)
      {
         min = index;
         for (int scan = index+1; scan < data.length; scan++)
            if (data[scan].compareTo(data[min]) < 0)
               min = scan;

         swap (data, min, index);
      }
   }

   //-----------------------------------------------------------------
   //  Swaps two elements in the specified array.
   //-----------------------------------------------------------------
   private static void swap (Comparable[] data, int index1, int index2)
   {
       Comparable temp = data[index1];
       data[index1] = data[index2];
       data[index2] = temp;
   }

}

This is the final part of this program, called Searching, it searches the list of flights for a specific flight, using the source city and destintion city as search criterias:

//********************************************************************
//  Searching.java       Java Foundations
//
//  Contains various search algorithms that operate on an array of
//  Comparable objects.
//********************************************************************
import java.lang.*;
public class SearchingFlights
{
   //-----------------------------------------------------------------
   //  Searches the specified array of objects using a linear search
   //  algorithm. Returns null if the target is not found.
   //-----------------------------------------------------------------
   public static Comparable linearSearch (Comparable[] data, Comparable target)
   {
      Comparable result = null;
      int index = 0;

      while (result == null && index < data.length)
      {
         if (data[index].compareTo(target) == 0)
            result = data[index];
         index++;
      }

      return result;
   }
}

I am still working on it so. I will have more changes next time. I hope to get this done soon. I wouldnt be this far without your help. Also the last 2 classes have no errors when the are compiled. The only one showing errors is the FlightListMain class, but I believe mmost of them are being generated by the Flight class and the Searching class. I do not believe I am doing the search correctly. The statement in the FlightListMain that searches for the given flight I believe is wrong, I can not seem to figure out why. Also the rest of them are basically having to do with the Gregorian Calender and its setup, which I know very little about how to make it operate. All I kknow about it is what I have read online and what you have helped me with. Most of them online thougha are GUI programs.

Ok your problem is the way you are instantiating (creating) a Flight. The flight constructor requires that you give it String, String, String, Calendar, Calendar, but you are providing it all Strings.

So you need to change each of the times to be of a Calendar type.

The first flight in your list should become:

planes[0] = new Flight ("Reno, NV", "Los Angeles, CA", "Us Airways", 
		  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 9, 30), 
		  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 11, 0));

I used the 27th of December 2010 as the date but you could put whatever you want in there. Just remember that the month in GregorianCalendars go from 0 - 11 (Jan - Dec).

Also make sure just not to split your strings in the middle as that will also cause compile errors. I'm not sure if it was my copy paste that did that, but when I copied your code, some of the Strings where split.

For example do not do this:

String s = "s
23";
//note the line break between s and 23

It should be written as

String s = "s23";

//or

s = "s" +
"23"
//notice the concatenation operator

Okay maybe that will fix me. I have fixed all the errors, but the ones calling the Flight class for the new flight info and search. I don't know if I am doing the search correctly. I am to do a search based on the source city and destination city. If they are the same then they are equal (does not include the times and airline info with this). I think I can change the search staement to include the airline and times and that will fix that error. Maybe.

OOPs! this is for the time, not date. Also he is saying in the lab assignment he does not want us to use the gregorian calender. He wants us to use the getDate method instead. He said the gregorian calender provides more information than I need. I guess I read that wrong earlier, well 2 days ago. But I still do not know how to do this. We did not cover it in any classes I took and I sure have not gotten it from my instructor in this class. I am sorry if I have caused you any isssues. I am alittle frustrated about this too! But maybe it will be easier. Don't know.

Okay I think I got it almost solved but I am still getting another error.

Here is the error:

F:\CPT244\Programs\Tests\Chapter 13 & 14 Test\Lab 1\FlightListMain.java:54: cannot find symbol
symbol : constructor Flight(java.lang.String,java.lang.String)
location: class Flight
Flight target = new Flight("Detroit, MI", "Miami, FL");
^
1 error

Tool completed with exit code 1

Now heres the FlightListMain class that issues the error:

//********************************************************************
//  FlightListMain.java---Linda Schwalbauch---Chapter 13 & 14 Test
//
//       Demonstrates a linear search of Comparable objects.
//********************************************************************
import java.util.*;
import java.lang.*;
import java.util.Calendar;

public class FlightListMain
{
   //-----------------------------------------------------------------
   //  Creates an array of Flight objects, then searches for a
   //  particular flight.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      Flight[] planes = new Flight[7];

      planes[0] = new Flight("Reno, NV", "Los Angeles, CA", "Us Airways",
      			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 9, 30),
      			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 11, 40));

	  planes[1] = new Flight("Charleston, SC", "New York, NY", "Con Air",
	  			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 8, 30),
      			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 12, 30));

	  planes[2] = new Flight("Indianapolis, IN", "Columbia, SC", "Pan Am",
	  			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 9, 30),
      			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 12, 40));

	  planes[3] = new Flight("Indianapolis, IN", "Columbus, OH", "Delta",
	  			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 9, 30),
      			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 12, 40));

	  planes[4] = new Flight("Reno, NV", "Los Angeles, CA", "Us Airways",
	  			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 2, 30),
      			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 4, 50));

	  planes[5] = new Flight("Detroit, MI", "Miami, FL", "Pan Am",
	  			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 9, 30),
      			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 10, 00));

	  planes[6] = new Flight("Philidelphia, PA", "Pittsburg, PA", "Con Air",
	  			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 8, 30),
      			  new GregorianCalendar(2010, Calendar.DECEMBER, 27, 9, 50));


      SortingFlight.selectionSort(planes);

	  for (Comparable plane : planes)
         System.out.println (plane);

      Flight target = new Flight("Detroit, MI", "Miami, FL");

      Flight found = (Flight)SearchingFlights.linearSearch(planes, target);
      if (found == null)
         System.out.println ("Flight was not found.");
      else
         System.out.println ("Found: ");
    	 System.out.println(found);
	 }
}

here is the FlightClass revised also:

//********************************************************************
//  Flight.java       Java Foundations
//
//  Represents a Commercial Flight List that implements Comparable.
//********************************************************************
import java.util.*;
import java.lang.*;

public class Flight implements Comparable
{
   private String sourceCity, destinationCity, airline;
   private Calendar departTime, arrivalTime;
   //------------------------------------------------------------------
   //  Sets up this flight with the specified information.
   //------------------------------------------------------------------
   public Flight(String sourceCity, String destinationCity, String airline,
                 Calendar departTime, Calendar arrivalTime)
   {
	   this.sourceCity = sourceCity;
       this.destinationCity = destinationCity;
       this.airline = airline;
       this.departTime = departTime;
       this.arrivalTime = arrivalTime;
   }
   
   public GregorianCalendar (int hour, int minutes)
   {
	   this.hour = hour;
	   this

   //-----------------------------------------------------------------
   //       Gets the source city, where flight will be leaving
   //-----------------------------------------------------------------
   public String getSourceCity()
   {
	   return sourceCity;
   }

   //-----------------------------------------------------------------
   //                 Sets the source city.
   //-----------------------------------------------------------------
   public void setSourceCity(String sourceCity)
   {
	   this.sourceCity = sourceCity;
   }

   //-----------------------------------------------------------------
   // Gets the Destination City, where this flights final landing
   // will be.
   //-----------------------------------------------------------------
   public String getDestinationCity()
   {
	   return destinationCity;
   }

   //-----------------------------------------------------------------
   // Sets The destination city, where this flights final landing
   // will be.
   //-----------------------------------------------------------------
   public void setDestinationCity(String destinationCity)
   {
	   this.destinationCity = destinationCity;
   }

   //-----------------------------------------------------------------
   //              Gets the name of the AirLine.
   //-----------------------------------------------------------------
   public String getAirline()
   {
	   return airline;
   }

   //-----------------------------------------------------------------
   //            Sets up the name of the airline.
   //-----------------------------------------------------------------
   public void setAirline(String airline)
   {
	   this.airline = airline;
   }

   //------------------------------------------------------------------
   // Gets the time the flight will leave the source city, the
   // departure time.
   //------------------------------------------------------------------
   public Calendar getDepartTime()
   {
	   return departTime;
   }

   //------------------------------------------------------------------
   //  Sets the Departure time, when flight will leave source city
   //------------------------------------------------------------------
   public void setDepartTime(Calendar departTime)
   {
	   this.departTime = departTime;
   }

   //-----------------------------------------------------------------
   // Gets the Arrival time, when the flight will arrive at the
   // destination city
   //-----------------------------------------------------------------
   public Calendar getArrivalTime()
   {
	   return arrivalTime;
   }

   //------------------------------------------------------------------
   // Sets the arrival date, day flight will reach the destination city
   //------------------------------------------------------------------
   public void setArrivalTime(Calendar arrivalTime)
   {
	   this.arrivalTime = arrivalTime;
   }

   //------------------------------------------------------------------
   //  Uses the destination city and source city, or departure city
   // to determine the alphabetical order of the elements.
   //------------------------------------------------------------------

   public int compareTo (Object other)
   {
	  int result;

	  if (destinationCity.equals(((Flight)other).destinationCity))
	 	   result = sourceCity.compareTo(((Flight)other).sourceCity);
	  else
	  	   result = destinationCity.compareTo(((Flight)other).destinationCity);

      return result;

   }
   //--------------------------------------------------------------------
   // Gets the calendar information to print the time and date in a
   // nice readable form and sets index for the month index + 1,
   // because the month of January is indexed at 0 so need to add
   // 1 to it to set index to 1 instead of 0.
   //--------------------------------------------------------------------
   public String getArrivalCalendarString()
   {
       Calendar cal = getArrivalTime();

       return (cal.get(Calendar.MONTH) + 1) + "\\" +
       		   cal.get(Calendar.DAY_OF_MONTH) +
	           "\\" + cal.get(Calendar.YEAR) +
	           " " + cal.get(Calendar.HOUR_OF_DAY) +
	           ":" + cal.get(Calendar.MINUTE);
    }

    //--------------------------------------------------------------------
    //            Sets the departure date up as a String.
    //--------------------------------------------------------------------
    public String getDepartureCalendarString()
    {
	    Calendar cal = getDepartTime();

        return (cal.get(Calendar.MONTH) + 1) + "\\" +
                cal.get(Calendar.DAY_OF_MONTH) +
	    	    "\\" + cal.get(Calendar.YEAR) +
	    	    cal.get(Calendar.HOUR_OF_DAY) +
        		":" + cal.get(Calendar.MINUTE);
    }

    //----------------------------------------------------------------------
    //  Returns a string representation of this bag.
    //----------------------------------------------------------------------
    public String toString()
    {
	    return ("Airline: " + getAirline() + "\n" +
    			"Departure " + getSourceCity() + " @ " + getDepartureCalendarString() + "\n" +
    			"Arrival " + getDestinationCity() +	" @ " + getArrivalCalendarString() + "\n");
    }
}

here is the FlightSearching class that goes and searches for a specific flight by the destination city and source sity:

//********************************************************************
//  Searching.java       Java Foundations
//
//  Contains various search algorithms that operate on an array of
//  Comparable objects.
//********************************************************************
import java.lang.*;
public class SearchingFlights
{
   //-----------------------------------------------------------------
   //  Searches the specified array of objects using a linear search
   //  algorithm. Returns null if the target is not found.
   //-----------------------------------------------------------------
   public static Comparable linearSearch (Comparable[] data, Comparable target)
   {
      Comparable result = null;
      int index = 0;

      while (result == null && index < data.length)
      {
         if (data[index].compareTo(target) == 0)
            result = data[index];
         index++;
      }

      return result;
   }
}

Almost done hopefully. Still gotta study for other final.

The error you are getting is because you do not have a constructor in Flight that takes in two String arguments for source and destination cities. Since (I assume) you are going to be using that as a comparison, and you comparison does not factor in flight times, you could add another constructor that does that.

public class Flight
{
   ...
   public Flight(String destinationCity, String arrivalCity)
   {
      //assign variables here
   }
}
This article has been dead for over six months. Start a new discussion instead.