CSE 007 Lab 2: Arithmetic Expressions & Input/Output (I/O)
Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit
[CSE 007] Lab 2: Arithmetic Expressions & Input/Output (I/O)
Date Assigned: 3 Feb 2023 Date Due: 9 Feb 2023 @11:59pm
Submission : PizzaParty.java, PhoneNumber.java
Part 1: Pizza Party Planner
Description: The purpose of this program is to use values input by the user to calculate the cost of hosting pizza parties on Friday and Saturday nights, and ultimately determine if you can afford to host both parties.
TODO:
1. Begin by creating a program called PizzaParty.java. Be sure to include a header comment at the top of the file that includes your name, the date, the assignment, and a brief description of the program.
2. Declare public class PizzaParty { }
3. Within the class, declare public static void main (String []args){}
a. This is where all of the following will go:
4. Declaring variables: Declare the following variables, keeping in mind that some will be initialized using the user’s input (below):
double budget; //this holds the total amount you have to spend
on the weekend (we will compare this to the weekendTotal)
int numFriends; //this holds the number of people that will join
you for pizza each night
double avgSlicePerson; //this holds the number of slices that
the avg person eats and will be used to calculate the total
number of slices (pies) needed
double costPerPizza; //this holds the cost for a single pizza
(whole pie; slices not available separately)
int wholePizzas; //will hold the number of whole pizzas required
based on the slices that are estimated
double totalPizzaCost; //subtotal of cost of pizzas
double totalTaxOwed; //uses rate below to calculate the tax owed
on the total
double total; //will hold the total owed each night including
pizza, tax and delivery
final int slicePerPizza = 8; //this is the number of slices cut
from each pizza
final double salesTax = .06; //this holds the salesTax rate used
for the order (6%)
final double deliveryFee = 4.99; //this holds the flat delivery
fee applied to each order
5. Getting user input: The most straightforward way for us to get user input is to create a Scanner object and direct that to read from System.in (STDIN)
a. In order to have access to the Scanner class, you will need to add the following before the line declaring public class PizzaParty{}:
i. import java.util.Scanner;
b. Once you imported the package, now you need to create a Scanner object (variable):
i. Scanner scan = new Scanner (System.in);
c. Prompt the user for input & use scan to read in the following values:
i. Hint: prompt using System.out.println ()
ii. budget = scan.nextDouble ();
iii. numFriends = scan.nextInt();
iv. avgSlicePerson = scan.nextDouble ();
v. costPerPizza = scan.nextDouble ();
6. Evaluate the following expressions:
a. wholePizzas : Calculate the number of pizzas needed:
i. Use the numFriends and avgSlicePerson variables to calculate the number of slices required
double slices = (numFriends + 1) * avgSlicePerson;
ii. Convert the total slices required into wholePizzas using the constant slicePerPizza
double pizzas = slices/slicePerPizza;
iii. Use the Math.ceil () method to round the required number of pizzas (a double) up to the nearest whole number:
wholePizzas = (int) (Math.ceil(pizzas));
b. totalPizzaCost: Calculate the amount owed using costPerPizza and wholePizzas
c. totalTaxOwed: Calculate the total tax on the order using totalPizzaCost and salesTax
d. total: Calculate the final total for the order including totalPizzaCost, totalTaxOwed, and deliveryFee.
7. Display the calculated values to match the sample output below.
Friday Night Party
4 Pizzas: $42.00
Tax: $2.52
Delivery: $4.99
Total: $49.51
a. Rounding dollar values: All dollar amounts should be rounded to two decimal places. To do this, there are a few options:
i. System.out.printf("Total: $%.2f", total);
1. this uses the formatted print function; note the commas between args not plus sign
ii. System.out.println ("Total: $"+( (int) (total*100))/100.0);
1. This cuts off everything after two decimal places by multiplying by 100, truncating then dividing by 100.0 (to return the number to its new double value)
8. Repeat Steps #4-#7 once more to calculate the same costs for Saturday night.
a. You may assume that all constants will remain the same, and that the user will enter different values for numFriends, avgSlicesPerson, and costPerPizza.
9. Be sure to display the grand total (including both Friday & Saturday costs) for the user.
10. Finally, display a message to the user indicating whether or not their plans fall within their budget.
a. Note: there are NO IF statements required here. It is OK (and preferred) that you simply print the boolean value as shown in the sample run below.
Sample output is included here:
Welcome to my Pizza Party Planner!
How much do you have to spend? 61.25
Enter the number of friends on friday: 4
Enter the average slices eaten per person: 8
Enter the cost per pizza: 10.50
Enter the number of friends on Saturday: 3
Enter the average slices eaten per person: 2
Enter the cost per pizza: 9.99
5 Pizzas: $52.50
Tax: $3.15
Delivery: $4.99
Total: $60.64
Saturday Night Party
1 Pizza: $9.99
Tax: $0.60
Delivery: $4.99
Total: $15.58
Weekend Total: 6 Pizzas for $76.22
Can you afford? False
Part 2: Formatting Phone Numbers
Description: The purpose of this program is to practice using the modulus operator to isolate separate digits in an int value in order to reformat the phone number as a String.
Format Rule: Every US phone number consists of (at least) the following three parts: (Area Code) PreFix-LineNumber
TODO:
1. Begin by creating a program called PhoneNumber.java. Be sure to include a header comment at the top of the file that includes your name, the date, the assignment and a brief description of the program.
2. Declare public class PhoneNumber { }
3. Within the class, declare public static void main (String []args){}
a. This is where all of the following will go:
4. Declare long phoneNumber;
a. Just as the double is more precise (holds more decimal places) than a float, a long is able to hold more digits than an integer.
b. The max value that can be stored using integers is: 2147483647
c. Try running your program using an int … what happens if the phone number goes over the max value?
5. Prompt the user to enter a 10 digit phone number.
6. Use a Scanner variable to read in the user’s input.
a. Don’t forget to:
i. import java.util.Scanner; at the top of the file
ii. Declare & create the scanner variable: Scanner in = new Scanner (System.in);
b. Use the built-in method .nextLong () to read in a long value from STDIN using your scanner
7. Using a combination of % (modulo) and / (division) operations, isolate the three groups of numbers in the following way:
a. Using % 10 allows us to isolate the rightmost digits in a number.
i. So, to isolate the 2 rightmost digits in 572, we would use 572 % 100;
ii. This would yield 72
b. Using / 10 (as integer) division allows us to perform an operation and truncate the results. This will allow us to shift the decimal place by a desired number of spots.
i. 572/100 will return 5 (representing the third/last digit)
c. LineNumber: the last 4 digits of the number
i. phoneNumber % 10000 would return the rightmost 4 digits
ii. phoneNumber /= 10000; would cut off the rightmost 4 digits (essentially removing LineNumber from the phoneNumber variable)
d. Prefix: the next 3 digits of the number. After removing the LineNumber,
i. phoneNumber % 1000 would return the rightmost 3 digits
ii. phoneNumber /= 1000; would cut off the rightmost 3 digits (essentially removing the prefix from the phone number)
e. AreaCode: After removing the previous 7 digits (PreFix - LineNumber), the remaining digits represent the area code.
8. Using concatenation (Strings), display the phone number entered in the format indicated above by the rule and below in the sample output.
a. Hint: you can print out multiple arguments in a System.out.println() statement, separated by + indicating concatenation (not integer addition)
Sample run:
Enter a 10 digit phone number: 8005551212
Formatted phone number: (800) 555-1212
Notes:
● You should include documentation throughout your code (but not on every line)
● Be sure to compile and test often
● Input validation is not required for either program
Grading Rubric:
1. PizzaParty.java [+60 points]
a. Reasonable Code Submitted: x/10
b. Good Programming Practices: x/5
i. Header, formatting
c. Code Compiles: x/15
d. Output matches expected: x/30
i. Prompt the user to enter budget plus details for both fri and sat: x/5
ii. Display Friday Nights totals: x/5
iii. Display Saturday nights’totals: x/5
iv. Display the weekend total: x/5
v. Determine if you can afford: x/5
vi. All dollar amounts (in output for i - v) should be displayed to 2 decimal places only: x/5
2. PhoneNumbers.java [+40 points]
a. Good Programming Practices: x/5
i. Header, formatting
b. Code Compiles: x/10
c. Program Requirements: x/10
i. Read input in as a long (+5)
ii. use modulo operation to isolate groups of digits (+5)
d. Output matches expected: x/15
i. Prompt the user to enter a 10 digit phone number: x/5
ii. Display formatted version of : x/10
2023-02-28