|
public class ArrayExamp { static int n[] = { 43, 74, 92, 56, 57, 94, 87, 21, 76, 57, 39, 57, 68 };
public static void main(String args[]) { for (int x = 0; x < n.length; x++) System.out.print(n[x] + " ");
System.out.println(); System.out.println("Average is " + findAvg()); }
static public double findAvg() { int sum = 0; for (int x = 0; x < n.length; x++) sum = sum + n[x]; return sum / n.length; }
}
/* Run this program. Now write a method called findBig that will return an integer with the highest number in the array.
Now add a method called findAs that will return how many scores were in the 80s.
(Difficult)Now see if you can write a method to find the score that is the most frequent (in this case 57). Call it findMode and it will return an integter.
MORE PRACTICE:
These are sample practice programs similar to the ones you will be asked to write on the test. They will use this template:
public class ArrayExamp { static int n[] = { 43, 74, 92, 56, 57, 94, 87, 21, 76, 57, 39, 57, 68 };
public static void main(String args[]) { for (int x = 0; x < n.length; x++) System.out.print(n[x] + " ");
System.out.println(); System.out.println("Average is " + findAvg()); }
static public double findAvg() { int sum = 0; for (int x = 0; x < n.length; x++) sum = sum + n[x]; return sum / n.length; }
}
You will be asked to write only a method similar to the findAvg method above. Use this template, however, to test your method. Change the values of the Array. You may use String arrays on some problems.
Write a method that returns:
- · the sum of all the values less than the average.
- · the value before the highest score (0 if the highest score is first) in the array
- · the avg of all numbers more than 10 below the high score
- · all scores that appear only once
- · the average of all the scores with the highest one excluded
- · the highest 2 scores
I will add more practice problems below. Some will involve passed in parameters, similar to the example below:
Write a method that passes in 2 integers and returns with the sum of all the numbers in the array that are between these two numbers. ( in this case, we are testing it with 50 and 80 )
public class ArrayExamp { static int n[] = { 43, 74, 92, 56, 57, 94, 87, 21, 76, 57, 39, 57, 68 };
public static void main(String args[]) { for (int x = 0; x < n.length; x++) System.out.print(n[x] + " ");
System.out.println(); System.out.println("Sum of nums between 50 and 80 = " + sumSpread(50,80)); }
static public int sumSpread(int low, int high) { int sum = 0; for (int x = 0; x < n.length; x++) { if ( n[x] >= low ) if ( n[x] <= high ) sum = sum + n[x]; }
return sum; }
}
import java.applet.Applet; import java.awt.*;
public class ArrayInput extends Applet {
TextField t1,t2; Button b1; String names[]; String scores[]; int acount;
public void init() { b1 = new Button("Enter"); t1 = new TextField(10); t2 = new TextField(10); add(b1); add(t1); add(t2);
names = new String[50]; scores = new String[50];
acount = 0; }
public void paint(Graphics g) { for (int x=0;x<acount;x++) { g.drawString(names[x] + ",",x*25,100); g.drawString(scores[x] + ",",x*25,120); } }
public boolean action(Event e,Object o) { if (e.target==b1) { names[acount] = t1.getText(); scores[acount] = t2.getText(); acount++; }
t1.setText(""); t2.setText("");
repaint();
return true; }
}
|