0% found this document useful (0 votes)
57 views4 pages

Cons Example

Example of Constructor by Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views4 pages

Cons Example

Example of Constructor by Java
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

OOP – 1 (Practical)

Lectures
By
L. Mohamed A. Abdulhamid
University of Basrah
Faculty of Computer Science and Information Technology
Computer Science Department

2024-2025
Q\ Write a Java program to create a class called "Book" with instance variables title,
author, and price. Implement a default constructor and two parameterized constructors:

• One constructor takes title and author as parameters.


• The other constructor takes title, author, and price as parameters.
• Print the values of the variables for each constructor.
Book Output:
- title: String Book1 Title: Unknown
- author: String Book1 Author: Unknown
- price: double Book1 Price: 0.0
Book2 Title: Amatka
+ Book() Book2 Author: Karin Tidbeck
+ Book(title: String, author: String) Book2 Price: 0.0
+ Book(title: String, author: String, price: double) Book3 Title: Altered Carbon
+ main(args: String[]): void Book3 Author: Richard K. Morgan
Book3 Price: 18.9
Dr. L. Mohamed A. Abdulhamid OOP-1 2024-2025
// Define the Book class // Parameterized constructor (title, author)
public class Book { public Book(String title, String author) {
// Private instance variables // Initialize title with the provided parameter
private String title; this.title = title;
private String author; // Initialize author with the provided parameter
private double price; this.author = author;
// Initialize price to 0.0
// Default constructor this.price = 0.0;
public Book() { }
// Initialize title to "Unknown"
this.title = "Unknown"; // Parameterized constructor (title, author, price)
// Initialize author to "Unknown" public Book(String title, String author, double price) {
this.author = "Unknown"; // Initialize title with the provided parameter
// Initialize price to 0.0 this.title = title;
this.price = 0.0; // Initialize author with the provided parameter
} this.author = author;
// Initialize price with the provided parameter
this.price = price;
}
Dr. L. Mohamed A. Abdulhamid OOP-1 2024-2025
// Main method to test the Book class // Create a new Book object using the parameterized
public static void main(String[] args) { constructor (title, author, price)
// Create a new Book object using the default constructor Book book3 = new Book("Altered Carbon", "Richard K.
Book book1 = new Book(); Morgan", 18.99);
// Print the values of the instance variables for book1 // Print the values of the instance variables for book3
System.out.println("Book1 Title: " + book1.title); System.out.println("Book3 Title: " + book3.title);
System.out.println("Book1 Author: " + book1.author); System.out.println("Book3 Author: " + book3.author);
System.out.println("Book1 Price: " + book1.price); System.out.println("Book3 Price: " + book3.price);
}
// Create a new Book object using the parameterized } Output:
constructor (title, author) Book1 Title: Unknown
Book book2 = new Book("Amatka", "Karin Tidbeck"); Book1 Author: Unknown
// Print the values of the instance variables for book2 Book1 Price: 0.0
System.out.println("Book2 Title: " + book2.title); Book2 Title: Amatka
System.out.println("Book2 Author: " + book2.author); Book2 Author: Karin Tidbeck
System.out.println("Book2 Price: " + book2.price); Book2 Price: 0.0
Book3 Title: Altered Carbon
Book3 Author: Richard K. Morgan
Book3 Price: 18.9

Dr. L. Mohamed A. Abdulhamid OOP-1 2024-2025

You might also like