I write a code sample about calculate the aritmetic mean,i did lots of thing but some parts are missing/incorrect.
This is my code sample :
public static void main(String[] args) {
int i;
int j;
int thelargest;
int thesmallest;
Scanner input = new Scanner(System.in);
System.out.println("Enter the list of number : ");
String input2 = input.nextLine();
String[] numbers = input2.split("\\s+");
int[] result = new int[numbers.length];
for (j = 0; j < numbers.length; j++) {
result[j] = Integer.parseInt(numbers[j]);
}
for (i = 0; i < result.length; i++) {
System.out.print("");
System.out.println(result[i]);
}
System.out.println("The Largest Number : "
+ findTheLargestNumber(result));
System.out.println("The Smallest Number : "
+ findTheSmallestNumber(result));
thelargest = findTheLargestNumber(result);
thesmallest = findTheSmallestNumber(result);
float arithmeticMean = (float) (result[i + j])// result.length;
System.out.println("The Arithmetic Mean : " + arithmeticMean);
/*There is a mistake and I tried to solve it but I didn't find any way to solve it.I want my programme to sum the results(numbers) and divide into number of result.(For example :10+20+30=60 and the aritmetic mean is 60/3=20.)Lastly,I think the mistake is about (float)(result[i+j]).
*/
}
public static int findTheSmallestNumber(int[] series) {
int thesmallest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] < thesmallest) {
thesmallest = series[i];
}
}
return thesmallest;
}
public static int findTheLargestNumber(int[] series) {
int thelargest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] > thelargest) {
thelargest = series[i];
}
}
return thelargest;
}
}
Thanks for everbody which gonna help to me .
public static float getAirthmeticMean(int[] result){
int sum = 0;
for (int i = 0; i < result.length; i++) {
sum+=result[i]);
}
return (float)sum/result.length;
}
Your instinct is correct. The result[i+j] line doesn't make sense. How about another function?
public static float calculateMean(int[] series) {
int sum = 0;
for(int i = 0; i < series.length; i++) {
sum = sum + series[i];
}
return ((float)sum) / series.length;
}
Just add all the numbers in the array together in a for loop, and then divide by the length of the array.
Related
OK, I attempted to understand your answers and have changed my code, but I am still running into similar problems. I am attempting to call the IntArray constructor into my IntMath constructor, as there are variables I need to get my math to work. After which, I am attempting to call both constructors into my Main class. However, it is telling my that all my variables "might not have been initialized.
I am so very new at this and just feel dumb and helpless. And I have used Google like crazy.
Thank you in advance for your help.
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//int sum, even, odd, min, max, size;
//int [] intArray;
//double average;
//int even = 0, odd = 0, min = 0, max = 0, size = 0, sum = 0;
//double average;
System.out.print("Please enter a number for the array size (zero or greater): ");
int n=in.nextInt();
if (n<0){
System.out.println("ERROR: Number must be at least zero!!!");
}else {
IntArray mainArray = new IntArray(n);
}
//com.company.IntMath intMath = new com.company.IntMath(average, even, odd, min, max, size, sum);
IntMath intMath = new IntMath();
//intMath.getAverage();
intMath.getEven();
intMath.getOdd();
intMath.getMin();
// intMath.getMax();
intMath.getSize();
intMath.getSum();
System.out.println("Average: " + intMath.getAverage() ); //print the average of the elements
System.out.println("Even Count: " + intMath.getEven()); //prints the count of all even numbers
System.out.println("Odd Count: " + intMath.getOdd()); //prints the count of all odd numbers
System.out.println("Min: " + intMath.getMin()); //prints the min number
//System.out.println("Max: " + intMath.getMax()); //prints the max number
System.out.println("Size: " + intMath.getSize()); //prints the size of the array
System.out.println("Sum: " + intMath.getSum()); //prints the sum of all elements
}
}
package com.company;
import java.util.Arrays;
import java.util.Random;
public class IntArray {
private int n;
private int [] intArray;
private double average;
private int sum;
private int even;
private int odd;
private int max;
private int min;
private int size;
public IntArray(int n) {
this.n = n;
Random randArray = new Random();
int[] intArray = new int[n];
intArray[0] = 0; //why does this not make the element 0 a 0?????????
for (int i = 0; i < n; i++) {
intArray[i] = randArray.nextInt(50); //I was getting very big random numbers, so I set the max to 50
}
System.out.println("Set: " + Arrays.toString(intArray));
}
public double getAverage() {
average = 0;
double total = 0;
for (int i = 0; i < n; i++) {
total = total + intArray[i];
}
return average;
}
public int getSum() {
//find the sum of all elements
sum = 0;
for (int i = 0; i < n; i++) {
sum += intArray[i];
}
return sum;
}
public int getEven() {
even = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 == 0) {
even++;
}
}
return even;
}
public int getOdd() {
odd = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 != 0) {
odd++;
}
}
return odd;
}
public int getMax() {
max = intArray[0];
for (int i = 1; i < n; i++) {
if (intArray[i] > max) {
max = intArray[i];
}
}
return max;
}
public int getMin() {
min = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < intArray.length; j++) {
if (intArray[i] > intArray[j]) {
min = intArray[i];
intArray[i] = intArray[j];
intArray[j] = min;
}
}
}
return min;
}
public int getSize() {
//find the size of the array
size = n + 1;
return size;
}
}
package com.company;
public class IntMath {
private double average;
private int sum;
private int even;
private int odd;
private int max;
private int min;
private int size;
private int[] intArray;
private int n;
//com.company.IntArray mainArray = new com.company.IntArray(n); //call IntArray to get variables ''n'' and ''intArray''
/*public IntMath(double average, int sum, int even, int odd, int max, int min, int size, int[] intArray) {
this.average = average;
this.sum = sum;
this.even = even;
this.odd = odd;
this.max = max;
this.min = min;
this.size = size;
this.intArray = intArray;
com.company.IntArray mainArray = new com.company.IntArray(n); //call IntArray to get variables ''n'' and ''intArray''
// average = 0;
// double total = 0;
// for (int i = 0; i < n; i++) {
// total = total + intArray[i];
// }
//find the sum of all elements
//sum = 0;
//for (int i = 0; i < n; i++) {
// sum += intArray[i];
//}
//find the count of the even numbers
//even = 0;
//for (int i = 0; i < n; i++) {
// if (intArray[i] % 2 == 0) {
// even++;
// }
//}
//find the count of the odd numbers
//odd = 0;
//for (int i = 0; i < n; i++) {
// if (intArray[i] % 2 != 0) {
// odd++;
// }
//}
//find the biggest number
//max = intArray[0];
//for (int i = 1; i < n; i++) {
// if (intArray[i] > max) {
// max = intArray[i];
// }
//}
//find the smallest number
//min = 0;
//for (int i = 0; i < n; i++) {
// for (int j = i + 1; j < intArray.length; j++) {
// if (intArray[i] > intArray[j]) {
// min = intArray[i];
// intArray[i] = intArray[j];
// intArray[j] = min;
// }
// }
/
//find the size of the array
//size = n + 1;
*/
public double getAverage() {
average = 0;
double total = 0;
for (int i = 0; i < n; i++) {
total = total + intArray[i];
}
return average;
}
public int getSum() {
//find the sum of all elements
sum = 0;
for (int i = 0; i < n; i++) {
sum += intArray[i];
}
return sum;
}
public int getEven() {
even = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 == 0) {
even++;
}
}
return even;
}
public int getOdd() {
odd = 0;
for (int i = 0; i < n; i++) {
if (intArray[i] % 2 != 0) {
odd++;
}
}
return odd;
}
public int getMax() {
max = intArray[0];
for (int i = 1; i < n; i++) {
if (intArray[i] > max) {
max = intArray[i];
}
}
return max;
}
public int getMin() {
min = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < intArray.length; j++) {
if (intArray[i] > intArray[j]) {
min = intArray[i];
intArray[i] = intArray[j];
intArray[j] = min;
}
}
}
return min;
}
public int getSize() {
//find the size of the array
size = n + 1;
return size;
}
}
In class Main, if you initialize all the variables, that class will compile without errors.
By going through your classes, I think what you might want to do is,
Accept a number to create an array of that size and populate the array with some random numbers
Next you want to perform various operations like finding average, sum, etc by using the array that you have populated.
If this is the case, then you would have to do something like this...
Effectively, you need not have the IntArray class.
Declare int[] intArray as a class level variable in IntMath class and populate that array as you are doing it now, in the constructor of IntArray class.
define multiple methods in your IntMath class for each of the operations..average, sum, etc and call them in the Main class while you are printing the answer.
You will have to correct the code in getAverage, getSize and getMin methods.
My goal is to print out a user input value (and its corresponding index) if it appears 1 or more times in a random generated array of 50 integers. If I search the array for a value, and it happens to appear more than once, however, only one location of the element is printed, and then the if-else statement is executed. If I remove the break at the end of the third for loop, the whole thing falls apart. I've attached an image but here is the part of it that is giving me an issue. Apologies if the code is not clean, I'm very new.
public static void main(String[] args) {
System.out.println("IDS201 HW3:\n");
System.out.println("1. Generate 50 random integer unsorted list.\n");
Scanner stdin = new Scanner(System.in);
int[] randomNumbers = new int[50];
for(int index = 0; index < randomNumbers.length; index++) {
randomNumbers[index] = (int) (Math.random()*100);
}//end for
int count = 0;
for(int i = 0; i < randomNumbers.length; i++) {
System.out.print(randomNumbers[i] + ",");
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}//end for
System.out.println("\nSearch value?");
int x = stdin.nextInt();
int i;
for(i = 0; i < randomNumbers.length; i++) {
if(randomNumbers[i] == x)
break;}
if (i != randomNumbers.length) {
System.out.println("\nFound " + x + " in array [" + i + "]");}
else {
System.out.println(x + " is not in the list");}
{int temp;
int size = randomNumbers.length;
for(i = 0; i<size; i++ ){
for(int j = i+1; j<size; j++){
if(randomNumbers[i]>randomNumbers[j]){
temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.println("\nSmallest element of the array is: " + randomNumbers[0]);}
System.out.println("\n3. Sort the list:");
int size = randomNumbers.length;
for(i=0; i<size; i++)
{
for(int j=i+1; j<size; j++)
{
if(randomNumbers[i] > randomNumbers[j])
{
int temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
System.out.print("Now the Array after Sorting is :\n\n");
int count1 = 0;
for(i=0; i<size; i++)
{
System.out.print(randomNumbers[i]+ ",");
count++;
if(count == 10) {
System.out.println();
count = 0;
}
}
}
}
if statement in for loop
Implementing the comments to your question:
import java.util.Scanner;
public class NumCount {
private static final int RANDOM_NUMBER_COUNT = 50;
private static void display(int[] randomNumbers) {
int count = 0;
for (int i = 0; i < RANDOM_NUMBER_COUNT; i++) {
System.out.print(randomNumbers[i] + ",");
count++;
if (count == 10) {
System.out.println();
count = 0;
}
}
}
private static int[] generateRandomNUmbers() {
int[] randomNumbers = new int[RANDOM_NUMBER_COUNT];
for (int index = 0; index < RANDOM_NUMBER_COUNT; index++) {
randomNumbers[index] = (int) (Math.random() * 100);
}
display(randomNumbers);
return randomNumbers;
}
private static int search(int[] randomNumbers, int x) {
int i;
int count = 0;
for (i = 0; i < randomNumbers.length; i++) {
if (randomNumbers[i] == x) {
System.out.println("\nFound " + x + " in array [" + i + "]");
count++;
}
}
return count;
}
private static int[] sort(int[] randomNumbers) {
int size = randomNumbers.length;
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (randomNumbers[i] > randomNumbers[j]) {
int temp = randomNumbers[i];
randomNumbers[i] = randomNumbers[j];
randomNumbers[j] = temp;
}
}
}
return randomNumbers;
}
/**
* Start here.
*/
public static void main(String[] args) {
System.out.println("IDS201 HW3:\n");
System.out.println("1. Generate " + RANDOM_NUMBER_COUNT + " random integer unsorted list.\n");
int[] randomNumbers = generateRandomNUmbers();
System.out.print("\n2. Search value? ");
Scanner stdin = new Scanner(System.in);
int x = stdin.nextInt();
int count = search(randomNumbers, x);
if (count == 0) {
System.out.println(x + " is not in the list");
}
System.out.println("\n3. Sort the list:");
sort(randomNumbers);
System.out.print("Now the Array after Sorting is :\n\n");
display(randomNumbers);
}
}
Of-course there is no need to search for the smallest number because it will be the first element in the sorted array. Hence I removed that part of your code.
How can I get this to calculate properties about arrays of doubles. If everything else is an int inside, would that still as an array of doubles? Or is it still an array of doubles anyway because of the method type? Here is my class. Thanks so much!
import java.util.*;
import java.lang.Math;
public class Statistics {
private double min;
private double max;
private double mean;
private double median;
private double deviation;
private double mode;
public static double findMin(int[] array){
int min = array[0];
for(int i=1;i<array.length;i++){
if(array[i] < min){
min = array[i];
}
}
return min;
}
public static double findMax(int[] array){
int max = array[0];
for(int i=1;i<array.length;i++){
if(array[i]>max){
max=array[i];
}
}
return max;
}
public static double calcMean(int[] n){
int sum=0;
for (int i=0; i<n.length; i++){
sum+= n[i];
}
return sum/n.length;
}
public static double calcMedian(int[] n){
int middle = n.length/2;
if (n.length%2==1){
return n[middle];
} else {
return (n[middle]+n[middle])/2;
}
}
public static double calcDeviation(int[] n){
int mean = (int)calcMean(n);
int squareSum = 0;
for (int i = 0; i < n.length; i++) {
squareSum += Math.pow(n[i] - mean, 2);
}
return Math.sqrt((squareSum) / (n.length - 1));
}
public static double calcMode(int n[]){
int value=0; int max=0;
for (int i=0;i<n.length;++i){
int count=0;
for (int j=0; j<n.length; ++j){
if (n[j]==n[i]) ++count;
}
if (count>max){
max=count;
value=n[i];
}
}
return value;
}
}
Here is my main method.
import java.util.*;
public class StatisticsTester {
public static void main(String[] args) {
Statistics test = new Statistics();
Scanner input = new Scanner(System.in);
//Read user input.
System.out.print("How many numbers do you want to enter?: ");
int num = input.nextInt();
double array[] = new double[num];
System.out.println("Enter the " + num + "numbers now.");
for (int i = 0; i < array.length; i++ )
{
array[i] = input.nextInt();
}
System.out.print("Here is the minimum, ");
System.out.print("maximum, mean, median, ");
System.out.println("mode, and standard deviation: ");
System.out.print(test.findMin(num) +", " + test.findMax(num));
System.out.print(", "+ test.calcMean(num) +", ");
System.out.print(test.calcMedian(num) +", ");
System.out.print(test.calcMode(num) +", ");
System.out.print(test.calcDeviation(num));
}
}
These are the errors when it compiles.
StatisticsTester.java:25: findMin(int[]) in Statistics cannot be applied to (int)
System.out.print(test.findMin(num) +", " + test.findMax(num));
^
StatisticsTester.java:25: findMax(int[]) in Statistics cannot be applied to (int)
System.out.print(test.findMin(num) +", " + test.findMax(num));
^
StatisticsTester.java:26: calcMean(int[]) in Statistics cannot be applied to (int)
System.out.print(", "+ test.calcMean(num) +", ");
^
StatisticsTester.java:27: calcMedian(int[]) in Statistics cannot be applied to (int)
System.out.print(test.calcMedian(num) +", ");
^
StatisticsTester.java:28: calcMode(int[]) in Statistics cannot be applied to (int)
System.out.print(test.calcMode(num) +", ");
^
StatisticsTester.java:29: calcDeviation(int[]) in Statistics cannot be applied to (int)
System.out.print(test.calcDeviation(num));
^
6 errors
There are two errors :
First : When you do this - test.findMin(num) you are trying to pass parameter num. But num is not array! It is a number. You probably want to do this : test.findMin(array)
Second : You can implicitly convert integer to double, because you can be sure that it remains same. But you cant convert double to integer implicitly, because you cant convert for example 2,7 to integer. And for arrays, even the "implicit" conversion does not work.
Solution for you, change this line double array[] = new double[num]; to int array[] = new int[num]; and then change all your parameters which looks like this test.findMin(num) to this test.findMin(array)
For working with doubles this would compile (does not know if it works as expected) :
import java.util.*;
public class StatisticsTester {
public static void main(String[] args) {
Statistics test = new Statistics();
Scanner input = new Scanner(System.in);
//Read user input.
System.out.print("How many numbers do you want to enter?: ");
int num = input.nextInt();
double array[] = new double[num];
System.out.println("Enter the " + num + "numbers now.");
for (int i = 0; i < array.length; i++) {
array[i] = input.nextDouble();
}
System.out.print("Here is the minimum, ");
System.out.print("maximum, mean, median, ");
System.out.println("mode, and standard deviation: ");
System.out.print(test.findMin(array) + ", " + test.findMax(array));
System.out.print(", " + test.calcMean(array) + ", ");
System.out.print(test.calcMedian(array) + ", ");
System.out.print(test.calcMode(array) + ", ");
System.out.print(test.calcDeviation(array));
}
}
import java.util.*;
import java.lang.Math;
public class Statistics {
private double min;
private double max;
private double mean;
private double median;
private double deviation;
private double mode;
public static double findMin(double[] array) {
double min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
public static double findMax(double[] array) {
double max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
public static double calcMean(double[] n) {
int sum = 0;
for (int i = 0; i < n.length; i++) {
sum += n[i];
}
return sum / n.length;
}
public static double calcMedian(double[] n) {
int middle = n.length / 2;
if (n.length % 2 == 1) {
return n[middle];
} else {
return (n[middle] + n[middle]) / 2;
}
}
public static double calcDeviation(double[] n) {
int mean = (int) calcMean(n);
int squareSum = 0;
for (int i = 0; i < n.length; i++) {
squareSum += Math.pow(n[i] - mean, 2);
}
return Math.sqrt((squareSum) / (n.length - 1));
}
public static double calcMode(double n[]) {
double value = 0;
int max = 0;
for (int i = 0; i < n.length; ++i) {
int count = 0;
for (int j = 0; j < n.length; ++j) {
if (n[j] == n[i]) {
++count;
}
}
if (count > max) {
max = count;
value = n[i];
}
}
return value;
}
}
My question is about discarding the largest and smallest number when my program calculates an arithmetic mean.
This is my code sample:
public static void main(String[] args) {
int k;
int r;
int thelargest;
int thesmallest;
Scanner input = new Scanner(System.in);
System.out.println("Enter the list of number : ");
String input2 = input.nextLine();
String[] numbers = input2.split(" ");
int[] result = new int[numbers.length];
for (r = 0; r < numbers.length; r++) {
result[r] = Integer.parseInt(numbers[r]);
}
for (k = 0; k < result.length; k++) {
System.out.print("");
System.out.println(result[k]);
}
System.out.println(" LargestNumber : " + TheLargestNumber(result));
System.out.println(" SmallestNumber : " + TheSmallestNumber(result));
thelargest = TheLargestNumber(result);
thesmallest = TheSmallestNumber(result);
System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));
}
public static int TheSmallestNumber(int[] series) {
int thesmallest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] < thesmallest) {
thesmallest = series[i];
}
}
return thesmallest;
}
public static int TheLargestNumber(int[] series) {
int thelargest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] > thelargest) {
thelargest = series[i];
}
}
return thelargest;
}
public static float AirthmeticMean(int[] result) {
int sum = 0;
for (int i = 0; i < result.length; i++) {
sum += result[i];
}
return (float) sum / result.length;
}
I tried to find the way and I wrote this sample but I don't know how to embed this code sample:
for (int i = 0; i < result.length; i++) {
if (series[i] != thesmallest && series[i] != thelargest) {
total = total + seriess[i];
}
}
Will this code sample be helpful to me?
Just before your
System.out.println("The Arithmetic Mean : " + AirthmeticMean(result));
write
thenewmean = (AirthmeticMean(result)*result.length - thesmallest - thelargest)/(result.length-2)
and then print thenewmean
System.out.println("The Arithmetic Mean : " + thenewmean);
You don't need to write your
for (int i = 0; i < result.length; i++) {
if (series[i] != thesmallest && series[i] != thelargest) {
total = total + seriess[i];
}
}
code anywhere. Even then if you wish to use your own code,
then use it in your AirthmeticMean() function
Need to keep track of count as well as sum if removing highest and smallest
public static float AirthmeticMean(int[] result, int theSmallest, int theLargest) {
int sum = 0;
int cnt = 0;
for (int i = 0; i < result.length; i++) {
if (result[i] != theSmallest && result[i] != theLargest) {
sum += result[i];
cnt++;
}
}
return (float) sum / cnt;
}
The code sample you've got:
for (int i = 0; i < result.length; i++) {
if (series[i] != thesmallest && series[i] != thelargest) {
total = total + seriess[i];
}
}
is almost OK. Almost, because you retrieve the length of the array named result in the for-loop and access elements of array series.
You can use this code in the following way. Extend AirthmeticMean with two parameters theSmallest and theLargest and keep track of the number of summed elements:
public static float AirthmeticMean(int[] result, int theSmallest, int theLargest) {
int sum = 0;
int numElements = 0;
for (int i = 0; i < result.length; i++) {
if (result[i] != theSmallest && result[i] != theLargest) {
sum += result[i];
numElements++;
}
}
return (float) sum / numElements;
}
EDIT: added numElements.
I want ask a little question about my program.
This is my code sample:
public static void main(String[] args) {
int q;
int p;
int thelargest;
int thesmallest;
Scanner input = new Scanner(System.in);
System.out.println("Enter the list of number : ");
String input2 = input.nextLine();
String[] numbers = input2.split(" ");
int[] result = new int[numbers.length];
for (p = 0; p < numbers.length; p++) {
result[p] = Integer.parseInt(numbers[p]);
}
for (q = 0; q < result.length; q++) {
System.out.print("");
System.out.println(result[q]);
}
System.out.println("Largest Number : " + LargestNumber(result));
System.out.println(" Smallest Number : " + SmallestNumber(result));
thelargest = LargestNumber(result);
thesmallest = SmallestNumber(result);
System.out.println("The Arithmetic Mean : "
+ AirthmeticMean(result, thesmallest, thelargest));
}
public static int SmallestNumber(int[] series) {
int thesmallest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] < thesmallest) {
thesmallest = series[i];
}
}
return thesmallest;
}
public static int LargestNumber(int[] series) {
int thelargest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] > thelargest) {
thelargest = series[i];
}
}
return thelargest;
}
public static float AirthmeticMean(int[] result, int thesmallest,
int thelargest) {
int sum = 0;
for (int i = 0; i < result.length; i++) {
sum += result[i];
}
sum -= thesmallest;
sum -= thelargest;
return (float) sum / result.length;
}
How can I convert this code sample to the ConsoleProgram (which is in the ACM library)?
Which parts must I change or add?
I started with:
public class ArithmeticMean extends ConsoleProgram {
}
But I do not know what to do next.
In acm library no main method though you need to use instead the following construction:
public void run() {}
Here is an API of this library http://jtf.acm.org/javadoc/student/
Select acm.program package ConsoleProgram class and find appropriate methods
see also acm.io / class IOConsole
e.g. System.out.println() --> println()
Scanner (String input) --> readLine(String prompt) etc.
the rest is the same as you in your code.
Ok, here you are your code in acm: (a bit ugly but works fine:)
import acm.program.ConsoleProgram;
public class StackOverflow extends ConsoleProgram
{
private static final long serialVersionUID = 1L;
public void run()
{
int q;
int p;
int thelargest;
int thesmallest;
String input2 = "";
String[] numbers = null;
println("Enter the list of number : ");
while (true) {
String input = readLine();
if (input.equals(""))
break;
input2 += input + " ";
}
numbers = input2.split(" ");
int[] result = new int[numbers.length];
for (p = 0; p < numbers.length; p++) {
result[p] = Integer.parseInt(numbers[p]);
}
for (q = 0; q < result.length; q++) {
print("");
println(result[q]);
}
println("Largest Number : " + LargestNumber(result));
println(" Smallest Number : " + SmallestNumber(result));
thelargest = LargestNumber(result);
thesmallest = SmallestNumber(result);
println("The Arithmetic Mean : "
+ AirthmeticMean(result, thesmallest, thelargest));
}
public static int SmallestNumber(int[] series)
{
int thesmallest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] < thesmallest) {
thesmallest = series[i];
}
}
return thesmallest;
}
public static int LargestNumber(int[] series)
{
int thelargest = series[0];
for (int i = 1; i < series.length; i++) {
if (series[i] > thelargest) {
thelargest = series[i];
}
}
return thelargest;
}
public static float AirthmeticMean(int[] result, int thesmallest,
int thelargest)
{
int sum = 0;
for (int i = 0; i < result.length; i++) {
sum += result[i];
}
sum -= thesmallest;
sum -= thelargest;
return (float) sum / result.length;
}
}
And Run as JavaApplet