report an amount in decimal places - java

I am developing an income tax scheme, where the calculation is based on -filing status and -taxable income
All parts must be with exactly two digits following the decimal point.
The problem is that the output displays two zeros after the decimal places instead of the expected numbers.
For example:
Expected Value> Single Filing: $42806.50
Actual Value> Single Filing: $42806.00
Here is my code so far:
// Single Status
if (status == 1) {
if (income > 0 && income <= 8350) {
double firstTax = (int)(income * (0.10));
double totalTax = firstTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ")";
}
else if (income >= 8350 && income <= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (income - 8350) * (0.15));
double totalTax = firstTax + secondTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ")";
}
else if (income >= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (33950 - 8350) * (0.15));
double thirdTax = (int)((income - 33950) * (0.25));
double totalTax = firstTax + secondTax + thirdTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ", Part III: $" + String.format(("%.2f"),thirdTax) + ")";
}
}

You are casting them to int. This will get rid of any decimals.
Remove the (int) and it should have the correct decimals.

// Single Status
if (status == 1) {
if (income > 0 && income <= 8350) {
double firstTax = (int)(income * (0.10));
double totalTax = firstTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ")";
}
else if (income >= 8350 && income <= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (income - 8350) * (0.15));
double totalTax = firstTax + secondTax;
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ")";
}
else if (income >= 33950) {
double firstTax = (int)(8350 * (0.10));
double secondTax = (int)( (33950 - 8350) * (0.15));
double thirdTax = (int)((income - 33950) * (0.25));
result = "Single Filing: $" + String.format("%.2f",totalTax) + "(Part I: $" + String.format(("%.2f"),firstTax) + ", Part II: $" + String.format(("%.2f"),secondTax) + ", Part III: $" + String.format(("%.2f"),thirdTax) + ")";
}
}

You are type casting to int but you want your result to be double. Remove "(int)" from code. You can change code like that,
double firstTax = (int)(income * (0.10)) => double firstTax = (income * (0.10))

Related

One input doesn't work, while the rest does

The user must input the total purchase amount and how old they are, and then calculate the final payment.
If the total amount is $100 or over, there is a 20% discount off the total price. If the age is 65 or older, there is a 10% discount off the total price.
double discount1 = 0.10;
double discount2 = 0.20;
double totalPrice = 0.0;
double finalPrice = 0.0;
System.out.print("Enter total amount: ");
double purchase = input.nextDouble();
System.out.print("Enter age: ");
int age = input.nextInt();
if (purchase >= 100) {
totalPrice = purchase * discount2;
finalPrice = purchase - totalPrice;
System.out.print("The final amount is $" + finalPrice);
}
else if (purchase < 100 && age < 65) {
System.out.println("The final amount is $" + purchase);
}
else if (age >= 65) {
totalPrice = purchase * discount1;
finalPrice = purchase - totalPrice;
System.out.print("The final amount is $" + finalPrice);
}
The user would input 200 as the total amount and 75 as the age. The output is supposed to be $140.00. However, I receive the output as $160.00.
The first if statement will be executed first. Because the price is above 100. So the other statements will not be executed. Try to change the if expressions because of thats the problem why it's not giving the result you may expect
My approach would be to add all of the discounts together and then multiply once at the end.
Than you can add other discounts if needed
double totalDiscount = 0.0;
if (purchase >= 100) {
totalDiscount += discount2;
}
if (age >= 65) {
totalDiscount += discount1;
}
totalPrice = purchase * (1.0 - totalDiscount);
System.out.print("The final amount is $" + totalPrice);
You nee to change below code,
because when ever price is more then 100 it will run first if block and wont enter into last block.
so change it in below manner :-
if (purchase >= 100 && age < 65) {
totalPrice = purchase * discount2;
finalPrice = purchase - totalPrice;
System.out.print("The final amount is $" + finalPrice);
}
else if (purchase < 100 && age < 65) {
System.out.println("The final amount is $" + purchase);
}
else if (purchase < 100 &&age >= 65) {
totalPrice = purchase * discount1;
finalPrice = purchase - totalPrice;
System.out.print("The final amount is $" + finalPrice);
}
else if (age >= 65) {
totalPrice1 = purchase * discount2;
totalPrice = purchase * discount1;
finalPrice = purchase - totalPrice - totalPrice1 ;
System.out.print("The final amount is $" + finalPrice);
}

BMI Calculator not printing results

As part of my CS studies we are starting to learn java and I wanted to do a little side project to try to get better at Java than I am.
So far it isn't working as it should.
I have written this code:
import java.util.Scanner;
public class BodyWeightCalculator {
public static void main(String[] args) {
double weight;
double height;
Scanner input = new Scanner(System.in);
System.out.print("What's your height in meters?");
height = input.nextDouble();
System.out.print("What's your weight in kilograms?");
weight = input.nextDouble();
double bmi = ((weight / height) / height);
System.out.printf("Your BMI is " + bmi);
if (bmi < 18.5)
System.out.println("Your BMI is " + bmi + ", you are underweight.");
else if (bmi <= 18.5 & bmi > 24.9)
System.out.println("Your BMI is " + bmi + ", you are at a normal weight.");
else if (bmi < 25 & bmi > 29.9)
System.out.println("Your BMI is " + bmi + ", you are overweight");
else if (bmi > 30) {
System.out.println("Your BMI is " + bmi + ", you are extremely overweight");
}
}
}
This programs asks the user to input his/her weight and height, and then it outputs the BMI of the user and whether he/she is of normal weight, or underweight, etc.
The program only outputs the BMI ignoring all the if-else statements.
Anything wrong with this?
You have an issue in the set of if-else statements, it should cover the whole ranges. And you should use the boolean operator '&&' rather than using the bitwise-operator '&'.
if (bmi < 18.5)
System.out.println("Your BMI is " + bmi + ", you are underweight.");
else if (bmi >= 18.5 && bmi <= 24.9)
System.out.println("Your BMI is " + bmi + ", you are at a normal weight.");
else if (bmi >= 25 && bmi <= 29.9)
System.out.println("Your BMI is " + bmi + ", you are overweight");
else if (bmi >= 30.) {
System.out.println("Your BMI is " + bmi + ", you are extremely overweight");
}
Your if-statements do not seem to cover the ranges that you are intending. Further, you should be using "&&" for "and" rather than "&", which is a bit-wise "and". You should also get in the habit of always using braces for if statement blocks, even if the block is one line.
You should rewrite your statements like so:
if (bmi < 18.5) {
System.out.println("Your BMI is " + bmi + ", you are underweight.");
}
else if (18.5 <= bmi && bmi < 25.0) {
System.out.println("Your BMI is " + bmi + ", you are at a normal weight.");
}
else if (25.0 <= bmi && bmi < 30.0) {
System.out.println("Your BMI is " + bmi + ", you are overweight");
}
else if (30.0 <= bmi) {
System.out.println("Your BMI is " + bmi + ", you are extremely overweight");
}
else {
throw new RuntimeException("Should not be reached. BMI=" + bmi);
}

Trying to compute payroll in java

I have to figure out if gross pay is between "so and so" it's "this" tax percentage, etc. I thought I was doing alright, but it keeps outputting every single tax answer as one answer if I enter a high number for hours worked... like this "Deductions are 275.0165.0770.0000000000001".
Am I also doing this an extremely long way because I'm overthinking?
Thanks so much for any help!
import java.util.Scanner;
public class prob2
{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
double range = (168);
System.out.println("Enter the number of hours worked in a week:");
double hours = in.nextDouble();
System.out.println("Enter rate per hour:");
double rate = in.nextDouble();
double overtimeHours = hours - 40;
double overtimePay = (overtimeHours * rate) * 1.5;
double basePay = (hours - overtimeHours) * rate;
double grossPay = basePay + overtimePay;
double socialSecurity = .1 * grossPay;
double medical = .06 * grossPay;
if (overtimeHours < 0 )
{
System.out.println("Number of overtime hours are " + 0);
}
else
{
System.out.println("Number of overtime hours are " + overtimeHours);
}
if (overtimeHours < 0 )
{
System.out.println("Base pay is " + hours * rate);
}
else
{
System.out.println("Base pay is " + basePay);
}
if (overtimeHours < 0 )
{
System.out.println("Overtime pay is " + 0);
}
else
{
System.out.println("Overtime pay is " + overtimePay);
}
if (grossPay < 0 )
{
System.out.println("Gross pay is " + hours * rate);
}
else
{
System.out.println("Gross pay is " + grossPay);
}
if (grossPay > 0 && grossPay < 43)
{
System.out.println("Deductions are " + socialSecurity + medical);
}
else
if (43.01 < grossPay && grossPay < 218.00)
{
System.out.println("Deductions are " + socialSecurity + medical + (.10 * grossPay));
}
else
if (218.01 < grossPay && grossPay < 753.00)
{
System.out.println("Deductions are " + socialSecurity + medical + (.15 * grossPay));
}
else
if (grossPay > 0 && 753.01 < grossPay && grossPay < 1762.00)
{
System.out.println("Deductions are " + socialSecurity + medical + (.25 * grossPay));
}
else
if (1762.01 < grossPay && grossPay < 3627.00)
{
System.out.println("Deductions are " + socialSecurity + medical + (.28 * grossPay));
}
}
}
Please wrap your sum into parenthesis:
System.out.println("Deductions are " + (socialSecurity + medical));
In this case it will create sum at first then concatenate result to string, otherwise it will concat socialSecurity then medical one by one.
The same rule is right for similar cases in your code.

Removing decimals from dividing

In a recent game I'm developing, I've made shops.
When you buy an item and try to sell it, the selling price drops to 75% of what the buying price is.
After buying an item for 154 gold pieces, It says the shop will buy for 115.5 gold pieces, but you get 115 gold pieces from selling.
I wish to remove the ".5" from "115.5"
Any help is appreciated.
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000 && ShopValue < 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
I would just use Math.floor.
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000 && ShopValue < 1000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1) + " coins)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000000000) {
ShopAdd = " (" + Math.floor(ShopValue*.75 / 1000000000) + " billion)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
If you wanted to be a little more generous to your players you might use Math.ceil instead ;-)
You can get what you want by either:
Decalring ShopAdd as an integer. This might cause other problems if you're (going to be) using ShopAdd somewhere else that needs it to be a floating point data type though.
Casting ShopAdd into an int right before it's printed. This is a quick fix, and isn't that great if you plan to print ShopAdd in many places, because they'll all have to be casted.
Some example java code:
public class PrintingNumbers {
public static void main(String[] args) {
int i = 1;
double d = 1;
System.out.println("printing integer: " + i);
System.out.println("printing double: " + d + " (not what you want)");
System.out.println("printing casted double: " + (int) d);
}
}
Output of above java code:
printing integer: 1
printing double: 1.0 (not what you want)
printing casted double: 1
In your case, option 1 would look something like this:
int ShopAdd // declare ShopAdd as an integer
...
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}
And, option 2 would look like this:
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
// casting ShopAdd into an int when printing
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+(int)ShopAdd+"</col> coins");
}
}
I hope this helps! :)
As Kayaman already wrote in his comment, I would just cast it to an int or to an long - This will cut of the decimals without rounding.
If you want an integer as a result, the simple solution is to use integer maths.
long ShopValue = (long) (getItemShopValue(removeId, 1, removeSlot))*3/4;
String ShopAdd = " (";
if (ShopValue >= 750000000) {
ShopAdd = ShopValue / 750000000 + " billion";
} else if (ShopValue >= 1000000) {
ShopAdd = ShopValue / 750000 + " million";
} else if (ShopValue >= 1000) {
ShopAdd = ShopValue / 750 + "k";
} else {
ShopAdd = ShopValue + " coins";
}
ShopAdd += ")";
Note: your current implementation will print 800 as 0k
You can use Math.floor() as others said.
Apart from that: you may have a bug in your code logic. The 3rd and the 4th branches of the if statement are unreachable as the 2nd if clause covers them
if (ShopValue >= 100)
The correct way is to arrange them such that comparisons with greater numbers comes before comparisons with smaller numbers:
int ShopValue = (int)Math.floor(getItemShopValue(removeId, 1, removeSlot));
String ShopAdd = "";
if (ShopValue >= 1000000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000000) + " billion)";
} else if (ShopValue >= 1000000) {
ShopAdd = " (" + (ShopValue*.75 / 1000000) + " million)";
} else if (ShopValue >= 1000) {
ShopAdd = " (" + (ShopValue*.75 / 1000) + "k)";
} else if (ShopValue >= 100) {
ShopAdd = " (" + (ShopValue*.75 / 1) + " coins)";
}
c.sM(c.getItems().getItemName(removeId)+": shop will buy for <col=255>"+ShopAdd+"</col> coins");
}
}

Proper msg display

I don't get any errors but I want to display this msg "No discount. Your total is $_"
using this code:
if (!(sales < 10))
System.out.print("No discount. " +
"Your total is: $" + (int)total);
inside this code:
if (!(sales < 10))
System.out.print("No discount. " +
"Your total is: $" + (int)total);
else if (sales >= 10 || sales <= 19)
rate = 0.20;
else if (sales >= 20 || sales <=49)
rate = 0.30;
else if (sales >= 50 || sales <=99)
rate = 0.40;
else if (sales > 100)
rate = 0.50;
else
System.out.println("Your discount is $" + (int)discount +
". Your total is: $" + (int)total);
and it does show but i only want it to show when sales< 10
but if sales is NOT < 10 then i want it to show "Your discount is $___. Your total is $____."
any help will be very much appreciated. thanks for your time.
whole code:
import java.util.Scanner;
public class SoftwareSales
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int sales = 99;
int quantity;
double total;
double rate = 0;
double discount;
System.out.print ("Enter amount of packages purchased: ");
quantity = keyboard.nextInt();
total = quantity * sales;
discount = total * rate;
total = total - discount;
if (sales < 10) {
if (sales >= 10 || sales <= 19)
rate = 0.20;
else if (sales >= 20 || sales <=49)
rate = 0.30;
else if (sales >= 50 || sales <=99)
rate = 0.40;
else if (sales > 100)
rate = 0.50;
System.out.println("Your discount is $" + (int)discount +
". Your total is: $" + (int)total);
} else {
System.out.print("No discount. " +
"Your total is: $" + (int)total);
}
}
}
Try this:
if (sales > 10) {
if (sales >= 10 || sales <= 19)
rate = 0.20;
else if (sales >= 20 || sales <=49)
rate = 0.30;
else if (sales >= 50 || sales <=99)
rate = 0.40;
else if (sales > 100)
rate = 0.50;
System.out.println("Your discount is $" + (int)discount +
". Your total is: $" + (int)total);
} else {
System.out.print("No discount. " +
"Your total is: $" + (int)total);
}
So why are you testing for the exact opposite of what you want to do? Notice the exclamation mark (!) which inverts your sales test in your first if. Remove that and your code should work as intended.

Categories