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...

  • Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){...

    Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next();    String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word);    if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...

  • 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 Chpt7_Project {    public static void bubbleSort(int[] list)    {    int...

    import java.util.Scanner; public class Chpt7_Project {    public static void bubbleSort(int[] list)    {    int temp;                       for (int i = list.length - 1; i > 0; i--)    {    for (int j = 0; j < i; j++)    {    if (list[j] > list[j + 1])    {    temp = list[j];    list[j] = list[j + 1];    list[j + 1] = temp;    }    }    }   ...

  • import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc...

    import java.util.Scanner; public class SieveOfEratosthenes {    public static void main(String args[]) {       Scanner sc = new Scanner(System.in);       System.out.println("Enter a number");       int num = sc.nextInt();       boolean[] bool = new boolean[num];            for (int i = 0; i< bool.length; i++) {          bool[i] = true;       }       for (int i = 2; i< Math.sqrt(num); i++) {          if(bool[i] == true) {             for(int j = (i*i); j<num; j = j+i) {                bool[j] = false;...

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