java_program 12

 Practical Name: Write a program by using class with the following specifications:

Class name — Sale

Data members/ Instance variables:

String title, author,publication

double price

Member methods:

void input() — to accept title, author name and publication name and price of a book

void display() — to display title, author name and publication name and price of a book


___________________________________________________________________________

import java.util.Scanner;

 

 class Purchase{

    protected String title;

    protected String author;

    protected String publication;

    protected double price;

 

    public void input() {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter book title: ");

        title = in.nextLine();

        System.out.print("Enter book author: ");

        author = in.nextLine();

        System.out.print("Enter publication name: ");

        publication = in.nextLine();

        System.out.print("Enter book price: ");

        price = in.nextDouble();

    }

 

    public void display() {

        System.out.println("Book Title: " + title);

        System.out.println("Book Author: " + author);

        System.out.println("Publication: " + publication);

        System.out.println("Price: " + price);

    }

}

 

public class Sale extends  Purchase{

    private int noc;

    private double amount;

 

    public void accept() {

        Scanner in = new Scanner(System.in);

        System.out.print("Enter no. of copies purchased: ");

        noc = in.nextInt();

    }

 

    public void calculate() {

        amount = noc * price;

    }

 

    public void show() {

        display();

        System.out.println("No. of copies: " + noc);

        System.out.println("Amount: " + amount);

    }

 

    public static void main(String args[]) {

        Sale obj = new Sale();

        obj.input();

        obj.accept();

        obj.calculate();

        obj.show();

    }

}




Post a Comment

Previous Post Next Post