Practice Constructors
Implementing Constructors: New Exercises to Practice Writing Constructors
Here are a few new exercises to help you practice writing constructors in Java. Each exercise involves creating a class with fields and adding constructors to initialize those fields.
Exercise 1: Library Class
Scenario: Create a class to represent a library.
- Class Name:
Library
- Fields:
String libraryName
– The name of the library.String address
– The address of the library.int numberOfBooks
– The number of books available in the library.
Tasks:
- Default Constructor: Write a constructor that initializes the library with a default name, address, and number of books.
- Parameterized Constructor: Write a constructor that takes
libraryName
,address
, andnumberOfBooks
as parameters to initialize the fields.
Instructions for Testing:
- Create a
LibraryTester
class. - In the
main
method, create instances ofLibrary
using both the default and parameterized constructors. - Print out the field values to verify that the constructors work as expected.
Exercise 2: MovieTicket Class
Scenario: Create a class to represent a movie ticket.
- Class Name:
MovieTicket
- Fields:
String movieName
– The name of the movie.String seatNumber
– The seat number for the ticket.double price
– The price of the ticket.
Tasks:
- Default Constructor: Write a constructor that sets default values for
movieName
,seatNumber
, andprice
. - Parameterized Constructor: Write a constructor that takes
movieName
,seatNumber
, andprice
as parameters to initialize the fields.
Instructions for Testing:
- Create a
MovieTicketTester
class. - In the
main
method, create instances ofMovieTicket
using both the default and parameterized constructors. - Print out the field values to ensure they are correctly set by the constructors.
Exercise 3: Smartphone Class
Scenario: Create a class to represent a smartphone.
- Class Name:
Smartphone
- Fields:
String brand
– The brand of the smartphone.String model
– The model of the smartphone.double price
– The price of the smartphone.
Tasks:
- Default Constructor: Write a constructor that assigns default values to
brand
,model
, andprice
. - Parameterized Constructor: Write a constructor that initializes
brand
,model
, andprice
with provided values.
Instructions for Testing:
- Create a
SmartphoneTester
class. - In the
main
method, create instances ofSmartphone
using both the default and parameterized constructors. - Print out the field values to check if the constructors are working as expected.
Exercise 4: Course Class
Scenario: Create a class to represent a course in a school or university.
- Class Name:
Course
- Fields:
String courseName
– The name of the course.String courseCode
– The code for the course.int credits
– The number of credits the course is worth.
Tasks:
- Default Constructor: Write a constructor that sets default values for
courseName
,courseCode
, andcredits
. - Parameterized Constructor: Write a constructor that accepts
courseName
,courseCode
, andcredits
as parameters to initialize the fields.
Instructions for Testing:
- Create a
CourseTester
class. - In the
main
method, create instances ofCourse
using both the default and parameterized constructors. - Print out the field values to verify the constructors are working properly.
Exercise 5: WeatherReport Class
Scenario: Create a class to represent a weather report.
- Class Name:
WeatherReport
- Fields:
String location
– The location of the weather report.double temperature
– The temperature at that location.String condition
– The weather condition (e.g., sunny, rainy).
Tasks:
- Default Constructor: Write a constructor that initializes the fields with default values.
- Parameterized Constructor: Write a constructor that takes
location
,temperature
, andcondition
as parameters to initialize the fields.
Instructions for Testing:
- Create a
WeatherReportTester
class. - In the
main
method, create instances ofWeatherReport
using both the default and parameterized constructors. - Print out the field values to ensure that the constructors are setting the fields correctly.
Updating Previous Exercises with Constructors
Now, let’s update the classes from the previous exercises by adding constructors.
1. Student Class
- Add:
- A default constructor that sets default values for
studentId
,name
, andgpa
. - A parameterized constructor that takes
studentId
,name
, andgpa
as arguments.
- A default constructor that sets default values for
- Instructions for Testing:
- In the
StudentTester
class, create instances using both constructors and print the field values to verify the updates.
- In the
2. Book Class
- Add:
- A default constructor that initializes
isbn
,title
, andauthor
with default values. - A parameterized constructor that accepts
isbn
,title
, andauthor
as arguments.
- A default constructor that initializes
- Instructions for Testing:
- In the
BookTester
class, create instances using both constructors and print the field values to check if they are correctly initialized.
- In the
3. Car Class
- Add:
- A default constructor that sets default values for
licensePlate
,make
, andmodel
. - A parameterized constructor that takes
licensePlate
,make
, andmodel
as arguments.
- A default constructor that sets default values for
- Instructions for Testing:
- In the
CarTester
class, create instances using both constructors and print the field values to confirm the constructors are working.
- In the
4. Movie Class
- Add:
- A default constructor that initializes
movieId
,title
, anddurationMinutes
with default values. - A parameterized constructor that accepts
movieId
,title
, anddurationMinutes
as arguments.
- A default constructor that initializes
- Instructions for Testing:
- In the
MovieTester
class, create instances using both constructors and print the field values to verify the updates.
- In the
5. Employee Class
- Add:
- A default constructor that sets default values for
employeeId
,name
, andsalary
. - A parameterized constructor that takes
employeeId
,name
, andsalary
as arguments.
- A default constructor that sets default values for
- Instructions for Testing:
- In the
EmployeeTester
class, create instances using both constructors and print the field values to ensure the constructors are functioning correctly.
- In the
Summary
These exercises will help reinforce your understanding of constructors in Java, including default constructors, parameterized constructors, and overloading. Testing in the respective Tester
classes will confirm that your constructors are correctly implemented and working as expected.
Next Topic:
Go to Methods to learn more with examples.