Topic Array: Array is consecutive location in Ram. You can have array of object
ID: 3634760 • Letter: T
Question
Topic Array:Array is consecutive location in Ram. You can have array of object
Lab 1:
Write a complete program to create an Array of integer for 10 locations.
Assign random number from 10 to 99 inclusive to that array
Write a method printArray and pass array name to be printed.
Lab 2:
Write a complete program to create an Array of double for 10 locations.
Use loop to get 10 double numbers and assign to array.
Write a method printArray and pass array name to be printed.
Lab3:
Think of methods that are useful to operate on array of double.
Write and test at least two methods. findLargest, and sumArray.
Lab4:
Write a complete program to create an Array of String for 10 locations.
Write java code to pick up 10 names from keyboard and assign to array.
Use loop to print all names.
Explanation / Answer
Please Rate:Thanks
Please post question individually
Lab 1:
import java.util.*;
public class PrintArray {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
int[] a;
a=new int[10];
for(int i=0;i<10;i++)
{
a[i]=(int) (10+Math.random()*90);
}
printArray(a);
}
public static void printArray(int[] a){
System.out.println("The array elements are ");
for(int i=0;i<10;i++)
System.out.print(a[i]+" ");
}
}
----------------------------------------------------------------
Output:
The array elements are
73 75 2 62 44 31 76 44 49 19 BUILD SUCCESSFUL (total time: 0 seconds)
---------------------------------------------------------------------------------------------
Lab 2:
import java.util.*;
public class ArrayDouble {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
double[] a;
a=new double[10];
for(int i=0;i<10;i++)
{
System.out.println("Enter a double value ");
a[i]=input.nextDouble();
}
printArray(a);
}
public static void printArray(double[] a){
System.out.println("The array elements are ");
for(int i=0;i<10;i++)
System.out.print(a[i]+" ");
}
}
---------------------------------------------------------------
Output:
Enter a double value
1.5
Enter a double value
2.9
Enter a double value
3.9
Enter a double value
4.56
Enter a double value
11.4
Enter a double value
20.7
Enter a double value
10.90
Enter a double value
100.2
Enter a double value
34.4
Enter a double value
0.001
The array elements are
1.5 2.9 3.9 4.56 11.4 20.7 10.9 100.2 34.4 0.0010 BUILD SUCCESSFUL (total time: 26 seconds)
-----------------------------------------------------------------------------
Lab 3:
import java.text.DecimalFormat;
import java.util.*;
public class ArrayDouble {
public static void main(String[] args){
Scanner input=new Scanner(System.in);
DecimalFormat dec=new DecimalFormat("###.###");
double[] a;
a=new double[5];
for(int i=0;i<5;i++)
{
System.out.println("Enter a double value ");
a[i]=input.nextDouble();
}
printArray(a);
System.out.println(" Largest number "+findLargest(a));
System.out.println("sum of array "+dec.format(sumArray(a)));
}
public static void printArray(double[] a){
System.out.println("The array elements are ");
for(int i=0;i<5;i++)
System.out.print(a[i]+" ");
}
public static double findLargest(double[] a){
double max=a[0];
for(int i=0;i<5;i++){
if(a[i]>max)
a[i]=max;
}
return max;
}
public static double sumArray(double[] a){
double sum=0.0;
for(int i=0;i<5;i++)
sum=sum+a[i];
return sum;
}
}
---------------------------------------------------------------------------
Output:
Enter a double value
2.3
Enter a double value
4.6
Enter a double value
1.1
Enter a double value
10.0
Enter a double value
9.11
The array elements are
2.3 4.6 1.1 10.0 9.11
Largest number 2.3
sum of array 10.3
BUILD SUCCESSFUL (total time: 9 seconds)
------------------------------------------------------------------------------------------------------------------
Lab 4:
import java.util.Scanner;
public class ArrayString {
public static void main(String[] args){
String[] a;
a=new String[10];
Scanner input=new Scanner(System.in);
for(int i=0;i<10;i++)
{
System.out.print("Enter a String ");
a[i]=input.next();
}
System.out.println("The Strings in array are ");
for(int i=0;i<10;i++)
{
System.out.print(a[i]+" ");
}
}
}
-----------------------------------------------------------------
Output:
Enter a String Eswar
Enter a String gopal
Enter a String karuna
Enter a String ram
Enter a String john
Enter a String vani
Enter a String suresh
Enter a String govind
Enter a String phani
Enter a String meghana
The Strings in array are
Eswar gopal karuna ram john vani suresh govind phani meghana BUILD SUCCESSFUL (total time: 30 seconds)