0% found this document useful (0 votes)
8 views

Array Homework 2

The Java program contains multiple methods for array manipulation and analysis. It includes functionalities to print distinct numbers from an array, analyze user-input scores, generate random integers, and count occurrences of user-input integers. Each method utilizes loops and conditionals to perform its respective tasks and outputs results to the console.

Uploaded by

eyalprihar23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Array Homework 2

The Java program contains multiple methods for array manipulation and analysis. It includes functionalities to print distinct numbers from an array, analyze user-input scores, generate random integers, and count occurrences of user-input integers. Each method utilizes loops and conditionals to perform its respective tasks and outputs results to the console.

Uploaded by

eyalprihar23
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

import java.util.

ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class ArrayHomework2 {

public static void main(String[] args) {

}
public static void PrintDistinctNum() {
int[] TenNum = {1, 2, 3, 2, 1, 6, 3, 4, 5, 2};
int[] DistinctNum = new int[10];
int count = 0;

for (int i = 0; i < TenNum.length; i++) {


boolean isDistinct = true;

for (int j = 0; j < i; j++) {


if (TenNum[j] == TenNum[i]) {
isDistinct = false;
break;
}
}

if (isDistinct) {
DistinctNum[count] = TenNum[i];
count++;
}
}

int[] distinctNumbers = Arrays.copyOf(DistinctNum,


count);
System.out.println(Arrays.toString(distinctNumbers));
}

public static void AnalyzeScores() {

Scanner scan = new Scanner(System.in);

ArrayList<Integer> Score = new ArrayList<>();


System.out.println("Enter scores (enter -1 to
stop):");
int input = scan.nextInt();
while (input != -1) {
Score.add(input);
input = scan.nextInt();

//int[] Score = {100, 90, 80, 60, 50, 69, 120, 100};
int sum = 0;
for (int j : Score) {
sum += j;
}
int Avg = sum/(Score.size()-1);
int AboveOrEqualAvg = 0;
int BelowAvg = 0;

for (int j : Score) {


if (j >= Avg) {
AboveOrEqualAvg++;
} else {
BelowAvg++;
}

System.out.println(AboveOrEqualAvg + " numbers are


above or equal to average");
System.out.println(BelowAvg + " numbers are below
average");

public static void RandomInt () {

int[] numbers = new int[10];

for (int i = 0; i < 100; i++) {


int random = (int) (Math.random() * 10);
numbers[random]++;

System.out.println(Arrays.toString(numbers));
}

public static void CountOccurance() {

Scanner scan = new Scanner(System.in);

ArrayList<Integer> count = new ArrayList<>();

System.out.println("Enter the integers between 1


and 100 (input 0 to stop):");
int input = scan.nextInt();
while (input != -0) {
count.add(input);
input = scan.nextInt();

}
System.out.println(count);
int[] occurance = new int[100];
int length = count.size();

for (int i = 0; i < length; i++) {


for (int j = 0; j < occurance.length; j++) {
if (count.get(i) == j) {
occurance[j]++;
}
}

for (int i = 0; i < occurance.length; i++) {


if (occurance[i] == 1) {
System.out.println("The number " + i + "
occured " + occurance[i] + " time");
}
else if (occurance[i] != 0) {
System.out.println("The number " + i + "
occured " + occurance[i] + " times");
}
}
}

You might also like