Develop a Java Program that: . Reads input from the keyboard using a Scanner Obj
ID: 3873875 • Letter: D
Question
Develop a Java Program that:
. Reads input from the keyboard using a Scanner Object and its methods
. Displays information to the user in various formats, including formatting numbers
. Manipulates String objects using various String methods
Project Requirements:
1. In this project, you need to use methods of Scanner and String class that will allow the user to enter infromation about two prodcuts sold at a Grocery Store and display information about the products
2. Products will consist of the following data:
a. productName- String
b. quantity- interger
c. unitPrice- double
3. The program will
a. prompt the user to enter the information for two products and store this information in the appropriately named variable. Assume product name is one word only and less than 10 characters
b. store the product name in proper case (first letter to upper case and the rest of the word to lower case. For example: apple becomes Apple, and ORANGE become Orange
c. display the data for each product and the total price in a table (see below) with the following requirements
i. Product name in Proper case
ii. Product name will be left justified
iii. Numbered data (qty, price and total price) will be right justified
iv. The price and total price will be 2 decimal places. (Use printf format specifiers)
4. Include javadoc class comment following Project Comment Template on the content page.
Explanation / Answer
Note : Could you please check the output .If you required any changes Just intimate.I will modify it.Thank You.
________________
Product.java
public class Product {
//Declaring instance variables
private String productName;
private int quantity;
private double unitPrice;
//Parameterized constructor
public Product(String productName, int quantity, double unitPrice) {
super();
setProductName(productName);
this.quantity = quantity;
this.unitPrice = unitPrice;
}
//getters and setters
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
String rest = productName.substring(1);
String first = null;
if ((productName.charAt(0)) >= 'a' && (productName.charAt(0)) <= 'z') {
first = Character.toString((char)(productName.charAt(0) - 32));
}
this.productName = first + rest.toLowerCase();
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
}
_________________
Test.java
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
//Declaring variables
String prodName;
int qty;
double unitPrice;
Product prods[]=new Product[2];
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the products entered by the user
for(int i=0;i<prods.length;i++)
{
System.out.println(":: Product#"+(i+1)+" ::");
//Getting the input entered by the user
System.out.print("Enter the Product name :");
prodName=sc.next();
System.out.print("Enter the quantity :");
qty=sc.nextInt();
System.out.print("Enter the Price :");
unitPrice=sc.nextDouble();
//Creating an Product Objects and populating into an array
prods[i]=new Product(prodName,qty,unitPrice);
}
//Displaying the Products info
for(int i=0;i<prods.length;i++)
{
System.out.printf("%-10s %10d %.2f ",prods[i].getProductName(),prods[i].getQuantity(),prods[i].getUnitPrice());
}
}
}
____________________
Output:
:: Product#1 ::
Enter the Product name :apple
Enter the quantity :20
Enter the Price :45
:: Product#2 ::
Enter the Product name :orange
Enter the quantity :50
Enter the Price :20
Apple 20 45.00
Orange 50 20.00
_____________Could you rate me well.Plz .Thank You
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.