In: Computer Science
Arrays Assignment in Java
1. Suppose you are doing a report on speeding. You have the data from 10 different people who were speeding and would like to find out some statistics on them. Write a program that will input the speed they were going. You may assume that the speed limit was 55. Your program should output the highest speed, the average speed, the number of people who were between 0-10 miles over, the number between 10 and 20, and the number over 20. Your program should print out these results.
2. Create a list of 100 randomly generated numbers (between 1 and 100). Print the list out in order, print the list out in reverse order, print out how many of each number was generated.
JAVA PROGRAM
1
import java.util.*;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// array declaration
int speed[] = new int[10];
int i = 0,c1=0,c2=0,c3=0,highspeed
= 0;
float avg = 0;
// runs the loop
for(i=0;i<10;i++)
{
// reads the input from user and store it in
array
speed[i] = sc.nextInt();
}
// runs the loop
for(i=0;i<10;i++)
{
// checks the conditon for number between 0 - 10
if(speed[i]>=0 && speed[i]<=10)
{
// keeps track of 0 - 10
c1++;
}
// checks the conditon for number between 10 -
20
if(speed[i]>10 && speed[i]<=20)
{
// keeps track of 10 - 20
c2++;
}
// checks the conditon for number over 20
if(speed[i]>20)
{
// keeps track of over 20
c3++;
}
// checks the conditon to find highspeed
if(highspeed<speed[i])
{
highspeed = speed[i];
}
// calculating sum of all
avg+=speed[i];
}
// calculating Average
avg = avg/10;
// prints the statement
System.out.println("The Number
Between:0-10:"+c1);
System.out.println("The Number
Between:10-20:"+c2);
System.out.println("The Number Over 20:"+c3);
System.out.println("Average Speed:"+avg);
System.out.println("Highest Speed:"+highspeed);
}
}
OUTPUT
2.
import java.util.*;
import java.util.Scanner;
import java.util.Random;
public class Main
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<Integer> array = new
ArrayList<Integer>(100);
Random rand = new Random();
int i=0,j=0,count=0,temp=0;
for(i=0;i<100;i++)
{
temp = rand.nextInt(101 - 1) + 1;
array.add(temp);
}
System.out.println("List Inorder:");
// runs the loop to print in in order
for(i=0;i<100;i++)
{
// prints the element of list
System.out.print(array.get(i)+" ");
}
System.out.println("\nList In Reverse:");
for(i=99;i>=0;i--){
System.out.print(array.get(i)+" ");
}
System.out.println("\nEach Number Generated:");
//boolean array
boolean visited[] = new boolean[100];
// fills the boolean array with false
Arrays.fill(visited, false);
// runs the loop
for ( i = 0; i < 100; i++) {
// checks the condition
//Skip this element if already processed
if (visited[i] == true)
continue;
// keeps track of Count frequency
count = 1;
// runs the loop
for ( j = i + 1; j < 100; j++)
{
if (array.get(i) == array.get(j))
{
visited[j] = true;
count++;
}
}
System.out.println(array.get(i) + " " + count);
}
}
}
output
thumbs up
.