Question

*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 = gws;
            gws = tempo;
        }
        for (int i = 2; i < numCount; ++i) {
            numbers = Array[i];
            if (numbers < jasc) {
                gws = jasc;
                jasc = numbers;
            } else if(numbers < gws) {
                gws = numbers;
            }
        }
        System.out.println(jasc + " " + gws);
    }
}

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

thanks for the question, here are the comments. The program find the smallest two numbers provided by the user.

I have commented each line so that you can follow whats going on and understand.

Hope this helps, and let me know for any help with any other questions. thanks

=============================================================================

import java.util.Scanner;

public class LabProgram {
    public static void main(String[] args) {
        // create an object of Scanner to read user input
        Scanner scnr = new Scanner(System.in);
        int numCount = scnr.nextInt(); // get the size of the array
        int[] Array = new int[numCount]; // create an empty int array of size = numCount

        // get the numbers for each element in the array
        for (int i = 0; i < numCount; ++i) {
            Array[i] = scnr.nextInt();
        }
        // assign the first two numbers two jasc and gws
        // create temporary variables for swapping the numbers
        int jasc = Array[0], gws = Array[1], numbers, tempo;
        // if the first number is greater than the second lowest number
        // swap the numbers
        if (jasc > gws) {
            tempo = jasc;
            jasc = gws;
            gws = tempo;
        }
        // iterate over each element in the array starting at index 2
        for (int i = 2; i < numCount; ++i) {
            numbers = Array[i]; // get the current number
            // if current number is smaller than the smallest number
            // swap the numbers
            if (numbers < jasc) {
                gws = jasc; // assign the 2nd smallest number to the current smallest number
                jasc = numbers; // assign the current smallest number to the current number
            } else if (numbers < gws) {
                // if the current number is smaller than the 2nd smallest number assign the 2nd smallest number to the current number
                gws = numbers;
            }
        }
        // print the smallest ans 2nd smallest number to the console
        System.out.println(jasc + " " + gws);
    }
}

=============================================================================

Add a comment
Know the answer?
Add Answer to:
*This needs comments for each line of code. Thanks in advance!* import java.util.Scanner; public class LabProgram...
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 LabProgram { public static void main(String[] args) { Scanner scnr = new...

    import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); String inputMonth; int inputDay; inputMonth = scnr.nextString(); inputDay = scnr.nextInt(); String season; if(inputMonth == "April","May","June"){ String season = "Spring"; }else if(inputMonth == "July","August","September"){ String season = "Summer"; }else if(inputMonth == "October","November","December"){ String season = "Autumn"; }else if(inputMonth == "January","February","March"){ System.out.println("Winter"); }else{ System.out.println("Invalid"); }    if((inputMonth == "March") && (inputDay >= 20)){ String season = "Spring"; }else if((inputMonth == "June") && (inputDay >= 21)){...

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

  • Please complete in Java. Thanks in advance! Also, please note the solution in bold text. Thanks!...

    Please complete in Java. Thanks in advance! Also, please note the solution in bold text. Thanks! Array testGrades contains NUM_VALS test scores. Write a for loop that sets sumExtra to the total extra credit received. Full credit is 100, so anything over 100 is extra credit. Ex: If testGrades = {101, 83, 107, 90}, then sumExtra = 8, because 1 + 0 + 7 + 0 is 8. import java.util.Scanner; public class SumOfExcess {    public static void main (String...

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

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

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

  • Write a statement that outputs variable numTickets. End with a newline. import java.util.Scanner; public class VariableOutput...

    Write a statement that outputs variable numTickets. End with a newline. import java.util.Scanner; public class VariableOutput ( public static void main (String D args) int numTickets; Scanner scnr new Scanner(System.in) scnr.nextint0:I/ Program will be tested with values: 15, 40. numTickets / Your solution goes here/

  • Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks {...

    Please Refactor this Assignment by using Arraylist instead of Array. import java.util.Scanner; public class DaysOfWeeks { public static void main(String[] args) { String DAY_OF_WEEKS[] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; char ch; int n; Scanner scanner = new Scanner(System.in); do { System.out.print("Enter the day of the Week: "); n = scanner.nextInt() - 1; if (n >= 0 && n <= 6) System.out.println("The day of the week is " + DAY_OF_WEEKS[n] + "."); else System.out.println("Invalid Entry"); System.out.print("Try again (Y/N): "); ch = scanner.next().charAt(0); }while(ch=='Y'); }...

  • 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