0

How to find maximum number in this program I am new to collection so please help me. Thanks in advance

public class Main {
   public static void main(String args[])
   {   
       Integer inums[] = {4,8,0,6,1};
       Stats<Integer> iob = new Stats<Integer>(inums);
       int i = iob.getmax();
       System.out.println("maximum value is" );

       Double dums[] = {7.1,3.2,9.1,9.4,5.5};
       Stats<Double> dob = new Stats<Double>(dums);
       double d = dob.getmax();
       System.out.println("maximum value is");

       Float fnums[] = {8.1f,5.9f,9.7f,7.4f};
       Stats<Float> fob = new Stats<Float>(fnums);
       float f = fob.getmax();
       System.out.println("maximum value is");
    }   
}   
reto
  • 9,995
  • 5
  • 53
  • 52
Annu
  • 31
  • 6
  • Stats a new class to define getmax() function – Annu Dec 22 '14 at 08:08
  • What parts of Stats have you written yet? Where exactly are you stuck? – reto Dec 22 '14 at 08:09
  • If i am declaring int and getting double and float error so I am unable to write in genric. – Annu Dec 22 '14 at 08:10
  • Possible duplicate of http://stackoverflow.com/questions/1484347/finding-the-max-min-value-in-an-array-using-java – Raghuveer Dec 22 '14 at 08:11
  • Show that code anyways. Have you looked at some basic tutorials on Generics? – reto Dec 22 '14 at 08:15
  • check my answer below. it should work for any array you pass to it. – Biu Dec 22 '14 at 08:21
  • The above program is read only program I can not alter in this program so need to write complete stats class inorder to get proper output – Annu Dec 22 '14 at 09:03
  • that program is read only file using that program we have to write stats class to get the maximum value given in above program. I am new to programming so unable to understand the proper way to write – Annu Dec 22 '14 at 13:26

2 Answers2

1

If you have a collection containing the desired numbers, all you need to do is

Collections.max(myCollection,null);

This will sort according to the natural ordering of the elements in the collection.

In your case, that max value will be something like:

int i=Collections.max(iob);

and so on for each of the cases, if your Stats is a collection.

SummerCode
  • 1,403
  • 1
  • 14
  • 28
  • for more help, please provide also the Stats class implementation. – SummerCode Dec 22 '14 at 08:10
  • Did you know that there is a `Arrays.asList()` method? However the exercise of OP is about writing their own generic class, not using an existing one. – reto Dec 22 '14 at 08:20
  • I removed that information, but i was thinking that seeing how it is solved with ArrayList it would be easier for him to figure out how to implement it himself. – SummerCode Dec 22 '14 at 08:23
0

This should work for any array you have.

  class Stats<T extends Object & Comparable<? super T>> {

    public Stats(){}

    public T getMax(T[] list)
    {

        T maxElement = list[0];

        for (int i = 1; i < list.length; i++) 
        {
            if (maxElement.compareTo(list[i]) == -1)
            {
                maxElement = list[i];
            }
        }

        return maxElement;
    }
Biu
  • 1,066
  • 10
  • 18
  • I think this does miss the point of the exercise - a generic class `Stats` – reto Dec 22 '14 at 08:14
  • Wait, I dont understand what you are saying? Could you rephrase that? – Biu Dec 22 '14 at 08:16
  • OP says in the comment "Stats a new class to define getmax() function" - so this code needs to be in that new class, that new class needs to support generic types – reto Dec 22 '14 at 08:17
  • I didnt see it in the OP, thanks for that. I have changed it. – Biu Dec 22 '14 at 08:19
  • Exception in thread "main" java.lang.Error: Unresolved compilation problems: The constructor Stats(Integer[]) is undefined The method getmax() is undefined for the type Stats The constructor Stats(Double[]) is undefined The method getmax() is undefined for the type Stats The constructor Stats(Float[]) is undefined The method getmax() is undefined for the type Stats at Main.main(Main.java:6) If i am using your program the above errors are coming – Annu Dec 22 '14 at 08:59
  • The method `getMax()` I wrote takes an argument of an Array, from the stack trace you provided, you didnt pass it any argument, also, the constructor doesnt take any arguments. You are not doing it correctly. To use this class, instantiate it like this `Stats stats = new Stats<>();` , then to use the `getMax()` method, do this `stats.getMax(integerArray);` – Biu Dec 22 '14 at 09:43
  • Sir please explain a bit more. – Annu Dec 22 '14 at 09:49
  • When you look at my code, you can see that the constructor doesnt take any argument since there is nothing in the parenthesis, so when you create a new object of the `Stats` class, you dont pass to it any thing like this `Stats stats = new Stats()`. Then, lets assume that you have an array of `Integer` called `integerArray` , looking at the method `getMax()`, inside the parenthesis, it requires an array of some type that is the subtype of `Comparable` interface, so you pass it your Integer array like this `stats.getMax(integerArray)` – Biu Dec 22 '14 at 10:14