Question: SO ALL I NEED IS TO WRITE COMMENTS FOR EACH LINE OF CODE, BUT I'M UNSURE WHAT ROLE EACH FUNCTION PLAYS. Can anyone help with

SO ALL I NEED IS TO WRITE COMMENTS FOR EACH LINE OF CODE, BUT I'M UNSURE WHAT ROLE EACH FUNCTION PLAYS. Can anyone help with this? The comments are supposed to be detailed enough so that someone who didn't program could figure out what the program is doing. Thanks.

*For this code, it take user input and outputs middle integer (assuming an odd amount of integers). It quits after reading a negative integer, and does not exceed 9 inputs.*

Code:

import java.util.Scanner;

public class LabProgram {

public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int[] sortedNum = new int[9]; int totalNum; int midNum;

totalNum = 0; midNum = scnr.nextInt();

while (midNum != -1) { if (totalNum == 9) { System.out.println("Too many inputs"); return; } sortedNum[totalNum++] = midNum; midNum = scnr.nextInt(); }

if (totalNum % 2 == 1) { System.out.println(sortedNum[totalNum / 2]); } else { System.out.println(sortedNum[(totalNum - 1) / 2] + " " + sortedNum[totalNum / 2]); } } }

*This code counts the number of times each inputted word is mentioned and outputs to screen. Assume no more than 20 words.*

Code:

import java.util.Scanner; public class LabProgram { public static void main(String[] args){ Scanner scnr = new Scanner(System.in); int wordDisplays = scnr.nextInt(); String Array[] = new String[wordDisplays]; int NoOfWords[] = new int[wordDisplays]; int jasc;

for(int i = 0;i

for(int i = 0;i

*This code takes input of multiple integers beginning with the first that tells how many are in the list then outputs the two smallest integers in ascending order. Assume no more than 20 integers total.*

Code:

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

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!