Top
Previous Next
Java Arrays, Objects, Methods CS 161 - Java

Array Manipulation

In class example of some array manipulation

Write a Java class named Arrays.java. This class should have the following methods.
  1. public void listArgs( String [] args)
    To list out the arguments in an array of Strings

  2. public long product( int [] intArray )
    To compute the product of the integers in the array intArray

  3. public static void main( String[] args )
    Should have the following code:
    if (args.length == 0) {
      System.out.println("usage: Arrays [integers]");
    }
    else {
      Arrays inst = new Arrays();
      inst.listArgs(args);
    
      int input[] = new int[args.length];
      for (int i = 0; i < args.length; i++) {
        input[i] = Integer.parseInt(args[i]);
      }
      System.out.print("product = ");
      long answer = inst.product(input);
      System.out.println(answer);
    }
    
    


  4. Top
    Previous Next
    jwd