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

Java Class Examples: Book and Person

The document provides Java code for two classes: Book and Person. The Book class includes private instance variables for title, author, and price, with setter methods that validate the price to ensure it is greater than zero. The Person class has a constructor for initializing name and age, and both classes include getter methods to display their details, with user input functionality included in the main method.

Uploaded by

Atharv Ingale
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)
45 views4 pages

Java Class Examples: Book and Person

The document provides Java code for two classes: Book and Person. The Book class includes private instance variables for title, author, and price, with setter methods that validate the price to ensure it is greater than zero. The Person class has a constructor for initializing name and age, and both classes include getter methods to display their details, with user input functionality included in the main method.

Uploaded by

Atharv Ingale
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

Question: Create a class Book with private instance variables title, author, and price.

Write
setter methods to set the values of these variables, but ensure that price is greater than 0. If an
invalid price is set, print an error message. Use getter methods to retrieve and display the
details of the book in the main method.
import [Link];

class Book {
// Private instance variables
private String title; private
String author; private double
price; // Setter method for title
public void setTitle(String title) {
[Link] = title;
}
// Getter method for title
public String getTitle() {
return title;
}
// Setter method for author public
void setAuthor(String author) {
[Link] = author;
}
// Getter method for author
public String getAuthor() {
return author;
}
// Setter method for price with validation
public void setPrice(double price) {
if (price > 0) {
[Link] = price;
} else {
[Link]("Error: Price must be greater than 0.");
}
}

// Getter method for price


public double getPrice() {
return price;
}
}

public class Main{


public static void main(String[] args) {
// Create a Book object
Book book = new Book();

// set values for title ,authoe and price


[Link](“Programming using Java)”;
[Link](“[Link]”);
[Link](1000);

// Display book details using getter methods


[Link]("\nBook Details:");
[Link]("Title: " + [Link]());
[Link]("Author: " + [Link]());
[Link]("Price: " + [Link]());

}}

or_ if u want to take values from user__

public class Main { public static void


main(String[] args) { // Create a
Scanner object for user input
Scanner scanner = new Scanner([Link]);

// Create a Book object


Book book = new Book();

// Input book details


[Link]("Enter book title: ");
String title = [Link]();
[Link](title); // Set title using setter method
[Link]("Enter book author: ");
String author = [Link]();
[Link](author); // Set author using setter method
[Link]("Enter book price: ");
double price = [Link]();
[Link](price); // Set price using setter method with validation

// Display book details using getter methods


[Link]("\nBook Details:");
[Link]("Title: " + [Link]());
[Link]("Author: " + [Link]());
[Link]("Price: " + [Link]());
} }
Create a class Person with two instance variables: name and age. Write a constructor to
initialize these variables. And input from user.
import [Link];
class Person { //
Instance variables
private String name;
private int age;

// Constructor to initialize name and age


public Person(String n, int a) {
[Link] = n; [Link] = a;
}
// Getter method for name
public String getName() {
return name;
}
// Getter method for age
public int getAge() {
return age;
}
}

public class Main { public static void


main(String[] args) { // Create a
Scanner object for user input
Scanner scanner = new Scanner([Link]);

// Input details for the Person object


[Link]("Enter person's name: ");
String name = [Link]();

[Link]("Enter person's age: ");


int age = [Link]();

// Create a Person object using the constructor


Person person = new Person(name, age);

// Display person's details using getter methods


[Link]("\nPerson Details:");
[Link]("Name: " + [Link]());
[Link]("Age: " + [Link]());
}
}

You might also like