Instructions
Objective
Write a program to implement soda machine in java language.
Requirements and Specifications
Homework 1- Introduction to Java Applications and Problem Solving.
There should be one document that provides the problem-solving methodology to each problem. Each Step of the Engineering Problem Solving Process needs to be clearly documented. I. Understand the Problem, II. Design, III. Implementation (Code), IV Test Plan.
Problems 1 and 2 are not programming exercises, they are problem solving exercises.
For these problems, execute all the steps of the problem-solving process, except the implementation (Code) step III.
Problem 1. Soda Machine.Please write a Java Program that steps through the process of buying a soda from a Soda Machine. You must interact with the consumer. Each Soda is one dollar. You can assume the soda machine is always full of your favorite sodas. Your machine takes in only 1- and 5-dollar bills. Your machine calculates and returns change.
Problem2. Letter Grade. Please write a Java assignment that steps through the process of assigning letter grades based on number grades. Your program should ask the user to input a grade (integer from 1-100) and then output the letter grade. Scale: Greater than 90 is an "A"; Greater than 80 to 90 is a "B", Greater than 70 to 80 is a "C", Greater than 60 to 70 is a "D", and 60 or less is an "F".
Source Code
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
Explore our collection of free Java assignment samples to gain insights into coding techniques, problem-solving strategies, and Java programming best practices. Access them now to enhance your understanding and proficiency in Java development.
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java
Java