import java.text.NumberFormat; import uwcse.io.*; /** * Practicing using conditionals */ public class Conditionals { /** * A method to illustrate the use of de Morgan's laws */ public void testDeMorganLaws() { // ask for the family income // ask for the number of children // if the family income is not greater than 20000 // or if the number of children is greater than 3 // display a message stating that the family might // be eligible for a stipend // do this in 2 ways // Input Input input = new Input(); double income = input.readDouble("What is your income? "); int children = input.readInt("How many children do you have? "); // Test for the stipend eligibility in 2 ways // Both ways should give the same result // 1) write the condition with an || (or) if (!(income > 20000) || children > 3) System.out.println("You might be eligible for a stipend"); else System.out.println("You are not eligible for a stipend"); // 2) write the condition with an && (and) if (!(income > 20000 && children <= 3)) System.out.println("You might be eligible for a stipend"); else System.out.println("You are not eligible for a stipend"); } public void printGrade() { // given a grade between 0 and 100 // print the corresponding letter grade // 90-100 is A, 80-89 is B, 70-79 is C, 60-69 is D, below 60 is F // // use an if-else if structure // // do the same with a switch statement int grade; Input input = new Input(); grade = input.readInt("Enter the grade: "); // The grade must be between 0 and 100 if (grade < 0 || grade > 100) { System.out.println("Invalid input"); return; } // Print the letter grade using nested ifs if (grade >= 90) System.out.println("A"); else if (grade >= 80) System.out.println("B"); else if (grade >= 70) System.out.println("C"); else if (grade >= 60) System.out.println("D"); else System.out.println("F"); // Print the letter grade using a switch switch (grade / 10) { case 10: case 9: System.out.println("A"); break; case 8: System.out.println("B"); break; case 7: System.out.println("C"); break; case 6: System.out.println("D"); break; default: System.out.println("F"); break; } } /** * more ifs */ public void reportToIRS() { // IRS informants are paid cash awards based on the // value of the money recovered. If the information // was specific enough to lead to a recovery, the // informant receives 10 percent of the first $75,000, // 5 percent of the next $25,000 and 1 percent of the // remainder, up to a maximum award of $50,000. // Write a function that takes as input the amount of // the recovery and returns the award. // Test your program by having main call the function // for different amounts. // money recovered double moneyBack; Input input = new Input(); moneyBack = input.readDouble("How much money has been recovered? "); // print the reward double reward = computeReward(moneyBack); NumberFormat nf = NumberFormat.getCurrencyInstance(); System.out.println("Reward=" + nf.format(reward)); } private double computeReward(double m) { double r = 0; if (m <= 75000) // below 75000: 10% r += 0.1 * m; else { r += 7500; // 10% of the first 75000 m -= 75000; if (m <= 25000) // below 100000: 5% of what is above 75000 r += 0.05 * m; else { r += 0.05 * 25000; // 5% of the next 25000 m -= 25000; r += 0.01 * m; // 1% of what is left if (r > 50000) r = 50000; // can't get more than 50000 } } return r; } /** * Starts the application */ public static void main(String[] args) { Conditionals c = new Conditionals(); // test de Morgan's laws c.testDeMorganLaws(); // if else if and switch c.printGrade(); // irs reward computation c.reportToIRS(); } }