Visual Learners

Open java in Netbeans. Have a look at it, but don’t worry about all the code (it will all make sense in a few weeks!). Scroll down to this section:

Change the position on screen where the colour change happens

Add more if or else if statements to make four colour bands

Add two nested if statements to split the screen into four quarters: top left, top right, bottom left, bottom

Try changing the colours. You can create your own colours like this:

Color myColor = new Color(0, 255, 0); graphics.setColor(myColor);

Change drawOval to

Change the size of the oval/circle

Change to drawRect or

Enjoy!

Area of a Circle

In your lab this week, you worked on various simple problems. Here, we will solve a real problem, the area of a circle, 𝑎 = 𝜋𝑟! . Comment out your previous code, but do not delete it!

Assuming that the radius is 7, use variables to create a calculation. Look at the results of your calculations. Do they match what you expect? If you do not understand, ask a

Assume that the radius is defined using the double variable radius, and then write a small program to calculate and display the area of a circle. The console output should look like:

Once you have done this, the next thing we wish to do is build a simple if statement to decide if the circle is big enough. IF the area of a circle is greater than a certain threshold, then we want to display an additional message saying it is a big circle, ELSE, we want to say this is a small

When you have it working, set the threshold variable to be 50, and experiment with different values of radius. For example, here are two different messages for under and over the limit:

The radius is 3.0

The area is 28.274333882308138

This is a small circle

———————————————–

The radius is 7.0

The area is 153.93804002589985

This is a big circle

When you have this working, you have successfully made use of Java to solve a real world calculation, and you created a working IF statement!

Gender switches with Booleans

The very first program you made last week was a “hello” paragraph to describe yourself. The output should look something like:

Hello Andrew Hello Joe

My name is Andrew, and I’m a lecturer in CS

My name is Andrew, my school was Montrose, and my hometown is Arbroath.

Name: Andrew, Age: 32, Student number: 12345678

You should also have replaced the individual values with variables, so that you could easily substitute your classmate’s details

The next challenge is that we wish to make this third person, and we should also use a Boolean variable (i.e. true or false) to determine whether to use the male or female. For example, if female = true, then we should see:

Her name is Andrew, and she is a lecturer in CS

Whereas if female = false, then we should see His name is Andrew, and he is a lecturer in CS

This is challenging, and will require string variables for gender, an If statement, and Boolean variables!

Days of The Week – Multiple Condition If statements

An alternative way to represent the data above is to use the AND (&&) operator, as discussed in the lecture notes. This will prevent confusing nested if statements. This is an extra challenge, to do in your own time!

If you have completed your Days of the Week project this week, you should have a series of complex nested if statements, that look like this: