Ex1. Declare and initialize arrays of –10 chars –5 integers –10 boolean –25 floa
ID: 3680379 • Letter: E
Question
Ex1.
Declare and initialize arrays of
–10 chars
–5 integers
–10 boolean
–25 float
Ex2.
•Consider the array
char[] letters = {'a','e','i','o','x'};
–Access the element in the first position
–Change the value of the last element to letter 'u'
Ex 3,
•Go back to Exercise 1. Print out the last element of each of the arrays using length property (array.lenght – 1).
•Check the error occurring if you try to access the element in position array.length
Ex 4
int[] scores = { 10, 9, 7, 10, 5, 20, 74 };
for(int i = 0; i < scores.length; i++){
System.out.println(scores[i]);
}
Starting from the above code as an example, write a piece of code that computes the average and the sum of the array scores.
Ex5
•Write a method to find the minimum and maximum values within an array of double
Explanation / Answer
Hi, Please find the below answers for your questions.
Ex1:
Declare and initialize arrays of
–10 chars
–5 integers
–10 boolean
–25 float
Answer:
char charobj[] = new char[10];
int intobj[] = new int[5];
boolean booleanobj[] = new boolean[10];
float floatobj[] = new float[25];
Ex2:
•Consider the array
char[] letters = {'a','e','i','o','x'};
–Access the element in the first position
–Change the value of the last element to letter 'u'
Answer:
for accessing the element in the first position we have to use like letters[0]
For printing first element:
System.out.println("First Element from Array : "+letters[0]);
–Change the value of the last element to letter 'u': Use below statement.
letters[letters.length - 1] = 'u'
Ex 3:
•Go back to Exercise 1. Print out the last element of each of the arrays using length property (array.lenght – 1).
•Check the error occurring if you try to access the element in position array.length
Answer:
-> Print out the last element of each of the arrays using length property (array.lenght – 1).
System.out.println("Char Array Last Element Display : "+charobj[charobj.length - 1]);
System.out.println("Integer Array Last Element Display : "+intobj[intobj.length - 1]);
System.out.println("Boolean Array Last Element Display : "+booleanobj[booleanobj.length - 1]);
System.out.println("Float Array Last Element Display : "+floatobj[floatobj.length - 1]);
-> Check the error occurring if you try to access the element in position array.length:
If you try to access the element from array by array.length property then you will get an exception
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException:
Ex 4
int[] scores = { 10, 9, 7, 10, 5, 20, 74 };
for(int i = 0; i < scores.length; i++){
System.out.println(scores[i]);
}
Starting from the above code as an example, write a piece of code that computes the average and the sum of the array scores.
Answer:
public class AverageAndSum {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] scores = { 10, 9, 7, 10, 5, 20, 74 };
int sum = 0;
for(int i = 0; i < scores.length; i++){
System.out.println(scores[i]);
sum = sum + scores[i];
}
System.out.println("The Sum of the array score is ["+sum+"]");
System.out.println("The Average of the array score is ["+((double)sum/scores.length)+"]");
}
}
Output:
10
9
7
10
5
20
74
The Sum of the array score is [135]
The Average of the array score is [19.285714285714285]
Ex5
•Write a method to find the minimum and maximum values within an array of double
Answer:
public class MinMaxValues {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
double values[] = {11.4,2.5,115.4,116,321,9.7,15.5,7.7,999.9,1232.1};
// Calling getMax() method for getting max value
double max = getMax(values);
System.out.println("Maximum Value is: "+max);
// Calling getMin() method for getting min value
double min = getMin(values);
System.out.println("Minimum Value is: "+min);
}
// Method for getting the maximum value
public static double getMax(double[] inputArray){
double maxValue = inputArray[0];
for(int i=1;i < inputArray.length;i++){
if(inputArray[i] > maxValue){
maxValue = inputArray[i];
}
}
return maxValue;
}
// Method for getting the minimum value
public static double getMin(double[] inputArray){
double minValue = inputArray[0];
for(int i=1;i<inputArray.length;i++){
if(inputArray[i] < minValue){
minValue = inputArray[i];
}
}
return minValue;
}
}
Output:
Maximum Value is: 1232.1
Minimum Value is: 2.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.