Cody B
Cody B

Reputation: 153

How to find maximum value of set of variables

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 + ".");

Upvotes: 14

Views: 84098

Answers (8)

arjabbar
arjabbar

Reputation: 6404

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

DoubleStream.of(firstQuarter , secondQuarter , thirdQuarter , fourtQuarter).max();

Upvotes: 4

brendon
brendon

Reputation: 353

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

Upvotes: 0

nybbler
nybbler

Reputation: 4841

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);

Upvotes: 24

mfc
mfc

Reputation: 3026

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;
}

Upvotes: 4

anan
anan

Reputation: 1

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");

}

Upvotes: -3

Eldius
Eldius

Reputation: 320

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.

Upvotes: 2

emory
emory

Reputation: 10891

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

Upvotes: 1

Rado
Rado

Reputation: 8953

Max = firstQuarter;
If(secondQuarter > max)
   Max = secondQuarter;

... And so on

Upvotes: 0

Related Questions