Google Answers Logo
View Question
 
Q: Java ( No Answer,   2 Comments )
Question  
Subject: Java
Category: Computers > Programming
Asked by: cb153-ga
List Price: $2.00
Posted: 17 Aug 2005 06:26 PDT
Expires: 17 Aug 2005 19:08 PDT
Question ID: 556756
Does anyone know why I am getting a "null" output?
What is the correct code for addRoute() and addAircraft()?
This is what I get when I run Assign1.java:
 
----Hit any key to start.

Route Costs
HNK : $0.00
SNG : $0.00
AUC : $0.00
PER : $0.00

Dingo Air
        Hong Kong, 9.5 hours, 300 passengers
        null
        Singapore, 7.5 hours, 500 passengers
        null
        Auckland, 4.0 hours, 400 passengers
        null
        Perth, 3.5 hours, 270 passengers
        null

Daily profit: $0.00

 ----Hit any key to continue.
This is what the output is supposed to look like:
Output of Assign1.java
Route Costs
HNK : $180,500.00
SNG : $170,250.00
AUC : $84,800.00
PER : $58,800.00
Dingo Air
Adelaide - Hong Kong, 9.5 hours, 300 passengers
Airbus A330, 220
Boeing 737, 150
Adelaide - Singapore, 7.5 hours, 500 passengers
Boeing 747, 350
Boeing 737, 150
Adelaide - Auckland, 4.0 hours, 400 passengers
Airbus A330, 220
Airbus A330, 220
Adelaide - Perth, 3.5 hours, 270 passengers
Boeing 737, 150
Boeing 737, 150
Daily profit: $537,150.00


And this is the code I have done so far (some methods not finished)

import java.text.*;

public class Assign1
{

    public static void main (String [] args)
    {
        Assign1 a1 = new Assign1();
        a1.runAirline();
    }
    
    public void runAirline()
    {
        NumberFormat cf= NumberFormat.getCurrencyInstance();
        
        //--- Create Aircraft objects ---------------
        Aircraft A330 = new Aircraft ("Airbus A330", 220,14,600,4.0);
        Aircraft B747 = new Aircraft ("Boeing 747", 350,20,1000,7.0);
        Aircraft B737 = new Aircraft ("Boeing 737", 150,7,450,2.5);
        Aircraft BAe464 = new Aircraft ("BAe 464", 60,4,300,1.3);
        
        //--- Create Route objects ---------------
        Route HNK = new Route("Hong Kong", 9.5, 300);
        Route SNG = new Route("Singapore", 7.5, 500);
        Route AUC = new Route("Auckland", 4.0, 400);
        Route PER = new Route("Perth", 3.5, 270);
        
        //--- Add Aircraft to Routes ---------------
        HNK.addAircraft(A330);
        HNK.addAircraft(B737);
        SNG.addAircraft(B747);
        SNG.addAircraft(B737);
        AUC.addAircraft(A330);
        AUC.addAircraft(A330);
        PER.addAircraft(B737);
        PER.addAircraft(B737);
        
                
        //--- Display Route costs ---------------
        System.out.println("\nRoute Costs");
        HNK.setFare(1100);
        System.out.println("HNK : "+cf.format(HNK.breakEven()));
        SNG.setFare(800);
        System.out.println("SNG : "+cf.format(SNG.breakEven()));
        AUC.setFare(450);
        System.out.println("AUC : "+cf.format(AUC.breakEven()));
        PER.setFare(450);
        System.out.println("PER : "+cf.format(PER.breakEven()));
        
        //--- Create an Airline ---------------
        Airline da = new Airline("Dingo Air");
        
        //--- Add Routes to Airline ---------------
        da.addRoute(HNK);
        da.addRoute(SNG);
        da.addRoute(AUC);
        da.addRoute(PER);
        
        //--- Display Airline details -------------
        System.out.println("\n"+da);
        
        //--- Display Airline daily profit -------------
        System.out.print("\nDaily profit: ");
        System.out.println(cf.format(da.calcDailyProfit()));
    }   
}       


public class Airline    {
    private String name;
    private final int MAX_ROUTE = 10;
    private int rcount = 0;
    private Route[] routes = new Route[MAX_ROUTE];
    
    public Airline(String name)    {
        this.name = name;
    }
    
    public String getName()   {
        return name;
    }
    
    public void addRoute(Route r)    {
        routes[rcount++] = r;
        if (rcount > MAX_ROUTE)   
            System.out.println("That is too many routes");
    }
    
    public double calcDailyProfit()    {
        double dailyProfit = 0;
            for (int i = 0; i < rcount; i++)
                dailyProfit += routes[i].income()
                - routes[i].breakEven();
            return dailyProfit;
    }
    
    public String toString()    {
        String str = name;   
            for (int i =0; i < rcount; i++) {       
                str += "\n\t" + routes[i];
             }    
        return str;
    }
 }      


public class Aircraft    {
    private String type;
    private int passengers;
    private int crew;
    private double maintCost;
    private double fuelEffic;

    public Aircraft(String type, int passengers, int crew, 
        double maintCost, double fuelEffic)    {
        this.type = type;
        this.passengers = passengers;
        this.crew = crew;
        this.maintCost = maintCost;
        this.fuelEffic = fuelEffic;
    }
    
    public String toString()    {
                   
        return getType() + ", " + getPassengers();
    }
    
    public String getType()    {
        return type;
    }
    
    public int getPassengers()    {
        return passengers;
    }
    
    public int getCrew()    {
        return crew;
    }
    
    public double getMaintCost()    {
        return maintCost;
    }
    
    public double getFuelEffic()    {
        return fuelEffic;
    }

    public double calcRunningCosts()    {
        return (crew * 100) + (fuelEffic * 900) 
               + maintCost + 5000;
    }
}

public class Route    {
    private String destination;
    private double flyTime;
    private int passengerVol;
    private double fare;
    private int acount;
    private final int MAX_AIRCRAFT = 10;
    private Aircraft [] planes = new Aircraft[MAX_AIRCRAFT];
    
    public Route(String destination, double flyTime, int passengerVol)    {
        this.destination = destination;
        this.flyTime = flyTime;
        this.passengerVol = passengerVol;  
    }
    
    public String getDestination()    {
        return destination;
    }
    
    public double getFlyTime()    {
        return flyTime;
    }
    
    public int getPassengerVol()    {
        return passengerVol;
    }
    
    public void setFare(double fare)    {
       this.fare = fare;
    }
    
    public String toString()    {
        return getDestination() + ", " + getFlyTime()+ " hours, " 
               + getPassengerVol() + " passengers" + "\n\t" + planes[acount++];
    }
    
    
    public double breakEven()    {
        return 0;
    } 
    
    public double income()    {
       return 0;
    }
    
    public void addAircraft(Aircraft a)    {
        int acount = 0;
        for (int i = 0; i < acount; i++)
            if (acount != MAX_AIRCRAFT)    {
                planes[acount] = a;
                acount++;
             }
                 else
                 System.out.println("That is too many aircraft");
    }
}
Answer  
There is no answer at this time.

Comments  
Subject: Re: Java
From: rpingol-ga on 17 Aug 2005 13:02 PDT
 
You getting null output because planes[acount++] in your toString()
method of the Route class is null. Your addRoute() method seems to be
OK. But in addAircraft() you probably want to delete the first to line
( I'm not sure what your trying to do there but it seems like it would
work with out the first 2 lines). In the toString() method of class
Route you will probably want to have a String and add each Aircraft in
planes array onto the String using a loop something like:

for( int i=0; i< acount; i++)
{
     /* Add planes[i] onto string */
}
// return the String you have created.

The code for this method should look something like the toString()
method in class Airline.
Subject: Re: Java
From: cb153-ga on 17 Aug 2005 19:08 PDT
 
Yes, thats it. Ive got it working now. Thanks for your help.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy