What This Assignment Is About: To declare and use variables. Primitive Data Type
ID: 3786663 • Letter: W
Question
What This Assignment Is About:
To declare and use variables.
Primitive Data Types
Arithmetic Expressions
Using the Scanner class to get some input from a user
Use the following Coding Guidelines:
Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc).
Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects).
Use tabs or spaces to indent code within blocks (code surrounded by braces). This includes classes, methods, and code associated with ifs, switches and loops. Be consistent with the number of spaces or tabs that you use to indent.
Use white space to make your program more readable.
Part 1: Written Exercises (7 points)
Note: The answers to the following questions should be typed in the block of comments in the Assignment2.java file. Please make sure they're commented out. Type them neatly and make them easy to read for the graders.
Exercise1: What output is produced by the following statement? (1 pt)
System.out.println("25 plus 20 is " + 25 + 20);
Exercise2: What value is contained in the integer variable size after the following statements are executed? (1 pt)
size = 16;
size = size + 6;
size = size * 3;
size = size / 4;
Exercise3: Given the following declarations, what result is stored in each of the listed assignment statements (what is the value of iResult or fResult)? (1 pt each)
int iResult, num1 = 15, num2 = 2;
double fResult, val1 = 2.0, val2= 12.68;
a). iResult = num1/num2;
b). fResult = num1/val1;
c). fResult = val2/num2;
d). fResult = num1/ (float) num2;
e). fResult = (float) (num1 / num2);
Part 2: Programming (13 points)
Write a complete Java program in a source file to be named Assignment2.java.
The program prints out the following menu to a user, using ' ' (tabs) and ' ' (newline), and print or println methods of System.out:
Welcome to our Smoothie Drink Shop!
---------------------------------------------
Price (Size):
Large (40oz) $8.50
Medium (30oz) $6.50
Small (20oz) $4.50
Then prompt to a user:
How many large size smoothie drinks would you like to buy?
and read in the number.
Then prompt to a user:
How many medium size smoothie drinks would you like to buy?
and read in the number.
Then prompt to a user:
How many small size smoothie drinks would you like to buy?
and read in the number.
Then the program computes and prints the following:
The total cost for the large size drinks
The total cost for the medium size drinks
The total cost for the small size drinks
The total cost for all drinks
The total number of drinks
The average cost per drink for this purchase
Use the NumberFormat class to format dollar amounts to be printed. The NumberFormat class is defined in the java.text package.
Output Sample: the user input is in bold
Welcome to our Smoothie Drink Shop!
---------------------------------------------
Price (Size):
Large (40oz) $8.50
Medium (30oz) $6.50
Small (20oz) $4.50
How many large size smoothie drinks would you like to buy?
9
How many medium size smoothie drinks would you like to buy?
6
How many small size smoothie drinks would you like to buy?
12
The total cost for the large size drinks: $76.50
The total cost for the medium size drinks: $39.00
The total cost for the small size drinks: $54.00
The total cost for all drinks: $169.50
The total number of drinks: 27
The average cost per drink for this purchase: $6.28
---------------------------------------------
For this and all subsequent assignments, provide a heading (in comments) which includes:
The assignment number.
Its author (your name).
Your Lab Letter.
A description of what this program is doing.
Grading Criteria for the part 2:
2 pts : it contains header information, code is nicely commented
1 pt : it has good coding style (indentation, spacing, etc.)
2 pts : it compiles
1 pt : it prints the correct cost for the large size drinks
1 pt : it prints the correct cost for the medium size drinks
1 pt : it prints the correct cost for the small size drinks
1 pt : it prints the correct cost for all drinks
1 pt : it prints the correct number of drinks
2 pt : it prints the correct average cost with the dollar format
1 pt : it outputs as expected and nicely formatted (messages to the monitor)
Submit your assignment by following the instructions below:
*********************************************************************************
Explanation / Answer
Assignment2.java
package game;
import java.util.Scanner;
/*
* What This Assignment Is About:
* To declare and use variables.
* Primitive Data Types
* Arithmetic Expressions
* Using the Scanner class to get some input from a user
*/
public class Assignment2 {
private static Scanner scanner;
public Assignment2() {
}
public static void main(String[] args) {
/*System.out.print("Excercise1: ");
System.out.println("25 plus 20 is " + 25 + 20);
int size = 16;
size = size + 6;
size = size * 3;
size = size / 4;
System.out.println("Excercise2: Size vale: "+size);
int iResult, num1 = 15, num2 = 2;
double fResult, val1 = 2.0, val2= 12.68;
iResult = num1/num2;
fResult = num1/val1;
fResult = val2/num2;
fResult = num1/ (float) num2;
fResult = (float) (num1 / num2);
System.out.println("Excercise3: ");
System.out.println("iResult: "+iResult+" fResult: "+fResult);*/
scanner = new Scanner(System.in);
System.out.println("Welcome to our Smoothie Drink Shop!");
System.out.println("---------------------------------------------");
System.out.println("Price (Size):");
System.out.println("Large (40oz) $8.50");
System.out.println("Medium (30oz) $6.50");
System.out.println("Small (20oz) $4.50");
int largeSize;
int mediumSize;
int smallSize;
System.out.print("How many large size smoothie drinks would you like to buy? ");
largeSize = scanner.nextInt();
System.out.print("How many medium size smoothie drinks would you like to buy?");
mediumSize = scanner.nextInt();
System.out.print("How many small size smoothie drinks would you like to buy?");
smallSize = scanner.nextInt();
double largeSizeDrinksCost = 8.50*largeSize;
double mediumSizeDrinksCost = 6.50*mediumSize;
double smallSizeDrinksCost = 4.50*smallSize;
double totalCost = largeSizeDrinksCost+mediumSizeDrinksCost+smallSizeDrinksCost;
System.out.println("The total cost for the large size drinks: $"+largeSizeDrinksCost);
System.out.println("The total cost for the medium size drinks: $"+mediumSizeDrinksCost);
System.out.println("The total cost for the small size drinks: $"+smallSizeDrinksCost);
System.out.println("The total cost for all drinks: $"+totalCost);
System.out.println("The total number of drinks: "+(largeSize+mediumSize+smallSize));
System.out.println("The average cost per drink for this purchase: $"+totalCost/(largeSize+mediumSize+smallSize));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.