If the customer pays less than the ticket price [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Help me here i tried make success , program but theres a mistakes here can?
any one solve it and send it ,
If the customer pays less than the ticket price, I want to print the money less than the ticket price and print the rest to pay, or if the customer pays more than the ticket price, the rest of the money is printed to the customer, and finally if he pays the ticket amount, you print welcome
import java.util.Scanner;
public class Flow {
static Scanner input = new Scanner(System.in);
static int age;
static double ticketpricetax = 0.15 * 100 + 10;
public static double remainigamount = ticketpricetax;
public static double howmuchpay = 10;
public static void main(String[] args) {
System.out.println("Age");
int age = input.nextInt();
if (age > 12 || age <= 50) {
System.out.println("ticket price is ");
System.out.println(ticketpricetax);
System.out.println("Please pay your ticket");
}
input.nextInt();
if (ticketpricetax > howmuchpay) {
System.out.println("the money less than ticket price");
} else if (ticketpricetax > howmuchpay) {
System.out.println("the money is greater than ticket price");
} else {
System.out.println("welcome");
}
}
}

Your price input is not stored in any variable and it should be double not int, and then you are comparing ticketpricetax and howmuchpay with their initialized values,howmuchpay is not updated with input. In addition you're making an incorrect check and missing some statements.
Here is the correction :
import java.util.Scanner;
public class Flow {
static Scanner input = new Scanner(System.in);
static int age;
static double ticketpricetax = 0.15 * 100 + 10;
public static double howmuchpay = 10;
public static void main(String[] args) {
System.out.println("Age");
int age = input.nextInt();
if (age > 12 || age <= 50) {
System.out.println("ticket price is ");
System.out.println(ticketpricetax);
System.out.println("Please pay your ticket");
}
howmuchpay = input.nextDouble();
if (ticketpricetax > howmuchpay) {
System.out.println("the money less than ticket price");
System.out.println("rest of the money to pay = "+(ticketpricetax-howmuchpay));
} else if (ticketpricetax < howmuchpay) {
System.out.println("the money is greater than ticket price");
System.out.println("rest of the money = "+(howmuchpay-ticketpricetax));
} else {
System.out.println("welcome");
}
}
}

Related

Debugging my letter grade calculator in Java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am supposed to create a program to do the following:
A student must take two quizzes and two exams during the semester. One quiz and one exam are dropped from the calculation of their final grade. The remaining quiz counts as 40% of the final grade and the exam counts for 60% of their final grade.
The final grade is calculated on a straight scale: 90% and above is an “A”, less than 90% to 80% is a B, etc.
This program should allow a professor to enter a student’s name, id number, two quiz scores and two exam scores and then compute and output the student’s final grade.
Below is the code I have written. It executes just fine, without any errors, and I feel like I have completed everything correctly; however, the program returns the letter grade "A" no matter what scores I put in.
public static void main(String[] args)
{
//Declare all variables
String name;
String idNum;
int q1;
int q2;
int e1;
int e2;
int bestQuiz;
int bestExam;
double score;
char letterGrade;
Scanner kbd;
kbd = new Scanner(System.in);
System.out.println("Enter student's name: ");
name = kbd.nextLine();
System.out.println("Enter student's ID number: ");
idNum = kbd.nextLine();
System.out.println("Enter the quiz scores: ");
q1 = kbd.nextInt();
q2 = kbd.nextInt();
System.out.println("Enter the exam scores: ");
e1 = kbd.nextInt();
e2 = kbd.nextInt();
bestQuiz = max(q1, q2);
bestExam = max(e1, e2);
score = computeRawPercentage(bestQuiz, bestExam);
letterGrade = finalGrade( score );
System.out.print(name + " " + idNum + " ");
System.out.println("Final Grade: " + letterGrade);
}
//Max method
public static int max(int n1, int n2){
int big;
if (n1 > n2){
big = n1;
}
else {
big = n2;
}
return big;
}
//computeRawPercentage method
public static double computeRawPercentage(int quizScore, int examScore){
return ((quizScore * .4)+ (examScore * .6))*100;
}
//finalGrade method
public static char finalGrade(double grade){
char letterGrade;
if (grade >= 90.0)
letterGrade = 'A';
else if ((grade >= 80.0)&&(grade < 90.0))
letterGrade = 'B';
else if ((grade >= 70.0)&&(grade < 80.0))
letterGrade = 'C';
else if ((grade >= 60.0)&&(grade < 70.0))
letterGrade = 'D';
else
letterGrade = 'F';
return letterGrade;
}
}
Your problem is this line:
return ((quizScore * .4)+ (examScore * .6))*100;
Here you are are multiplying your score by 100 which will give you a score of something like 7100 when it should be 71 (which is clearly above 90.0). Delete the 100 multiply and it will work:
return ((quizScore * .4) + (examScore * .6));
Test Run:
Enter student's name:
TestName
Enter student's ID number:
12345
Enter the quiz scores:
55
65
Enter the exam scores:
65
86
77.6
TestName 12345 Final Grade: C
This is assuming you enter a score of 75% as 75.

Why isn't my else statement working? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
pretty straightforward as to what I am curious about. I dont understand why when I type in an amount that is 0 or less than for the withdrawal I do not get the error message that I have in the class.
Code is here:
Mainclass:
import java.text.NumberFormat;
public class Account {
private final double RATE = 0.03; // interest rate of 3%
private String customer;
private int id;
private long account_id;
public double balance;
// Creates new account with specified information
public Account(int id, String name, long acctNum, double acctBalance) {
customer = name;
account_id = acctNum;
balance = acctBalance;
}
public double deposit(double amount) {
// If deposit is larger than 0 increase by deposit amount
if (amount > 0)
balance = balance + amount;
else {
System.out.println("Invalid deposit amount");
}
return balance;
}
public double withdraw(double amount) {
// If withdrawal is larger than 0 reduce balance by amount
if (amount <= balance || amount > 0)
balance = balance - amount;
else {
System.out.println("Invalid withdrawal amount or insufficient funds");
}
return balance;
}
// Method to add interest to account
public double addInterest() {
balance += (balance * RATE);
return balance;
}
public double getBalance() {
return balance;
}
#Override
public String toString() {
NumberFormat fmt = NumberFormat.getCurrencyInstance();
return (customer + "\t" + account_id + "\t" + fmt.format(balance));
}
}
Driver class:
import java.util.Scanner;
public class AccountManager {
public static void main(String[] args) {
// Create array space for 30 accounts
String customer;
long account_id;
double balance;
int id = 0;
int count = 0;
int amount = 0;
Account[] account_array = new Account[30];
Scanner scan = new Scanner(System.in);
String input = "";
System.out.println("Would you like to create an account?");
System.out.println("Type 'yes' to add an account");
input = scan.nextLine();
while (input.equals("yes")) {
System.out.println("To create an account please enter the following: ");
System.out.println("ID number (0,1,2,3..etc)");
id = scan.nextInt();
scan.nextLine();
System.out.println("Name: ");
customer = scan.nextLine();
System.out.println("Account Number: ");
account_id = scan.nextLong();
System.out.println("Current Balance: ");
balance = scan.nextDouble();
scan.nextLine();
account_array[count] = new Account(id, customer, account_id, balance);
count++;
System.out.println("Would you like to create another account?");
System.out.println("Type 'yes' to add another account");
input = "";
input = scan.nextLine();
}
System.out.println("Enter the ID of the account you would like to make changes to: ");
id = scan.nextInt();
scan.nextLine();
System.out.println("Would you like to deposit or withdraw cash? ");
input = scan.nextLine();
if (input.equals("withdraw")) {
System.out.println("How much would you like to withdraw?");
amount = scan.nextInt();
account_array[id].withdraw(amount);
System.out.println("Balance for account: " + id + " = " + account_array[id].balance);
}
if (input.equals("deposit")) {
System.out.println("How much would you like to deposit?");
amount = scan.nextInt();
account_array[id].deposit(amount);
System.out.println("Balance for account: " + id + " = " + account_array[id].balance);
}
}
}
Because amount <= balance holds true. Use && instead of ||.
if(amount <= balance && amount > 0){

Java counter is off---Not like anything I've seen here yet [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The counter for pass or fail is backwards. It uses a sentinel and counts fails and passes. Only it is counting in reverse. If you enter a passing grade it adds it to fails, etc. This is total code:
I just edited it...
import java.util.Scanner;
public class PassOrFail1 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total;
int gradeCounter;
int grade;
double average;
int passes = 0;
int fails = 0;
int studentCounter = 0;
total = 0;
gradeCounter = 0;
System.out.println("Enter a grade");
grade = input.nextInt();
while (grade != 000) {
total = total + grade;
gradeCounter++;
studentCounter++;
System.out.println("Enter a grade");
grade = input.nextInt();
if( grade >= 70 ){
passes++;
}
else
{
fails++;
}
}
if(gradeCounter != 0){
average = (double)total/gradeCounter;
System.out.printf("The average for the grades is %.2f\nGradeCounter is %11d",average,gradeCounter);
}
System.out.printf("\nNumber of Passes is %7d \nNumber of Fails is %8d\nand number of students is %d", passes, fails, studentCounter);
}
}
You may try this code instead >>> !
import java.util.Scanner;
public class PassOrFail {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int total;
int gradeCounter;
int grade;
double average;
int passes = 0;
int fails = 0;
int studentCounter = 0;
total = 0;
gradeCounter = 0;
System.out.println("Enter a grade");
grade = input.nextInt();
while (grade != -1) {
total = total + grade;
gradeCounter++;
studentCounter++;
if( grade >=70 ){
passes++;
}
else
{
fails++;
}
System.out.println("Enter a grade");
grade = input.nextInt();
}
if(gradeCounter != 0){
average = (double)total/gradeCounter;
System.out.printf("The average for the grades is %.2f\n GradeCounter is %d",average,gradeCounter);
}
System.out.printf("\nNumber of Passes is %d \n Number of Fails is %d ", passes, fails);
}
}

Java Program Loop

i want to be able to have the program to start over (show the enter loan amount prompt) after typing 'y' when asked "to calculate the program again" or end program if user input 'n'.
import java.util.Scanner;
public class MonthlyMortgageRate {
public static void main(String[] args) {
double Amount;
double Rate;
double Months;
double outputNum1;
Scanner in = new Scanner(System.in);
System.out.print("Enter loan amount:");
Amount = in.nextDouble();
System.out.print("Enter rate:");
Rate = in.nextDouble() / 100 / 12;
System.out.print("Enter year:");
Months = in.nextDouble() * 12;
outputNum1 = Rate * Amount / (1 - Math.pow(1 + Rate, -Months));
if (Amount <= 0)
System.out.println("You must enter positive numeric data!");
else
System.out.printf("Monthly payment is: $ %.2f%n", outputNum1);
System.out.println("would you like to calculate again?(y/n)");
}
private static double inputNum2(double d) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Basically you need to loop the code in main.
do {
// your code here
System.out.println("would you like to calculate again?(y/n)");
} while (in.next().equalsIgnoreCase("y"))
Add the following to your code:
Boolean doagain = true;
// after Scanner in
while(doagain)
{
// your code
System.out.println("would you like to calculate again?(y/n)");
if ( in.next().toLowerCase().equals("n") ) doagain = false;
} // end while
// your code
Enjoy Cliff

Reading user input in Java

I need to design and implement an application called CinemaPrice to determine how much a person pays to go the the cinema. The program should generate an age from 1 - 100 using the Random class and prompt the user for the full ticket price. Then display the appropriate ticket price using the currency format (an example in your book ). You may want to refer to the example we did together in class to help you with the "if statement". Decide ticket price on the following basis:
1. under 5, free;
2. aged 5 to 12, half price;
3. aged 13 to 54, full price;
4. aged 55, or over, free.
I would really like some help on this I'm new to java and been spending hours on this now I would love to finish it :)
This is what I have so far:
import java.util.Scanner; //Needed for the Scanner class
import java.util.Random;
import java.text.DecimalFormat;
public class CinemaPrice
{
public static void main(String[] args) //all the action happens here!
{ Scanner input = new Scanner (System.in);
int age = 0;
double priceNumber = 0.00;
Random generator = new Random();
age = generator.nextInt(100) + 1;
if ((age <= 5) || (age >=55) {
priceNumber = 0.0;
}else if (age <= 12){
priceNumber = 12.50;
}else {
system.out.println("Sorry, But the age supplied was invalid.");
}
if (priceNumber <= 0.0) {
System.out.println("The person age " + age + " is free!);
}
else {
System.out.println("Price for the person age " + age + "is: $" + priceNumber);
}
} //end of the main method
} // end of the class
I don't know how to prompt and read input from a user though - can you help?
The first issue that I see is that you need to update your conditional statement here as anything from 13 to 54 will be an invalid age...
if ((age <= 5) || (age >=55) {
priceNumber = 0.0;
}else if (age <= 12){
priceNumber = 12.50;
}else if (age < 55){
//whatever this ticket price is
}else {
system.out.println("Sorry, But the age supplied was invalid.");
}
Something like that would work...
You have stated that your real problem is getting data into your program, the following should demonstrate using the Scanner class
public static void main(String[] args) {
System.out.println("Enter an age");
Scanner scan=new Scanner(System.in);
int age=scan.nextInt();
System.out.println("Your age was " + age);
double price=scan.nextDouble();
System.out.println("Your price was " + price);
}
Now thats the basic idea, but if you provide an incorrect input (like a word) you can get an exception, you can however check that the input you're getting is correct and only accept it if its what you want, like this;
public class Main{
public static void main(String[] args) {
System.out.println("Enter an age");
Scanner scan=new Scanner(System.in);
while (!scan.hasNextInt()) { //ask if the scanner has "something we want"
System.out.println("Invalid age");
System.out.println("Enter an age");
scan.next(); //it doesn't have what we want, demand annother
}
int age = scan.nextInt(); //we finally got what we wanted, use it
System.out.println("Your age was " + age);
}
}

Categories

Resources