KingHarry
KingHarry

Reputation: 11

Getting Variables From Java constructor

I'm new to Java programming, sorry if this is a dumb question.

I find it hard to word this question properly, but I have an assignment to create a aircraft class that can make aircraft land, takeoff etc. And need to test it using Testclass. When the new object are entered it automatically assigns a unique ID to the aircraft in the constructor.

I can do this using a instance method fine as it has a return value which is returned to to Testclass. The question wants me to do this in the constructor itself, however, the constructor never returns anything. So the variable never gets sent to the Testclass. I clearly am not understanding OOP properly. Even when I try to just use a getter method to get the ID created in the constructor it gives me the initialized variable before the the constructor has worked on this. This is the code I have so far and its completely wrong I know but if someone could point me in the right direction or tell me how to word this question better it would be a massive help.

// I need to enter 3 aircraft into the system in the testclass

public class Aircraft {

  private int aircraftID;
  private static int lastID;
  private String airportcode;
  private int ID = 100;

  private int count;



  public Aircraft(int a, int b, int c){
    // Constructor


     // Assign ID
     this.ID = a;
     lastID = ID;
     ID++;

     this.ID =b;
     lastID = ID;
     ID++;
  }
}

Upvotes: 0

Views: 274

Answers (2)

Bill Horvath
Bill Horvath

Reputation: 1717

OK, you want to create an Aircraft that has an automatically-assigned unique identifier, and can take off and land. That implies you need a field for tracking the identifier, a field for tracking whether it's in the air (or not), and methods for the take off and land operations. You also need a static field for generating the unique identifiers. (Note that this implementation isn't thread safe.)

private class Aircraft {

    private static int staticId = 0;
    private int uniqueId = 0;
    private boolean onGround = true; // Aircraft start on the ground in this implementation

    public Aircraft(){
        this.uniqueId = staticId; // putting this line first makes uniqueId zero-indexed in effect
        staticId++;
    }

    public void land(){
        onGround = true;
    }

    public void takeoff(){
        onGround = false;
    }

    public boolean isFlying(){
        return !onGround; // If it's not on the ground, it's flying
    }

    public int getUniqueId(){
        return uniqueId;
    }
}

Unit tests checks all of the methods and expected functionality of the class in question:

import org.junit.Test;
import static org.junit.Assert.*;
import Aircraft;

class Testclass {

    private final Aircraft aircraft = new Aircraft();

    @Test
    public void hasId(){
        aircraft.getUniqueId() >= 0;
    }

    @Test
    public void canLand(){
        assertTrue(aircraft.land());
    }

    @Test
    public void canTakeOff(){
        assertTrue(aircraft.takeOff());
    }

    @Test
    public void checkFlightOperationsAreTrackedCorrectly(){
        aircraft.land();
        assertFalse(aircraft.isFlying());
        aircraft.takeOff();
        assertTrue(aircraft.isFlying());
    }
}

Upvotes: 1

Raven221221221
Raven221221221

Reputation: 142

As pointed out a constructor does not return anything (the simplified version is that with new it returns an object instance). I am kinda guessing at what you are trying to acomplish, but I'll have a go anyways. It seems to me that you are trying to cram the construction of 3 objects into one constructor - which is why your constructor has 3 parameters. Also you are playing havoc with the IDs.

I have removed all the variables that I didnt quite understand, leaving only ID that increments with each instantiated Aircraft. The @Override is mainly just for show.

public class Aircraft {
    private int aircraftID;
    private static int lastID = 0;

    @Override
    public String toString(){
        return "Aircraft_" + this.aircraftID;
    }

    public Aircraft() {
        lastID++;
        this.aircraftID = lastID;
    }
}

I took the liberty and wrote the TestClass just to see if we have the same thing in mind. Again the printAircraft() method is for show.

public class TestClass {
    private List<Aircraft> aircrafts;
    public TestClass(){
        aircrafts = new ArrayList<>();
    }

    public void addAircraft(Aircraft a){
        aircrafts.add(a);
    }

    public void printAircraft(){
        Iterator<Aircraft> it = aircrafts.iterator();
        while(it.hasNext()){
            System.out.println(it.next().toString());
        }
    }
}

and to test it, we create and instance of TestClass add 3 Aircraft instances and print out the contents

public static void main(String[] args) {
    TestClass tc = new TestClass();
    tc.addAircraft(new Aircraft());
    tc.addAircraft(new Aircraft());
    tc.addAircraft(new Aircraft());
    tc.printAircraft();
}

This would be the case if you are to write the TestClass. If that is given, it would help to know what it looks like - maybe that would help us understand better.

Upvotes: 0

Related Questions