Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

import java.util.Scanner; public class FirstAndLast { public static void main(St

ID: 3915289 • Letter: I

Question

import java.util.Scanner;

public class FirstAndLast
{
   public static void main(String[] args)
   {
      final int LENGTH = 100;
      double[] prices = new double[LENGTH];

      int numberOfPrices = 0;
      Scanner in = new Scanner(System.in);
      while (in.hasNextDouble())
      {
         if (numberOfPrices < prices.length)
         {
            prices[numberOfPrices] = in.nextDouble();
            numberOfPrices++;
         }
      }

      double sum = . . .;
      System.out.printf("Sum of first and last: %.2f ", sum);
   }
}

Explanation / Answer

import java.util.Scanner;

class Main {

public static void main(String[] args) {

// DECLARING variables

final int LENGTH = 100;

double[] prices = new double[LENGTH];

int numberOfPrices = 0;

Scanner in = new Scanner(System.in);

// as long as there are double values, taking input

while (in.hasNextDouble())

{

if (numberOfPrices < prices.length)

{

// putting that prices in array

prices[numberOfPrices] = in.nextDouble();

numberOfPrices++;

}

}

// giving the sum, value of first and last price entered

double sum = prices[0] + prices[numberOfPrices-1];

System.out.printf("Sum of first and last: %.2f ", sum);

}

}

/*SAMPLE OUTPUT

1

2

3

4

5

6

q

Sum of first and last: 7.00

*/