OBJECTIVE: Code a program that uses the concepts covered in chapters 1-5 and lecture. This is the same program as PA1 except you’ll be recoding it with a combination of void, value-returning, and value-receiving methods. The output is the same except for validation of numeric input from the keyboard.
PREP WORK: Chapters 1-5 (including Shepherd’s PowerPoint slides); labs, in-class exercises or demos.
GRADING: You’ll be graded on how well you follow the program instructions and the accuracy of your output as reflected in the prompts, the output specifications, and the sample output.
This includes spacing and line advancing. Each line of output can be associated with multiple points in the code! Please make sure you correct your PA1 for formatting (line advancing, spacing) errors and that it is running with the correct output before attempting PA3. You are not at liberty to change anything, but code to these requirements. You’ll also be graded on the code given to you by your professor for this PA. The instructions are practically identical to PA1 except for the inclusion of methods, and changes to decision structures.
PROGRAM INSTRUCTIONS: Create a program that allows a user to purchase one or more of the laptops listed.
Logical Control Structures from PA1 with changes in brown:
Use a while loop to process for multiple laptops a given purchase. Use a sentinel-controlled loop
Use
if-else to figure out the laptop name and its price;
or the printing of the error message "Invalid choice! Try "
if, nested if to figure out when the choice for the laptop is valid to proceed with the rest of the processing for the laptop (quantity, calculations, addition of the laptop information to the final output);
the determination of the laptop name and price will be moved to their respective methods (see method instructions below) and their nested if-else structures will be replaced with switch statements.
if, nested if to only print order summary when laptops are actually
Use printf() with format specifiers where needed. Don’t forget to insert the exit statement at the end of main().
Code for Proper Output Alignment & Justification: where orderSummary is the String variable you’ve already declared and populated with the header information. Now you’ll add each laptop purchase (line item) to that header information. Use right and left justification to align the output. Use += to add to what is already in the orderSummary
Java Statements: ALL Java statements are to be typed exactly as shown. The following if-else prints a $ sign for the first line item; otherwise, there’s no $ sign printed. Change the if-else to a switch with only 2 cases and no default. Look at the output specs above or the sample output. The trigger variable determines the first line item. NOTE: There is a space between the double quotes.
if(trigger == 1)
{
orderSummary += String.format("%n%,-9d %-30s %8s $%,17.2f",
qty, laptop, " ", lineItem);
|
trigger = 0;
}//END if for $ sign. else
{
orderSummary += String.format("%n%,-9d %-30s %9s %,17.2f",
qty, laptop, " ", lineItem);
}//END else for no $ sign.
Use printf() with format specifiers where
CODE FOR FINAL OUTPUT:
//The following Java statement prints the order summary of the laptops purchased.
//The print should be done when there are no more laptops to process.
if(choice > 0 && choice < 6)
{
tax = subtotal * .0825; total = subtotal + tax;
orderSummary += String.format("%n%n%34s Subtotal %6s %,17.2f"
+ "%n%31s Tax @ 8.25%% %6s %,17.2f"
|
+ "%n%n%37s TOTAL %5s $%,17.2f%n",
" ", " ", subtotal,
" ", " ", tax,
" ", " ", total);
System.out.printf("%s", orderSummary);
}//END if valid choice from 1 through 5
Program Logic: NO plan for this
The prompts (to return press Alt then left arrow) tell you what input variables you will
The final output specifications will tell you the type of calculations you will need (if any) and whether you will need to declare additional
The sample output will tell you the order of logic for your
Instructions for Coding Methods: Only code what is asked for in these methods and nothing more.
Values that are returned by methods, and used multiple times in the method that calls for the value, should be stored in local
Variables used in more than one method should be fields (class variables). There are 3 fields: objects for the classes that are imported; orderSummary.
main(): Only 2 lines of code: processOrder() is called and the exit
processOrder(): This method is not value-receiving or value-returning. It contains all the code that was originally in main().
Move the code from main() to this method, then work line-by-line to determine which variables will be fields (class variables), and which will be local, and which statements are to be moved to the other
The while-loop, the IF-ELSEs, the calculations remain in this
Use a logical operator to range test the
Replace the if-else for the trigger with a switch. Use 2 cases no
The print for the error message will now be in an else option for the first range test of
The other methods will be called in processOrder(). In processOrder(), these methods are called in place of the code that was moved into
Other methods:
setLaptopChoice(): Prompts for the laptop choice and returns it from the keyboard. Validate the integer input before returning it using a while validation
setLaptopName(): Receives the laptop choice and returns the name of the laptop. Use a switch statement to determine the laptop
setLaptopPrice(): Receives the laptop choice and returns the price of the laptop. Use a switch statement to determine the laptop price.
setLaptopQty(): Receives the laptop name, prompts for the quantity, and returns it from the keyboard. Validate the integer input before returning it using a while validation loop. Use in calculation of the lineItem.
getErrorMsg(): Returns the formatted String for the error message “Invalid choice! Try ”.
DescriptionIn this final assignment, the students will demonstrate their ability to apply two ma
Path finding involves finding a path from A to B. Typically we want the path to have certain properties,such as being the shortest or to avoid going t
Develop a program to emulate a purchase transaction at a retail store. Thisprogram will have two classes, a LineItem class and a Transaction class. Th
1 Project 1 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of
1 Project 2 Introduction - the SeaPort Project series For this set of projects for the course, we wish to simulate some of the aspects of a number of