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

Java coding 1. a) Write a java program that gets N integers from the command lin

ID: 3870102 • Letter: J

Question

Java coding 1. a) Write a java program that gets N integers from the command line, finds the average of the numbers and counts how many numbers are above the average. The program prints out the length of the array, the average and how many numbers are above the average. b) Write a java program that finds the smallest and the second smallest value in the N- item array. The program receives the integer from N on the command line, creates a random array in the range [-20.0, 20.0] and then finds the smallest and second smallest in the random array. The program should print these two items Please take a screenshot or write the full code

Explanation / Answer

1 A

class HelloWorld{

public static void main(String []args){
int n,p;
int i;
n=args.length;
int numbers[]=new int[n];
int sum=0;
for(i=0;i<args.length;i++)
{
numbers[i]=Integer.parseInt(args[i]);
sum=sum+Integer.parseInt(args[i]);
}
int average=sum/n,count=0;
for(i=0;i<n;i++)
{
if(numbers[i]>average)
count++;
}
System.out.println("Length of array is "+args.length);
System.out.println("Average = "+average);
System.out.println("Numbers greater than Average "+count);
}
}

import java.util.Random;
class HelloWorld{

public static void main(String []args){
int n,p;
int i;
n=Integer.parseInt(args[0]);
int numbers[]=new int[n];
  
Random rand = new Random();
for(i=0;i<n;i++)
numbers[i]=rand.nextInt(20)-20;
  
print2Smallest(numbers);   
}

static void print2Smallest(int arr[])
{
int first, second, arr_size = arr.length;

  
first = second = Integer.MAX_VALUE;
for (int i = 0; i < arr_size ; i ++)
{

if (arr[i] < first)
{
second = first;
first = arr[i];
}

else if (arr[i] < second && arr[i] != first)
second = arr[i];
}
if (second == Integer.MAX_VALUE)
System.out.println("There is no second" +
"smallest element");
else
System.out.println("The smallest element is " +
first + " and second Smallest" +
" element is " + second);
}
}

1 B

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote