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
Related
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);
}
i am trying to output the minimum amount of notes needed from two inputs. one being the cost and the other being the amount handed over by the customer.
so if i enter 400 and then 500, it should say 2 $50 dollars notes.
or if i enter 60 and then 80, it should say 1 $20 dollar note.
this is a small bit of my code:
import java.util.Scanner;
public class bank {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter amount due: ");
int amount = input.nextInt();
System.out.println("Amount tendered: ");
int tmp = input.nextInt();
int change;
if(amount >= 50)
{
change =(tmp - amount)/50;
System.out.println (change + " $50 bills");
}
if(amount >= 20)
{
change =(tmp - amount)/20;
System.out.println (change + " $20 bills");
}
You can achieve it by using mod operator %
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter amount due: ");
int amount = input.nextInt();
System.out.println("Amount tendered: ");
int tmp = input.nextInt();
int change;
int diff = tmp - amount;
if (diff % 50 == 0) {
change = diff / 50;
System.out.println(change + " $50 bills");
}
else if (diff % 20 == 0) {
change = diff / 20;
System.out.println(change + " $20 bills");
}
}
Once you calculate the amount of change in a value of a bill, you need to lower the amount of change required. This is done in the code changeRequired = changeRequired - change;
System.out.println("Amount tendered: ");
int tmp = input.nextInt();
int changeRequired = tmp - amount;
int change;
if(amount >= 50)
{
change =(changeRequired)/50;
changeRequired = changeRequired - change;
System.out.println (change + " $50 bills");
}
if(amount >= 20)
{
change =(changeRequired )/20;
changeRequired = changeRequired - change;
System.out.println (change + " $20 bills");
}
Once you get your change which is the variable amount, you can use the following code to find out how many notes of each of the $50, $20, $10, $5, $1 there are in the change. The number of notes is minimal.
numOf50 = 0; // number of $50 dollar bills, the rest is similar.
numOf20 = 0; // ...
numOf10 = 0; // ...
numOf5 = 0;
numOf1 = 0;
if (amount / 50 > 1){
numOf50 += (amount / 50);
amount %= 50; // update the amount with the remainder
}
else if (amount / 20 > 1){
numOf20 += (amount /20);
amount %= 20;
}
else if (amount / 10 > 1){
numOf10 += (amount / 10);
amount %= 10;
}
else if (amount / 5 > 1){
numOf5 += (amount / 5);
amount %= 5;
}
else if (amount <=5 && amount >0){
numOf1 = amount;
}
// output is trivial, omitted.
Haven't tested on a machine but it should work or give you an idea.
My program for some reason is calculating the GPA average wrong. If I enter 4.0 three times, then it says the average GPA is 3.0 but should be 4.0. Can someone help me find the issue?
//variables
double gpa = 0;
double total = 0;
int counter = 0;
int counter2 = 0;
do
{
String gpaEntry = JOptionPane.showInputDialog("Please enter GPAs:");
gpa = Double.parseDouble(gpaEntry);
if (gpa >= 3.5)
counter2 ++;
total += gpa;
counter ++;
}
while (gpa != 0);
double average = (double) (total/counter);
JOptionPane.showMessageDialog(null, "The Average GPA is: " + average);
JOptionPane.showMessageDialog(null, "Number of students:" + counter2);
Let's walk through the code
gpa = 0
get user input (user enters '2')
now gpa = 2
total += 2
counter ++
while(gpa != 0) // nope, gpa is 2
loop back
get user input (user enters '0')
now gpa = 0
total += 0
counter ++ // oops!
while(gpa != 0) // yep, quit the loop
but it's too late, we already incremented counter, so our average calculation is wrong
What is wrong is that if a user enters 0, then it runs the program and then exits.
Try this code (Sorry, I do not have an editor at the moment so you might have to fix some small thing).
//variables
double gpa = 0;
double total = 0;
int counter = 0;
int counter2 = 0;
String gpaEntry = JOptionPane.showInputDialog("Please enter GPAs:");
gpa = Double.parseDouble(gpaEntry);
while (gpa != 0) {
if (gpa >= 3.5)
counter2 ++;
total += gpa;
counter ++;
gpaEntry = JOptionPane.showInputDialog("Please enter GPAs:");
gpa = Double.parseDouble(gpaEntry);
}
JOptionPane.showMessageDialog(null, "The Average GPA is: " + average);
JOptionPane.showMessageDialog(null, "Number of students:" + counter2);
Comment if you have any more questions.
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.
I'm taking a beginners course in Java and they asked us to:
Write a program to tally the number of A's, B's, C's, D's and F's based upon a list of scores entered by a user.
After all the scores are entered, the program must display a
horizontal bar graph of the number tallied for each grade like that shown in the Operation section.
The tally graph must display a single '*' for each unit tallied.
When a user enters a -1, the program must display the final graph and
exit.
The output of your program must display prompts and a tally graph
like that shown in the Operation section above.
You may assume that a user will enter numbers only.
Operation:
The program starts and prompts you to either enter a test score or end
the program by entering a -1. Like the following:
Number of A's: 0
Number of B's: 0
Number of C's: 0
Number of D's: 0
Number of F's: 0
Enter a score (%) or -1 to end: 90
As you enter each score, the application decides whether the score is
an A, B, C, D or F and adds one to the letter-grade tally. Like the
following:
Number of A's: 1
Number of B's: 0
Number of C's: 0
Number of D's: 0
Number of F's: 0
Enter a score (%) or -1 to end: 95
Each time you enter a score, the program updates the tally. Like the following:
Number of A's: 2
Number of B's: 0
Number of C's: 0
Number of D's: 0
Number of F's: 0
Enter a score (%) or -1 to end: -1
When you are done entering scores, the program displays a horizontal
bar graph of the tally for A's, B's, C's, D's and F's. Like the
following:
A's: **
B's:
C's:
D's:
F's:
Specifications:
Numerical Grade Letter Grade
greater than or equal to 90 A
less than 90 but greater than or equal to 80 B
less than 80 but greater than or equal to 70 C
less than 70 but greater than or equal to 60 D
less than 60 F
When the program ends, display the number of scores, average score, and best score.
Example:
Number of scores: 2
Average score: 92.5
Best score: 95.0
We have also been instructed to code in at least two methods : one that has a void and one that returns something.
So far I have only been able to tally up the scores entered, but I don't seem to be able to wrap my head around recording all the inputs and calculating the average and picking out the best score
This is what I have so far :
import java.util.*;
public class ScoreTally {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int scoreCount = -1;
double score = 0;
while (score != -1) {
if (score >= 90)
aCount++;
else if ((score < 90) && (score >= 80))
bCount++;
else if ((score < 80) && (score >= 70))
cCount++;
else if ((score < 70) && (score >= 60))
dCount++;
else if ((score < 60) && (score > 0))
fCount++;
System.out.println("Number of A's: " + aCount);
System.out.println("Number of B's: " + bCount);
System.out.println("Number of C's: " + cCount);
System.out.println("Number of D's: " + dCount);
System.out.println("Number of F's: " + fCount);
System.out.print("Enter a score (%) or -1 to end: ");
score = input.nextDouble();
scoreCount++;
}
if (score == -1)
System.out.println("Number of scores: " + scoreCount);
}
}
Use a function to return the greatest number to get your best score.
And another function to print the final graph.
That should take care of the two function requirement.
Also, make the loop an exit-controlled one, as it has to run atleast once.
As a coding practice, you should not be using star imports(import java.util.*) instead use only what you need.
Also, good work on setting the scoreCount to -1 in the beginning.
import java.util.Scanner;
public class ScoreTally {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int scoreCount = -1;
double score;
double totalScore = 0;
double bestScore = -1;
do {
System.out.println("Number of A's: " + aCount);
System.out.println("Number of B's: " + bCount);
System.out.println("Number of C's: " + cCount);
System.out.println("Number of D's: " + dCount);
System.out.println("Number of F's: " + fCount);
System.out.print("Enter a score (%) or -1 to end: ");
score = input.nextDouble();
if (score >= 90) {
aCount++;
} else if ((score < 90) && (score >= 80)) {
bCount++;
} else if ((score < 80) && (score >= 70)) {
cCount++;
} else if ((score < 70) && (score >= 60)) {
dCount++;
} else if ((score < 60) && (score > 0)) {
fCount++;
}
scoreCount++;
totalScore = totalScore + score;
bestScore = greaterNumber(bestScore, score);
} while (score != -1);
printGraph('A', aCount);
printGraph('B', bCount);
printGraph('C', cCount);
printGraph('D', dCount);
printGraph('F', fCount);
System.out.println("Number of scores: " + scoreCount);
System.out.println("Average scores: " + totalScore / scoreCount);
System.out.println("Best scores: " + bestScore);
}
public static void printGraph(char grade, int count) {
System.out.print("Number of " + grade + "'s: ");
for (int i = 0; i < count; i++) {
System.out.print("*");
}
System.out.println();
}
public static double greaterNumber(double firstNum, double secondNum) {
if (firstNum >= secondNum) {
return firstNum;
} else {
return secondNum;
}
}
}
To begin with you already have the code to tally the results.
int aCount = 0;
int bCount = 0;
int cCount = 0;
int dCount = 0;
int fCount = 0;
int scoreCount = -1;
What you need is another field that will hold the total score i.e.
int totalScore = 0;
You need to prompt for the first score before you enter the while loop otherwise it won't run properly the first iteration. At the end (or the beginning) you need to update totalScore with whatever was the input for that round, and not update it if the input was -1.
The average score will just be the totalScore/scoreCount you should compute that after you finish the while loop.
For the best score you can have another field for int = maxScore
you can update it every iteration of the loop with
maxScore = Math.max(maxScore, score);
Math.max returns the maximum of the two numbers.
It's rather simple. Declare 2 variables, one for having avgScore, one for in beginning of your program.
double bestScore = 0.0;
double avgScore = 0.0;
double totalScore = 0.0;
while (score != -1) {
totalScore = totalScore + score;
if(score > bestScore)
bestScore = score;
if (score >= 90)
aCount++;
else if ((score < 90) && (score >= 80))
bCount++;
else if ((score < 80) && (score >= 70))
cCount++;
else if ((score < 70) && (score >= 60))
dCount++;
else if ((score < 60) && (score > 0))
fCount++;
System.out.println("Number of A's: " + aCount);
System.out.println("Number of B's: " + bCount);
System.out.println("Number of C's: " + cCount);
System.out.println("Number of D's: " + dCount);
System.out.println("Number of F's: " + fCount);
System.out.print("Enter a score (%) or -1 to end: ");
score = input.nextDouble();
scoreCount++;
}
if (score == -1){
System.out.println("Number of scores: " + scoreCount);
avgScore = totalScore/scoreCount;
System.out.println("Best Score: " + bestScore);
System.out.println("Avg Score: " + avgScore);
}
Also for displaying the chart you can use your final tallies.