Do you need an umbrella? Download

Christina Cacioppo

random, external classes, if statements Polish needed


Introduction

Weather reports are helpful, but they're often pretty dull. Let's write a program that'll give us more interesting, friendly weather forecasts.


Task

Let's finish off the Forecaster class. Given a temperature and rain prediction, our forecaster should print statements about the weather: something like "Woah, looks like rain! Better bring an umbrella!" or "Ooh, it'll be a hot one! Don't forget those shades." (Or whatever you like.)

How does the forecaster know what the weather? It asks the Meteorologist! Notice there's a Meteorologist class, too. It works fine on its own, or you can edit it if you like.

Once you get things working, run your program a few times to get a sense for how the forecast changes from day to day (or from program run to program run.)


Starter code

import java.util.Random;

class Foreceaster{
	void readWeather(){
		Meteorologist prediction = new Meteorologist();
		int temperature = prediction.getTemperature();
		boolean willRain = prediction.willRain();

		System.out.println("Temperature: " + temperature + " degrees");
		if (temperature < 50){
			// What happens if it's cold?
		}

		if(willRain){
			// What do you advise?
		}else{
			// What about now?
		}
	}
}

class Meteorologist{
	int getTemperature(){
		return (int) Math.floor(Math.random() * 120);
	}

	boolean willRain(){
		Random gen = new Random();
		int willRain = gen.nextInt(2);
		return willRain == 0;
	}
}

Expected output

Temperature: 86 degrees
It's so warm you can wear flipflops!
Sunny skies! Leave your umbrella at home!

Hints



Useful tools to solve the problem