Im having a class Woning (house) and a subclass KoopWoning (buyable House) and a subclass HuurWoning (rentable House). KoopWoning and Huurwoning extend Woning. HuurWoning is just a Woning, whereas KoopWoning has an extra variable energylevel. KoopWoning has also a function getEnergylevel, which returns the energylevel of the KoopWoning. I also have a class Portefeuille which has an arraylist of Woningen.
Im reading all Woningen in a Portefeuille from a textfile. In a 5th class, I want to be able to sort the ArrayList of Woningen of Portefeuille (from the textfile). I have a function woningenTot(int maxprijs) which returns an ArrayList with all the Woningen that fullfil the requirement (having a price below maxprijs). These Woningen I want to print on the screen.
The problem is as follows:
It can be possible that there is also a KoopWoning in the file. In that case I also want to be able to sort on energylevel. However, I can't sort on the energylevels. I can't call the function getEnergylevel because it's an ArrayList, and Woning doesn't contain the function getEnergylevel.
So how can I solve this? If it's too vague, I could include the code, however it's quite big :O
Any help is appreciated; i have spent a couple of hours on this program, from which at least 1.5 hours on this problem alone :(
EDIT: Here is the code for class KoopWoning
public class KoopWoning extends Woning implements EnergiepeilWoning {
private char energiepeil;
public KoopWoning (Adres adres, int kamers, int vraagPrijs, char energiepeil) {
super(adres, kamers, vraagPrijs);
this.energiepeil = energiepeil;
}
public char getEnergiepeil () {
return energiepeil;
}
public boolean compareEnergiepeil (Object other) {
boolean res = false;
if (other instanceof KoopWoning) {
KoopWoning that = (KoopWoning) other;
res = (this.getEnergiepeil() == that.getEnergiepeil());
}
return res;
}
public String toString () {
String res = adres + ", " + kamers + " kamers, prijs " + prijs + ", energiepeil " + energiepeil;
return res;
}
And here is the code for class Woning
public class Woning {
protected int kamers;
protected int prijs;
protected Adres adres;
protected String tag;
public Woning (Adres adres, int kamers, int prijs) {
this.adres = adres;
this.kamers = kamers;
this.prijs = prijs;
}
public String toString () {
String res = adres + ", " + kamers + " kamers, prijs " + prijs;
return res;
}
public void setTag (String tag) {
this.tag = tag;
}
public String getTag () {
return tag;
}
public boolean kostHooguit (int maxprijs) {
return (prijs <= maxprijs);
}
public boolean equals (Object other) {
boolean res = false;
if (other instanceof Woning) {
Woning that = (Woning) other;
if (this.adres.equals(that.adres))
res = true;
}
return res;
}
public static Woning read (Scanner sc) {
try {
Adres adress = Adres.read(sc);
int kamer = sc.nextInt();
sc.next();
sc.next();
int prijs = sc.nextInt();
String check = sc.next();
if (check.equals("energiepeil")) {
char peil = sc.next().charAt(0);
KoopWoning kwoning = new KoopWoning (adress, kamer, prijs, peil);
return kwoning;
}
else {
Woning woning = new Woning (adress, kamer, prijs);
return woning;
}
}
catch (Exception e) {
System.out.println("Woning: Exception is caught");
System.out.println(e.getMessage());
Adres adress = new Adres ("", "", "", "");
Woning woning = new Woning (adress, 0, 0);
return woning;
}
}
}
And lastly, the code for the class Portefeuille
public class Portefeuille {
private ArrayList<Woning> woninglijst;
public Portefeuille () {
woninglijst = new ArrayList<Woning>();
}
public void voegToe (Woning woning) {
if (!woninglijst.contains(woning))
woninglijst.add(woning);
}
public ArrayList<Woning> woningenTot (int maxprijs) {
ArrayList<Woning> woninglijst2 = new ArrayList<Woning>();
for (int i = 0; i < woninglijst.size(); i++) {
if(woninglijst.get(i).kostHooguit(maxprijs))
woninglijst2.add(woninglijst.get(i));
}
return woninglijst2;
}
public String toStringExt () {
String res = "[";
for (int i = 0; i < woninglijst.size(); i++)
res = res + woninglijst.get(i).toString() + "; ";
if (woninglijst.size() != 0)
res = res.substring (0, res.length() - 2);
res = res + "]";
return res;
}
public String toString () {
String res = "";
for (int i = 0; i < woninglijst.size(); i++)
res = woninglijst.get(i).toString2();
return res;
}
public boolean equals (Object other) {
boolean res = false;
if (other instanceof Portefeuille) {
Portefeuille that = (Portefeuille) other;
if (this.woninglijst.size() == that.woninglijst.size()) {
int i = 0;
while (i < this.woninglijst.size() && this.woninglijst.get(i).equals(that.woninglijst.get(i)))
i = i + 1;
res = (i == this.woninglijst.size());
}
}
return res;
}
public static Portefeuille read (String infile) {
try {
Scanner sc = new Scanner (new File(infile));
ArrayList<Woning> wlijst = new ArrayList<Woning>();
Portefeuille p = new Portefeuille();
int woningen = sc.nextInt();
int i = 0;
while (i < woningen) {
sc.nextLine();
String tag = sc.nextLine();
wlijst.add(Woning.read(sc));
p.voegToe(wlijst.get(i));
i++;
}
sc.close();
return p;
}
catch (Exception e) {
System.out.println("Portefeuille: Exception is caught");
Portefeuille p = new Portefeuille();
return p;
}
}
}
EDIT
I fixed it myself. Thanks for answering you all :)
You could define, on the top-level class, a method like getSortableValue(), and implement it to return a default field (you didn't mention the field you need to sort on for Woningen). In the KoopWoning you override this method to return the energyLevel instead. Then you always sort on the value returned by getSortableValue().
You can let the them implement Comparable, so like Woning implements Comparable<Woning>. This will let you implement the (required) method:
#override
public int compareTo(Woning other) {
int result = Integer.compareto(maxPrijs, other.maxPrijs);
if (result != 0) return result;
result = Integer.compareto(someField, other.someField);
if (result != 0) return result;
// etc...
return 0;
}
The subclass KoopWoning extends Woning implements Comparable<KoopWoning> can have a method like this:
#override
public int compareTo(KoopWoning other) {
int result = Integer.compareto(energylevel, other.energylevel);
if (result != 0) return result;
return super.compareTo(other);
}
Then all you need to do is load all the Woning instances in a list and execute
Collections.sort(list);
Having subclasses inherit Comparable is optional, so HuurWoning will just sort like Woning.
You could define a Comparator on Woning that determines the relative ordering of two Woning. You could do this either by having a method that looks at the actual types of the two arguments and then acts appropriately, or, better, by having an overrideable method of Woning that returns some value that you can use for sorting purposes.
If, for instance, you decide that anything with an energy level should come after anything without one, then you can have KoopWoning return something with the energy level in the high order bits of a long, so that it always comes out higher than anything without one (essentially you'd be setting a default energy level of zero).
Then, you can use
Collections.sort(arrayList, myComparator);
to sort the list based on the Comparator you've created.
There are some nice classes in the Guava library that help with Comparator building on multiple keys, but if your case is fairly simple, you probably won't need them.
Related
I have a class, lets say CargoShip, which is a derived class of 'Starcraft', which implements the interface IStarcraft.
I have a function public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet)
Question: The CargoShip has toString which prints name, commissionYear, etc..
I want to do two things: First, I want to use each Ship's toString (like the one in the CargoShip), and second I want it to be sorted by CommissionYear.
Problem: I don't know how to access the commissionYear field after I've added the toString to the arrayList.
ArrayList<String> strCommissions = new ArrayList<String>();
for(ISpacecraft flee : fleet)
{
strCommissions.add(flee.toString());
}
//Collections.sort(//What to write here??//);
return strCommissions;
}
Here is the CargoShip class if you need it:
package starfleet;
public class CargoShip extends Spacecraft{
private int numberOfSpaceCranes;
static int count = 0;
public CargoShip(String name, int commissionYear, float maximalSpeed,int cargoCapacity, int numberOfSpaceCranes)
{
this.name = name;
this.commissionYear = commissionYear;
if(MaximalSpeed())
this.maximalSpeed = maximalSpeed;
this.cargoCapacity = cargoCapacity;
this.numberOfSpaceCranes = numberOfSpaceCranes;
count++;
}
public int getNumberOfSpaceCranes ()
{
return this.numberOfSpaceCranes;
}
#Override
public String getName() {
return this.name;
}
#Override
public int getCommissionYear() {
return this.commissionYear;
}
#Override
public float getMaximalSpeed() {
if(MaximalSpeed())
return this.maximalSpeed;
else
return 0f;
}
#Override
public int getCargoCapacity() {
return this.cargoCapacity;
}
#Override
public int getFirePower() {
return this.firePower;
}
#Override
public int getAnnualMaintenanceCost() {
int cost = 0;
this.commissionYear = 2000;
cost += getCommissionYear();
cost += (500 * this.numberOfSpaceCranes);
cost += (2 * getCargoCapacity()); //To check: TotalCargoWeightCapacity?
// TODO Auto-generated method stub
return 0;
}
public String toString()
{
return
"Name = " + getName() + System.lineSeparator() +
"CommissionYear = " + getCommissionYear() + System.lineSeparator() +
"MaximalSpeed = " + getMaximalSpeed() + System.lineSeparator() +
"CargoCapacity = " + getCargoCapacity() + System.lineSeparator() +
"FirePower = " + getFirePower() + System.lineSeparator() +
"AnnualMaintenanceCost = " + getAnnualMaintenanceCost() + System.lineSeparator() +
"numberOfSpaceCranes = " + getNumberOfSpaceCranes() + System.lineSeparator();
}
}
First, you can copy the list fleet into a new one:
ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
then you can sort it by commission year:
Collections.sort(objects, Comparator.comparingInt(ISpacecraft::getCommissionYear));
then create the list of Strings:
ArrayList<String> strCommissions = new ArrayList<String>();
objects.forEach(o -> strCommissions.add(o.toString()));
thus your function becomes:
public static ArrayList<String> getSpacecraftDescriptionsByCommissionYear(ArrayList<ISpacecraft> fleet){
ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
Collections.sort(objects, Comparator.comparingInt(ISpacecraft::getCommissionYear));
ArrayList<String> strCommissions = new ArrayList<String>();
objects.forEach(o -> strCommissions.add(o.toString()));
return strCommissions;
}
reading:
Java 8 Lambda : Comparator example
You should implement Comparable and implement compareTo method or as mentioned above Comparator can be used.
public int compareTo(CargoShip otherCargoShip) {
int i = Name .compareTo(other.Name );
if (i != 0) return i;
i = CommissionYear.compareTo(other.CommissionYear);
if (i != 0) return i;
return Integer.compare(MaximalSpeed , other.MaximalSpeed );
}
According to an exercise for school I am supposed to println the int variable 'aantalWoorden' and the Arraylist 'woorden' after they are updated in the 'woordenNaarLijst' method. But whenever I do that, I get the values from the default constructor (Because they are updated inside a method). The method is supposed to be void, so I can't return the values. How do I get the values of the int and array as they are defined in the 'woordenNaarLijst' method without changing the method itself.
I am sorry if this is a easy question or if I am doing something wrong, but I am relatively new to programming.
public class AnalyseZin {
private String zin;
int aantalWoorden;
int i1 = 0, i2 = 0;
private ArrayList<String> woorden;
public AnalyseZin() {
woorden = new ArrayList<>();
aantalWoorden = 0;
}
public int getIndex(int i) {
char c;
for (; i < zin.length(); i++) {
c = zin.charAt(i);
if (c == '\n' || c == '.' || c == ',' || c == ' ')
return i;
}
return i;
}
public String getWoord() {
i2 = getIndex(i1);
StringBuilder sb = new StringBuilder();
for (; i1 < i2; i1++) {
sb.append(zin.charAt(i1));
}
return sb.toString();
}
public void woordenNaarLijst() {
String s;
while (true) {
s = getWoord();
if (s.length()==0) break;
woorden.add(s);
aantalWoorden++;
}
}
}
I left out irrelevant pieces of code, the code does work at my end.
Maybe if you just want to get the values of aantalWoorden and woorden regardless if it has been properly updated or not, you might want to create getters for those.
Example:
// Additional getter methods in AnalyseZin
public int getAantalWoorden(){
return aantalWoorden;
}
public ArrayList<String> getWoorden(){
return woorden;
}
So if you create the AnalyseZin object you can do this:
// create variables for the container of values from the AnalyseZin
int newIntVal = 0;
ArrayList<String> newListVal = new ArrayList<String>();
// create instance of the object
AnalyseZin test = new AnalyseZin();
// call woordenNaarLijst method to update the woorden and aantalWoorden
test.woordenNaarLijst();
// pass values from AnalyseZin
newIntVal = test.getAantalWoorden
newListVal = test.getWoorden();
// print out int values
System.out.println("new int value = " + Integer.toString(newIntVal));
// print out list contents
for (String item : newListVal){
System.out.println("item from list = " + item);
}
Hope this helps.
I have an object class
public class Film implements Comparable<Film>
I'm using Eclipse and would like to know why Film is underlined in red with the error saying:
The type Film must implement the inherited abstract method Comparable<Film>.compareTo<Film>
And now to my main question:
How would I get the max/min user submitted film length and title?
My object class Film has getter and setter methods for the Title of the film and the Length of the film and a toString method. Following this article (#3) I created two more methods in my object class:
public int max(Film maxLength){
int compareLength = ((Film) maxLength).getLength();
return this.length - compareLength;
}
public int min(Film minLength){
int compareLength = ((Film) minLength).getLength();
return compareLength - this.length;
}
Could I use these to find and print max/min values of the user submitted film lengths?
If so, how?
If not, what is the proper way of doing this?
The test class is as follows:
import java.util.Scanner;
public class test {
public static void main (String[] args){
Film[] f = new Film[3];
Scanner input = new Scanner(System.in);
for (int i=0;i<3;i++){
f[i] = new Film();
System.out.println("Enter Film Length:");
f[i].setLength(input.nextInt());
input.nextLine();
System.out.println("Enter Title:");
f[i].setTitle(input.nextLine());
}
input.close();
for (int i = 0; i < 3; i++) {
System.out.println(f[i].toString());
}
}
}
The Film class implements Comparable<Film>. What this means is that you must implement a method called compareTo() in class Film that will provide an ordering for objects of this class.
#Override
public int compareTo(Film that) {
// Order by film length
return Integer.compare(this.length, that.length);
}
If you only need to sort the objects by film length you can just use Arrays.sort():
Film[] films = new Film[3];
// put the objects into the array
Arrays.sort(films);
Then films[0] will contain the film with the shortest length, while the last element will be the film with the longest length.
If you need to compare by other fields, such as film title, you can create a custom comparator:
class FilmTitleComparator implements Comparator<Film> {
public int compare(Film a, Film b) {
return Integer.compare(a.getTitle().length(), b.getTitle().length());
}
}
And pass it to Arrays.sort()
FilmTitleComparator titleComparator = new FilmTitleComparator();
Arrays.sort(films, titleComparator);
Then films[0] will contain the film with the shortest title, while the last element will be the film with the longest title.
For simplicity, I stubbed your Film class to show a trivial example of how to implement Comparable
public class Film implements Comparable<Film> {
int maxLength;
int minLength;
String title;
public Film() {
this.maxLength = 0;
this.minLength = 0;
this.title = "";
}
// implement this method to accomplish comparison
public int compareTo(Film f) {
int result = 0; // the result to compute.
if ( this.equals(f) ) {
result = 0; // these objects are actually equal
}
// compare using meaningful data
else if ( f != null) {
// check to see if this film is greater than the specified film
if ( this.getMaxLength() > f.getMaxLength() ) {
// this film is comparatively greater, return > 0
result = 1;
}
else if ( this.getMaxLength() == f.getMaxLength() ) {
// these two films are comparatively equal
result = 0;
}
else {
// this film is comparatively less than the specified film
result = -1;
}
// similarly, you could also check min, but there's really no reason to do that unless your implementation calls for it.
}
else {
throw new IllegalArgumentException("null Film object not allowed here...");
}
return result;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Film film = (Film) o;
if (maxLength != film.maxLength) return false;
if (minLength != film.minLength) return false;
if (!title.equals(film.title)) return false;
return true;
}
#Override
public int hashCode() {
int result = maxLength;
result = 31 * result + minLength;
result = 31 * result + title.hashCode();
return result;
}
public int getMaxLength() {
return maxLength;
}
public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}
public int getMinLength() {
return minLength;
}
public void setMinLength(int minLength) {
this.minLength = minLength;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
To fix your test to actually use such an implementation (it doesn't really test anything...), you could do:
import java.util.Scanner;
public class test {
public static void main (String[] args){
Film lastFilm = null; // arbitrary reference to film
Film[] f = new Film[3];
Scanner input = new Scanner(System.in);
for (int i=0;i<3;i++){
f[i] = new Film();
System.out.println("Enter Film Length:");
f[i].setLength(input.nextInt());
input.nextLine();
System.out.println("Enter Title:");
f[i].setTitle(input.nextLine());
}
input.close();
for (int i = 0; i < 3; i++) {
if ( lastFilm != null ) {
// compare the films to test. current to last film
if ( f[i].compareTo(lastFilm) > 0 ) {
System.out.println(f[i].getTitle() + " is greater than " + lastFilm.getTitle()");
}
else if ( f[i].compareTo(lastFilm) < 0 ) {
System.out.println(f[i].getTitle() + " is less than " + lastFilm.getTitle()");
}
else {
System.out.println(f[i].getTitle() + " is equal to " + lastFilm.getTitle()");
}
}
System.out.println(f[i].toString());
lastFilm = f[i];
}
}
}
Something like this can get you started... good luck
Another solution would be to implement Comparable<Film>:
#Override
public int compareTo(Film that) {
return this.length - that.length;
}
And use org.apache.commons.lang3.ObjectUtils#min or org.apache.commons.lang3.ObjectUtils#max like:
Film min = ObjectUtils.min(film1, film2);
Film max = ObjectUtils.max(film1, film2);
I am having some difficulties getting my custom equation evaluator to work. I pass it a string read from a text file (no spaces except between string words) as equation as well as passing it a map of keywords which link to the values they represent. I have tested that and all of my maps are working properly. Below is my attempt to handle the result regardless of it is an int or a string. These will be the only two allowed entry types. Each side of the equation can have one or two elements to it, separated by either a plus or a minus. The only three operators allowed to evaluate the two sides are <,>,=. Sides are restricted to either having only keywords or only integers, so you can't have something like dexterity + 1 = strength + 2.
The error I am currently getting when I try to compile this class is "no suitable method found for parseint" "method Integer.parseInt(String,int) is not applicable". If I am not mistaken since I am compiling this class directly and not the main class it wouldn't even have the map to make that kind of judgement call. Is this a problem? I am compiling in this way because I have been having issues where recompiling the main class did not recompile secondary class files causing problems.
Any example equation: dexterity>3 or background=Ex Legionary
import java.lang.String;
import java.util.HashMap;
import java.util.Map;
public class Equation {
private String[] sides = new String[2];
private String[] rawEquation = new String[3];
private String[] parts = new String[2];
private String type;
private int[] tempInt = new int[2];
private int[] finalSide = new int[2];
private String[] finalStride = new String[2];
public boolean solve(String equation, Map gladMap) {
if (equation.indexOf("<") > -1) {
sides = equation.split("<");
rawEquation[1] = "<";
} else if (equation.indexOf(">") > -1) {
sides = equation.split(">");
rawEquation[1] = ">";
} else if (equation.indexOf("=") > -1) {
sides = equation.split("=");
rawEquation[1] = "=";
}
rawEquation[0] = sides[0];
rawEquation[2] = sides[1];
for (int d = 0; d < 2; d++) {
if (sides[d].indexOf("+") > -1) {
parts = rawEquation[0].split("\\+");
for (int a = 0; a < 2; a++) {
if (isInteger(parts[a])){
tempInt[a] = Integer.parseInt(parts[a]);
} else {
tempInt[a] = Integer.parseInt(gladMap.get(parts[a]));
}
}
finalSide[d] = tempInt[0]+tempInt[1];
type = "Int";
} else if (rawEquation[0].indexOf("-") > -1) {
parts = rawEquation[0].split("\\-");
for (int a = 0; a < 2; a++) {
if (isInteger(parts[a])){
tempInt[a] = Integer.parseInt(parts[a]);
} else {
tempInt[a] = Integer.parseInt(gladMap.get(parts[a]));
}
}
finalSide[d] = tempInt[0]-tempInt[1];
type = "Int";
} else {
if (isInteger(sides[0])){
finalSide[d] = Integer.parseInt(sides[0]);
} else {
if (isInteger(gladMap.get(sides[0]))) {
finalSide[d] = Integer.parseInt(gladMap.get(sides[0]));
type = "Int";
} else {
finalStride[d] = gladMap.get(sides[0]);
type = "Str";
}
}
}
}
if (rawEquation[1].equals("<")) {
if (type.equals("Int")) {
if (finalSide[0] < finalSide[1]) {
return true;
}
}
} else if (rawEquation[1].equals(">")) {
if (type.equals("Int")) {
if (finalSide[0] > finalSide[1]) {
return true;
}
}
} else {
if (type.equals("Int")) {
if (finalSide[0] == finalSide[1]) {
return true;
}
} else if (type.equals("Str")) {
if (finalStride[0].equals(finalStride[1])) {
return true;
}
}
}
return false;
}
public boolean isInteger( String input ) {
try {
Integer.parseInt( input );
return true;
}
catch( Exception NumberFormatException ) {
return false;
}
}
}
I tried to separate the Integer.parseInt() from the gladMap.get(sides[0]) by creating a temporary string variable, but it didn't change anything. Any help would be appreciated!
Here, the map which you are passing is not with the generic types. Hence, get() will always return an object, which is not an appropriate argument for parseInt() method.
Changing the method signature to
public boolean solve(String equation, Map< String ,String > gladMap) {
should solve the errors.
The problem might be following: your map is untyped so calls like gladMap.get(sides[0]) return Object. Integer.parseInt expects String. You can change it to
gladMap.get(sides[0]).toString().
It think it should work. If value is actual String then toString will return itself, it it is Integer it will be converted to string and parsed back.
I am making a program for airplane seating arrangements for a class and i ended up making two toString methods but when I run the program the toString method in my airplane class is making something not work specifically:
str= str + seats[i][j].toString();
I believe that simply deleting the toString method in the seat class and somehow putting it back into the airplane class toString method would fix the problem or make it simpler. What's wrong?
Airplane class:
public class Airplane
{
private Seat [ ] [ ] seats;
public static final int FIRST_CLASS = 1;
public static final int ECONOMY = 2;
private static final int FC_ROWS = 5;
private static final int FC_COLS = 4;
private static final int ECONOMY_ROWS = 5;
private static final int ECONOMY_COLS = 6;
public Airplane()
{
seats = new Seat[FC_ROWS][ECONOMY_COLS];
}
public String toString()
{
String str = "";
for (int i=0; i<FC_ROWS; i++) {
for (int j=0; j<ECONOMY_COLS; j++)
{
str= str + seats[i][j].toString();
}
str = str + "\n";
}
return str;
}
}
Seat Class:
public class Seat
{
private int seatType;
private boolean isReserved;
public static final int WINDOW = 1;
public static final int AISLE = 2;
public static final int CENTER = 3;
public Seat(int inSeatType)
{
seatType = inSeatType;
isReserved = false;
}
public int getSeatType()
{
return seatType;
}
public void reserveSeat()
{
isReserved = true;
}
public boolean isAvailable()
{
if (!isReserved)
{
return true;
}
else return false;
}
public String toString()
{
if(isReserved == false)
{
return "*";
}
else return "";
}
}
In Seat.toString you should print a " " not "".
You're array is FC_ROWS by ECONOMY_COLS, so you're not creating all the seats. You should probably have two arrays (one for FC, one for Economy), since FC_ROWS != ECONOMY_ROWS.
You aren't actually creating Seats in your constructor. Use a nested loop to create them, otherwise you will get a NullPointerException. Creating an array doesn't create the objects contained in the array.
When you're creating the seats in the Airplane constructor, use if statements to figure out if the seat is supposed to be a Window, Aisle, etc.
seats seems to does not have Seat's instance.
Add this code :
for (int i=0; i<FC_ROWS; i++) {
for (int j=0; j<ECONOMY_COLS; j++)
{
seats[i][j] = new Seat();
}
}
below this :
seats = new Seat[FC_ROWS][ECONOMY_COLS];
I think that in Seat::toString, you mean to return " " (a space) if it isn't reserved.