04-2016 – CSKC
Sign In

Search





Q&A #7316 siteicon  

Question:

Define a class named BookFair with the following description: [15]
Instance variables/Data members:
String Bname – stores the name of the book.
double price – stores the price of the book.
Member Methods:
(i) BookFair() – Default constructor to initialize data members.
(ii) void Input() – To input and store the name and the price of the book.
(iii) void calculate() – To calculate the price after discount. Discount is calculated based on the following criteria.
PRICE DISCOUNT
Less than or equal to Rs 1000 2% of price
More than Rs 1000 and less than or equal to Rs 3000 10% of price
More than Rs 3000 15% of price
(iv) void display() – To display the name and price of the book after discount.
Write a main method to create an object of the class and call the above member methods.

Answer:

import java.util.Scanner;

 public class BookFair {
 private String Bname;
 private double price;

 public BookFair() {
 Bname = null;
 price = 0.0;
 }

 public void Input() {
 Scanner scanner = new Scanner(System.in);
 System.out.print("Enter book name: ");
 Bname = scanner.nextLine();
 System.out.print("Enter price: ");
 price = scanner.nextDouble();
 }

 public void calculate() {
 double discountPercentage = 0;
 if (price <= 1000) { 23 discountPercentage = 2; 24 } else if (price > 1000 && price <= 3000) { 25 discountPercentage = 10; 26 } else if (price > 3000) {
 discountPercentage = 15;
 }
 price = price - (price * discountPercentage / 100);
 }

 public void display() {
 System.out.println("Name: " + Bname);
 System.out.println("Price after discount: " + price);
 }
public static void main(String[] args) {
 BookFair bookFair = new BookFair();
 bookFair.Input();
 bookFair.calculate();
 bookFair.display();
 }
 }

Sample output
Enter book name: How to Java
Enter price: 2500
Name: How to Java
Price after discount: 2250.0


Share

C. Sir| Created: 16-Apr-2019 | Updated: 16-Apr-2019|ICSE2016









×
ssignment Introductory Sessions Beginning to Program Tokens Keyword and Identifiers Data Types Variables and Constants Operators Simple User Input Building Expressions and Formulas Simple Real World Problems Simple If and If Else Multiple-Nested-Ladder of If Else Switch case selection Simple Loops Tricks in Loops - break continue scope Loop Applications - Handling numerals Series printing loops Nested Loops Pattern printing loops Number Varieties and Crunches String Handling (Null Terminated) Strings - string class type Functions (Built-in) Functions - user defined Functions Reference Passing/Returning Arrays Concepts and 1-D Arrays Array Data Management Two dimensional arrays and Matrices Structures Basics Structures passing/returning Display Using IO Manipulation User Defined Data Types Enumerated Types Preprocessor Directives And Macros Programming Paradigms and OOPs Advantages Abstraction and Encapsulation Polymorphism Inheritance Function Overloading Concepts Function Overloading Varieties Defining Classes Creating and Using Class Objects Class Members Accessibility Class Function Types Inline Functions Constructor Concepts Default Constructor Copy Constructor Constructor Overloading Destructors Inheritance Fundamentals Public Derivations Private and Protected Derivations Multiple Inheritance Mult-Level Inheritance Data File Concepts Handling Text Files Handling Binary Files Pointer Concepts Pointer and Arrays Stacks Queues Introductory Sessions Beginning to Program Tokens Keywords Identifiers Literals Data Types Variables and Constants Operators (Arithmetic-Assignment-Unary-Relational-Logical) Expressions Statements and Formula Based Calculations Class Object Creation and Access Wrapper Classes Constructor Basics Conversion and Checking Functions Math Functions Function building and calling Simple If and If-Else Selection Switch Based Selections Basic Loop Types Array Basics Array Search Array Sorting Character Handling String Basics String Class Functions Types of Errors Try Catch Introductory Sessions Beginning to Program Introductory Sessions Beginning to Program Introductory Sessions Beginning to Program Data Types Variables and Constants Introductory Sessions Beginning to Program Date and Time Handling Regular Expressions Command Line Arguments Concurrent Execution Making Executable File Python Introduction (Features Python Introduction (Features, Installation, Modes) Handling Unicode Internet Interactions Imaging and Compression Special Data File Handling Python Basics Cryptography Blockchain and Cryptocurrency Generative AI keywords, identifiers, variables Beginning To Program Tokens, Keywords, Identifiers Variables and Expressions Data Types Operators Concept Learning Examples Operators Solved Practical Examples elational nary)