If-else quick exercises:

 

1. Ask the user for a number. If it is less than 12, add 10. Print the number.  

 

2.  Ask the user for a number. Give an error if the number is negative.

 

3. Ask the user for the day portion of the date. If it is greater than 31 or less than 1, give them an error message.

 

4. Ask the user for a word that is less than 5 characters. Give them an error if it is more than 5 characters. (Remember the String.length() command.)

 

5. Ask the user for a number. If it is less than 12, add 5, otherwise add 8. Print the number.

 

6. Ask the user for a number. If it is less than 12, print the number. Otherwise, print nothing.

 

7. Ask the user for a number. If it is less than 12, add 5. If it is greater than 20, add 50. Print the number.

 

7.5 Ask the user for a quantity. If they are buying over 100, the price will be 30. If it they are buying over 50, the price is 50. Otherwise, the price is 70. Print the price and total price.

 

7.6. Ask the user for a quantity. The base price is 70. Give a 10% discount if they buy more than 50. Give a 20% discount if they buy more than 100. Print the price and total price.


8. Prompt the user for a number and print good if the number is greater than 5, between 8 & 10 or greater than 33. Otherwise, print bad. Use the || operator in your if statement.

 

  1. Prompt the user for two words. If they are the same, print Great - the same. If they are the same except for the case, print Okay - almost the same. If they are the same length, print At least the same length. You will need the following string functions:

·        compareTo(otherString)  (which returns true if matched)

·        compareToIgnoreCase(otherString)  (which returns true if matched regardless of case)

·        length() (which returns the length)

 

10. An Auto Insurance Program

         Example - Write a program to determine the cost of an automobile insurance premium, based on driver's age and the number of accidents that the driver has had.

                   The basic insurance charge is $500.  There is a surcharge of $100 if the driver is under 25 and an additional surcharge for accidents:

                        # of accidents      Accident Surcharge

                                    1                           50

                                    2                          125

                                    3                          225

                                    4                          375

                                    5                          575

                                    6 or more         No insurance

11. Write a program that recommends the number of calories a person should eat each day. Calories are units of energy found in all foods. Base your recommendation on the person's weight and whether the person has an active or sedentary(inactive) lifestyle. If the person is sedentary, that person's activity factor is 13. If the person is active, that person's activity factor is 15. Multiply the activity factor by the person's weight to get the recommended number of calories. Start your program by:

 

12. Write a Java program which reads a year (integer) from the user and decides whether that year is a leap year. A year is a leap year (and so contains a February 29) if it is divisible by 4. But if the year is also divisible by 100 then it is not a leap year, unless it is divisible by 400.  This means that years such as 1992, 1996 are leap years because they are divisible by 4 and are not affected by the rest of the rule which applies to century years such as 1900 and 2000. Century years are not leap years except where they are a multiple of 400. Hence, the years 1700, 1800 and 1900 were not leap years and did not contain a February 29. But the year 2000 was a leap year, the first such century leap year since 1600.

Challenge 1: If you finish the exercises early, try to solve this problem.  Write a Java program that reads a date from the user in numeric form.  For example, February 17, 2003 would be entered as the three integers 2, 17, and 2003.  Your program must then determine if the date is a “valid” date.  Use the following information to determine if the date is valid:  January, March, May, July, August, October, and December all have 31 days.  April, June, September, and November all have 30 days.  February has 28 days in a non-leap year and 29 days in a leap year.  Echo the input and print either “valid date” or “invalid date” as output.

 


 

13. Extend #12 to use a window pane interface instead of a system console. Instead of printing to and from the console window, print to and from a JOptionPane. The format is:

String yourVariable = (JOptionPane.showInputDialog("Ask a question"));

 

You can only enter a string, but you can convert a string to an Integer with:

Integer.parseInt(stringValue) and to a double with Double.parseDouble(stringValue).

 

To print out:

 

When you test, you may find your window pops up behind the other windows, so be sure to minimize everything.

 

JOptionPane.showMessageDialog(null, "Good job! sweetener is " + value3);

 

Sample prompting with JOptionPane:

 

import javax.swing.JOptionPane;

public class mouse{

public static void main (String [] args){

 

 

String value = JOptionPane.showInputDialog("Enter Sweetner name need to kill mouse in grams here");

JOptionPane.showMessageDialog(null, "Good job! sweetener is " + value);

int value2 = Integer.parseInt(JOptionPane.showInputDialog("Enter integer Sweetner need to kill mouse in grams here"));

JOptionPane.showMessageDialog(null, "Good job! sweetener is " + value2);

double value3 = Double.parseDouble(JOptionPane.showInputDialog("Enter Double Sweetner need to kill mouse in grams here"));

JOptionPane.showMessageDialog(null, "Good job! sweetener is " + value3);

}}