For the following class Book:
1- Write a default constructor to give default values (title= Intro to Programming and price = 20) to the class’s variables.
2 - Write a parameterized constructor to allow the user to initialize the class variables.
3- Write getter & setter for each variable of this class.
4- Then write a main code to create an instance of this class using parameterized constructor, ask user for inputs to initialize the variables of this instance and then print the instance's variable.
class Book{
private String title;
private int price;
}
public class Book { private String title; private int price; public Book() { this.title = "Intro to Programming"; this.price = 20; } public Book(String title, int price) { this.title = title; this.price = price; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } public static void main(String[] args) { Book b1 = new Book(); System.out.println(b1.getTitle()); System.out.println(b1.getPrice()); } }
Get Answers For Free
Most questions answered within 1 hours.