# How to get the largest number of a java method?

2

I have to create a system that can calculate the sea temperature according to the month, using an initial month, initial temperature, and the month of which I want to know the reference temperature. In certain months the temperature rises and in others it drops a specific percentage. Additionally, I have to tell the month with the highest temperature of all the months calculated. (Example: My initial month is February and I want to know the temperature in June, the answer would be the month number with the highest temperature of these, for example May, which would be month 5) The system to calculate the temperature is done but I need help writing the method to get the month with the highest temperature.

``````package fenomenoninio;

import java.util.Scanner;

/**
*
* @author erodriguez
*/
public class FenomenoNinio {

/**
* @param args the command line arguments
*/

//Declaración variables
public int mesActual ;
public double temperaturaActual;

/*Método en la cual vamos a calcular la temperatura proyectada
Se ingresa como datos de entrada el mes actual, el mes proyectado y la temperatura
actual.
nueva_temperatura(1, 12, 25.0)

La respuesta será la nueva temperatura
*/

public double nueva_temperatura(int mesActual,
{

{
for (int i=mesActual; i<=mesProyectado ; i++)
{
if (mesActual == 1 || mesActual == 4 || mesActual == 7
|| mesActual == 10)
{

//continue;
}

else if (mesActual == 2 || mesActual == 3 || mesActual ==11
|| mesActual == 6)
{
//continue;
}

else if (mesActual == 8 || mesActual == 9 || mesActual ==5
|| mesActual == 12)
{
//continue;
}

}
System.out.println("La temperatura Proyectada es : "

}

else
{
System.out.println("Ingresar un valor entre 1 y 12");
}

}

public static void main(String[] args) {
// TODO code application logic here

FenomenoNinio fenomeno = new FenomenoNinio();
Scanner sc = new Scanner(System.in);
System.out.println("Ingresar el mes actual : ");
try {
fenomeno.mesActual= sc.nextInt();
Scanner sc1 = new Scanner(System.in);
System.out.println("Ingresa la temperatura actual : ");
fenomeno.temperaturaActual = sc1.nextDouble();
System.out.println("Ingresa el mes a proyectar : ");
fenomeno.nueva_temperatura(fenomeno.mesActual,
fenomeno.temperaturaActual);

} catch (Exception e) {
System.out.println("Por favor debe ingresar un valor válido");
}

}
}
``````

asked by Jose Sosa 01.10.2016 в 21:05
source

1

It seems to me that the correct algorithm for projecting the temperature is:

``````public boolean nueva_temperatura()
{
float[] variacion = {    0f, 1.15f,  .87f,  .87f, 1.15f,
1.12f,  .87f, 1.15f, 1.12f,
1.12f, 1.15f,  .87f, 1.12f    };

float    tem = medicionActual.Temperatura,    // ¨\      Estas variables
min = tem,                           //   \    no son
max = 0;                             //    |_  necesarias pero
//    |   ayudan a una
int      mesMin = medicionActual.Mes,         //   /    mejor
mesMax = 0;                          // _/      visualización.

{
for (int mes = medicionActual.Mes + 1; mes <= medicionProyectada.Mes; mes++)
{
tem *= variacion[mes];

if (tem > max)
{
max = tem;
mesMax = mes;
}

if (tem < min)
{
min = tem;
mesMin = mes;
}
}

medicionMaxima.Temperatura = max;
medicionMinima.Temperatura = min;
medicionMaxima.Mes = mesMax;
medicionMinima.Mes = mesMin;

return true;
}
else
{
System.out.println("El mes actual debe ser menor que el mes a proyectar.");
return false;
}
}
``````

## Explanation:

The `nueva_temperatura` function is type `boolean` because you must return `true` when you can perform the projection, and `false` when you can not do it. This is useful for the construction of a loop that controls that the user places the correct data:

``````do
{
fenomeno.medicionActual.Mes         = inputInt("Ingresar el mes actual: ", 1, 12);
fenomeno.medicionActual.Temperatura = inputFloat("Ingresa la temperatura actual: ");
fenomeno.medicionProyectada.Mes     = inputInt("Ingresa el mes a proyectar: ", 1, 12);

}while( !fenomeno.nueva_temperatura() );
``````

`nueva_temperatura` does not receive parameters because it is part of the class `fenomenoNinio` , and the variables on which it acts are members of that class:

``````class FenomenoNinio
{
Medicion medicionActual;
Medicion medicionMaxima;
Medicion medicionMinima;

public FenomenoNinio()
{
medicionActual     = new Medicion();
medicionMaxima     = new Medicion();
medicionMinima     = new Medicion();
}
// Aquí iría la función nueva_temperatura().
}
``````

The `Medicion` class is used to record the temperature according to the month:

``````class Medicion
{
int Mes;
float Temperatura;
}
``````

This structuring facilitates the reporting of temperatures along with the respective months:

``````System.out.format("El mes actual es: \t %10s \t Temperatura = %5.2f ºC\n",
meses[fenomeno.medicionActual.Mes], fenomeno.medicionActual.Temperatura);
System.out.format("El mes proyectado es: \t %10s \t Temperatura = %5.2f ºC\n",
System.out.println();
System.out.format("Temperatura mínima = %5.2f ºC \t en el mes %s\n",
fenomeno.medicionMinima.Temperatura, meses[fenomeno.medicionMinima.Mes]);
System.out.format("Temperatura máxima = %5.2f ºC \t en el mes %s\n",
fenomeno.medicionMaxima.Temperatura, meses[fenomeno.medicionMaxima.Mes]);
``````

Vector `meses` is based on the same trick as vector `variacion` , but in this case it allows to show the name of the months:

``````/* Permite hacer un reporte más amigable para el usuario,
mostrando el nombre del mes y no sólo el número. */
static String[] meses = {"", "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio",
"agosto", "septiembre", "octubre", "noviembre", "diciembre"};
``````

Inside the function `nueva_temperatura` we find the vector `variacion` :

``````    float[] variacion = {    0f, 1.15f,  .87f,  .87f, 1.15f,
1.12f,  .87f, 1.15f, 1.12f,
1.12f, 1.15f,  .87f, 1.12f    };
``````

The type `float` seems more appropriate than `double` to represent temperatures, because it has an accuracy of 3 to 6 significant figures in the fractional part.

This vector stores the percentages of variation of the temperature according to the months. The positions 1 to 12 of the vector correspond to the months, so in the zero position the value `0f` is established, which allows the projection to be calculated in a simple line of code:

``````tem *= variacion[mes];
``````

For that, a condition must be verified:

``````if (medicionActual.Mes <= medicionProyectada.Mes)
``````

The other conditions were delegated to a function that ensures that the data is correct at the moment of requesting it from the user:

``````static int inputInt(String solicitud, int menor, int mayor)
{
Scanner sc = new Scanner(System.in);
int r = 0;

do    // De este bucle no se sale hasta que el usuario coloque un
{     //  número y que además sea válido.
System.out.print(solicitud);

if ( sc.hasNextInt() )
r = sc.nextInt();
else                // Cuando ingresa por aquí es porque
{                   // el usuario no colocó un número.
sc.next();
System.out.println("Por favor, debe ingresar un número.");
continue;
}

if (r < menor || r > mayor)
System.out.format("Por favor, el número debe estar dentro del rango [%d..%d].\n",
menor, mayor);

} while (r < menor || r > mayor);

return r;
}
``````

The function `inputInt` returns the integer number requested from the user, controlling the errors that the user may commit. For this purpose the parameters `menor` and `mayor` allow to control that the number is within the range of the months.