0% found this document useful (0 votes)
357 views

Unit 5 AP Computer Science Practice Exam

The document describes a Train class that is created with a name and whether it is diesel. The class has fields for name, isDiesel, and numPassengers which is initially 0. The class contains addPassengers and unloadPassengers methods and a constructor. The task is to write the complete Train class based on the specifications provided.

Uploaded by

shandilyaabhijay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
357 views

Unit 5 AP Computer Science Practice Exam

The document describes a Train class that is created with a name and whether it is diesel. The class has fields for name, isDiesel, and numPassengers which is initially 0. The class contains addPassengers and unloadPassengers methods and a constructor. The task is to write the complete Train class based on the specifications provided.

Uploaded by

shandilyaabhijay
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Unit 5 AP Computer Science A Practice Exam

Writing Classes

Section I – Free Response Section


Optional Time – 20 minutes
1 Question

1) This question involves the implementation of a Train class. A Train object is created
with two parameters that specify the name of the train, and if the train is diesel or not.

The Train class has the following fields:

• name, which stores the name of the train.


• isDiesel, which stores whether or not the train has a diesel engine.
• numPassengers, which stores the number of passengers on the train. This
field is initially set to 0.

The Train class has a constructor, demonstrated below:

Train thomas = new Train(“Thomas”, false);

This constructor initializes a Train object with the name of “Thomas” and it not
being having a diesel engine, as well as the number of passengers to 0.

The Train class has the following methods:

• addPassengers(numPassengersToAdd), which adds


numPassengersToAdd to the numPassengers field.
• unloadPassengers(), which sets the numPassengers field to 0.

This practice test was created by Ajay Gandecha.


This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!
Write the complete Train class, including the constructor and any required instance variables
and methods. Your implementation must meet all specifications and conform to the example.

public class Train {

//fields

private String name;


private boolean isDiesel;
private int numPassengers;

//methods

public void addPassengers( int numPassengersToAdd) {


this.numPassengers += numPassengersToAdd;
}

public void unloadPassengers() {


this.numPassengers = 0;
}

//constructor

public Train (String name; boolean isD;) {


this.name = name;
this.isDiesel = isD;
this.numPassengers = 0;
}

END OF SECTION I

This practice test was created by Ajay Gandecha.


This test and I are not affiliated with, or endorsed by, the College Board.
No questions are copied from the College Board and were made on my own for you to prepare.
Good luck!

You might also like