In: Computer Science
could you do all the challenges?
public class Main
{
public static void main(String[] args)
{
// declare an array so you can easily use them under
// one name
//ex1 array of test score 0-100
int[] testScores = new int[100];
//ex2 array of 200 gpa
double[] gpa = new double[200];
//ex3 50 element array of age
int[] age; //1 - declares age array
age = new int[50]; //2 - instantiates age array
age[0] = 10; //3 - put initial values in
age[1] = 15; // each element
// challange: initialize testScores with random
{
Random int=new Random();
int score;
for (int i = 0; i < 100 ; i++)
{testScores[i] = rand.nextInt(101);
System.out.println(testscores[i] + " "); }
// scores using a loop. Do not print (we'll later)
// challenge: using the OTHER LOOP print the elements of the
array
int i=0;
while(i< 100)
{testScores[i] = rand.nextInt(101);
System.out.println(testscores[i] + " ");
i++;
}
// challange: look through your testScores array and count
// how many falling scores were there i.e. <68
int falling = 0;
// challenge: ask the user for a numberScore i.e. 77
// and count how many times a testScore is
// within 5 of numberScore
//challenge: put random gpa's into the gpa array.
// print the average GPA.
//challenge: on each element of the GPA array, print the
// GPA and the next GPA if they are equal
}
}
****This requires some effort so please drop a like if you are satisfied with the solution*****
I'm providing screenshots of code and output for your reference.
Code:
import java.util.Random;
import java.util.Scanner;
public class Main
{
public static void main (String[]args)
{
// declare an array so you can easily use them under
// one name
//ex1 array of test score 0-100
int[] testScores = new int[100];
//ex2 array of 200 gpa
double[] gpa = new double[200];
//ex3 50 element array of age
int[] age; //1 -
declares age array
age = new int[50]; //2 -
instantiates age array
age[0] = 10; //3 - put initial
values in
age[1] = 15; // each element
// challange: initialize testScores with random
Random rand = new Random ();
int score;
System.out.println("Test Scores initialised with Random numbers:
");
for (int i = 0; i < 100; i++)
{
testScores[i] = rand.nextInt (101);
System.out.print(testScores[i] + " ");
}
System.out.println("\n\nPrinting random Test Scores array:
");
// scores using a loop. Do not print (we'll later)
// challenge: using the OTHER LOOP print the elements of the
array
int i = 0;
while (i < 100)
{
System.out.print (testScores[i] + " ");
i++;
}
// challange: look through your testScores array and count
// how many falling scores were there i.e. <68
int falling = 0;
for(i=0;i<testScores.length;i++){
if(testScores[i]<68)
falling++;
}
System.out.println("\n\nNumber of Falling Scores is :
"+falling);
// challenge: ask the user for a numberScore i.e. 77
// and count how many times a testScore is
// within 5 of numberScore
System.out.print("\nEnter a value for number score: ");
Scanner in=new Scanner(System.in);
int numScoreCount=0;
int numberScore=in.nextInt();
for(i=0;i<testScores.length;i++){
if(Math.abs(testScores[i]-numberScore)<=5)
numScoreCount++;
}
System.out.println("\nNumber of times numberScore is
within 5 of in text Scores array is : "+numScoreCount);
//challenge: put random gpa's into the gpa array.
// print the average GPA.
double gpaAverage=0;
for(i=0;i<200;i++){
gpa[i]=0 + (10 - 0) * rand.nextDouble();
gpaAverage+=gpa[i];
}
System.out.println("\nGpa array: ");
for(i=0;i<gpa.length;i++){
System.out.printf("%.2f ",gpa[i]);
}
System.out.printf("\n\nGpa Average:
%.2f",gpaAverage/gpa.length);
//challenge: on each element of the GPA array, print the
// GPA and the next GPA if they are equal
System.out.println("\n\nGpa's which are equal");
int flag=0;
for(i=0;i<gpa.length-1;i++){
if(gpa[i]==gpa[i+1]){
System.out.println("The Gpa "+gpa[i]+" and Gpa "+gpa[i+1]+" are
equal and are next to each other");
flag=1;
}
}
if(flag==0)
System.out.println("None of the Gpa values which are next to each
other are equal");
}
}
Output Screenshot:
*****************************
For the last challenge which is to print any gpa's which are next to each other and equal....it has a very less chance of generating two numbers with decimals also completely equal next to each other while using random number generation as you are generation numbers of double data type every number will be like ex: 2.345678 etc so getting two such equal numbers in random generation is very rare so you might not see any values like that...so I printed a message that " None of Gpa values which are next to each other are equal "... for your reference..
****************************
Code Screenshots: