I need to know how to write program excercise 4 in chapter 8 of Java Programming
ID: 667151 • Letter: I
Question
I need to know how to write program excercise 4 in chapter 8 of Java Programming by Joyce Farrell.
Create an application containing an array that stores eight integers. The application should (1) display all the integers, (2) display all the integers in reverse order, (3) display the sum of the eight integers, (4) display all values less than 5, (5) display the lowest value, (6) display the highest value, (7) calculate and display the average, and (8) display all values that are higher than the calculated average value. Save the file as NumberListDemo.java.
Explanation / Answer
Program in Java
//file name:NumberListDemo.java
1./*display all integers*/
public class NumberArray
{ private static int[] arr=new arr[8];//array declaration of data type integer.
public static void main(String args[] ) //public is an access specifier which allows the
{ programmer to control the visibility of lass member
arr[0]=1; //assigning value 1 in 0th address of an array
arr[1]=2;
arr[2]=3;
arr[3]=4;
arr[4]=5;
arr[5]=6;
arr[6]=7;
arr[7]=8;
System.out.print(“display integers: ”);
for(int i=0;i<=8,i++) {
System.out.println(“$“+arr[i]); //display the 8 integer values
}
}
}
//sum of 8 integers
System.out.print(“ sum:$.%2d ”,sum());
//value less than 5
System.out.print(“values less than $5: %s ”,valueLessThan5());
//lowest value
System.out.print(“lowest value: %s ”,lowestValue());
//higest value
System.out.print(“highest value: %s ”,highestValue());
}
2./*display all integers in reverse order*/
3./*display sum of 8 integers*/
Follow the above program :
private static int sum() //declaration of a function sum of integer type
{ int total=0;
for(i=0;i<=8;i++)
{
total+=arr[i]; }
return total;
}
4./*display all values less than 5 */
private static String valueLessThan5()
{
String values=” ”;
for(i=0;i<=8;i++)
{ if(arr[i]<5)
{ values+=” $“+arr[i]+” ” ;}
} return values
}
5./*display lowest value */
private static String lowestValue()
{ int lowest =lowestValue();
String values=” “;
for(i=0;i<=8;i++)
{ if(arr[i]<lowest)
{value+=”$”+arr[i]+” ” ;}
}}
6./*highest value*/
private static String highestValue()
{ int highest =highestValue();
String values=” “;
for(i=0;i<=8;i++)
{ if(arr[i]>highest)
{value+=”$”+arr[i]+” ” ;}
}}