[go: up one dir, main page]

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

MId Sem Java Anser

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)
4 views4 pages

MId Sem Java Anser

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
You are on page 1/ 4

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 java.util.Scanner;

class Book {
// Private instance variables
private String title; private
String author; private double
price; // Setter method for title
public void setTitle(String title) {
this.title = title;
}
// Getter method for title
public String getTitle() {
return title;
}
// Setter method for author public
void setAuthor(String author) {
this.author = author;
}
// Getter method for author
public String getAuthor() {
return author;
}
// Setter method for price with validation
public void setPrice(double price) {
if (price > 0) {
this.price = price;
} else {
System.out.println("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


Book.setTitle(“Programming using Java)”;
Book.setAuthor(“Mr.Gosline”);
Book.setPrice(1000);

// Display book details using getter methods


System.out.println("\nBook Details:");
System.out.println("Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor());
System.out.println("Price: " + book.getPrice());

}}

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(System.in);

// Create a Book object


Book book = new Book();

// Input book details


System.out.print("Enter book title: ");
String title = scanner.nextLine();
book.setTitle(title); // Set title using setter method
System.out.print("Enter book author: ");
String author = scanner.nextLine();
book.setAuthor(author); // Set author using setter method
System.out.print("Enter book price: ");
double price = scanner.nextDouble();
book.setPrice(price); // Set price using setter method with validation

// Display book details using getter methods


System.out.println("\nBook Details:");
System.out.println("Title: " + book.getTitle());
System.out.println("Author: " + book.getAuthor());
System.out.println("Price: " + book.getPrice());
} }
Create a class Person with two instance variables: name and age. Write a constructor to
initialize these variables. And input from user.
import java.util.Scanner;
class Person { //
Instance variables
private String name;
private int age;

// Constructor to initialize name and age


public Person(String n, int a) {
this.name = n; this.age = 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(System.in);

// Input details for the Person object


System.out.print("Enter person's name: ");
String name = scanner.nextLine();

System.out.print("Enter person's age: ");


int age = scanner.nextInt();

// Create a Person object using the constructor


Person person = new Person(name, age);

// Display person's details using getter methods


System.out.println("\nPerson Details:");
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
}
}

You might also like