Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 + ".");
like image 411
Cody B Avatar asked Feb 16 '12 00:02

Cody B


People also ask

What is the formula to find the maximum value?

If you are given the formula y = ax2 + bx + c, then you can find the maximum value using the formula max = c - (b2 / 4a). If you have the equation y = a(x-h)2 + k and the a term is negative, then the maximum value is k.

How do you find the maximum of a data set?

The maximum and minimum also make an appearance alongside the first, second, and third quartiles in the composition of values comprising the five number summary for a data set. The minimum is the first number listed as it is the lowest, and the maximum is the last number listed because it is the highest.

What is the maximum and minimum values of a variable?

Maxima/minima occur when f (x) = 0. x = a is a maximum if f (a) = 0 and f (a) < 0; • x = a is a minimum if f (a) = 0 and f (a) > 0; A point where f (a) = 0 and f (a) = 0 is called a point of inflection.


1 Answers

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

DoubleStream.of(firstQuarter , secondQuarter , thirdQuarter , fourtQuarter).max();
like image 104
arjabbar Avatar answered Sep 22 '22 02:09

arjabbar