views:

109

answers:

4

This is my assignment about setter and getter and it is not working for some reason. Could anyone check what the problem is for me? Thank you.

public class FlightTest
{
    public static void main (String [] args)
    {

        String name;
        String number;
        String Orig;
        String Desti;

        Scanner scan = new Scanner (System.in);

        Flight data = new Flight ();

        System.out.print ("Airline Name: ");
        String AirlineName = scan.nextLine ();
        data.setAirlineName (name);

        System.out.print ("Flight Number: ");
        String FlightNumber = scan.nextLine ();
        data.setFlightNumber (number);

        System.out.print ("Origin: ");
        String Origin = scan.nextLine();
        data.setOrigin (Orig);

        System.out.print ("Destination: ");
        String Destination = scan.nextLine ();
        data.setDestination (Desti);

        System.out.println (data);
    }   
}





public class Flight

{

    private String AirlineName;

    private String FlightNumber;

    private String Origin;

    private String Destination;

    public String setAirlineName()
    {
        String Name = AirlineName;
        return Name;
    }

    public Flight ()
    {
        AirlineName = "";
        FlightNumber = "";
        Origin = "";
        Destination = "";
    }

    public String getAirlineName()
    {
        return AirlineName;
    }

    public void setAirlineName (String name)
    {
        AirlineName = name;
    }

    public String getFlightNumber ()
    {
        return FlightNumber;
    }

    public void setFlightNumber (String number)
    {
        FlightNumber = number;
    }

    public String getOrigin ()
    {
        return Origin;
    }

    public void setOrigin (String Orig)
    {
        Origin = Orig;
    }

    public String getDestination ()
    {
        return Destination;
    }

    public void setDestination (String Desti)
    {
        Destination = Desti;
    }

    public String toString ()
    {
        String result = AirlineName + " flight number " + FlightNumber + " leaves from " + Origin + " to " + Destination + ".";
        return result;
    }
}
+1  A: 

You have this method:

public String setAirlineName()
{
    String Name = AirlineName;
    return Name;
}

Which conflicts with your actual setter because they are the same name but of different return types, and hence the compiler might be spitting errors, which I assume is what you mean by "not working":

public void setAirlineName (String name)
{
    AirlineName = name;
}

String setAirlineName(void) was probably meant to be String getAirlineName(void), which you have already implemented just as a basic getter is intended to be:

public String getAirlineName()
{
    return AirlineName;
}

As Dante617 has answered, another mistake is that you're using different local variables to read input and ignoring the ones you declared (and didn't initialize) before your Scanner object.

BoltClock
This is my second [homework] answer, however I think it's perfectly fine to be more direct but compensate by offering an explanation of what's happening, since conflicting method signatures can be challenging to track down.
BoltClock
+2  A: 

It looks like you're reading the Scanner newLine() calls into new variables, and then passing in variables that haven't been initialized. For example, I think you want something like this:

System.out.print ("Airline Name: ");
name = scan.nextLine ();
data.setAirlineName (name);

Note that the second line reads the nextLine() into the variable that you're passing into the setter.

Dante617
Note that it's this way for all of individual sections.
Dante617
+3  A: 

Here's the problem:

    String AirlineName = scan.nextLine ();
    data.setAirlineName (name);

You are reading a name and putting it into AirlineName, then calling the setter with a different variable as its argument.

In fact, you should get a compilation error telling you that name is not initialized.

You also have a second (bogus) setAirlineName method as follows:

public String setAirlineName()
{
    String Name = AirlineName;
    return Name;
}

... which is not a proper setter. But it looks like your main method doesn't call it, so that's not the cause of your problems. (You are calling the setAttributeName(String) overload ...)

Finally, please, please, PLEASE, learn to follow standard Java style conventions for identitiers. A variable or attribute name should not start with an uppercase letter. Change your AirlineName attributes and variables to airlineName, and so on.

Stephen C
+1  A: 

You are reading the variable AirlineName but passing a different uninitialized variable name to the setter:

String AirlineName = scan.nextLine ();
data.setAirlineName (name);
                     ^^^^

Same is the case with other 3 variables FlightNumber, Origin and Destination.

codaddict