Learn Java Easy. Chapter 19 POO Exercises

Chapter 19 Exercises – OOP in Java

Chapter 19 Exercises

Typical Object Exercises Using Constructors

Exercise 1

Create and Display Book Information. Create a Book class with title and author attributes. Use a constructor to initialize these attributes and a method to display the book’s information.

Solution:

public class Book {

    private String title;
    private String author;

    // Constructor
    public Book(String title, String author) {
        this.title = title;
        this.author = author;
    }

    // Method to display book information
    public void displayInformation() {
        System.out.println("Title: " + title + ", Author: " + author);
    }

}
public class Main {

      public static void main(String[] args) {

              Book book1 = new Book("One Hundred Years of Solitude", "Gabriel G. Márquez"); 

             book1.displayInformation();

      }

}

Exercise 2

Create a Student class with name and age attributes. Use a constructor to initialize these attributes and a method to display the student’s information.

Solution:

public class Student {

    private String name;
    private int age;

    // Constructor
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    // Method to display student information
    public void displayInformation() {
        System.out.println("Name: " + name + ", Age: " + age);
    }

}
public class Main {

      public static void main(String[] args) {

          Student student1 = new Student("Ana", 20);     

          student1.displayInformation();

     }

}

Exercise 3

Create a Product class with name and price attributes. Use a constructor to initialize these attributes and a method to display the product’s information.

Solution:

public class Product {

    private String name;
    private double price;

    // Constructor
    public Product(String name, double price) {
        this.name = name;
        this.price = price;
    }

    // Method to display product information
    public void displayInformation() {
        System.out.println("Name: " + name + ", Price: $" + price);
    }

}
public class Main {

     public static void main(String[] args) {

                Product product1 = new Product("Laptop", 799.99); 

                product1.displayInformation();

     }

}

Exercise 4

Create a Car class with brand and model attributes. Use a constructor to initialize these attributes and a method to display the car’s information.

Solution:

public class Car {

    private String brand;
    private String model;

    // Constructor
    public Car(String brand, String model) {
        this.brand = brand;
        this.model = model;
    }

    // Method to display car information
    public void displayInformation() {
        System.out.println("Brand: " + brand + ", Model: " + model);
    }

}
public class Main {

    public static void main(String[] args) {

         Car car1 = new Car("Toyota", "Corolla"); 

         car1.displayInformation();

    }

}

Exercise 5

Create a Movie class with title and director attributes. Use a constructor to initialize these attributes and a method to display the movie’s information.

Solution:

public class Movie {

    private String title;
    private String director;

    // Constructor
    public Movie(String title, String director) {
        this.title = title;
        this.director = director;
    }

    // Method to display movie information
    public void displayInformation() {
        System.out.println("Title: " + title + ", Director: " + director);
    }

}
public class Main {

      public static void main(String[] args) {

              Movie movie1 = new Movie("Inception", "Christopher Nolan"); 

              movie1.displayInformation();

      }

}

Typical Object Exercises Using Getters and Setters

Exercise 1

Create a Book class with title and author attributes. Use getter and setter methods to access and modify these attributes, and a method to display the book’s information.

Solution:

public class Book {

    private String title;
    private String author;

    // Getter for title
    public String getTitle() {
        return title;
    }

    // Setter for title
    public void setTitle(String title) {
        this.title = title;
    }

    // Getter for author
    public String getAuthor() {
        return author;
    }

    // Setter for author
    public void setAuthor(String author) {
        this.author = author;
    }

    // Method to display book information
    public void displayInformation() {
        System.out.println("Title: " + title + ", Author: " + author);
    }

}
public class Main {

       public static void main(String[] args) { 

               Book book1 = new Book();

               book1.setTitle("One Hundred Years of Solitude"); 

               book1.setAuthor("Gabriel García Márquez"); 

               book1.displayInformation();

      }

}

Exercise 2

Create a Student class with name and age attributes. Use getter and setter methods to access and modify these attributes, and a method to display the student’s information.

Solution:

public class Student {

    private String name;
    private int age;

    // Getter for name
    public String getName() {
        return name;
    }

    // Setter for name
    public void setName(String name) {
        this.name = name;
    }

    // Getter for age
    public int getAge() {
        return age;
    }

    // Setter for age
    public void setAge(int age) {
        this.age = age;
    }

    // Method to display student information
    public void displayInformation() {
        System.out.println("Name: " + name + ", Age: " + age);
    }
}
public class Main {

       public static void main(String[] args) { 

                 Student student1 = new Student(); 

                 student1.setName("Ana"); student1.setAge(20); 

                 student1.displayInformation();

       }

}

Exercise 3

Create a Product class with name and price attributes. Use getter and setter methods to access and modify these attributes, and a method to display the product’s information.

Solution:

public class Product {

    private String name;
    private double price;

    // Getter for name
    public String getName() {
        return name;
    }

    // Setter for name
    public void setName(String name) {
        this.name = name;
    }

    // Getter for price
    public double getPrice() {
        return price;
    }

    // Setter for price
    public void setPrice(double price) {
        this.price = price;
    }

    // Method to display product information
    public void displayInformation() {
        System.out.println("Name: " + name + ", Price: $" + price);
    }
}
public class Main {

         public static void main(String[] args) { 

               Product product1 = new Product(); 

               product1.setName("Laptop"); 

               product1.setPrice(799.99); 

               product1.displayInformation();

         }

}

Exercise 4

Create a Car class with brand and model attributes. Use getter and setter methods to access and modify these attributes, and a method to display the car’s information.

Solution:

public class Car {

    private String brand;
    private String model;

    // Getter for brand
    public String getBrand() {
        return brand;
    }

    // Setter for brand
    public void setBrand(String brand) {
        this.brand = brand;
    }

    // Getter for model
    public String getModel() {
        return model;
    }

    // Setter for model
    public void setModel(String model) {
        this.model = model;
    }

    // Method to display car information
    public void displayInformation() {
        System.out.println("Brand: " + brand + ", Model: " + model);
    }
}
public class Main {

       public static void main(String[] args) { 

               Car car1 = new Car(); 

               car1.setBrand("Toyota"); 

                car1.setModel("Corolla"); 

                 car1.displayInformation();

       }

}

Exercise 5

Create a Movie class with title and director attributes. Use getter and setter methods to access and modify these attributes, and a method to display the movie’s information.

Solution:

public class Movie {

    private String title;
    private String director;

    // Getter for title
    public String getTitle() {
        return title;
    }

    // Setter for title
    public void setTitle(String title) {
        this.title = title;
    }

    // Getter for director
    public String getDirector() {
        return director;
    }

    // Setter for director
    public void setDirector(String director) {
        this.director = director;
    }

    // Method to display movie information
    public void displayInformation() {
        System.out.println("Title: " + title + ", Director: " + director);
    }
}
public class Main {

      public static void main(String[] args) { 

              Movie movie1 = new Movie(); 

              movie1.setTitle("Inception"); 

              movie1.setDirector("Christopher Nolan"); 

              movie1.displayInformation();

      }

}

Did you like it? Don’t keep it to yourself — share it like juicy gossip! 😏