Are my comments correct? My code works as it should. My friend helped me write t
ID: 3594547 • Letter: A
Question
Are my comments correct? My code works as it should. My friend helped me write the code, but I'm not entirely sure I understood what he helped me write.
Here's a paste-bin if the formatting is better that way: https://pastebin.com/U4MByTaR
import java.util.Scanner;
// import scanner so that user may input numbers
public class FindLargest {
// declare the class
static final int size = 10;
// we use final so that the value can't change
// size is set to 10, so that anything beyond the 10th number won't be considered in the program
public static void main(String[] args) {
double[] numbers = new double [size];
// numbers is declared to 10 because size is
// if 10 numbers are not inputed then the program will not output until 10 are
Scanner input = new Scanner(System.in);
// used to gather input from the user
System.out.print("Enter " + size + " numbers: ");
// prompts user to enter 10 numbers
for (int i=0; i < numbers.length; i++)
// i is set to 0. if i is less than the length of numbers(10), then the program will
// continue to run
numbers[i] = input.nextDouble();
System.out.println("The index of the largest number is: " + indexofLargestElement(numbers));
// prints out the index of the largest number of the first 10 numbers entered
// if more than 10 numbers are entered, they will not be considered
}
public static int indexofLargestElement(double[] array) {
int index = 0;
// the index technically starts at 0
// because we only want to find the max of 10 numbers, it will end on 9
double max = array[index];
//
for (int i = 0; i < array.length; i++) {
if (array[i]> max) {
max = array[i];
index = i;
}
}
return index;
}
}
Explanation / Answer
// import Scanner class to scan elements from user
import java.util.Scanner;
// declare class FindLargest
public class FindLargest
{
// we use final so that the value can't change
// variable size has a definite value of 10
static final int size = 10;
// main method
public static void main(String[] args)
{
// initialize an array numbers of length = size, which is equal to 10
double[] numbers = new double [size];
// create a Scanner object to recieve input from user
Scanner input = new Scanner(System.in);
// prompts user to enter 10 numbers
System.out.print("Enter " + size + " numbers: ");
// iterate through the array from i = 0 to length of array
for (int i=0; i < numbers.length; i++)
// scan the number and save it in i th element of array numbers
numbers[i] = input.nextDouble();
// prints out the index of the largest number of the first 10 numbers entered
// if more than 10 numbers are entered, they will not be considered
System.out.println("The index of the largest number is: " + indexofLargestElement(numbers));
}
// function that returns the maximum index of array
public static int indexofLargestElement(double[] array)
{
int index = 0;
// the index technically starts at 0
// because we only want to find the max of 10 numbers, it will end on 9
// set max to first element of array
double max = array[index];
// iterate through the array
for (int i = 0; i < array.length; i++)
{
// if current element is greater than the element in max
if (array[i]> max)
{
// set max to current element
max = array[i];
// also update the index which stores the index of element having maximum value
index = i;
}
}
// return the index
return index;
}
}
// Code works fine and I have corrected some comments and also added some.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.