Q:

Create a class called Book that includes two data members; Publisher (of type string), and Price (of type float). The class should also contain the following:

0

Create a class called Book that includes two data members; Publisher (of type string), and Price (of type float).

The class should also contain the following:

  1. Two overloaded constructors for class (default and full parametrized).
  2. Set method for the publisher, make sure it is more than 10 characters.
  3. Set method for the price, negative numbers are not allowed.
  4. Get method for the price.
  5. Print method to print the book’s data.
  6. In the main method, create two objects of the Book class, and print the Book’s data only if their price exceeds 100 S.R

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

class Book {
    private String publisher;
    private float price;

    public Book() {
        this.publisher = "";
        this.price = 0.0f;
    }

    public Book(String publisher, float price) {
        this.publisher = publisher;
        this.price = price;
    }

    public void setPublisher(String publisher) {
        if (publisher.length() > 10) {
            this.publisher = publisher;
        } else {
            System.out.println("Publisher name should be more than 10 characters.");
        }
    }

    public void setPrice(float price) {
        if (price >= 0) {
            this.price = price;
        } else {
            System.out.println("Price cannot be negative.");
        }
    }

    public float getPrice() {
        return this.price;
    }

    public void printBookData() {
        System.out.println("Publisher: " + this.publisher);
        System.out.println("Price: " + this.price + " S.R.");
    }
}

public class Main {
    public static void main(String[] args) {
        Book book1 = new Book("PHP Book", 150.0f);
        Book book2 = new Book("Java Book", 80.0f);

        if (book1.getPrice() > 100.0f) {
            book1.printBookData();
        }

        if (book2.getPrice() > 100.0f) {
            book2.printBookData();
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now