Title: if statement examples
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

If Statement Examples

1. ***Demo operators <, >, <=, >= .... Grade example
2. Take a look at IncomeTax.java example in chapter 3 of textbook. Note the book is using && ... see if you can rewrite using only what we have covered so far.
3. ***Using Braces to have more than one statement. Note 2 different placement styles for the first {
4. Using a while(true) loop which we will discuss in the next section, create a program that reads in number grades and converts them to letter grades.
Count the number of A's, B's, etc. Also compute the average grade. Depending on the overall average, publish some statement.
5. Create another program where you enter height in feet and inches and then print out some statement about it.

IncomeTax.java



import java.util.Scanner;

public class IncomeTax
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        double netIncome, tax, fivePercentTax, tenPercentTax;

        System.out.println("Enter net income.\n"
                      + "Do not include a dollar sign or any commas.");
        netIncome = keyboard.nextDouble( );

        if (netIncome <= 15000)
            tax = 0;
        else if ((netIncome > 15000) && (netIncome <= 30000))
            //tax = 5% of amount over $15,000
            tax = (0.05*(netIncome - 15000));
        else //netIncome > $30,000
        {
            //fivePercentTax = 5% of income from $15,000 to $30,000.
            fivePercentTax = 0.05*15000;
            //tenPercentTax = 10% of income over $30,000.
            tenPercentTax = 0.10*(netIncome - 30000);
            tax = (fivePercentTax + tenPercentTax);
        }

        System.out.printf("Tax due = $%.2f", tax);
    }
}


MinOfThree.java


//********************************************************************
//  MinOfThree.java
//  Demonstrates the use of nested if statements.
//********************************************************************

import java.util.Scanner;

public class MinOfThree
{
   //-----------------------------------------------------------------
   //  Reads three integers from the user and determines the smallest
   //  value.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      int num1, num2, num3, min = 0;

      Scanner scan = new Scanner (System.in);

      System.out.println ("Enter three integers: ");
      num1 = scan.nextInt();
      num2 = scan.nextInt();
      num3 = scan.nextInt();

      if (num1 < num2)
         if (num1 < num3)
            min = num1;
         else
            min = num3;
      else
         if (num2 < num3)
            min = num2;
         else
            min = num3;

      System.out.println ("Minimum value: " + min);
   }
}



Programs created on the video:


import java.util.Scanner;


public class Grades {

    /**
     * @param args
     */
    public static void main(String[] args) {

        int numA = 0, numB=0, numC=0, numD=0, numE=0;
        char letterGrade='E';
        double total=0.0;
        int count =0;

        Scanner keyboard = new Scanner(System.in);

        while(true)
        {
            System.out.println("Enter next grade:");
            int grade = keyboard.nextInt();
            if (grade < 0)
                break;

            count +=1;
            total += grade;

            if (grade >= 90)
            {
                numA += 1;
                letterGrade = 'A';
            }
            else if (grade >= 80)
            {
                numB +=1;
                letterGrade = 'B';
            }
            else if (grade >= 70)
            {
                numC +=1;
                letterGrade = 'C';
            }
            else if (grade >= 60)
            {
                numD +=1;
                letterGrade ='D';
            }
            else
            {
                numE +=1;
                letterGrade = 'E';
            }

            System.out.println(grade + " is equavalent to a letter Grade of "+ letterGrade);

        }

        if (count > 0)
            System.out.printf("We received "+ count + " grades with an average of: %.2f %n",
                    total/count);

        System.out.println("Done!");

    }

}


Java Practice(JPHD):

Many of the following questions will use the following template


import java.util.Scanner;

class IfTest
{
    public static void main(String[] args)
    {
        Scanner keyboard = new Scanner(System.in);
        int x = keyboard.nextInt();

        //************************************
    }
}

1. (1 point) Create IfTest1 like IfTest above (change the name from IfTest to IfTest1).
  - After the line of     "*******"   add logic to test if x is greater than 10. If it is then print out:
x is greater than 10
The value of x is _________ (replace the blanks with the actual value of x)
2. (1 point) Create IfTest2 like IfTest above (change the name from IfTest to IfTest2).
  - After the line of     "*******"   add logic to test if x is less than or equal to 10. If it is then print out:
x is less than or equal to 10
The value of x is _________ (replace the blanks with the actual value of x)
3. (1 point) Create IfTest3 like IfTest above (change the name from IfTest to IfTest3).
  - After the line of     "*******"   add logic to test if x is equal to 10. If it is then print out:
x is equal to 10
The value of x is _________ (replace the blanks with the actual value of x)
4. (1 point) Create IfTest4 like IfTest above (change the name from IfTest to IfTest4).
  - After the line of     "*******"   add logic to test if x is NOT equal to 10. If it is then print out:
x is NOT equal to 10
The value of x is _________ (replace the blanks with the actual value of x)
  - Provide an "else" clause on your if statement that prints out:
x is equal to 10
The value of x is _________ (replace the blanks with the actual value of x)
5. (1 point) Why does the following code generate output. Fix the code to produce the expected results (no output):


 public class Bad_IfTest {

    public static void main(String[] args) {
        int x= 19;
        if (x == 20);
        {
            System.out.println("x == 20");
            System.out.println("The value of x="+x);
        }
    }

}


6. (5 points) Complete the following program( I realize we haven't covered while loops yet, but you don't need to change anything related to the while loop):


import java.util.Scanner;

public class Regions {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);

        int beforeBabyBoomers=0;
        int numBabyBoomers=0;
        int numGenX = 0;
        int numMillenials =0;
        int afterMillenials = 0;

        System.out.println("Enter a birth year");
        int birthYear = keyboard.nextInt();

        // Note that a birthYear that is 0 or negative gets us out of the loop.
        while (birthYear > 0)
        {
            //********************************
            // Enter your Logic here
            //********************************
            System.out.println("Enter a birth year");
            birthYear = keyboard.nextInt();
        }
        System.out.println("before Baby Boomers ="+ beforeBabyBoomers);
        System.out.println("number of Baby Boomers ="+ numBabyBoomers);
        System.out.println("number of Generation X ="+ numGenX);
        System.out.println("number of Millenials ="+ numMillenials);
        System.out.println("after  Millenials ="+ afterMillenials);


    }

}



At the point where it says "Enter your Logic here" , add "if logic" (see the Grades example above) such that we do the following:
  - Any birth year before 1942 is considered before Baby Boomers
  - Any birth year between 1942 and 1964 is considered a Baby Boomer
  - Any birth year between 1965 and 1984 is considered Gen X
  - Any birth year between 1985 and 2004 is considered a Millenial
  - Births after 2004 are considered "after Millenials"

Last Modified: February 5, 2015 10:57 AM