CS 1110/1111: Introduction to Programming

Lecture 10

Announcements

Guest Lecturer: Mark Sherriff!

Talking Points

How if-inside-else can be written like if-else

if (x < 0) {
    sign = "negative";
} else {
    if (x == 0) {
        sign = "zero";
    } else {
        sign = "positive";
    }
}
if (x < 0) {
    sign = "negative";
} else
    if (x == 0) {
        sign = "zero";
    } else {
        sign = "positive";
    }

if (x < 0) {
    sign = "negative";
} else if (x == 0) {

    sign = "zero";
} else {
    sign = "positive";
}

Using booleans as values

boolean t = true;
boolean f = false;
boolean taf = t && f;
boolean tof = t || f;
System.out.println( taf );
System.out.println( tof );
System.out.println( "not true = " + !t );
System.out.println( "not false = " + !f );

Declarations don't escape their braces (} throws out the boxes)

if (x == 1110 || x == 1111 || x == 1112) {
    String name = "Introduction to Programming";
} else {
    String name = "Some other course";
}
System.out.println(name); // this line won't work!
String name;
if (x == 1110 || x == 1111 || x == 1112) {
    name = "Introduction to Programming";
} else {
    name = "Some other course";
}
System.out.println(name); // this will work

A tool you can use, should be able to read, but don't need to be able to write: switch, case, and break.

Exmaple Programs

import java.util.Scanner;

public class CourseGrade {

    public static void main(String[] args) {
        
        // Allocate a scanner
        Scanner keyboard = new Scanner(System.in);
        
        // Define variables
        int grade;
        int tens;
        int ones;
        
        // Prompt the user
        System.out.print("What's your grade? ");
        grade = keyboard.nextInt();
        
        tens = grade / 10;
        ones = grade % 10;
        
        // Figure out letter grade
        
        if(grade < 0 || grade > 100) {
            System.out.println("Not a valid grade.");
            System.exit(0);
        }
        
        if(grade < 60) {
            System.out.println("You failed.");
            System.exit(0);
        }
        
        if(tens >= 9) {
            System.out.print("A");
        }
        else if(tens == 8) {
            System.out.print("B");
        }
        else if(tens == 7) {
            System.out.print("C");
        }
        else if(tens == 6) {
            System.out.print("D");
        }
        else {
            System.out.print("F");
        }
        
        // Figure out +/-
        
        if((tens == 9 && ones >= 8) || tens == 10) {
            System.out.print("+");
        }
        else if(tens <= 8 && ones >= 7) {
            System.out.print("+");
        }
        else if(tens <= 9 && ones <= 3) {
            System.out.print("-");
        }
    }
}
import java.util.Scanner;

public class Classroom {

    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);
        System.out.print("How many people are here today? ");
        int total = keyboard.nextInt();

        if (total >= 350) {
            System.out.println("Full!");
        } else if(total > 175){
            System.out.println("half full!");
        } else {
            System.out.println("Not full!");
        }

    }

}
// Tax problem
// Single - up to 32000, 10% / over 32000, 3200 + 25% on portion over 32000
// Married - up to 64000, 10% / over 64000, 6400 + 25% on portion over 64000
import java.util.Scanner;


public class Tax {

    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        System.out.print("Status: ");
        char status = keyboard.nextLine().charAt(0);
        System.out.print("Income: ");
        int income = keyboard.nextInt();
        double tax = 0;
        
        if(status == 's') {
            if(income <= 32000) {
                tax = income * .1;
            }
            else {
                tax = 3200 + (income-32000) * .25;
            }
         }
        else {
            if(income <= 64000) {
                tax = income * .1;
            }
            else {
                tax = 6400 + (income-64000) * .25;
            }
        }
        
        System.out.println("Tax = $" + tax);

    }

}
Copyright © 2014 by Luther Tychonievich. All rights reserved.