Modify the program provided so that it can produce the Price and the Total with
ID: 3757388 • Letter: M
Question
Modify the program provided so that it can produce the Price and the Total with two digits after the decimal place. (note: Modify given program in a way that a beginner Java programmer would)
Do not use: import java.text.*; & DecimalFormat f = new DecimalFormat("##.00");
Program Provided:
Expected:
/I Product 1 System.out.println("Enter info for Product 1"); System.out.print ("Product name: ") productNamescnr.next; System.out.print("Quantity:"); quantityProductscnr.nextInt); System.out.print ("Unit Price:") unitPricescnr.nextDouble); totalCost quantityProduct unitPrice; System.out.println(") Product 2 System.out.println("Enter info for Product 1"); System.out.print("Product name: ") productName2scnr.next); System.out-print("Quantity:") quantityProduct2·scnr.next Int(); System.out.print("Unit Price: ") unitPrice2scnr.nextDuble) totalCost quantityProduct2 unitPrice2; System.out.println("); // neccessary adjustments System.out.printf("n); System.out.printf("I %-les l %12s l %12s | %12s l ", "Product. "Qty", "Price", "Total"); System.out.printf(" System out printf(" %-10s X12s X12s %12s ", productName substring(0, 1) toUpperCase() System out printf(" %-10s X12s X12s $12n", productName2. substring(0, 1) tolpperCase() + productName toLoverCase().substring(1), quantityProduct, unitPrice, tota!Cost); + productName2.tolowerCase() substring(1), quantityProduct2, unitPrice2, tot alcost2);Explanation / Answer
import java.util.*;
class products{
public static double roundOffToTwo(double val){
return (double) Math.round(val * 100) / 100;
}
public static void main(String srgs[]){
String productName,productName2;
int quantityProduct,quantityProduct2;
double unitPrice,unitPrice2,totalCost,totalCost2;
Scanner scnr=new Scanner(System.in);
System.out.println("Enter info for Product 1");
System.out.print("Product name: ");
productName = scnr.next();
System.out.print("Quantity: ");
quantityProduct = scnr.nextInt();
System.out.print("Unit Price: ");
unitPrice = scnr.nextDouble();
totalCost = quantityProduct * unitPrice;
System.out.println(" ");
System.out.println("Enter info for Product 2");
System.out.print("Product name: ");
productName2 = scnr.next();
System.out.print("Quantity: ");
quantityProduct2 = scnr.nextInt();
System.out.print("Unit Price: ");
unitPrice2 = scnr.nextDouble();
totalCost2 = quantityProduct2 * unitPrice2;
System.out.println(" ");
System.out.printf("----------------------------------------------------------- ");
System.out.printf("| %-10s | %12s | %12s | %12s | ","Product","Qty","Price","Total");
System.out.printf("----------------------------------------------------------- ");
System.out.printf("| %-10s | %12s | %12.2f | %12.2f | ",productName.substring(0,1).toUpperCase() + productName.toLowerCase().substring(1),quantityProduct,roundOffToTwo(unitPrice),roundOffToTwo(totalCost));
System.out.printf("| %-10s | %12s | %12.2f | %12.2f | ",productName2.substring(0,1).toUpperCase() + productName2.toLowerCase().substring(1),quantityProduct2,roundOffToTwo(unitPrice2),roundOffToTwo(totalCost2));
System.out.printf("----------------------------------------------------------- ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.