You are on page 1of 2

Java Basics

Lab 2: if and if-else, for-loops, switch statements

Lab 2: Selection using if- and if-else and switch Statements


Aims
To experiment with the use of if-statements and if-else-statements in program design.

To experiment with the use of switch-statements in program design.

Exercises
Complete the following exercises. For each exercise, create a new Java source file. Give each new class a suitable name, and of course save it with the corresponding file name. Try to DESIGN your solutions in pseudo-code BEFORE you attempt to write the Java code. Use the JOptionPane methods showInputDialog and showMessageDialog for input and output operations. If you have time later, try adapting some of your programs to use the Scanner method for input using just the command window. Use if or if-else statements in Exercises 1-5 1. Implement a Java program that prompts the user to enter an integer number. Then 2. if the number is positive the program should display: if the number is negative the program should display: if the number is zero the program should display: positive negative zero

Implement a Java program that prompts the user to enter an integer number in the range 0-100, and that then uses a set of nested if-else conditions to return a grade letter A-F based on the following ranges: Values in the range less than 35 correspond to an F Values 35 or more but less than 40 correspond to an E Values 40 or more but less than 50 correspond to a D Values 50 or more but less than 60 correspond to a C Values 60 or more but less than 70 correspond to a B Values in the range 70 or more correspond to an A Implement a program that prompts the user for two inputs: first input should ask for a persons gender: m or f second input should ask for a persons age The application should then print a message saying whether the information that has been input implies that the person is a man, woman, boy or girl.

3.

School of Computing, The Robert Gordon University

2012 version

Java Basics

Lab 2: if and if-else, for-loops, switch statements

4.

Write a Java application that will calculate the cost of an order for a number of identical items. The requirements are that: The user should be prompted for the following information: cost of a single item the number of items ordered whether the item is subject to Valued Added Tax (VAT) (as a double value) (as an integer) (as a char value y or n)

The application should then calculate and display the cost of the order, adding VAT if appropriate. [Note: when VAT is added it increases the cost by 20% (i.e. multiplies it by 1.2)] 5. UK income tax for the tax year 2011-12 is charged at the following rates: the first 7475 of annual income is not taxed income between 7475 and 42475 is taxed at 20% any income between 42475 and 157475 is taxed at 40% any income above 157475 is taxed at 50%

Implement a Java application, which will prompt the user for their annual income (use double data type). The program should calculate and output the amount of tax to be paid.
[Note that some programmers would avoid using the double type for currency, and would work in pence using an int or long type to avoid rounding issues. It makes little difference in this example]

Use a switch statement in Exercises 6-8 6. Write a Java program that will prompt the user to enter a number in the range 1 .. 7. The program will output the full name of corresponding day of the week. For example if the user enters 1 then the program will output "sunday". If the user enters 2 then the program will output "monday" etc. 7. Write a Java program that will prompt the user to enter a number in the range 1 .. 12 representing a month of the year. The program will output the number of days in the corresponding month. (Initially, ignore leap years i.e. assume that month 2 (i.e. February) always has 28 days. Then improve your program by adding an extra user prompt and an if statement in the month 2 case to ask the user if it is a leap year, and then give the appropriate output) 8. Write a Java program that will prompt the user to enter an integer number in the range 1 ..99. The program should convert the number to its textual equivalent and display the text e.g. if the user enters 1 then the program should output "one" if the user enters 21 then the program should output "twenty one"

School of Computing, The Robert Gordon University

2012 version

You might also like