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.
Related
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");
}
}
}
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);
}
When try to run I got wrong answer. What's wrong in my program?
Got answer is only for discount_amount not able to get total amount.
Scanner input = new Scanner(System.in);
double dicount_amount = 0;
double discount1 = 0;
double total = 0;
System.out.println("Enter the cost of the software: ");
double cost = input.nextDouble();
System.out.println(" Enter the quantity sold: ");
int quantity = input.nextInt();
if (cost > 0 && quantity > 0){
if(quantity >= 10 && quantity >=19){
discount1 = 20/100;
}
else if(quantity >= 20 && quantity >=49){
discount1 = 30/100;
}
else if(quantity >= 50 && quantity >=99){
discount1 = 40/100;
}
else if(quantity <=100){
discount1 = 50/100;
}
}
else {
System.out.println( " please enter valid input ");
}
double total1 = cost * quantity;
dicount_amount = total1 * discount1;
total= total1 - dicount_amount;
System.out.println("Total Cost: " + total);
}
Simplest thing you can do is not to divide discount1 = 20/100;, instead that you can set directly value discount1 = 0.2;
Also you should check your if statement
Example:
if(quantity >= 10 && quantity >=19)
is equal to
if(quantity >=19)
I think you want to check if quantity is between. So you should use
if(quantity >= 10 && quantity <=19)
Guess what was your mistake? You were dividing int by int and you are excepting a double result. Typecast the division to double or do 20.0/100. Also, I assume you are want to give a discount if the quantity is between 10 and 19. What you have been doing doesn't make sense because every time you run your program it will never fall in else if() statement because you are setting quantity>=19 in if statement.
here are few changes I made in your code :
Scanner input = new Scanner(System.in);
double dicount_amount = 0;
double discount1 = 0;
double total = 0;
System.out.println("Enter the cost of the software: ");
double cost = input.nextDouble();
System.out.println(" Enter the quantity sold: ");
int quantity = input.nextInt();
if (cost > 0 && quantity > 0){
if(quantity >= 10 && quantity <=19){
discount1 = (double)20/100;
}
else if(quantity >= 20 && quantity <=49){
discount1 = (double)30.0/100;
}
else if(quantity >= 50 && quantity <=99){
discount1 = (double)40.0/100;
}
else if(quantity >=100){
discount1 = (double)50.0/100;
}
}
else {
System.out.println( " please enter valid input ");
}
double total1 = cost * quantity;
dicount_amount = total1 * discount1;
total= total1 - dicount_amount;
System.out.println("Total Cost: " + total);
Output :
Enter the cost of the software:15
Enter the quantity sold:16
Total Cost: 192.0
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.
My code is correct but it looks too big.I was wondering if it could be more efficient and with even fewer variables.Do you have any hints?Thanks
sample output
Enter three scores: 87 42 94
The lowest score was: 42
The average without the lowest score is: 90.5
The grade is: A
<code>
import java.util.Scanner;
public class GradeAverager
{
public static void main(String[] args)
{
int score1,score2,score3;
double average,average_no_lowest;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter three scores: ");
score1 = keyboard.nextInt();
score2 = keyboard.nextInt();
score3 = keyboard.nextInt();
average = (score1 + score2 + score3) / 3.0;
System.out.println();
System.out.println("The average is: " + average);
if (score1 < score2 && score1 < score3)
System.out.println("The lowest score was:" + score1);
else if (score2 < score1 && score2 < score3)
System.out.println("The lowest score was:" + score2);
else if (score3 < score1 && score3 < score2)
System.out.println("The lowest score was:" + score3);
if (score1 < score2 && score1 < score3){
average_no_lowest = (score2 + score3)/2.0;
System.out.println("The average without the lowest score is: " + average_no_lowest);
if(average_no_lowest > 90)
System.out.print('A');
else if(average_no_lowest < 90 && average_no_lowest > 80 )
System.out.print('B');
else if(average_no_lowest > 70 && average_no_lowest < 80)
System.out.print('C');
else
System.out.print('D');
}
else if (score2 < score1 && score2 < score3){
average_no_lowest = (score1 + score3)/2.0;
System.out.println("The average without the lowest score is: " + average_no_lowest);
if(average_no_lowest > 90)
System.out.print('A');
else if(average_no_lowest < 90 && average_no_lowest > 80 )
System.out.print('B');
else if(average_no_lowest > 70 && average_no_lowest < 80)
System.out.print('C');
else
System.out.print('D');
}
else if (score3 < score1 && score3 < score2){
average_no_lowest =(score1 + score3)/2.0;
System.out.println("The average without the lowest score is: " + (score1 + score3)/2.0);
if(average_no_lowest > 90)
System.out.print('A');
else if(average_no_lowest < 90 && average_no_lowest > 80 )
System.out.print('B');
else if(average_no_lowest > 70 && average_no_lowest < 80)
System.out.print('C');
else
System.out.print('D');
}
}
}
</code>
Your section of code which reads
if(average_no_lowest > 90)
System.out.print('A');
else if(average_no_lowest < 90 && average_no_lowest > 80 )
System.out.print('B');
else if(average_no_lowest > 70 && average_no_lowest < 80)
System.out.print('C');
else
System.out.print('D');
}
is duplicated 3 times. Put it into a method.
Don't be worried about performance until it is a proven problem. Be worried about how easy it is to read and maintain the code.
Code has 2 audiences - the compiler and humans. Humans matter more than compilers.
Store the lowest score in a variable e.g. lowest_score.
and do the average by (total - lowest_score) / 2
It is the most efficient
You can use the following code,these have fewer lines of code and gives the same output as you want::
public static void main(String[] args) {
int score1, score2, score3, status;
double average, average_no_lowest;
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter three scores: ");
score1 = keyboard.nextInt();
score2 = keyboard.nextInt();
score3 = keyboard.nextInt();
average = (score1 + score2 + score3) / 3;
System.out.println("Average Score was:: " + average);
if (score1 < score2 && score1 < score3) {
status = 1;
System.out.println("The lowest score was:: " + score1);
} else if (score2 < score1 && score2 < score3) {
status = 2;
System.out.println("The lowest score was:: " + score2);
} else {
status = 3;
System.out.println("The lowest score was:: " + score3);
}
if (status == 1) {
average_no_lowest = (score2 + score3) / 2;
System.out.println("The average without the lowest score is: "
+ average_no_lowest);
} else if (status == 2) {
average_no_lowest = (score1 + score3) / 2;
System.out.println("The average without the lowest score is: "
+ average_no_lowest);
} else {
average_no_lowest = (score1 + score2) / 2;
System.out.println("The average without the lowest score is: "
+ average_no_lowest);
}
}