Review Questions (Chapter 6 ARRAYS)
Section 6.2 Array Basics
6.3 ...What is the printout of the following code?int x = 30;
int[] numbers = new int[x];
x = 60;
System.out.println("x is " + x);
System.out.println("The size of numbers is " + numbers.length);
6.9 Identify and fix the errors in the following code:
1 public class Test {
2 public static void main(String[] args) {
3 double[100] r;
4
5 for (int i = 0; i < class="docEmphStrong">100;
7 }
8 }
Programming Exercises
Section 6.2 Array Basics (Liang)6.1 Write a program that reads ten numbers, computes their average, and finds out how many numbers are above the average.
6.3 Write a program that reads ten integers and displays them in the reverse of the order in which they were read.
Solution
Question 6.1
/**
* @(#)lab30_ogos_10.java
*
* lab30_ogos_10 application
*
* @author
* @version 1.00 2010/8/30
*/
import javax.swing.JOptionPane;
public class lab30_ogos_10 {
public static void main(String[] args) {
int[] r = new int[10];
// read 10 integers
for (int i = 0; i < r.length; i++)
{
String intString = JOptionPane.showInputDialog("Enter integer ");
r[i] = Integer.parseInt(intString);
}
// print out the 10 integers
for (int i = 0; i < r.length; i++)
{
System.out.println(r[i]);
}
// find average of 10 integers
int sum=0;
double average;
for (int i = 0; i < r.length; i++)
{
sum = sum + r[i];
}
average = sum/10.0;
System.out.println("Average of 10 integers = " +average);
// count the numbers that are above of average
int above_average =0;
for (int i = 0; i < r.length; i++)
{
if(average > r[i])
above_average++;
}
System.out.println("There are = " +above_average +" numbers that are above of average");
}
}
No comments:
Post a Comment