Problem 1
- Understand the problem
In this programming problem, it has been requested to program a soda vending machine. To do this, the user must enter the number of sodas to be purchased and must enter the number of $1 bills and the number of $5 bills that he will use to pay.
The vending machine must calculate the total cost of the sodas and the change that must be returned to the user. The machine will also need to verify that the payment delivered by the customer is sufficient to pay for all the sodas.
- Design
To solve this problem, the Scanner class will be used to request input from the user. This input will be checked (verifying that it does not enter negative values) and then the calculations will be performed, and finally the results will be displayed on the console.
A try-catch block will also be used which will help with java assignment detect non-numeric input, preventing the program from crashing. If the try-catch block detects that the user entered a non-numeric value, the program will display an error message indicating this problem.
- Test Plan
The tests will be very simple and a minimum of 4 tests will be executed, those will be:
- The first test will consist of entering valid values to verify that the program performs the correct calculations and displays the desired results on the screen. For example, if the user decides to buy 17 sodas, and decides to pay with 0 $1 bills and 4 $5 bills, the program should show that the total purchase is $17, that the user has paid with $20 and the change is $3.
- The second test will consist of entering valid values, but verifying that, if the user enters a number of bills that are not enough to pay for the purchase, the program will display an error message indicating that there is not enough money.
- The third test will consist of testing that the program correctly detects the input of negative values.
- Finally, the fourth test will consist of testing that the program correctly detects the input of non-numerical values.
Problem 2
- Understand the problem
This problem requires determining the letter grade for a numeric grade entered by the user.
The program will ask the user for the numerical value of the grade, between 0 and 100, and will determine the letter grade (A, B, C, D or F)
- Design
To solve this problem, the Scanner class will be used to request input from the user. This input will be checked (verifying that it does not enter negative values) and that the value is between 0 and 100.
A try-catch block will also be used which will help us to detect non-numeric input, preventing the program from crashing. If the try-catch block detects that the user entered a non-numeric value, the program will display an error message indicating this problem.
- Test Plan
The tests will be very simple and a minimum of 3 tests will be executed, those will be:
- The first test will consist of entering valid values to verify that the program performs the correct calculations and displays the desired results on the screen. For example, if the user enters a value of 62, the program should return ‘D’.
- The second test will consist of entering a value outside the admitted range (less than 0 or higher than 100). If the user enters a value outside the admitted range, the program should display the correct error message.
- Finally, the third test will consist of testing that the program correctly detects the input of non-numerical values.
Solution:
Problem 1:
import java.util.Scanner;
public class Problem1
{
public static void main(String[] args)
{
// Create scanner to read user's input
Scanner sc = new Scanner(System.in);
// define as constant, the price of each soda
final double SODA_PRICE = 1.0;
try
{
// Ask user for number of soda cans that he/she will buy
System.out.print("Enter the number of sodas: ");
int n_sodas = Integer.valueOf(sc.nextLine());
System.out.print("Enter the number if $1 bills that you'll use to pay: ");
int n_one = Integer.valueOf(sc.nextLine());
System.out.print("Enter the number of $5 bills that you'll use to pay: ");
int n_five = Integer.valueOf(sc.nextLine());
if(n_sodas < 1 || n_one < 0 ||n_five < 0)
{
System.out.println("The number of sodas must be positive, and the number of bills must be equal or higher than 0");
return;
}
// calculate total
double total = n_sodas*SODA_PRICE;
// Calculate payment
double payment = n_one + 5*n_five;
if(payment >= total)
{
// Calculate change
double change = payment - total;
System.out.println("\n*** INVOICE ***");
System.out.println(String.format("\t- %dx Soda (%.2f each)", n_sodas, SODA_PRICE));
System.out.println("---------------------------");
System.out.println(String.format("TOTAL: $%.2f", total));
System.out.println("---------------------------");
System.out.println(String.format("You paid: $%.2f", (double)(n_one + n_five*5.0)));
System.out.println(String.format("Your change is: $%.2f", change));
}
else
System.out.println("There is no enough money to pay all the sodas.");
}
catch(Exception ex)
{
System.out.println("You must enter valid numeric values.");
}
}
}
Problem 2:
import java.util.Scanner;
public class Problem2 {
public static void main(String[] args)
{
// Create Scanner
Scanner sc = new Scanner(System.in);
try
{
// ask for grade
System.out.print("Enter grade: ");
int grade = Integer.valueOf(sc.nextLine());
// check that grade entered by user is between 0 and 100
if(grade < 0 ||grade > 100)
{
System.out.println("Grade must be between 0 and 100.");
return;
}
// calculate letter grade
char letter_grade;
if(grade > 90)
letter_grade = 'A';
else if(grade > 80 && grade <= 90)
letter_grade = 'B';
else if(grade > 70 && grade <= 80)
letter_grade = 'C';
else if(grade > 60 && grade <= 70)
letter_grade = 'D';
else
letter_grade = 'F';
// Display
System.out.println("The final grade is: " + letter_grade);
}
catch(Exception ex)
{
System.out.println("You must enter a numeric value for the grade.");
}
}
}
Related Samples
At ProgrammingHomeworkHelp.com, we offer comprehensive assignment support to students, including an extensive range of related samples of Java assignments. Our expertly crafted samples cover various Java topics, ensuring you have access to high-quality, relevant content to guide your own work. Whether you're tackling basic Java programming, object-oriented design, or advanced Java concepts, our samples serve as excellent references to help you excel in your assignments. Trust ProgrammingHomeworkHelp.com to provide the resources and support you need to succeed in your Java coursework.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java