mertha mertha - 4 months ago 4
Java Question

How to write my Java program with the ACM library (ConsoleProgram)?

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.

Answer

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

Comments