OOPS! Deep Dive 1: Classes, Objects & Constructors
Object-Oriented Programmingbeginnerdotnetjavaoop
Site Admin · 11/12/2026
Build a Parking Lot, part 1 of 5. Code and practice for the video, in Java and C#.
What you'll learn
- Find the classes in a problem: nouns become classes, details become fields, verbs become methods
- Write a class with fields and a constructor, and create objects with new
- Overload a constructor and chain it with this(...)
- Give one object a reference to another (a ParkingSpot holds a Vehicle)
Download
- parking-lot-dd01-java.zip: Java (lesson code, practice starter, solution)
- parking-lot-dd01-csharp.zip: C# (the same)
The code from the video
Java
Vehicle.java
// Deep Dive 1: a class is a blueprint, an object is a thing built from it.
public class Vehicle {
// Fields: what every vehicle knows about itself.
// (Open to everyone for now; Deep Dive 2 locks them down.)
String plate;
String type; // "CAR" or "BIKE"
// Constructor: same name as the class, no return type.
// It runs once, at the moment `new Vehicle(...)` creates the object.
Vehicle(String plate, String type) {
this.plate = plate; // this.plate = the field, plate = the parameter
this.type = type;
}
// Overloaded constructor: only a plate, so assume a car.
Vehicle(String plate) {
this(plate, "CAR"); // hand the work to the other constructor
}
void describe() {
System.out.println(type + " " + plate);
}
}ParkingSpot.java
public class ParkingSpot {
int number;
Vehicle parked; // null while the spot is free
ParkingSpot(int number) {
this.number = number;
}
boolean isFree() {
return parked == null;
}
void park(Vehicle v) {
parked = v;
System.out.println("Spot " + number + ": parked " + v.plate);
}
void leave() {
System.out.println("Spot " + number + ": " + parked.plate + " left");
parked = null;
}
}Main.java
public class Main {
public static void main(String[] args) {
// One blueprint, three separate objects.
Vehicle car = new Vehicle("ABC-1234", "CAR");
Vehicle bike = new Vehicle("XYZ-77", "BIKE");
Vehicle taxi = new Vehicle("TAX-5555"); // type defaults to CAR
car.describe();
bike.describe();
taxi.describe();
ParkingSpot spot1 = new ParkingSpot(1);
ParkingSpot spot2 = new ParkingSpot(2);
spot1.park(car);
spot2.park(bike);
System.out.println("Spot 1 free? " + spot1.isFree());
spot1.leave();
System.out.println("Spot 1 free? " + spot1.isFree());
}
}Output:
CAR ABC-1234
BIKE XYZ-77
CAR TAX-5555
Spot 1: parked ABC-1234
Spot 2: parked XYZ-77
Spot 1 free? false
Spot 1: ABC-1234 left
Spot 1 free? trueC#
Vehicle.cs
// Deep Dive 1: a class is a blueprint, an object is a thing built from it.
public class Vehicle
{
// Fields: what every vehicle knows about itself.
// (Open to everyone for now; Deep Dive 2 locks them down.)
public string Plate;
public string Type; // "CAR" or "BIKE"
// Constructor: same name as the class, no return type.
// It runs once, at the moment `new Vehicle(...)` creates the object.
public Vehicle(string plate, string type)
{
this.Plate = plate; // this.Plate = the field, plate = the parameter
this.Type = type;
}
// Overloaded constructor: only a plate, so assume a car.
public Vehicle(string plate) : this(plate, "CAR") // hand the work to the other constructor
{
}
public void Describe()
{
Console.WriteLine(Type + " " + Plate);
}
}ParkingSpot.cs
public class ParkingSpot
{
public int Number;
public Vehicle Parked; // null while the spot is free
public ParkingSpot(int number)
{
this.Number = number;
}
public bool IsFree() => Parked == null;
public void Park(Vehicle v)
{
Parked = v;
Console.WriteLine("Spot " + Number + ": parked " + v.Plate);
}
public void Leave()
{
Console.WriteLine("Spot " + Number + ": " + Parked.Plate + " left");
Parked = null;
}
}Program.cs
public class Program
{
public static void Main(string[] args)
{
// One blueprint, three separate objects.
Vehicle car = new Vehicle("ABC-1234", "CAR");
Vehicle bike = new Vehicle("XYZ-77", "BIKE");
Vehicle taxi = new Vehicle("TAX-5555"); // type defaults to CAR
car.Describe();
bike.Describe();
taxi.Describe();
ParkingSpot spot1 = new ParkingSpot(1);
ParkingSpot spot2 = new ParkingSpot(2);
spot1.Park(car);
spot2.Park(bike);
Console.WriteLine("Spot 1 free? " + spot1.IsFree());
spot1.Leave();
Console.WriteLine("Spot 1 free? " + spot1.IsFree());
}
}Output:
CAR ABC-1234
BIKE XYZ-77
CAR TAX-5555
Spot 1: parked ABC-1234
Spot 2: parked XYZ-77
Spot 1 free? False
Spot 1: ABC-1234 left
Spot 1 free? TruePractice: Build a Ticket class
Practice (Deep Dive 1): build the Ticket class.
A ticket remembers:
- id (int)
- plate (String) the vehicle's plate
- spotNumber (int)
- entryHour (int) 0 to 23
1. Add the four fields.
2. Add a constructor Ticket(int id, Vehicle v, ParkingSpot s, int entryHour)
that copies the plate from v and the number from s.
3. Add print(), which prints: Ticket #1 | ABC-1234 | spot 1 | in at 9:00
Bonus: add fee(int exitHour) that returns 20 for every started hour.(Names above are the Java ones; the C# starter below uses the C# names in its comments.)
Start from the lesson code above. Only these files change; the rest are the same as the lesson.
Starter (Java)
Ticket.java
// Practice (Deep Dive 1): build the Ticket class.
//
// A ticket remembers:
// - id (int)
// - plate (String) the vehicle's plate
// - spotNumber (int)
// - entryHour (int) 0 to 23
//
// 1. Add the four fields.
// 2. Add a constructor Ticket(int id, Vehicle v, ParkingSpot s, int entryHour)
// that copies the plate from v and the number from s.
// 3. Add print(), which prints: Ticket #1 | ABC-1234 | spot 1 | in at 9:00
// Bonus: add fee(int exitHour) that returns 20 for every started hour.
public class Ticket {
// your code here
}Main.java
public class Main {
public static void main(String[] args) {
Vehicle car = new Vehicle("ABC-1234", "CAR");
ParkingSpot spot1 = new ParkingSpot(1);
spot1.park(car);
// When Ticket is done, uncomment these lines:
// Ticket t = new Ticket(1, car, spot1, 9);
// t.print();
// System.out.println("Fee at 12:00 = " + t.fee(12));
}
}Starter (C#)
Ticket.cs
// Practice (Deep Dive 1): build the Ticket class.
//
// A ticket remembers:
// - Id (int)
// - Plate (string) the vehicle's plate
// - SpotNumber (int)
// - EntryHour (int) 0 to 23
//
// 1. Add the four fields.
// 2. Add a constructor Ticket(int id, Vehicle v, ParkingSpot s, int entryHour)
// that copies the plate from v and the number from s.
// 3. Add Print(), which prints: Ticket #1 | ABC-1234 | spot 1 | in at 9:00
// Bonus: add Fee(int exitHour) that returns 20 for every started hour.
public class Ticket
{
// your code here
}Program.cs
public class Program
{
public static void Main(string[] args)
{
Vehicle car = new Vehicle("ABC-1234", "CAR");
ParkingSpot spot1 = new ParkingSpot(1);
spot1.Park(car);
// When Ticket is done, uncomment these lines:
// Ticket t = new Ticket(1, car, spot1, 9);
// t.Print();
// Console.WriteLine("Fee at 12:00 = " + t.Fee(12));
}
}Solution
Try it yourself first!
Show the Java solution
Ticket.java
public class Ticket {
int id;
String plate;
int spotNumber;
int entryHour;
Ticket(int id, Vehicle v, ParkingSpot s, int entryHour) {
this.id = id;
this.plate = v.plate;
this.spotNumber = s.number;
this.entryHour = entryHour;
}
void print() {
System.out.println("Ticket #" + id + " | " + plate + " | spot "
+ spotNumber + " | in at " + entryHour + ":00");
}
// Bonus: 20 for every started hour, at least one hour.
int fee(int exitHour) {
return Math.max(1, exitHour - entryHour) * 20;
}
}Main.java
public class Main {
public static void main(String[] args) {
Vehicle car = new Vehicle("ABC-1234", "CAR");
ParkingSpot spot1 = new ParkingSpot(1);
spot1.park(car);
Ticket t = new Ticket(1, car, spot1, 9);
t.print();
System.out.println("Fee at 12:00 = " + t.fee(12));
}
}Output:
Spot 1: parked ABC-1234
Ticket #1 | ABC-1234 | spot 1 | in at 9:00
Fee at 12:00 = 60Show the C# solution
Ticket.cs
public class Ticket
{
public int Id;
public string Plate;
public int SpotNumber;
public int EntryHour;
public Ticket(int id, Vehicle v, ParkingSpot s, int entryHour)
{
Id = id;
Plate = v.Plate;
SpotNumber = s.Number;
EntryHour = entryHour;
}
public void Print()
{
Console.WriteLine("Ticket #" + Id + " | " + Plate + " | spot "
+ SpotNumber + " | in at " + EntryHour + ":00");
}
// Bonus: 20 for every started hour, at least one hour.
public int Fee(int exitHour) => Math.Max(1, exitHour - EntryHour) * 20;
}Program.cs
public class Program
{
public static void Main(string[] args)
{
Vehicle car = new Vehicle("ABC-1234", "CAR");
ParkingSpot spot1 = new ParkingSpot(1);
spot1.Park(car);
Ticket t = new Ticket(1, car, spot1, 9);
t.Print();
Console.WriteLine("Fee at 12:00 = " + t.Fee(12));
}
}Output:
Spot 1: parked ABC-1234
Ticket #1 | ABC-1234 | spot 1 | in at 9:00
Fee at 12:00 = 60How to run
- Java (17 or newer): in the folder,
javac -d out *.javathenjava -cp out Main - C# (.NET SDK 9 or newer): in the folder,
dotnet run
Comments
Loading comments…