Giter VIP home page Giter VIP logo

csc120-a4's Introduction

CSC120-A4: Chugging Along

Outline

In this assignment, we'll explore Encapsulation by building a virtual Train!

A cartoon train with an engine and three passenger cars

As before, all the files necessary for this assignment are contained within this repository. When you submit, please remember to include:

  • all files necessary to compile your program
  • reflection.md containing your reflections and notes
  • rubric.md where you document which elements of the assignment you have attempted and/or completed.

Program files

For this assignment, you'll be writing four interrelated classes:

  • The Passenger class (Passenger.java) will store information about an individual passenger
  • The Engine class (Engine.java) will represent the locomotive engine, storing information about its fuel type, level, etc.
  • The Car class (Car.java) will be used as a container for Passenger objects
  • and the Train (Train.java) class will tie them all together

You'll also notice a 5th file in the repository (FuelType.java), which contains something that looks like an extremely simple class:

public enum FuelType {
   STEAM, INTERNAL_COMBUSTION, ELECTRIC, OTHER;
}

In Java, we can use the keyword enum to establish simple type that must take as its value one of a set of predefined constant values. We'll use this in the Engine class instead of a String to keep track of what kind of fuel the Engine uses. You don't need to change this file, but you can use the values it contains like this:

FuelType f = FuelType.ELECTRIC;

Relationships between the Classes

Let's pause a moment to think about the different kinds of relationships we'll want to establish:

  • The Train class will have a relationship with the Engine class, because the Engine is responsible for powering the Train.
  • The Train class also has a relationship with the Car class: the Train has a collection of Cars associated with it at any given time, and you can add / remove Cars as necessary (without destroying either the Train or the Cars themselves).
  • The Passenger class has relationships with both the Car and Train classes (Passengers board Cars as their means of using the Train to move around more efficiently).

Step 1: The Engine class

We recommend you start by implementing the Engine class. Your Engine class will need:

  • a FuelType attribute to indicate what type of fuel it uses, and doubles to store the current and maximum fuel levels (along with appropriate accessors for each).
  • a constructor, which takes in initial values for the attributes named above and sets them appropriately
  • a refuel() method which will reset the Engine's current fuel level to the maximum, and which doesn't need to return anything
  • a go() which will decrease the current fuel level, print some useful information (e.g. remaining fuel level), and return True if the fuel level is above 0 and False otherwise.

Remember, OOP is all about deciding which classes are responsible for which parts of the end solution. As you program, consider which of these attributes/methods should be public, and which should be private. These questions may be helpful to ask yourself:

  • Does another class need to be able to read this value? (If so, it could either be marked public or have an accessor)
  • Does another class need to be able to modify this value? (If so, it could either be marked public or have a manipulator)

You can use the main method defined below as a starting point for testing:

   public static void main(String[] args) {
       Engine myEngine = new Engine(FuelType.ELECTRIC, 100.0);
       while (myEngine.go()) {
           System.out.println("Choo choo!");
       }
       System.out.println("Out of fuel.");
   }

Step 2: the Car class

Next, we'll set to work on the Car class. The Car class will need:

  • an ArrayList where it will store the Passengers currently onboard, and an int for the Car's maximum capacity (since ArrayLists will expand as we add objects, we'll need to manually limit their size)
  • a constructor, which takes in an initial value for the Car's maximum capacity and initializes an appropriately-sized ArrayList
  • accessor-like methods public int getCapacity() and public int seatsRemaining() that return the maximum capacity and remaining seats, respectively
  • addPassenger(Passenger p) and removePassenger(Passenger p) methods to add or remove a Passenger from the Car and return True if the operation was successful, and False otherwise. (Hint: don't forget to check that there are seats available if someone wants to board, and to confirm that the Passenger is actually onboard before trying to remove them! If you encounter a problem, you should return False.)
  • and a final method printManifest() that prints out a list of all Passengers aboard the car (or "This car is EMPTY." if there is no one on board).

As before, consider which of these should be public and which should be private (potentially with accessors and/or manipulators).

Step 3: completing the Passenger class

Now that you've got a functional Car class, the Passenger class can be expanded to use the Car's methods to implement some of its own:

  • boardCar(Car c) can call c.addPassenger(this) to board a given Car (Hint: this method should check the value that gets returned by c.addPassenger(...) in case the selected car is full.)
  • getOffCar(Car c) can call c.removePassenger(this) to get off a given Car (Hint: this method should check the value that gets returned by c.removePassenger(...) in case the Passenger wasn't actually onboard.)

Step 4: the Train class

Now we're in the home stretch! To assemble your Train, you'll need (at minimum):

  • an Engine
  • an ArrayList to keep track of the Cars currently attached
  • a constructor Train(FuelType fuelType, double fuelCapacity, int nCars, int passengerCapacity) which will initialize the Engine and Cars and store them
  • a few accessors:
    • public Engine getEngine()
    • public Car getCar(int i) to return the ith car
    • public int getMaxCapacity() which will return the maximum total capacity across all Cars
    • public int seatsRemaining() which will return the number of remaining open seats across all Cars
  • and finally, its own printManifest() that prints a roster of all Passengers onboard (Hint: ask your Cars to help!)

csc120-a4's People

Contributors

jcrouser avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.