Question
Inventory Valuation - Using Methods in Java Open Eclipse or NetBeans and create a Java project with the following details. For Project Name include: Inventory
Inventory Valuation - Using Methods in Java
Open Eclipse or NetBeans and create a Java project with the following details.
For Project Name include: Inventory
For the Main Class include: Inventory
In your Code window, shown below, copy in the program code shown in Figure 1 below, in the appropriate places, except substitute your own name in place of Sammy Student.
PROJECT Inventory Valuation - Using Methods in Java
Figure 1 Source Code for the Inventory Valuation Program
package lab6; import java.util.Scanner; //Sammy Student, Programmer public class Inventory { static Scanner sc = new Scanner(System.in); public static void main(String args[]) { // begin local variable declaration / initialization zone char answer = 'Y'; double average = 0.0, cost = 0.0, totValue = 0.0; int number = 0, sumItems = 0, quantity = 0; String item = ""; // end local variable declaration / initialization zone
// begin program menu System.out.println(""); System.out.println(""); System.out.println("***************************"); System.out.println("----Inventory Valuation---"); System.out.println("***(Weighted Average Method)***"); System.out.println(""); System.out.println(""); System.out.println("--------- M E N U ---------"); System.out.println(""); // end program menu
while(answer == 'Y' || answer == 'y') { // begin code block for inventory evaluation System.out.println("number of item types in the inventory ->"); number = sc.nextInt();
for(int i = 1; i <= number; i++) { System.out.println("enter the item's description"); item = sc.next(); System.out.println("item description: " + item); System.out.println("enter item quantity"); quantity = sc.nextInt(); sumItems += quantity; System.out.println("enter item cost"); cost = sc.nextDouble(); totValue += cost * quantity; } // outside the for() loop |
PROJECT Inventory Valuation - Using Methods in Java
Figure 1 Source Code for the Inventory Valuation Program ( continued )
average = totValue / sumItems; // end code block for inventory evaluation
// begin code block to display results System.out.printf("average cost: $%.2f ", average); System.out.println("***************************"); // end code block to display results
// begin code block to perform additional program run System.out.println("run again(Y or N)?"); answer = sc.next().charAt(0); // end code block to perform additional program run } System.out.println("***************************"); }// end main() method }// end class |
STEP 2 Build, Compile and Run the Program
From the menu select [ Run ] and click [ Run Project ] to run your app.
STEP 3 Test the Program
Once you have successfully compiled your program, review the output Console window of your Java editor.
Enter the sample information shown in Figure 2 that follows as a single run.
You can verify your output with this MS Excel formula:
=(5*750+6*3250+10*926)/(5+6+10)
STEP 4 Review the Starter Code Statements
With the starter code running successfully and the output showing similar results to that given in Figure 2 , review the statements that comprise the code.
The starter code has two core program blocks: one to display a menu and another to evaluate the inventory.
In the next few steps you will now segment your program into these two core methods. Separating your program into code blocks or methods assists in having less program statements occupying space in the main() program method.
You will also move some of the local variables in the starter code and make them class - level variables, such that these variables will be recognized and available to all the methods in your program.
PROJECT Inventory Valuation - Using Methods in Java
Figure 2 Initial Test Run
*************************** ----Inventory Valuation--- ***(Weighted Average Method)*** --------- M E N U --------- number of item types in the inventory -> 3 enter the item's description Chair item description: chair enter item quantity 5 enter item cost 750 enter the item's description Computer item description: computer enter item quantity 6 enter item cost 3250 enter the item's description Desk item description: desk enter item quantity 10 enter item cost 926 average cost: $1548.10 *************************** run again(Y or N)? |
STEP 4 Construct a Method
Locate the following code statements, that were given in the initial starter code.
System.out.println("***************************"); }// end main() method }// end class |
As shown below, define a method that will be used to display the program menu.
PROJECT Inventory Valuation - Using Methods in Java
System.out.println("***************************"); }// end main() method static void displayMenu() { // place method body statements below }// end method }// end class |
Locate the following block of code statements.
// end local variable declaration / initialization zone
// begin program menu |
Between the above two comment statements, write the statements below that will call the method that you just created.
// call a method
displayMenu();
You will now move a block of the original start code into the method.
Locate the following block of code that is given in the original code.
// begin program menu System.out.println(""); System.out.println(""); System.out.println("***************************"); System.out.println("----Inventory Valuation---"); System.out.println("***(Weighted Average Method)***"); System.out.println(""); System.out.println(""); System.out.println("--------- M E N U ---------"); System.out.println(""); // end program menu |
Cut this block of code in your original code and paste the block in the body of the method displayMenu() .
PROJECT Inventory Valuation - Using Methods in Java
After you move the code block into the method, run your program and test it again. Did the program run the same as it did before?
STEP 5 Construct Another Method
Locate the following block of code that appears within the current version of your program.
static void displayMenu() { // place method body statements below . . . }// end method }// end class |
Construct another method named averageCost() , as shown below.
}// end main() method static void displayMenu() { // place method body statements below . . . }// end method static double averageCost() { // place method body statements below . . . return average; }//end method }// end class |
Cut the following variable declarations from your current program and place them in your new method, at the top of the methods body.
double average = 0.0, cost = 0.0, totValue = 0.0;
int number = 0, sumItems = 0, quantity = 0;
String item = "";
Also, within the while() loop of your current program, cut these comment
statements and all of the statements that they enclose. Place this group of statements into the new averageCost() method before the return statement.
PROJECT Inventory Valuation - Using Methods in Java
// begin code block for inventory evaluation
. . .
// end code block for inventory evaluation
Finally, modify the output statement in the while() loop to call the method, instead of displaying a variable value.
// begin code block to display results
System.out.printf("average cost: $%.2f ", average);
System.out.println("***************************");
// end code block to display results
The output statement will then look as follows.
// begin code block to display results
System.out.printf("average cost: $%.2f ", averageCost());
System.out.println("***************************");
// end code block to display results
After you move the code block into the new method and modify the output, run your program and test it again. Did the program run the same as it did before?
STEP 6 Construct Even Another Method
You will now create another method as well as create two other variables to help
you modify your display. Your method will calculate the tax on the inventory total cost under the weighted average method by taking the product of the number of items in the inventory and the average cost multiplied by the tax rate.
Use these suggestions to perform this task.
Declare some static ( global ) integer variable. This integer will hold the
number of items that are in the inventory.
Example: static int itemCount = 0; //item count
Declare some static ( global ) double variable. This double will hold the
average inventory cost.
Example: static int averageCost = 0; //average cost
Use these variables in a strategic location to help store a total item count of inventory items as well as the average weighted cost that can be used for your computeTax() function.
Declare a method named computeTax() that will be used to calculate the tax on the inventory. This method should return the resulting tax amount computed as the product of your average cost and item count and tax at a rate of 6 percent. Create an output statement that will include a method call for computeTax() will show a display example statement such as:
" at a level of 133 units, the inventory tax is $ 284.73 . "
PROJECT Inventory Valuation - Using Methods in Java
STEP 7 Submit Your Project
Once you have determined that your modified program is correctly displaying the average cost of the inventory and the tax on the average cost, complete the submission process as follows:
Open MS Word and type a heading for a new document that includes your full name, course number, lab number and date.
Within the document paste in a snapshot of your modified code. Label your snapshot of your modified run with a reasonable description. After your snapshot, paste in your finished source code as well copied in from your Java editor. Submit your MS Word document, when complete.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started