user3015835
user3015835

Reputation: 13

Need to get highest and lowest value without array

I am doing an assignment where I have to take 7 days of the week, each as variables and create two functions. One to find the average temperature of the 7 days and the other to find the coldest and hottest temperature of the 7 days. We are not allowed to use arrays. I know this is a lot of useless code but its what the assignment asks for.

My problem is I can figure out how to find the Min and Max value. Any suggestions?

import java.util.Scanner;

public class Temperature {
public static void getAverage(double day1, double day2, double day3, double day4, 
                              double day5, double day6, double day7){
double average = (day1 + day2 + day3 + day4 + day5 + day6 + day7)/7;

System.out.printf("The average is: %.2f\n", average);
}
public static void getHotAndCold( double day1, double day2, double day3, double day4, 
                               double day5, double day6, double day7){

}

public static void main(String [] args){
Scanner s = new Scanner(System.in);
double day1, day2, day3, day4, day5, day6, day7;

System.out.println("Enter the temperatures for each day of the week starting with,     Monday");
day1 = s.nextDouble();

System.out.println("Tuesday");
day2 = s.nextDouble();

System.out.println("Wednesday");
day3 = s.nextDouble();

System.out.println("Thursday");
day4 = s.nextDouble();

System.out.println("Friday");
day5 = s.nextDouble();

System.out.println("Saturday");
day6 = s.nextDouble();

System.out.println("Sunday");
day7 = s.nextDouble();

getAverage(day1, day2, day3, day4, day5, day6, day7);
 }
}

Upvotes: 1

Views: 1149

Answers (4)

bcorso
bcorso

Reputation: 47078

Variant to other answers.

double max, min;

public static void getMaxMin(double day1, double day2, double day3, double day4,
                             double day5, double day6, double day7){

    max = Math.max(day1, Math.max(day2, Math.max(day3, Math.max(day4,
          Math.max(day5, Math.max(day6, day7))))));

    min = Math.min(day1, Math.min(day2, Math.min(day3, Math.min(day4,
          Math.min(day5, Math.min(day6, day7))))));

}

Upvotes: 0

bcorso
bcorso

Reputation: 47078

If you really want to loop (or just be spiteful):

// create your own node to link days
public static class Day{
    double temp;
    Day next;
    Day(double temp, Day next){
        this.temp = temp;
        this.next = next;
    }
}

public static double coldest(Day day){
    double  minTemp = day.temp;
    while((day = day.next) != null) minTemp = Math.min(day.temp, minTemp);
    return minTemp;
}

//etc...

public static void main(String... args){
    Day days = new Day(70, new Day(60, new Day(65, new Day(45,
               new Day(83, new Day(72, new Day(55,null)))))));

    System.out.println("Coldest:" + coldest(days));
}

Upvotes: 0

Paul Samsotha
Paul Samsotha

Reputation: 208984

You could do something like this. Use Math.max to get the max of a pair, then keep on doing it til you get to seventh day. The last comparison with give the value to the global variable.

double max;
double min;

public static void getMaxAndMin(
                   double day1, double day2, double day3, double day4, 
                                double day5, double day6, double day7){

    max = Math.max(day1, day2);
    max = Math.max(max, day3);
    max = Math.max(max, day4);
    max = Math.max(max, day5);
    max = Math.max(max, day6);
    max = Math.max(max, day7);

    min = Math.min(day1, day2);
    min = Math.min(min, day3);
    min = Math.min(min, day4);
    min = Math.min(min, day5);
    min = Math.min(min, day6);
    min = Math.min(min, day7);

}

Upvotes: 1

Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17486

You can do something like:

public static void getHotAndCold( double day1, double day2, double day3, double day4, 
                               double day5, double day6, double day7)
{
    double min = day1;
    double max = day1;

    if (day2 < min)
    {
        min = day2;
    }
    if (day2 > max)
    {
       max = day2;
    }

    // and so on.
}

Upvotes: 0

Related Questions