Question

Now create the Book class that inherits from Product. Dont forget that each class must go in a file with a matching name, so

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code(in simple notepad and executed in command prompt:

class Product {//add public before class keyword
    int stockNumber;
    int price;

    public int getStockNumber() {//getter for stockknumber
        return stockNumber;
    }

    public void setStockNumber(int stockNumber) {//setter for stocknumber
        this.stockNumber = stockNumber;//setting our object stocknumber to user given stock number
    }

    public int getPrice() {//getter for price
        return price;
    }

    public void setPrice(int price) {//setter for price
        this.price = price;
    }
  
  
  
}//end of product class
class Book extends Product{//add public before class keyword
String author;
String title;
int numberPages;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getNumberPages() {
        return numberPages;
    }

    public void setNumberPages(int numberPages) {
        this.numberPages = numberPages;
    }


}//end of class Book
public class demo{//add public before class keyword
public static void main(String[] args){
Book b1=new Book();//creating book object b1
b1.setStockNumber(35);//setting stockknumber to 35 price to 432 title to abstarct algebra and author to gilbert and number of pages to 543 in this method and subsequent 4 calls
b1.setPrice(432);
b1.setTitle("abstract algebra");
b1.setAuthor("gilbert");
b1.setNumberPages(543);
System.out.println("stocknumer is = "+b1.getStockNumber());//using gtters
System.out.println("price is = "+b1.getPrice());
System.out.println("authorname is = "+b1.getAuthor());
System.out.println("title is is = "+b1.getTitle() );
System.out.println("number of pages is is = "+b1.getNumberPages());


}//end of main
}//end of demo class


Note:if your working in some ide please type public in front of 3 classes and save 3 classes in 3 files

for ex save Product as Product.java

save Book as Book.java

save demo as demo.java

i worked in simple command prompt and created only one file and also food class was not needed in this code so i didnt code food class

Output in command prompt

C:\Users\DELL\Desktop\GATE\che>java demo stocknumer is = 35 price is = 432 authorname is = gilbert title is is abstract algeb

Add a comment
Know the answer?
Add Answer to:
Now create the Book class that inherits from Product. Don't forget that each class must go...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Create a class named Book that contains data fields for the title and number of pages....

    Create a class named Book that contains data fields for the title and number of pages. Include get and set methods for these fields. Next, create a subclass named Textbook, which contains an additional field that holds a grade level for the Textbook and additional methods to get and set the grade level field. Write an application that demonstrates using objects of each class. Save the files as Book.java, Textbook.java, and DemoBook.java.

  • Create a base class named Book. Data fields include title and author; functions include those that...

    Create a base class named Book. Data fields include title and author; functions include those that can set and display the fields. Derive two classes from the Book class: Fiction, which also contains a numeric grade reading level, and NonFiction, which contains a variable to hold the number of pages. The functions that set and display data field values for the subclasses should call the appropriate parent class functions to set and display the common fields, and include specific code...

  • You need to program a simple book library system. There are three java classes Book.java   //...

    You need to program a simple book library system. There are three java classes Book.java   // book object class Library.java   //library class A2.java //for testing The Book.java class represents book objects which contain the following fields title: a string which represents the book title. author: a string to hold the book author name year: book publication year isbn: a string of 10 numeric numbers. The book class will have also 3 constructors. -The default no argument constructor - A constructor...

  • JAVA Objective : • Learning how to write a simple class. • Learning how to use...

    JAVA Objective : • Learning how to write a simple class. • Learning how to use a prewritten class. • Understanding how object oriented design can aid program design by making it easier to incorporate independently designed pieces of code together into a single application. Instructions: • Create a project called Lab13 that contains three Java file called Book.java , Bookstore.java and TempleBookstore.java • I’ve provided a screenshot of how your project should look like. • Be sure to document...

  • Look at this partial class definition, and then answer questions a - b below: Class Book...

    Look at this partial class definition, and then answer questions a - b below: Class Book    Private String title    Private String author    Private String publisher    Private Integer copiesSold End Class Write a constructor for this class. The constructor should accept an argument for each of the fields. Write accessor and mutator methods for each field. Look at the following pseudocode class definitions: Class Plant    Public Module message()       Display "I'm a plant."    End Module...

  • USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat....

    USING BLUEJ: You will write from scratch a class called Heater that represents an adjustable thermostat. Follow the detailed steps below. This is based on Exercises 2.93 - 2.94 (6e) / 2.92 - 2.93 (5e) in the book, but with slight modifications, so be sure to follow the instructions below. Create a new BlueJ project named LastName-heater using your last name. Create a class named Heater At the top of the source code for Heater, add documentation like this: /**...

  • PHP Programming Question. Use the techniques you learned so far to create an Address Book application...

    PHP Programming Question. Use the techniques you learned so far to create an Address Book application that stores names, e-mail addresses, and phone numbers in a text file. Validate all input fields and include functionality that allows the user to view the address book. Also, include code that sorts the address book by name and deletes duplicate entries. Each page in the application should have a link back to the main page. Be creative and add extra features if you...

  • Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an...

    Additional code needed: PartA: BurgerOrder class (1 point) Objective: Create a new class that represents an order at a fast-food burger joint. This class will be used in Part B, when we work with a list of orders. As vou work through this part and Part B, draw a UML diagram of each class in using the UML drawing tool 1) Create a new Lab5TestProject project in Netbeans, right-click on the lab5testproject package and select New>Java Class 2) Call your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT