// ArrayList
// Integer Array
/*
Write a Java program to perform the
following task.
Take an integer array of size 20,
initialize values randomly between 10 and 90, simultaneously sum all values and
calculate average. Now separate values below average and above average in
ArrayLists. Finally print both lists in 2 separate rows.
*/
import java.util.*;
class ALRandInt
{
public
static void main(String as[])
{
Integer
[] ia = new Integer[20];
ArrayList
<Integer>al1 = new ArrayList<Integer>();
ArrayList
<Integer>al2 = new ArrayList<Integer>();
int
i = 0;
int
Sum = 0;
while(i<20)
{
int
v=((int)(Math.random()*1000))%90;
if(v>10)
{
ia[i]=v;
Sum+=v;
i++;
}
}
int
avg = (int)(Sum/20);
for(Integer
val:ia)
{
int
x = Integer.valueOf(val);
if(x>avg)
al1.add(val);
else
al2.add(val);
}
ArrayList<Integer>
al = new ArrayList<>(Arrays.asList(ia));
System.out.println("ArrayList
1 - All Elements : "+al);
System.out.println("Average
= "+avg);
System.out.println("ArrayList
2 - Elements which are above average : "+al1);
System.out.println("ArrayList
3 - Elements which are below average : "+al2);
}
}
/*
Sample Output
D:\oopjava>javac ALRandInt.java
D:\oopjava>java ALRandInt
ArrayList 1 - All Elements : [12, 84, 68,
79, 30, 66, 67, 11, 30, 82, 56, 37, 43, 49, 48, 72, 21, 56, 35, 21]
Average = 48
ArrayList 2 - Elements which are above
average : [84, 68, 79, 66, 67, 82, 56, 49, 72, 56]
ArrayList 3 - Elements which are below
average : [12, 30, 11, 30, 37, 43, 48, 21, 35, 21]
D:\oopjava>java ALRandInt
ArrayList 1 - All Elements : [60, 66, 39,
44, 23, 28, 70, 42, 71, 85, 31, 65, 71, 31, 59, 18, 76, 58, 86, 88]
Average = 55
ArrayList 2 - Elements which are above
average : [60, 66, 70, 71, 85, 65, 71, 59, 76, 58, 86, 88]
ArrayList 3 - Elements which are below
average : [39, 44, 23, 28, 42, 31, 31, 18]
D:\oopjava>
*/
No comments:
Post a Comment
Don't be a silent reader...
Leave your comments...
Anu