Click here to Skip to main content
15,072,735 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
import java.io.*;
import java.util.*;
public class Bookfair{
String bname;
double price;
double net_price;

Scanner sc=new Scanner(System.in);

public void input()
{
System.out.println("Enter book name:");
bname=sc.nextLine();
System.out.println("Enter book Price:");
price=sc.nextDouble();

}
public void calculate()
{
if(price<=1000)
{
net_price=price-2/100*price;

}
else if(price>1000&&price<=3000)
{
net_price=price-10/100*price;
}
else if(price>3000)
{
net_price=price-15/100*price;
}
}
public void display()
{
System.out.println("Book name:"+bname);
System.out.println("Net price:"+net_price);
}
public static void main(String[] args) {
Bookfair bf=new Bookfair();
bf.input();
bf.calculate();
bf.display();

}

}

What I have tried:

It looks like claculate function is not working.
Posted
Updated 3-Jul-21 23:43pm
Comments
[no name] 4-Jul-21 4:14am
   
Most probably here net_price=price-2/100*price; this 2/100 evaluates in 0 and therefore net_price= price - 0 * price

Try net_price=price - 2.0 / 100.0 * price;

And of course, same you need to correct then in all the other sections.

Quote:
It looks like claculate function is not working.

Function is working, you problem is that you don't know that a division of integers is an integer division with an integer result.
Java
net_price=price-10/100*price;
//                ^ offending integer division

you can rewrite it as:
Java
net_price=price-10*price/100;

to get desired result.
   
Because 2 and 100 are integer values, so 2 / 100 is an integer divide - and since 2 is less than 100, the result is 0.
Replace them with the floating point equivalents, and it will start to work:
Java
public class Main
{
	public static void main(String[] args) {
	    double price = 100.0;
	    double net_price = price - 2 / 100 * price;
		System.out.println(price);
		System.out.println(net_price);
	    net_price = price - 2.0 / 100.0 * price;
		System.out.println(price);
		System.out.println(net_price);
	}
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900