Question

I need the following java code commented import java.util.Scanner; public class Main { public static void...

I need the following java code commented

import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner input = new Scanner (System.in);
int productNo=0;
double product1;
double product2;
double product3;
double product4;
double product5;
int quantity;
double totalsales=0;

while(productNo !=0)
System.out.println("Enter Product Number 1-5");
productNo=input.nextInt();

System.out.println("Enter Quantity Sold");
quantity=input.nextInt();

switch (productNo) {

case 1:
product1=1.00;
totalsales+=(1.00*quantity);
break;

case 2:
product2=2.00;
totalsales+=(2.00*quantity);
break;

case 3:
product3=6.00;
totalsales+=(6.00*quantity);
break;

case 4:
product4=23.00;
totalsales+=(23.00*quantity);
break;

case 5:
product5=17.00;
totalsales+=(17.00*quantity);
break;
  
}
  
System.out.println(totalsales);

}
}


0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find below the code with comments and also please find below the code screenshot to understand the indentation.

Commented Code

//importing scanner class from util
//using scanner to get the input from user
import java.util.Scanner;

//create class Main
public class Main {

//Create Main class method
public static void main(String[] args) {
  
//create scanner class object "input"
//using it to get the input from user
Scanner input = new Scanner (System.in);
//declare integer variable productNo
//assigned 0 to productNo
int productNo=0;
//declare double variable product1
double product1;
//declare double variable product2
double product2;
//declare double variable product3
double product3;
//declare double variable product4
double product4;
//declare double variable product5
double product5;
//declare integer variable quantity
int quantity;
////declare double variable totalsales
//assigned 0 to totalsales
double totalsales=0;
  
//while loop
//runs untill productno has a value not equal to 0
while(productNo !=0)
System.out.println("Enter Product Number 1-5");
//using scanner class object "input"
//to get the integer input from user using input.nextInt
//and assign the input value to productNo
productNo=input.nextInt();
//prints Enter Quantity sold to console
System.out.println("Enter Quantity Sold");
//using scanner class object "input"
//to get the integer input from user using input.nextInt
//and assign the input value quantity
quantity=input.nextInt();
  
  
//Switch Statement
//contains productNo as expression in switch
//runs for different cases
switch (productNo)
{
//Case statement
//if productNo is given input value as 1
//case 1 will be executed
case 1:
//product1 is assigned double value 1.00
product1=1.00;
//calculating totalsales
//multiplying 1 with quantity given as input
//and adding the multiplication result with totalsales
totalsales+=(1.00*quantity);
//break Statement
//to come out of switch
break;
  
//Case statement
//if productNo is given input value as 2
//case 2 will be executed
case 2:
//product2 is assigned double value 2.00
product2=2.00;
//calculating totalsales
//multiplying 2 with quantity given as input
//and adding the multiplication result with totalsales
totalsales+=(2.00*quantity);
//break Statement
//to come out of switch
break;
//Case statement
//if productNo is given input value as 3
//case 3 will be executed
case 3:
//product3 is assigned double value 6.00
product3=6.00;
//calculating totalsales
//multiplying 6 with quantity given as input
//and adding the multiplication result with totalsales
totalsales+=(6.00*quantity);
//break Statement
//to come out of switch
break;
//Case statement
//if productNo is given input value as 4
//case 4 will be executed
case 4:
//product4 is assigned double value 23.00
product4=23.00;
//calculating totalsales
//multiplying 23 with quantity given as input
//and adding the multiplication result with totalsales
totalsales+=(23.00*quantity);
//break Statement
//to come out of switch
break;
//Case statement
//if productNo is given input value as 5
//case 5 will be executed
case 5:
//product5 is assigned double value 17.00
product5=17.00;
//calculating totalsales
//multiplying 17 with quantity given as input
//and adding the multiplication result with totalsales
totalsales+=(17.00*quantity);
//break Statement
//to come out of switch
break;
  
}
  
//prints total sales calculated obove in switch Statement
//on the console
System.out.println(totalsales);

}
}

Code Screenshot

Add a comment
Know the answer?
Add Answer to:
I need the following java code commented import java.util.Scanner; public class Main { public static void...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        //...

    import java.util.Scanner; public class TriangleMaker {    public static void main(String[] args) {        // TODO Auto-generated method stub        System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle.");        Scanner keyboard = new Scanner(System.in);    int size = keyboard.nextInt();    for (int i = 1; i <= size; i++)    {    for (int j = 0; j < i; j++)    {    System.out.print("*");    }    System.out.println();    }    for (int...

  • import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y,...

    import java.util.Scanner; public class SCAN { public static void main(String[ ] args) { int x, y, z; double average; Scanner scan = new Scanner(System.in); System.out.println("Enter an integer value"); x = scan.nextInt( ); System.out.println("Enter another integer value"); y = scan.nextInt( ); System.out.println("Enter a third integer value"); z = scan.nextInt( ); average = (x + y + z) / 3; System.out.println("The result of my calculation is " + average); } } What is output if x = 0, y = 1 and...

  • make this program run import java.util.Scanner; public class PetDemo { public static void main (String []...

    make this program run import java.util.Scanner; public class PetDemo { public static void main (String [] args) { Pet yourPet = new Pet ("Jane Doe"); System.out.println ("My records on your pet are inaccurate."); System.out.println ("Here is what they currently say:"); yourPet.writeOutput (); Scanner keyboard = new Scanner (System.in); System.out.println ("Please enter the correct pet name:"); String correctName = keyboard.nextLine (); yourPet.setName (correctName); System.out.println ("Please enter the correct pet age:"); int correctAge = keyboard.nextInt (); yourPet.setAge (correctAge); System.out.println ("Please enter the...

  • import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables...

    import java.util.Scanner; public class creditScore { public static void main(String[]args) { // declare and initialize variables int creditScore; double loanAmount,interestRate,interestAmount; final double I_a = 5.56,I_b = 6.38,I_c = 7.12,I_d = 9.34,I_e = 12.45,I_f = 0; String instructions = "This program calculates annual interest\n"+"based on a credit score.\n\n"; String output; Scanner input = new Scanner(System.in);// for receiving input from keyboard // get input from user System.out.println(instructions ); System.out.println("Enter the loan amount: $"); loanAmount = input.nextDouble(); System.out.println("Enter the credit score: "); creditScore...

  • Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895...

    Need help with the UML for this code? Thank you. import java.util.Scanner;    public class Assignment1Duong1895    {        public static void header()        {            System.out.println("\tWelcome to St. Joseph's College");        }        public static void main(String[] args) {            Scanner input = new Scanner(System.in);            int d;            header();            System.out.println("Enter number of items to process");            d = input.nextInt();      ...

  • Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args)...

    Fix this program package chapter8_Test; import java.util.Scanner; public class Chapter8 { public static void main(String[] args) { int[] matrix = {{1,2},{3,4},{5,6},{7,8}}; int columnChoice; int columnTotal = 0; double columnAverage = 0; Scanner input = new Scanner(System.in); System.out.print("Which column would you like to average (1 or 2)? "); columnChoice = input.nextInt(); for(int row = 0;row < matrix.length;++row) { columnTotal += matrix[row][columnChoice]; } columnAverage = columnTotal / (float) matrix.length; System.out.printf("\nThe average of column %d is %.2f\n", columnAverage, columnAverage); } } This program...

  • please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){...

    please debug this code: import java.util.Scanner; import edhesive.shapes.*; public class U2_L7_Activity_Three{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); double radius; double length; System.out.println("Enter radius:"); double r = scan.nextDouble(); System.out.println("Enter length:"); double l = scan.nextDouble(); System.out.println("Enter sides:"); int s = scan.nextInt(); Circle c = Circle(radius); p = RegularPolygon(l , s); System.out.println(c); System.out.println(p); } } Instructions Debug the code provided in the starter file so it does the following: • creates two Double objects named radius and length creates...

  • import java.util.Scanner; public class TempConvert { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class TempConvert { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); //ask the user for a temperature System.out.println("Enter a temperature:"); double temp = scnr.nextDouble(); //ask the user for the scale of the temperature System.out.println("Is that Fahrenheit (F) or Celsius (C)?"); char choice = scnr.next().charAt(0); if(choice == 'F') { //convert to Celsius if given temperature was Fahrenheit System.out.println(temp + " degrees Fahrenheit is " + ((5.0/9) * (temp-32)) + " degrees Celsius"); } else {...

  • I cannot get this to work in IntelliJ import java.util.Scanner; public class Fibonacci { public static...

    I cannot get this to work in IntelliJ import java.util.Scanner; public class Fibonacci { public static int fibonacci(int n) { if (n <= 1) return n; return fibonacci(n - 1) + fibonacci(n - 2); } public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); System.out.println(fibonacci(n)); in.close(); } }

  • import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25);...

    import java.util.Scanner; public class Client{ public static void main(String args[]){    Coin quarter = new Coin(25); Coin dime = new Coin(10); Coin nickel = new Coin(5);    Scanner keyboard = new Scanner(System.in);    int i = 0; int total = 0;    while(true){    i++; System.out.println("Round " + i + ": "); quarter.toss(); System.out.println("Quarter is " + quarter.getSideUp()); if(quarter.getSideUp() == "HEADS") total = total + quarter.getValue();    dime.toss(); System.out.println("Dime is " + dime.getSideUp()); if(dime.getSideUp() == "HEADS") total = total +...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT