Programs using IF

NumberType.java

  1. Write a program to get a number from the user.
  2. Print whether the number is negative, positive or zero.
  3. Print whether the number is odd or even (I guess this only applies to integers)
  4. Print whether the number is a whole number (integer) or a decimal. (This might be tricky).
In order to be able to enter decimals you shold probably use "double" input.

Num2Colour.java

Write a program that does the following:

  1. Read in two integers from the user and call them 'a' and 'b'
  2. if either number is negative, print "green"
  3. if both numbers are negative, print "navy"
  4. only if 'a' is even then do the following
  5. if the sum of 'a' and 'b' is either less than 10 or more than 100 print "yellow"

A number could fall into more than one category, so you can get more than one colour printed out.
Can you think of a clever way of doing step #2?

Note: you will probably have to look up how to tell if a number is even or not (for step #4). If you cannot get this done, hand it in anyway for part marks

Here is some test data. If you type in the values below for a,b, then your program should be printing the colours show in the 'Result' column

'a'  'b':	Result
======================
 2    4 :	crimson, yellow
 4    1 :	pink, yellow
 20   4 :	pink 
 4   20 :	crimson 
 4  100 :	crimson, yellow
 3    8 :	-- nothing --
 100  4 :	pink, yellow
51   -1 :	green
-5   -4 :	green, navy, yellow
-4   -5 :	green, navy, pink, yellow
-4   -3 :	green, navy, crimson, yellow
-1   -1 :	green, navy, yellow
 1   -1 :	green, yellow
-4   38 :	green, crimson
-4    8 :	green, crimson, yellow

(Let me know if you spot any errors! Thanks Nico and Aiden.)

NOTE: your program is not going to print out this table. What this table is for is to show that if your numbers are 4,1 then it should print out pink,yellow


IfOrder.java

This program is to examine the importance of the order of If statements


We only want 1 word to be printed out!


SKIP THIS. It's just an algorithm

Within10.java Challenge

Write a program that gets two numbers from the user.
Check to see if the numbers are within 10 of each other
If the numbers are within 10, print "yes".
If not, print "no".

Sample results:

5, 7		yes
7, 5		yes
122, 136	no
136, 122	no
-30, -36	yes
-3, +3		yes

Quadratic.java Oct 2017: Done in class

Do this if you want to get more than 80% in the course.

Write a program to calculate the quadratic formula:

  1. The user will enter three numbers a, b, and c. (these variables should be of the type double)
  2. Your program will check to make sure that they are all numbers (no words...)
  3. If one of them is not a number, pop up a message box, and then Exit Sub.
  4. Next you need to calculate the part under the square root: b*b - 4ac (store it in a variable; what will you call it?)
  5. If this part (technically called the discriminant) is negative, then you obviously can't take the square root. So the result you must display is "No roots"
  6. If this part is equal to zero, then there is only one root. Do the calculation to find it. Display the root.
  7. If this part is greater than zero, then there are two roots. Calculate each one, first using the "+" sign, and then again using the "-" sign. Display both roots.

 

Test with this data:
a=1, b=2, c=3.6		No roots
a=4, b=1, c= -3		x1 = 0.75 	x2 = -1
a=3, b= -2, c=0		x1 = 0.6667	x2 = 0
a=1, b=2, c=1		one root x = -1