14

I was wondering if anyone could help me find the maximum value of a set of variables and assign them to another variable. Here is a snippet of my code that may help with understanding what I am talking about.

// Ask for quarter values.
    System.out.println("What is the value of the first quarter?");
    firstQuarter = input.nextDouble();

    System.out.println("What is the value of the second quarter?");
    secondQuarter = input.nextDouble();

    System.out.println("What is the value of the third quarter?");
    thirdQuarter = input.nextDouble();

    System.out.println("What is the value of the fourth quarter?");
    fourthQuarter = input.nextDouble();

    //Tell client the maximum value/price of the stock during the year.     
    //maxStock = This is where I need help 
    System.out.println("The maximum price of a stock share in the year is: $" + maxStock + ".");
nybbler
  • 4,793
  • 28
  • 23
Cody B
  • 153
  • 1
  • 2
  • 8

8 Answers8

24

In Java, you can use Math.max like this:

double maxStock = Math.max( firstQuarter, Math.max( secondQuarter, Math.max( thirdQuarter, fourthQuarter ) ) );

Not the most elegant, but it will work.

Alternatively, for a more robust solution define the following function:

private double findMax(double... vals) {
   double max = Double.NEGATIVE_INFINITY;

   for (double d : vals) {
      if (d > max) max = d;
   }

   return max;
}

Which you can then call by:

double maxStock = findMax(firstQuarter, secondQuarter, thirdQuarter, fourthQuarter);
ulmangt
  • 5,343
  • 3
  • 23
  • 36
nybbler
  • 4,793
  • 28
  • 23
  • I apologize I thought this was under the Java forum or something along those lines. – Cody B Feb 16 '12 at 00:55
  • @CodyBennett No worries. I've added the Java tag for you. It might help get more responses if you're looking for a different answer. – nybbler Feb 16 '12 at 01:08
  • This solution is broken as written. Double.MIN_VALUE returns "the smallest positive nonzero value of type double", which is a number very close to 0, NOT the largest possible negative double. Instead, something like Double.NEGATIVE_INFINITY or -Double.MAX_VALUE could be used. – ulmangt Aug 22 '14 at 02:23
  • This was a good approach pre-java8 . For a modern approach see the (next) answer by @arjabbar – WestCoastProjects Feb 19 '21 at 18:46
4

One method to rule them all

public static <T extends Comparable<T>> T max(T...values) {
    if (values.length <= 0)
        throw new IllegalArgumentException();

    T m = values[0];
    for (int i = 1; i < values.length; ++i) {
        if (values[i].compareTo(m) > 0)
            m = values[i];
    }

    return m;
}
mfc
  • 3,018
  • 5
  • 31
  • 43
4

I believe now in Java 8 the most concise version would look like this:

DoubleStream.of(firstQuarter , secondQuarter , thirdQuarter , fourtQuarter).max();
arjabbar
  • 6,044
  • 4
  • 30
  • 46
2

With primitive variables the best choice maybe with Arrays or Collections:

Arrays:

double [ ] data = { firstQuarter , secondQuarter , thirdQuarter , fourtQuarter ) ;
Arrays . sort ( data ) [ 3 ] ;

Collections:

List<Double> data = new Arraylist<Double>();
data.add(firstQuarter);
data.add(secondQuarter);
data.add(thirdQuarter);
data.add(foutQuarter);
Collections.sort(data);
data.get(3);

And if you work with Objects you may use Collections with a Comparator:

class Quarter {
    private double value;
    private int order;

    // gets and sets
}

And to get the max value:

List<Quarter> list = new ArrayList<Quarter>();
Quarter fisrt = new Quarter();
first.setValue(firstQuarter);
first.setOrder(1);
list.add(first);
// Do the same with the other values
Collections.sort(list, new Comparator<Quarter>(){
    compare(Object o1, Object o2){
        return Double.valueOf(o1.getValue()).compareTo(o2.getValue());
    }
}

This may be more complex, but if you working with objects i think is better to work.

Eldius
  • 310
  • 2
  • 4
1

A little late to the party, but for anyone else viewing this question, java 8 has primitive stream types that implement exactly this

Collection<Integer> values = new ArrayList<>();
OptionalInt max = values.stream().mapToInt((x) -> x).max();

mapToInt is the key function that describes how to convert the input to an integer type. The resulting stream then has additional aggregators and collectors specific to integer types, one of the being max().

The result value can then be extracted from the OptionalInt, if the operation was successful

brendon
  • 363
  • 3
  • 11
1
double [ ] data = { firstQuarter , secondQuarter , thirdQuarter , fourtQuarter ) ;
Arrays . sort ( data ) [ 3 ] ;
emory
  • 10,725
  • 2
  • 30
  • 58
0
Max = firstQuarter;
If(secondQuarter > max)
   Max = secondQuarter;

... And so on

Rado
  • 8,634
  • 7
  • 31
  • 44
-3
import java.util.Scanner;


public class dmar {

public static void main ( String  []  srgs){

    Scanner dmar=new Scanner(System.in);{

        {

System.out.println ( "inter number of x  plz") ;

double x=dmar.nextDouble();

System.out.println ( "inter number of y plz") ;{


double y=dmar.nextDouble();

System.out.println ( "inter number of t  plz") ;
double t=dmar.nextDouble();

System.out.println ( "inter number of f  plz") ;
double f=dmar.nextDouble();

{

{
if (x>y);

System.out.println("x biger than y");


if (x>t);
System.out.println("x biger than t");

 if (x>f);

System.out.println("x biger than f");


 if (y>x);
System.out.println("y biger than x");



if (y>t);
System.out.println("y biger than t");

 if (y>f);
System.out.println("y biger than f");

if (t>x&t>f&t>y);
System.out.println("t biger than x");

if (t>y);
System.out.println("t biger than y");

if (t>f);
System.out.println("t biger than f");

 if (f>x);
System.out.println("f biger than x");


if (f>y);
System.out.println("f biger than y");


         if (f>t);
System.out.println("f biger than t");

}
BDM
  • 3,760
  • 3
  • 19
  • 27
anan
  • 1