import java.util.*; import java.lang.*; import java.io.*; class Main { public st
ID: 3820166 • Letter: I
Question
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// Initializing Scanner object to read the input
Scanner obj = new Scanner(System.in);
//Initializing variables total and numberOfItems
double total = 0;
int numberOfItems = 5;
//These arrays store the item names and their respective prices
String items[] = new String[numberOfItems];
Double price[] = new Double[numberOfItems];
//Iteraing in the loop
for(int i=1;i<=numberOfItems;i++){
//Reading the item name and the price of each Item
items[i-1] = obj.next();
price[i-1] = obj.nextDouble();
//Adding the price to total
total = total + price[i-1];
}
//printing the Items and total
for(int i=1;i<=numberOfItems;i++){
System.out.println(items[i-1]+" = $"+price[i-1]);
}
System.out.println("Total is "+total);
}
}
You should have had multiple java files, with the main file being the cashier class. You seem to have part of the logic in your file, but it is not complete. The naming convention is incorrect as well as the folder name and packaging.
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner obj = new Scanner(System.in);
double total = 0;
int numberOfItems = 5;
String items[] = new String[numberOfItems];
Double price[] = new Double[numberOfItems];
for(int i=1;i<=numberOfItems;i++){
items[i-1] = obj.next();
price[i-1] = obj.nextDouble();
total = total + price[i-1];
}
for(int i=1;i<=numberOfItems;i++){
System.out.println(items[i-1]+" = $"+price[i-1]);
}
System.out.println("Total is "+total);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.