I have the following code which contains a run-time error. The code was meant to print out:
Vehicle mode:flight Fuel:propane Max Altitude:10000
Vehicle mode:traversal Fuel:coal Horsepower:5000
I could not find it myself (as I am fairly new to coding) and would like some help if possible.
Thanks.
class Main {
public static void main(String[] args) {
HotAirBalloon airbag = new HotAirBalloon(10000);
Locomotive loco = new Locomotive(5000);
System.out.println(airbag.toString());
System.out.println(loco.toString());
}
}
class Vehicle {
String mode, fuel;
public String toString() {
return "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
}
class HotAirBalloon extends Vehicle {
int maxAltitude;
HotAirBalloon(int _alt) {
mode = "flight";
fuel = "propane";
maxAltitude = _alt;
}
public String toString() {
return toString() + " Max Altitude:" + maxAltitude;
}
}
class Locomotive extends Vehicle {
int horsePower;
Locomotive(int _hp) {
mode = "traversal";
fuel = "coal";
horsePower = _hp;
}
public String toString() {
return toString() + " Horsepower:" + horsePower;
}
}
Because you are trying to call the super classes version of the current method you need to add super.toString()
//old
return toString() + " Horsepower:" + horsePower;
//new
return super.toString() + " Horsepower:" + horsePower;
You also need to do this with your other subclass
When you a method calls itself its called recursion, where a method keeps calling itself until a certain condition.
This code will do fine. the problem was that you were calling toString() multiple times which was causing a Stack overflow. plus you have to declare a String in parent class vehicle and update it in the child classes with flight mode etc..run the code below:
class Main {
public static void main(String[] args) {
HotAirBalloon airbag = new HotAirBalloon(10000);
Locomotive loco = new Locomotive(5000);
System.out.println(airbag.toString());
System.out.println(loco.toString());
}
}
class Vehicle {
String mode, fuel;
String s;
}
class HotAirBalloon extends Vehicle {
int maxAltitude;
HotAirBalloon(int _alt) {
mode = "flight";
fuel = "propane";
maxAltitude = _alt;
s= "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
public String toString() {
return s + " Max Altitude:" + maxAltitude;
}}
class Locomotive extends Vehicle {
int horsePower;
Locomotive(int _hp) {
mode = "traversal";
fuel = "coal";
horsePower = _hp;
s= "Vehicle Mode:" + mode + " Fuel:" + fuel;
}
public String toString() {
return s+ " Horsepower:" + horsePower;
}
}
Related
Ok, I'm trying to create a hamburger object, it has to have an array of toppings and other things. I keep getting a compilation error every time I try to assign values to the array for each object. Here's the class
/*
This Hamburger class is to define how to make a Hamburger sandwich
Then this class will be used to create a Hamburger object in the tester class
Fields & their purpose
4. toppings – an array of Strings storing the toppings on the burger.
*/
public class Hamburger {
//declear private fields
private int weight;
private String doneness;
private boolean cheese;
private String[] toppings;
//creat arrays for each object, then use it in each specific object
//String[] hamburgerToppings = {"l", "m"};
//String[] cheseburgerToppings = {"l", "m"};
//String[] anotherCheseburgerToppings = {"l", "m"};
//create printable array of toppings
//full constructor
public Hamburger(int weightOZ, String done,
boolean WantCheese, String[] topps) {
weight = weightOZ;
doneness = done;
cheese = WantCheese;
toppings = topps;
}
//overloaded no-arg constructor
public Hamburger(){
}
//copy constructor
public Hamburger(Hamburger burger){
weight = burger.weight;
doneness = burger.doneness;
cheese = burger.cheese;
toppings = burger.toppings;
}
//declear getter methods
public int getWeight() {
return weight;
}
public String getDoneness() {
return doneness;
}
and here's the tester class
//This class will test & run the Hamburger class
//It has the main method to run the other methods in the Hamburger class
public class HamburgerTester {
public static void main(String[] args) {
//Creating Hamburger objects
Hamburger hamburger = new Hamburger();
Hamburger cheseburger = new Hamburger();
Hamburger anotherCheseburger = new Hamburger(cheseburger);
//Setting Values for hamburger object
hamburger.setWeight(7);
hamburger.setDoneness("Mediuem Rare");
hamburger.setCheese(false);
hamburger.setToppings();
//Setting Values for cheseburger object
cheseburger.setWeight(10);
cheseburger.setDoneness("Well Done");
cheseburger.setCheese(true);
//cheseburger.setToppings();
//anotherCheseburger object is a copy of cheseburger
//there's no need to set special values for it
//Printing Results
System.out.println("Hamburger weight is: " + hamburger.getWeight() +
" doneness: " + hamburger.getDoneness() +
" with/without cheese: " + hamburger.getCheese() +
" Toppings: " + hamburger.getToppings());
System.out.println("cheseburger: " + cheseburger.toString());
System.out.println("cheseburger copy: " + anotherCheseburger.toString());
System.out.println("cheseburger wieght after a bite: " + cheseburger.bite());
System.out.println("cheseburger wieght after a 2 bites: " + cheseburger.bite());
}
}
public boolean getCheese() {
return cheese;
}
public String[] getToppings() {
return toppings;
}
//declear setter methods
public void setWeight(int weightOZ) {
this.weight = weightOZ;
}
public void setDoneness(String done) {
this.doneness = done;
}
public void setCheese(boolean WantCheese) {
this.cheese = WantCheese;
}
public void setToppings(String[] topps) {
this.toppings = topps;
for(int i =0; i < toppings.length; i++) {
if (i > 0)
System.out.print(", ");
System.out.print(toppings[i]);
}
}
//copy method
public Hamburger copy(){
Hamburger burger = new Hamburger(weight, doneness, cheese, toppings);
return burger;
}
//bite methode
public int bite(){
if (weight > 0)
weight--;
return weight;
}
//toString method to print what each Hamburger contains
public String toString(){
String str = "Weight is: " + weight +
"\nDoneness is: " + doneness +
"\nCheese is: " + cheese +
"\nToppings are: " + toppings;
return str;
}
}
any help is appreciated, thanks!
Ill try to help, What Are you trying to do? Your getter and setter methods for the object Hamburger appear to be in the class Hamburgertester.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Hey guys I just had a question about toString. On a previous test my professor had this overrided method for toString that was similar to this method which I'm testing:
public String toString()
{
String s="";
s+="units: " + units;
s+="\n";
s+="owner: " +owner;
return s;
}
This method is is inside the class Residential which inherits from a base class Construction. Anyways, this mirrors a problem I had on a test where I would try to do say:
Residential R1 = new Residential();
R1.toString();
I thought R1.toString(); would display, which I put on the test, but obviously it was marked wrong and it doesn't.
So now I'm going over the problem and how to correct it. I tried doing say:
System.out.println(R1.toString());
but it's still giving me some weird output like "Residential#5c538b31". Why does it not overriden?
edit: The whole residential class, I'm aware it's not overridden, now but it wasn't annoted with a #Override by the professor in his code either so I assumed it wasn't needed.
public class Residential extends Construction {
private int units;
private String owner;
Residential ()
{
super();
units = 0;
owner = "Unknown";
}
Residential (String n, int y, double a, int u, String o)
{
super (n,y,a);
units = u;
owner = o;
}
public int getUnits()
{
return units;
}
public void setUnits(int u)
{
units = u;
}
public String getOwner()
{
return owner;
}
public void setOwner(String o)
{
owner = o;
}
public void display()
{
System.out.println("Name: " + getName() + " Year: " + getYear() + " Area: " + getArea() + " Number of Units: " + getUnits() + " Owner: " + getOwner());
}
public boolean isEqual (Residential r)
{
if (this.getName() == r.getName() && this.getYear() == r.getYear() && this.getArea() == r.getArea() && this.units == r.units && this.owner == r.owner)
{
return true;
}
return false;
}
public String toString()
{
String s="";
s += "the units is: " + units;
s += "\n";
s += "Owner: " + owner;
return s;
}
edit 2: Added construction class
public class Construction {
private String buildername;
private int year;
private double area;
Construction()
{
buildername = "Unknown";
year = 0;
area = -1;
}
Construction(String b, int y, double a)
{
buildername = b;
year = y;
area = a;
}
//Mutators
public void setName(String n)
{
buildername = n;
}
public void setYear(int y)
{
year = y;
}
public void setArea (double a)
{
area = a;
}
//Accessors
public String getName()
{
return buildername;
}
public int getYear()
{
return year;
}
public double getArea()
{
return area;
}
public void display()
{
System.out.println("Builder's Name: " + getName() + " Year: " + getYear() + " Area: " + getArea());
}
public boolean isEqual(Construction c)
{
if (this.buildername == c.buildername && this.year == c.year && this.area == c.area)
{
return true;
}
return false;
}
}
but it's still giving me some weird output like "Residential#5c538b31". What am I doing wrong?
This means that your version of the Residential class does not correctly override the toString() method.
To fix this, you need to give your class a proper toString override. I would also give the method an #Override annotation to be sure that it's truly overriding the method.
You also state:
On a previous test my professor had this overrided method for toString that was similar to this method which I'm testing...
... and yet you have not shown us the method that you're "testing". Perhaps you want to do this.
Edit
Regarding your posted code, that code is not what has produced the output that you've posted. Perhaps you need to refresh or restart your IDE, but the output could not possibly come from the posted code.
As an aside, your Residential toString() method should also call its parent class's toString() method in its method body, since the String returned should be part of Residential's String.
I'm new here (also new to programming) and I tried to look for an answer but couldn't come up with one. My assignment is due today so help would be very appreciated. This problem has happened to me twice, but the first I was able to ignore it by programming another way now I can't anymore. Every time I create a new object (Agent a = new Agent() <-- name of my class), it interferes with other objects of same type (does this have anything to do with reference types?). I'm trying to make a DiscussionDirector class that takes two Agent objects and creates a randomized conversation between them (Random based), but I can't even get started because I haven't been able to create two objects of type Agent yet.
Here's the code for Agent:
import java.util.Calendar;
import java.io.*;
import java.util.Random;
public class Agent{
private static boolean isMale;
private static String birthdate;
private static int birthyear;
private static int birthmonth;
private static int birthday;
private static String name;
private static String nativeCity;
private static String currentCity;
private static String major;
private static Random r = new Random();
public static void main(String[]args){
}
public String getCityNow(){
return this.currentCity;
}
public String getCityBorn(){
return this.nativeCity;
}
public String getName(){
return this.name;
}
public boolean getGender(){
return this.isMale;
}
public String getMajor(){
return this.major;
}
public String getBirthday(){
String birthdate = (this.birthday + "/" + this.birthmonth + "/" + this.birthyear);
return birthdate;
}
public void sayHelloTo(String name){
System.out.println(this.name + " says: Hi " + name + ", I'm " + this.name);
}
public void sayHello(){
System.out.println(this.name + " says: Hello, my name is " + this.name);
}
public void CityBorn(){
System.out.println(this.name + " says: I am from " + this.nativeCity);
}
public void howOldAreYou(){
System.out.print(this.name + " says: I am ");
if(Calendar.getInstance().get(Calendar.MONTH) < this.birthmonth){
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear - 1) + " years old");
}
else if((Calendar.getInstance().get(Calendar.MONTH) == this.birthmonth) && (Calendar.getInstance().get(Calendar.DAY_OF_MONTH) == this.birthday)){
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear - 1) + " years old");
}
else{
System.out.println((Calendar.getInstance().get(Calendar.YEAR) - this.birthyear) + " years old");
}
}
public void sayGender(){
System.out.println(this.name + " says: I am a ");
if(isMale == true){
System.out.print("man");
}
else{
System.out.print("woman");
}
}
public void CityNow(){
System.out.println(this.name + " says: I currently live in " + this.currentCity);
}
public void sayMajor(){
System.out.println(this.name + " says: I am studying " + this.major);
}
public void whoAreYou(){
sayHello();
CityBorn();
howOldAreYou();
sayMajor();
CityNow();
}
public Agent()throws IOException{
this.isMale = r.nextBoolean();
if(this.isMale == true){
WordList MaleNames = new WordList("MaleNames.txt");
this.name = MaleNames.getRandomWord();
}
else{
WordList FemaleNames = new WordList("FemaleNames.txt");
this.name = FemaleNames.getRandomWord();
}
this.birthyear = 1995 - r.nextInt(60); //Agents can't be too young or too old.
this.birthmonth = r.nextInt(11)+1;
if(this.birthmonth == 1|this.birthmonth == 3|this.birthmonth == 5|this.birthmonth == 7|this.birthmonth == 8|this.birthmonth == 10|this.birthmonth == 12){
this.birthday = r.nextInt(30)+1;
}
else if (this.birthmonth == 2){
this.birthday = r.nextInt(27)+1;
}
else{
this.birthday = r.nextInt(29)+1;
}
WordList Major = new WordList("Majors.txt");
this.major = Major.getRandomWord();
WordList Cities = new WordList("Cities.tx");
this.nativeCity = Cities.getRandomWord();
this.currentCity = Cities.getRandomWord();
}
public Agent generateAgent()throws IOException{
return new Agent();
}
}
So yeah, does anyone have any idea why when I create a two Agent() objects and then do generateAgent() objects on them, they're always the same?
Thanks
You've made all your class variables static, which means one copy for the whole class. Remove static on all of them, so they all have one value per object instance.
This is because all fields are declared static in your agent class.
A static field is shared among all instances of the class.
To solve your issue just remove the static keyword!
A typical use of static keyword inside a class is a counter of created instances of that class.
public class Agent{
private static int numberAgent;
private String birthdate;
private int birthyear;
private int birthmonth;
...
}
And in the constructors of the class, you do
numberAgent++;
As the static variable of a class are shared across all the instances of the class,
you will have the number of agents instanciated in all Agent objects.
I am working on 3 programs that contain the classes CarRental.java, LuxuryCarRental.java, and UseCarRental.java, On my LuxuryCarRental.java, I keep getting the error, Error; cannot find symbol; Symbol: variable super for the class, here is my program, I'm relatively new to Java, so please be detailed! Thanks in advance!
import java.text.DecimalFormat;
public class LuxuryCarRental extends CarRental{
private boolean chauffeur;
private double dailyChauffeurFee;
public LuxuryCarRental(String renterName, int renterZip, String sizeOfCar,int rentalDays, boolean chauffeur) {
super(renterName, renterZip, sizeOfCar, rentalDays);
this.chauffeur = chauffeur;
}
public void display(){
super.dailyRentalFee = 79.99;
this.dailyChauffeurFee = 0;
if(chauffeur){
this.dailyChauffeurFee = 200;
}
super.totalRentalFee = super.dailyRentalFee * super.getRentalDays() + this.dailyChauffeurFee * super.getRentalDays();
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Car Rental - Renter Name : " + super.getRenterName() + ", Renter Zip: " + super.getRenterZip() +
", Rental Days : " + super.getRentalDays() +
", Daily Rental Fee: " + dailyRentalFee + ", Daily Chauffer Fee: " + dailyChauffeurFee +
", Total Rental Fee: " + df.format(totalRentalFee));
}
}
And here are all the classes from all three of my programs that correspond to each other.
public class CarRental {
private String renterName;
private int renterZip;
private String sizeOfCar;
private int rentalDays;
protected double dailyRentalFee;
protected double totalRentalFee;
public class UseCarRental
public class LuxuryCarRental extends CarRental {
private boolean chauffeur;
private double dailyChauffeurFee;
public CarRental(String renterName, int renterZip, String sizeOfCar, int rentalDays)
{
renterName = renterName;
renterZip = renterZip;
sizeOfCar = sizeOfCar;
rentalDays = rentalDays;
And my altered code:
public class CarRental
{
public static void main(String[] args)
{
private String renterName;
private int renterZip;
private String sizeOfCar;
private int rentalDays;
protected double dailyRentalFee;
protected double totalRentalFee;
}
public CarRental(String renterName, int renterZip, String sizeOfCar, int rentalDays)
{
renterName = renterName;
renterZip = renterZip;
sizeOfCar = sizeOfCar;
rentalDays = rentalDays;
}
public void setDailyRentalFee(double dailyRentalFee)
{
this.dailyRentalFee = dailyRentalFee;
}
public double getDailyRentalFee()
{
return dailyRentalFee;
}
public void display(){
if(sizeOfCar.equalsIgnoreCase("economy"))
{
dailyRentalFee = 29.99;
}
else if(sizeOfCar.equalsIgnoreCase("midsize"))
{
dailyRentalFee = 38.99;
} else {
dailyRentalFee = 43.50;
}
//calculates total rental fee
this.totalRentalFee = this.dailyRentalFee * rentalDays;
DecimalFormat df = new DecimalFormat("0.00");
//displays output
System.out.println("Car Rental - Renter Name : " + renterName + ", Renter Zip: " + renterZip +
", Size of car: " + sizeOfCar + ", Rental Days : " + rentalDays +
", Daily Rental Fee: " + dailyRentalFee + ", Total Rental Fee: " + df.format(totalRentalFee));
}
public String getRenterName()
{
return renterName;
}
public int getRenterZip()
{
return renterZip;
}
public int getRentalDays()
{
return rentalDays;
}
}
super.dailyRentalFee = 79.99;
This doesn't work. The same goes for every other place you've used it.
I assume your class has a private field dailyRentalFee?
Make it protected instead. Or use public/protected getters and setters.
You're in a subclass which you should really view as an extension of the superclass. Everything that is available in the superclass is available in the subclass, provided you don't use private access but rather protected (available in current class and subclass) or public (available everywhere that has access to the current class).
Example:
class SuperClass {
protected int someValue = 5;
private int anotherValue = 10;
}
class SubClass extends SuperClass {
public void doSomething() {
someValue = 6; // I can access it because it's protected instead of private
anotherValue = 1; // I can't access it because it's private and only accessible in the SuperClass
}
}
To summarize:
Drop the super.X, super() is used to call the constructor of the superclass
Use protected or public access identifiers instead of private
First thing, super doesn't work the way you have used it.
when you extend a class, CarRental in your case, you inherite all public and protected members of that class. so to use a variable of your super class, you dont have to prefix super, you can just use the variable as if the child class holds it. so instead of
super.dailyRentalFee = 79.99;
use
dailyRentalFee = 79.99; // since dailyRentalFee is protected in class CarRental
// this will work
similarly,
super.totalRentalFee = super.dailyRentalFee * super.getRentalDays() + this.dailyChauffeurFee * super.getRentalDays();
should be written as
totalRentalFee = dailyRentalFee * getRentalDays() + this.dailyChauffeurFee * getRentalDays();
provided, the method getRentalDays is public in the CarRental class.
and about the error you mentioned in #Jeroen's answers' comments,
make sure LuxuryCarRental and CarRental are in the same package. To be simple,
make sure both files are in the same folder.
EDIT:
Your code doesn't contain a main method, that is why that error is produced. You should have a main method in your program for it to execute. That is the starting point of all java applications. so define a class with a main method inside and then create a LuxuryCarRental object and perform your computations there. for example,
class Sample {
public static void main(String[] args) { //this is how all main methods would look
LuxuryCarRental luxuryCar = new LuxuryCarRental("A",62020,"SUV",10,true);
//call the LuxuryCarRental methods as per your coding requirements
}
}
see, its simpe,
class CarRental {
//CarRental code
}
class LuxuryCarRental {
//LuxuryCarRental code
}
class Test {
public static void main(String[] args) {
LuxuryCarRental luxuryCar = new LuxuryCarRental("A",62020,"SUV",10,true);
luxuryCar.display();
}
}
Im trying to sort my planes by Ascending and Descending order. I have a hashmap of planes and i want to compare them so that i can get the next plane due and last plane due by sorting the map by timeLimitBeforeLand. I wrote a compareTo method which looks like :
//---------------------------------------------------------------------------------------
// CompareTo() used with the Comparable implementation.
//---------------------------------------------------------------------------------------
public int compareTo(Object arg0)
{
if((arg0 != null) && (arg0 instanceof Plane))
{
Plane p = (Plane) arg0;
return (int)Math.ceil(this.timeLimitBeforeLand - p.getLimitBeforeLand());
}
return 0;
}
CompareTo takes timeLimitBeforeLand:
// ---------------------------------------------------------------------------------------
// Name: getTimeLimitBeforeLand.
// Description: Get the time before every plane is going to land.
//---------------------------------------------------------------------------------------
public double getTimeLimitBeforeLand()
{
double fuelConsumption;
double timeLimitBeforeLand = 0;
for (TreeMap<String, Plane> theEntry : airlineMap.values()) {
for (Plane aPlane : theEntry.values()) {
if (aPlane.getPlaneType() == aPlane.getPlaneType().AIRBUS) {
System.out.println(" ");
System.out.println(aPlane);
fuelConsumption = 2;
timeLimitBeforeLand = (double) (aPlane.getFuelRemaining() / fuelConsumption);
System.out.println(timeLimitBeforeLand + " minutes to land.");
System.out.println(" ");
} else if (aPlane.getPlaneType() == aPlane.getPlaneType().CORPORATE) {
System.out.println(" ");
System.out.println(aPlane);
fuelConsumption = 3;
timeLimitBeforeLand = (aPlane.getFuelRemaining() / fuelConsumption);
System.out.println(timeLimitBeforeLand + " minutes to land.");
System.out.println(" ");
} else if (aPlane.getPlaneType() == aPlane.getPlaneType().PRIVATE) {
System.out.println(" ");
System.out.println(aPlane);
fuelConsumption = 4;
timeLimitBeforeLand = (double) (aPlane.getFuelRemaining() / fuelConsumption);
System.out.println(timeLimitBeforeLand + " minutes to land.");
System.out.println(" ");
}
}
}
return timeLimitBeforeLand;
}
My attempt so far in the mainApp:
TreeMap<String, PlaneStore> map = new TreeMap<String, PlaneStore>();
ArrayList<Plane> copyList = new ArrayList<Plane>(map.);
Plane comp = new Plane();
Collections.sort(copyList, plane);
Plane Class:
//---------------------------------------------------------------------------------------
// Name: Imports.
// Description: To allow the use of different Java classes.
//---------------------------------------------------------------------------------------
import java.io.Serializable;
//---------------------------------------------------------------------------------------
//Name: Class declaration.
//---------------------------------------------------------------------------------------
public class Plane implements Comparable, Serializable
{
//---------------------------------------------------------------------------------------
// Variable declarations.
//---------------------------------------------------------------------------------------
private String flightNumber;
public String airlineName;
private double fuelRemaining;
private int overdue;
private int passengerNumber;
//---------------------------------------------------------------------------------------
// Enum declaration.
//---------------------------------------------------------------------------------------
private AIRPLANETYPE planeType;
private boolean isLanded = false;
public double timeLimitBeforeLand;
//---------------------------------------------------------------------------------------
// Enum Constuctor.
//---------------------------------------------------------------------------------------
public enum AIRPLANETYPE
{
AIRBUS("1"), CORPORATE("2"), PRIVATE("3");
private String planeName;
private AIRPLANETYPE(String planeName)
{
this.planeName = planeName;
}
public String getPlaneName()
{
return this.planeName;
}
}
//---------------------------------------------------------------------------------------
// Constructor.
//---------------------------------------------------------------------------------------
public Plane(String flightNumber, String airlineName,
double fuelRemaining, int overdue, int passengerNumber,
AIRPLANETYPE planeType, boolean isLanded)
{
this.flightNumber = flightNumber;
this.airlineName = airlineName;
this.fuelRemaining = fuelRemaining;
this.passengerNumber = passengerNumber;
this.overdue = overdue;
this.planeType = planeType;
this.isLanded = isLanded;
}
//---------------------------------------------------------------------------------------
// Getters and Setters.
//---------------------------------------------------------------------------------------
public String getAirlineName()
{
return airlineName;
}
public void setAirlineName(String airlineName)
{
this.airlineName = airlineName;
}
public void setOverdue(int overdue)
{
this.overdue = overdue;
}
public int getOverdue()
{
return overdue;
}
public String getFlightNumber()
{
return flightNumber;
}
public void setFlightNumber(String flightNumber)
{
this.flightNumber = flightNumber;
}
public double getFuelRemaining()
{
return fuelRemaining;
}
public void setFuelRemaining(double fuelRemaining)
{
this.fuelRemaining = fuelRemaining;
}
public int getPassengerNumber()
{
return passengerNumber;
}
public void setPassengerNumber(int passengerNumber)
{
this.passengerNumber = passengerNumber;
}
public AIRPLANETYPE getPlaneType()
{
return planeType;
}
public void setPlaneType(AIRPLANETYPE planeType)
{
this.planeType = planeType;
}
public boolean isLanded()
{
return isLanded;
}
public void setLanded(boolean isLanded)
{
this.isLanded = isLanded;
}
public double getLimitBeforeLand()
{
return timeLimitBeforeLand;
}
public void setTimeLimitBeforeLand(double timeLimitBeforeLand)
{
this.timeLimitBeforeLand = timeLimitBeforeLand;
}
//---------------------------------------------------------------------------------------
// CompareTo() used with the Comparable implementation.
//---------------------------------------------------------------------------------------
public int compareTo(Object arg0)
{
if((arg0 != null) && (arg0 instanceof Plane))
{
Plane p = (Plane) arg0;
return (int)Math.ceil(this.timeLimitBeforeLand - p.getLimitBeforeLand());
}
return 0;
}
//---------------------------------------------------------------------------------------
// toString().
//---------------------------------------------------------------------------------------
public String toString()
{
return "Plane: flightNumber=" + flightNumber + "."
+ " airlineName=" + airlineName + "."
+ " fuelRemaining=" + fuelRemaining + " litres."
+ " overdue=" + overdue + " minutes."
+ " passengerNumber="+ passengerNumber + "."
+ " airplaneType=" + planeType +
"hasLanded=" + isLanded+ ".\n";
}
}
The second argument in Collections.sort is for a Comparator not a Plane. Since I saw no mention of a Comparator, you should be able to use the natural order (defined by the compareTo method in your Plane object) and not have a second argument in the Collections.sort
EDIT: Unless you have just excluded that code, you aren't creating any Plane instances and you're using empty collections here...
TreeMap<String, PlaneStore> map = new TreeMap<String, PlaneStore>();
ArrayList<Plane> copyList = new ArrayList<Plane>(map.);
and you will be sorting by PlaneStores so you have to obtain all the Planes in each PlaneStore and add them to your copyList before sorting.
I would consider researching each of the Collections a little more and deciding what the best one for your need would be.