Explain The B PG 03 Question Two Question One 3 Marks Learni
Explain The B Pg. 03 question Two question One3 Marks learning Outcomes
Please write a Java program that fulfills the following requirements:
1. Ask the user to enter his/her full name.
2. Print a welcome message using the user’s name.
3. Ask the user to enter the name of an item.
4. Ask the user to enter the price and quantity of the item.
5. Declare a constant variable that holds a tax rate of 15%.
6. Calculate the total price of the item, including the tax.
7. Print the grocery bill.
Additional specifications:
- The class name of your Java program must be your first name.
- Your submission should include both the Java code as text and a screenshot of the program output using your own name.
---
Paper For Above instruction
Introduction
Calculating the total cost of grocery items including tax is a common programming exercise that helps students understand basic principles of programming, such as input/output operations, variables, constants, arithmetic calculations, and formatted output. This program emphasizes user interaction through prompts, the use of constants for fixed values, and performing calculations to generate a detailed bill, which are fundamental in developing foundational programming skills.
Program Development and Implementation
The Java program begins with importing the Scanner class to facilitate user input. The class is named after the student’s first name, as specified. Inside the main method, the program follows a sequential flow:
1. It prompts the user for their full name and stores the input.
2. Displays a personalized welcome message.
3. Asks for the name of a grocery item.
4. Requests the price and quantity for that item.
5. Declares a constant `TAX_RATE` set at 15% (0.15).
6. Calculates the total price for the item, adds tax, and produces a detailed bill.
Below is the complete Java code implementing these functionalities:
```java
import java.util.Scanner;
public class [YourFirstName] {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get user's full name
System.out.print("Enter your full name: ");
String fullName = scanner.nextLine();
// Step 2: Welcome message
System.out.println("Welcome, " + fullName + "!");
// Step 3: Get item name
System.out.print("Enter the name of the item: ");
String itemName = scanner.nextLine();
// Step 4: Get price and quantity
System.out.print("Enter the price of the item: ");
double price = scanner.nextDouble();
System.out.print("Enter the quantity of the item: ");
int quantity = scanner.nextInt();
// Step 5: Declare constant tax rate
final double TAX_RATE = 0.15;
// Step 6: Calculate total price including tax
double totalPriceBeforeTax = price * quantity;
double taxAmount = totalPriceBeforeTax * TAX_RATE;
double totalPriceWithTax = totalPriceBeforeTax + taxAmount;
// Step 7: Print the grocery bill
System.out.println("\n--- Grocery Bill ---");
System.out.printf("Item: %s\n", itemName);
System.out.printf("Price per item: %.2f\n", price);
System.out.printf("Quantity: %d\n", quantity);
System.out.printf("Subtotal: %.2f\n", totalPriceBeforeTax);
System.out.printf("Tax (%.0f%%): %.2f\n", TAX_RATE * 100, taxAmount);
System.out.printf("Total amount: %.2f\n", totalPriceWithTax);
scanner.close();
}
}
```
(Replace `[YourFirstName]` with your own first name as the class name)
Sample Output:
Assuming the user enters:
- Name: John Doe
- Item: Milk
- Price: 2.50
- Quantity: 3
The output would look like:
```
Enter your full name: John Doe
Welcome, John Doe!
Enter the name of the item: Milk
Enter the price of the item: 2.50
Enter the quantity of the item: 3
--- Grocery Bill ---
Item: Milk
Price per item: 2.50
Quantity: 3
Subtotal: 7.50
Tax (15%): 1.13
Total amount: 8.63
```
---
Conclusion
This Java program demonstrates fundamental programming concepts such as user input handling, variable declaration, use of constants, arithmetic operations, formatted output, and control flow. Such exercises help build a strong foundation for understanding how programs process data, perform calculations, and present results effectively.