In: Computer Science
This assignment is 2 programs demonstrating IF statements and Decisions.
Program 1
Write a program that asks a user for a temperature for two storage containers for a spaceship filled with water and oxygen. The program should tell if either is ice and if either is boiling.
Numbers to use for your tests
The Freezing point of water is 32 and the boiling point is 212.
The Freezing point of oxygen is -362 and the boiling point is -306.
Program 2
People eating at a restaurant get to eat for free if they are younger than 10, pay full price if they are under 65, get a $3 discount if they are 65 or over. Write a program asking the user for an age of the next customer and the price of the food they ordered. Calculate how much to charge based on the price (set to 0, full price, or with a discount) along with a 15% tip. Then have the program show this amount to the user.
Examples:
What age is the person
5
What price is the meal
10
The charge for this person is 0
(a second program run)
What age is the person
70
What price is the meal
10
The charge for this person is 8.05
Program 1: Java language
import java.util.Scanner;
public class FreezeAndBoiling {
public static void main(String[] args) {
//Create a Scanner object for
keyboard input.
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a temperature: ");
double temperature = keyboard.nextDouble();
//if statements
if (temperature <= 32) {
System.out.println("Water will
freeze.");
}
if (temperature >= 212) {
System.out.println("Water will
boil.");
}
if (temperature <= -362) {
System.out.println("Oxygen will
freeze.");
}
if (temperature >= -306) {
System.out.println("Oxygen will
boil.");
}
}
}
Output:
Program 2: Java language
import java.util.Scanner;
public class CalculateBill {
public static void main(String[] args) {
//Create a Scanner object for
keyboard input.
Scanner keyboard = new Scanner(System.in);
System.out.println("What age is the person");
int age = keyboard.nextInt();
System.out.println("What price is the meal");
int mealPrice = keyboard.nextInt();
double charge=0;
int discount=3;
// eat for free if they are younger than 10,
if(age<=10) {
System.out.println("The charge for
this person is 0");
}else if(age<65) {
//calculating charge after applying
tip 15%(15/100=0.15)
charge = mealPrice +
(mealPrice*0.15);
System.out.println("The charge for
this person is "+charge);
}else if(age>=65){
//calculating charge. discount if
age >65
charge = mealPrice -
discount;
//calculating charge after applying
tip 15%(15/100=0.15)
charge = charge +
(charge*0.15);
System.out.println("The charge for
this person is "+charge);
}
}
}
Output: