how to show all the output - java

i want to make that program to calculate the average gross of 10 employees.
what edit should i make on the code so that it saves all the 10 inputs i do in the while loop.
the program only shows me one output and then goes to the loop but i want it to show me the 10 outputs for the 10 employees so i can calculate average gross and total gross.
import java.util.Scanner;
public class ss
{
public static void main(String[] args)
{
String emp;
char repeat = 'y';
double hours = 0, rate = 0, gross = 0, overtime = 0,STtax = 0, FEDtax = 0, union = 0, net = 0, Tgross = 0, Agross = 0;
int count = 0;
Scanner kb = new Scanner(System.in);
while(repeat == 'y' || repeat == 'Y')
{
System.out.printf("please enter the employee name\n ");
emp = kb.next();
System.out.printf("please enter worked hours\n ");
hours = kb.nextDouble();
if(hours >= 0 && hours <= 60)
{
System.out.printf("please enter the pay rate\n ");
rate = kb.nextDouble();
if(rate > 0 && rate <= 50)
{
if(hours <= 40)
{
gross = (rate * hours);
STtax = gross * 0.06;
FEDtax = gross * 0.12;
union = gross * 0.01;
net = gross - (STtax + FEDtax + union);
System.out.printf("hi %s\n", emp);
System.out.printf("worked hours %6.2f\n", hours);
System.out.printf("your pay rate %2.4f\n", rate);
System.out.printf("Gross income %6.2f\n", gross);
System.out.printf("State tax %6.2f\n", STtax);
System.out.printf("Federal tax %6.2f\n", FEDtax);
System.out.printf("Union fees %6.2f\n", union);
System.out.printf("NET %6.2f\n", net);
}
else
{
overtime = (hours - 40) * rate * 1.5;
gross = (rate * 40) + overtime;
STtax = gross * 0.06;
FEDtax = gross * 0.12;
union = gross * 0.01;
net = gross - (STtax + FEDtax + union);
System.out.printf("hi %s\n", emp);
System.out.printf("worked hours %6.2f\n", hours);
System.out.printf("your pay rate %2.4f\n", rate);
System.out.printf("OverTime is %2.2f\n", overtime);
System.out.printf("Gross income %6.2f\n", gross);
System.out.printf("State tax %6.2f\n", STtax);
System.out.printf("Federal tax %6.2f\n", FEDtax);
System.out.printf("Union fees %6.2f\n", union);
System.out.printf("NET %6.2f\n", net);
}
System.out.printf("please enter Y to add another employee\n ");
repeat = kb.next().charAt(0);
}
else
System.out.printf("Pay Rate can only be between 0 and 50\n");
}
else
System.out.printf("Hours can only be between 0 and 60\n");
}
}
}

i was able to solve it and here is the solution if anyone wanted it
import java.util.Scanner;
public class HomeWork3
{
public static void main(String[] args)
{
String emp;
double hours = 0, rate = 0, gross = 0, overtime = 0,STtax = 0, FEDtax = 0, union = 0, net = 0, Tgross = 0, Agross = 0;
int num = 0;
Scanner kb = new Scanner(System.in);
while(num < 10)
{
System.out.printf("please enter the employee name\n ");
emp = kb.next();
System.out.printf("please enter worked hours\n ");
hours = kb.nextDouble();
if(hours >= 0 && hours <= 60)
{
System.out.printf("please enter the pay rate\n ");
rate = kb.nextDouble();
if(rate > 0 && rate <= 50)
{
if(hours <= 40)
overtime = 0;
else
overtime = (hours - 40) * rate * 1.5;
gross = (rate * 40) + overtime;
STtax = gross * 0.06;
FEDtax = gross * 0.12;
union = gross * 0.01;
net = gross - (STtax + FEDtax + union);
System.out.printf("hi %s\n", emp);
System.out.printf("worked hours %6.2f\n", hours);
System.out.printf("your pay rate $%2.4f\n", rate);
System.out.printf("OverTime is %2.2f\n", overtime);
System.out.printf("Gross income $%6.2f\n", gross);
System.out.printf("State tax $%6.2f\n", STtax);
System.out.printf("Federal tax $%6.2f\n", FEDtax);
System.out.printf("Union fees $%6.2f\n", union);
System.out.printf("NET $%6.2f\n", net);
num = num +1;
}
else
System.out.printf("Pay Rate can only be between 0 and 50\n");
Tgross = Tgross + gross;
Agross = Tgross / num;
System.out.printf("Total gross for all entered employees is $%6.3f\n", Tgross);
System.out.printf("Average gross for all entered employees is $%6.4f\n", Agross);
System.out.printf("number of entered employees is %d\n\n", num);
}
else
System.out.printf("Hours can only be between 0 and 60\n");
}
}
}

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);
}

The program doesn't reassign the value in my if else statment

I am very new to Java and my assignment was to make a tax calculator. Everything seems to be working fine but when I print at the end it always prints 0 for federaltaxes. I make the double at the start and then try to reassign it depending on the situation through the else if statement.
//2353161 Tomas Pospisil
import java.util.Scanner;
public class PaymentCalculator {
public static void main(String[] args){
//Create Scanner for user input
Scanner input = new Scanner(System.in);
//Ask the user for Gross Pay
System.out.print("Please Enter Gross Pay: ");
double GrossPay = input.nextInt();
//Establish the integer for Number of Exemptions
int NoE = 0;
//Establish the integer for the value of exemption
double exemptionvalue = 0;
double federaltaxes = 0;
//Ask the user if they are married
System.out.print("Are you married: ");
String marriedstatus = input.next();
//If yes make the number of Exemptions equal the user input
if (marriedstatus.equals("yes")) {
System.out.print("Please enter Number of Exemptions: ");
NoE = input.nextInt();
}
//If not make the number of exemptions equal 1
else if(marriedstatus.equals("no")) {
NoE = 1;
}
//If NoE is greater than 4 make it 5 for later use
if (NoE >4) {
NoE = 5;
}
// Depending on the number of exemptions the program assigns it a value for later use
switch (NoE) {
case 1:
exemptionvalue = 1000;
break;
case 2:
exemptionvalue = 1800;
break;
case 3:
exemptionvalue = 2400;
break;
case 4:
exemptionvalue = 3600;
break;
case (5):
exemptionvalue = 4000;
break;
}
//Calculate adjusted wages
double adjustedwages = GrossPay - exemptionvalue;
// Figure out the tax percentage
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
}
else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15 ;
}
else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20 ;
}
//Program calculates the FICA
double FICA = (GrossPay * .065);
//Program calculates Medicare
double Medicare = (GrossPay * .014);
//Program calculates the federal taxes
//Program Calculates Net Pay
double NetPay = GrossPay - (FICA + Medicare + federaltaxes);
//Program prints the results
System.out.println(" Payroll Taxes");
System.out.println("Gross.....$ " + GrossPay);
System.out.println("Exemptions: " + NoE);
System.out.println("Federal Taxes.....$ " + federaltaxes);
System.out.println(" FICA.....$ " + FICA);
System.out.println(" Medicare.....$ " + Medicare);
System.out.println("-----------------------------");
System.out.println("Net Pay.....$ " + NetPay);
}
}
This is the block that is causing you trouble.
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
}
Any GrossPay amount above 10000 is going to pass the condition and be multiplied by 0.
You have a wrong condition in the if where you evaluate federal taxes, it should be:
if (GrossPay < 10000) {
federaltaxes = GrossPay * 0;
}
else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15 ;
}
else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20 ;
}
your code will never evaluate the else condition
It is because your condition logic is wrong
double federaltaxes = 0;
// Figure out the tax percentage
if (GrossPay >= 10000) {
federaltaxes = GrossPay * 0;
} else if (GrossPay <= 30000 && GrossPay >= 10000) {
federaltaxes = GrossPay * 0.15;
} else if (GrossPay > 30000) {
federaltaxes = GrossPay * 0.20;
}
Your first condition will always be true if the Grosspay is >= 10000 so federaltaxes here is 0, then the rest of your if else will not be evaluated because your first condition will always evaluate first. Also you dont have a separate else statement for the rest of the scenarios

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.

Java change app

So I have Been working on a Program and it has been awhile since I have last used java. I was wondering how to get my program to accept decimals. I have tried looking it up but I couldn't find anything helpful and anything I really understood.Below is what I have done so far...
package test;
import java.util.Scanner;
/**
*
* #author Thao
*/
public class Test {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
// create Scanner to obtain input from command window
Scanner input = new Scanner( System.in );
double cash; // double makes it allow decimals
int hundred;
int twenty;
int ten;
int toonies ;
int lonies;
int quarters;
int dimes;
int nickels;
int pennies;
int money;
int change;
System.out.println();// blank line
System.out.print("Hello ");
System.out.println();
System.out.println("Please enter a number with no decimal places ");
cash = input.nextInt(); // read first integer
System.out.println("you have Entered "+cash);
hundred = 100;
change = (int)hundred - (int)cash;
System.out.println("The change from $100.00 is:" + (double)change);
change = 100 * change; // multiply cash by 100
money = (int)change; //cash is now a int
twenty = money / 2000;
money = money - (twenty * 2000);
toonies = money / 200; // money is multiplied by 100 than / by 200
money = money - (toonies * 200); // ex. money = 1586 - (7.93 * 200 )
lonies = money / 100 ;
money = money - (lonies * 100);
quarters = money / 25;
money = money - (quarters * 25);
dimes = money / 10;
money = money - (dimes * 10);
nickels = money / 5;
money = money - (nickels * 5);
pennies = money / 1;
money = money - (pennies * 1);
if(twenty>0){
System.out.println();
System.out.print("You will have this many Twenties ");
System.out.print(twenty + ".");
}
else{
}
if(toonies>0){
System.out.println();
System.out.print("You will have this many Toonies ");
System.out.print(toonies + ".");
System.out.println();
}
else{
}
if(lonies>0){
System.out.print(" You will have this many Lonies ");
System.out.print(lonies + ".");
System.out.println();
}
else{
}
if(quarters>0){
System.out.print(" You will have this many quarters ");
System.out.print(quarters + ".");
System.out.println();
}
else{
}
if(dimes>0){
System.out.print(" You will have this many dimes ");
System.out.print(dimes + ".");
System.out.println();
}
else{
}
if(dimes>0){
System.out.print(" You will have this many nickels ");
System.out.print(nickels);
System.out.println();
}
else{
}
if(pennies>0){
System.out.print(" You will have this many pennies ");
System.out.print(pennies);
System.out.println();
}
else{
}
System.out.println();
System.out.println("Thank you for using my app ");
}
}
You can use double or float for the variable's data type. To read a double from your input you would do:
double cash = input.nextDouble();
or
float cash = input.nextFloat();

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