Computer Science, asked by sheelags83, 8 months ago

Write a java program to implement a Book class that stores the details of a book such as it's code, title, and price. The class has methods to tell you any of these details individually.​

Answers

Answered by renubala98154
2

Explanation:

Java, one of the most important types of objects is for a storage class. Unlike an application class, most storage classes are not meant to run at the command line. So, the main() method is not usually used inside a Java storage class.

A storage class is used to hold data, and the data represents the attributes of the object that the storage class is supposed to represent. Here is a simplified version of storage class that we will use for a Book object.

public class Book {

// instance variables

private String title;

private String author;

private double price;

// constructor

public Book(String t, String a, double p) {

title = t.trim();

author = a.trim();

price = p;

}

}

Note on line 1, that the visibility modifier for the class signature is public. This is required if you want to use the Book class from any other file. Lines 3-5 are instance variables (data) for the Book class. Instance variables are the variables that describe the object and can have different values for each object. A simple way to understand this is to think of having several Book objects. Each Book object should have its own title, author and price.

Instance variables are declared as private so that they cannot be directly accessed from outside the class definition. This is sometimes referred to as "data hiding". The idea behind data hiding is to make it easier to prevent unwanted changes to the data.

Lines 8-12 define a constructor for the Book class. The constructor is a special method that is used to construct an instance of a class. The constructor's signature has no return type and the name is the same as the class name. You can have multiple constructors as long as the parameter list is different for each constructor.

If you don't define a constructor for a class, the Java compiler will add a parameterless constructor that sets all the instance variables to a default value. The default value will be null for any reference types or zero for any numerical types.

At this point, the Book class is incomplete. Although the instance variables are defined, there is no way to change the value of those variables except when a Book object is initially constructed. In addition, there is no way to print out any variable values.

The toString() method

The toString() method is a method that is used to get the String representation of an object. If you don't put a toString() method into your class, the toString() method inherited from java.lang.Object (the cosmic superclass that all Java classes inherit from) will be used. To see this, let's create a driver class to test our Book class.

A driver class is just an application class that is used to test a storage class. Since we want to test the Book class, we can call this driver class the TestBook class. Here is the file "TestBook.java

plz mark me as a brainlist

Similar questions