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, andnumberOfBooksas parameters to initialize the fields.
Instructions for Testing:
- Create a
LibraryTesterclass. - In the
mainmethod, create instances ofLibraryusing 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, andpriceas parameters to initialize the fields.
Instructions for Testing:
- Create a
MovieTicketTesterclass. - In the
mainmethod, create instances ofMovieTicketusing 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, andpricewith provided values.
Instructions for Testing:
- Create a
SmartphoneTesterclass. - In the
mainmethod, create instances ofSmartphoneusing 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, andcreditsas parameters to initialize the fields.
Instructions for Testing:
- Create a
CourseTesterclass. - In the
mainmethod, create instances ofCourseusing 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, andconditionas parameters to initialize the fields.
Instructions for Testing:
- Create a
WeatherReportTesterclass. - In the
mainmethod, create instances ofWeatherReportusing 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, andgpaas arguments.
- A default constructor that sets default values for
- Instructions for Testing:
- In the
StudentTesterclass, 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, andauthorwith default values. - A parameterized constructor that accepts
isbn,title, andauthoras arguments.
- A default constructor that initializes
- Instructions for Testing:
- In the
BookTesterclass, 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, andmodelas arguments.
- A default constructor that sets default values for
- Instructions for Testing:
- In the
CarTesterclass, 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, anddurationMinuteswith default values. - A parameterized constructor that accepts
movieId,title, anddurationMinutesas arguments.
- A default constructor that initializes
- Instructions for Testing:
- In the
MovieTesterclass, 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, andsalaryas arguments.
- A default constructor that sets default values for
- Instructions for Testing:
- In the
EmployeeTesterclass, 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.