For the following Java code: import java.util.Scanner; //Student, Programmer pub
ID: 3844048 • Letter: F
Question
For the following Java code:
import java.util.Scanner;
//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';
// end local variable declaration / initialization zone
//call a method
displayMenu();
//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 to display results
System.out.printf("average cost: $%.2f ", averageCost());
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
static void displayMenu()
{
//place method body statements below
}// end method
static double averageCost()
{
double average = 0.0, cost = 0.0, totValue = 0.0;
int number = 0, sumItems = 0, quantity = 0;
String item = "";
// 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;
}
average = totValue / sumItems;
// end code block for inventory evaluation
return average;
}//end method
}// end class
I have to contruct another method,which will calculate the tax on the weighted average of the inventory. The tax is computed as 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 somestatic (global)integervariable. This integer will hold the number of items that are in the inventory.
• Declare some static (global) double variable. This double will hold the avaerage inventory cost.
• Declare a method named computeTax() that will be used to calculate the tax on the inventory. This method is to receivetwoarguments, the number of items that are in the inventory and the average cost of the inventory. The method will then return the tax on the inventory, based on a tax rate of 6 percent. Your output for this method will include an example statement such as: " at a level of 133 units, the inventory tax is $284.73."
Thank you for the help.
Explanation / Answer
import java.util.Scanner;
//Student, Programmer
public class Inventory
{
static int noofitems;
static double averageofinventory;
static Scanner sc = new Scanner(System.in);
public static void main(String args[])
{
// begin local variable declaration / initialization zone
char answer = 'Y'; // end local variable declaration / initialization zone
//call a method
displayMenu();//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 to display results
System.out.printf("Average cost: $%.2f ", averageCost());
System.out.println("***************************");//end code block to display results
// begin code block to perform additional program run
System.out.println("If you want to run again(Y or N)?");
answer = sc.next().charAt(0);//end code block to perform additional program run
}
System.out.println("***************************");
}// end main() method
static void displayMenu()
{
//place method body statements below
}// end method
static double averageCost()
{
double average = 0.0, cost = 0.0, totValue = 0.0;
int number = 0, sumItems = 0, quantity = 0;
String item = "";
//begin code block for inventory evaluation
System.out.println("Enter number of item types in the inventory:");
number = sc.nextInt();
noofitems=number;
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;
}
average = totValue / sumItems;
averageofinventory=average;
//end code block for inventory evaluation
computeTax(noofitems,averageofinventory);//Caling Compute Tax Method
return average;
}//end method
static void computeTax(int item,double avg)
{
double tax=0.0;
tax=avg*1.06;//tax calculation
System.out.println("at a level of"+item+ " units, the inventory tax is $"+tax);//tax printed on the console
}
}// end class