• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1326
  • Last Modified:

Java MAX, MIN values

Pls advice on how to add new methods for MAX, MIN values of the array and print array in reverse in the code.

TIA,
V.
import java.io.*;
import java.util.Scanner;
import java.util.Random;
 
public class Class4( int[]  pack1 ) {
    super(); //Call the superclass (Object) empty ctor
 this.pack1 = pack1 ;
  }
protected int[]  pack1 ; 
 
 
  int    i;
  int[]  pack1      = new int[20]; //pack1[0],  ..... pack1[19]
  Random randomizer = new Random();
 
  public Class4() {
 
  }
  public static Class4 getInstance(){
    return new Class4();
  }
 
  public static void main( String[] args ) {
    Class4 instance1 = Class4.getInstance();
    instance1.makeArray();
    instance1.dummy();
 
    Class4 instance2 = Class4.getInstance();
    instance2.makeArray();
    instance2.dummy();
     
    //Print out the odd numbers only
    for ( int i = 0; i < 20; i++ ) {
      if ( instance1.pack1[i] % 2 != 0 )
          System.out.println(instance1.pack1[i]==instance2.pack1[i]);
    }
  } //main
 
  private void dummy() {
    System.out.println("I a dummy method");    
  }
  public void makeArray() { // new function
    // 1) Fill in with random numbers
    for ( int i = 0; i < 20; i++ ) {
      pack1[i] = randomizer.nextInt( 1000 );
    }
  }
  
  public void print() {
          for ( int i = 0; i < 20; i++ ) {
              System.out.println(pack1[i]);
            }
        
  }
} //Class4

Open in new window

0
Nakuru1234
Asked:
Nakuru1234
6 Solutions
 
basav_comCommented:
0
 
basav_comCommented:
reverse array:
for(int i=a.length;i>=0;i--)
 System.out.println(a[i]);
0
 
CEHJCommented:
You can reverse thus:
public void printInReverse() {
	for ( int i = pack1.length -1; i >= 0; i-- ) {
		System.out.println(pack1[i]);
	}
}

Open in new window

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
CEHJCommented:
Min and max could be
public int max() {
	int max = Integer.MIN_VALUE;
	for (int i=0; i<pack1.length; i++) {
		max = Math.max(max, pack1[i]);
	}
	return max;
}
 
public int min() {
	int min = Integer.MAX_VALUE;
	for (int i=0; i<pack1.length; i++) {
		min = Math.min(min, pack1[i]);
	}
	return min;
}

Open in new window

0
 
Nakuru1234Author Commented:
Great! Thank you.
0
 
CEHJCommented:
OK - btw - you seem to have a lot of questions open ...
0
 
objectsCommented:
> Min and max could be

thats just repeats what basav_com has already posted.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now