Question

Provide comments for this code explaining what each line of code does import java.util.*; public class...

Provide comments for this code explaining what each line of code does

import java.util.*;
public class Chpt8_Project
{
   public static void main(String[] args)   
   {
       Scanner sc=new Scanner(System.in);
       int [][]courses=new int [10][2];
       for(int i=0;i<10;i++)
       {
           while(true)
           {
               System.out.print("Enter classes and graduation year for student "+(i+1)+":");
               courses[i][0]=sc.nextInt();
               courses[i][1]=sc.nextInt();
               if(courses[i][0]>=1 && courses[i][0]<=20)
                   break;
               else
                   System.out.println("The number of classes for student "+(i+1)+" is not valid.");
           }
       }
       sc.close();
       for(int i=0;i<10;i++)
       {
           System.out.println("Student "+(i+1)+" needs "+(courses[i][0]*3)+" credits to graduates in "+courses[i][1]+"\n");
       }
   }

}

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

Code Screenshot

Code

import java.util.*;

public class Chpt8_Project

{

   public static void main(String[] args)  

   {

       // create a Scanner class object to get user input

       Scanner sc=new Scanner(System.in);

      

       // create an array of dimension 10x2

       int [][]courses=new int [10][2];

      

       // traverse the rows in te matrix

       for(int i=0;i<10;i++)

       {

           // infinite loop

           while(true)

           {

               System.out.print("Enter classes and graduation year for student "+(i+1)+":");

              

               // get the classes for the current course

               courses[i][0]=sc.nextInt();

              

               // get the graduation year for the current course

               courses[i][1]=sc.nextInt();

              

               // if the classes and graduation year enetered are in valid range

               if(courses[i][0]>=1 && courses[i][0]<=20)

                   // break out of the loop

                   break;

               // if the classes and graduation year enetered are not in valid range

               else

                   // display an error message to the user

                   System.out.println("The number of classes for student "+(i+1)+" is not valid.");

           }

       }

      

       // close the Scanner object

       sc.close();

      

       // traverse each row in the array or each course

       for(int i=0;i<10;i++)

       {

           // display the desired output for i th course

           System.out.println("Student "+(i+1)+" needs "+(courses[i][0]*3)+" credits to graduates in "+courses[i][1]+"\n");

       }

   }

}

Add a comment
Know the answer?
Add Answer to:
Provide comments for this code explaining what each line of code does import java.util.*; public class...
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
  • I need a java flowchart for the following code: import java.util.*; public class Main {   ...

    I need a java flowchart for the following code: import java.util.*; public class Main {    public static void main(String[] args) {    Scanner sc=new Scanner(System.in);           System.out.print("Enter the input size: ");        int n=sc.nextInt();        int arr[]=new int[n];        System.out.print("Enter the sequence: ");        for(int i=0;i<n;i++)        arr[i]=sc.nextInt();        if(isConsecutiveFour(arr))        {        System.out.print("yes the array contain consecutive number:");        for(int i=0;i<n;i++)        System.out.print(arr[i]+" ");       ...

  • *This needs comments for each line of code. Thanks in advance!* import java.util.Scanner; public class LabProgram...

    *This needs comments for each line of code. Thanks in advance!* import java.util.Scanner; public class LabProgram {     public static void main(String[] args) {         Scanner scnr = new Scanner(System.in);         int numCount = scnr.nextInt();         int[] Array = new int[numCount];                for(int i = 0; i < numCount; ++i) {             Array[i] = scnr.nextInt();         }         int jasc = Array[0], gws = Array[1], numbers, tempo;         if (jasc > gws) {             tempo = jasc;             jasc...

  • This assignment requires comments for each line explaining why it is necessary for the coding to...

    This assignment requires comments for each line explaining why it is necessary for the coding to run properly. I understand most of it but not all. import java.util.Scanner; // comment here: public class Age { // comment here:       public static void main (String[] args){ // comment here: final int MINOR = 21;// Scanner scan = new Scanner (System.in); // comment here: System.out.print ("Enter your age: "); // comment here: int age = scan.nextInt();//comment here: System.out.println ("You entered: " +...

  • need help to write the main method as the template import java.util.*; public class oBST {...

    need help to write the main method as the template import java.util.*; public class oBST { static float optimalBST(String words[], float freq[], int n) { //2D dp matrix float dp[][] = new float[n + 1][n + 1]; int root[][] = new int[n+1][n+1]; // For a single word, cost is equal to frequency of the word for (int i = 0; i < n; i++) dp[i][i] = freq[i]; // Now consider for size 2, 3, ... . for (int L =...

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

  • I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public...

    I need help running this code. import java.util.*; import java.io.*; public class wordcount2 {     public static void main(String[] args) {         if (args.length !=1) {             System.out.println(                                "Usage: java wordcount2 fullfilename");             System.exit(1);         }                 String filename = args[0];               // Create a tree map to hold words as key and count as value         Map<String, Integer> treeMap = new TreeMap<String, Integer>();                 try {             @SuppressWarnings("resource")             Scanner input = new Scanner(new File(filename));...

  • Please make the following code modular. Therefore contained in a package with several classes and not...

    Please make the following code modular. Therefore contained in a package with several classes and not just one. import java.util.*; public class Q {    public static LinkedList<String> dogs = new LinkedList<String> ();    public static LinkedList<String> cats = new LinkedList<String> ();    public static LinkedList<String> animals = new LinkedList<String> ();    public static void enqueueCats(){        System.out.println("Enter name");        Scanner sc=new Scanner(System.in);        String name = sc.next();        cats.addLast(name);        animals.addLast(name);    }   ...

  • What is output 1 in the code below? import java.util."; public class StackDemo { public static...

    What is output 1 in the code below? import java.util."; public class StackDemo { public static void main(String [] args) { Stack<Integer> intStack = new Stack<>(); Stack<Integer> tempStack = new Stack<>(); intStack.push(18); intStack.push(21); intStack.push(25); tempStack = intStack; while (!tempStack.empty() { System.out.print(tempStack.peek()+"");//output 1 tempStack.pop(); 3 System.out.println(); System.out.println(intStack.peek()); //output 2 25 18 21 • 25 21 18

  • Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util...

    Change the following Shift Cipher program so that it uses OOP(constructor, ect.) import java.util.*; import java.lang.*; /** * * @author STEP */ public class ShiftCipher { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here Scanner input = new Scanner(System.in); String plainText; System.out.print("Please enter your string: "); // get message plainText = input.nextLine(); System.out.print("Please enter your shift cipher key: "); // get s int s = input.nextInt(); int...

  • 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();      ...

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