ifs

  • if: If the condition in the if statement is true, then the code is executed under it is executed.
  • if-else: If the condition in the if statement is true, then that code under that is executed. Otherwise (if false), then the code under else is executed.
  • if-elseif-else: There are 2 conditions to be met in this, with the first if's code being executed if that is true. The elseif's code will be executed if the statement for that is true. If it's false for both if and ifelse statements, then the else's code will be executed.
import java.util.Scanner;

Scanner input;
System.out.print("What color are apples?: ");
input = new Scanner(System.in);
String answer = input.nextLine();
answer = answer.toLowerCase();
System.out.println(answer);
if ((answer == "yellow") || (answer == "red") || (answer == "green")){ // will print correct if the user answers yellow, red, or green
    System.out.print("You are correct");
}
else {// if the user inputs anything other than yellow, red, green, then it outputs this
    System.out.print("What kind of apples are have you been looking at?");
}
System.out.println(" ");

System.out.print("On a scale of 1-10, how do you rate apples?: ");
input = new Scanner(System.in);
Double answer1 = input.nextDouble();
System.out.println(answer1);
if ((answer1 < 5) && (answer1 > 0)) { // if the user inputs a number between 1 and 4, then it prints this
                System.out.print("What is wrong with you.");    
        }   
        else if ((answer1 < 8) && (answer1 >= 5)) { //if the user inputs a number between 5 and 7 it inputs this   
                System.out.print("That makes sense.");    
        }   
        else if ((answer1 <= 10) && (answer1 >= 8)) { // if the user inputs a number between 8 and 10 it inputs this
                System.out.print("Very correct!");
        }
        else { // if the user doesn't input a number from 1 to 10 it inputs this
                System.out.print("Your choice has to be a number between 1 and 10.");
        }
What color are apples?: blue
What kind of apples are have you been looking at? 
On a scale of 1-10, how do you rate apples?: 5.0
That makes sense.
import java.util.Scanner;


public class GradeCalculator {
    public static void main(String args[]) {
        
        Scanner input = new Scanner(System.in);
        System.out.println("Seperate category?: ")
        boolean category = input.nextBoolean();
        
        if (category == true) {
            System.out.println("current grade?: ")
            double a = input.nextDouble();
            System.out.println("% final: ")
            double perc1 = input.nextDouble();
            System.out.println("desired grade: ")
            double b input.nextDouble();
    

            double cgrade = a/100 * (100-perc1)/100;
            double bgrade = b/100-cgrade; 
            double perc2 = bgrade/perc1;

            System.out.println("needed grade: " + perc2);

        }

    }
}
0.546

Switch case

  • switch is used to select one of the code blocks
  • switch makes it little easier to understand and a little less cluttered
  • the value inputted will be compared with the values of each case and if it matches, then that will executed
import java.util.Scanner;

System.out.print("How old are you?: ");
Scanner num = new Scanner(System.in);
int answer = Integer.parseInt(num.nextLine());; // scanner input


System.out.println(answer); // to do age ranges, each case number had to be defined by a range in switch
switch ((0 <= answer && answer <=1) ? 0 : // "?" is if and ":" is else, so in this case if the number inputted was between 0 and 1, then it would be set as case 0, if not then it would go on to the other cases
        (2 <= answer && answer <=4) ? 1 :
        (5 <= answer && answer <=12) ? 2 :
        (13 <= answer && answer <=19) ? 3 :
        (20 <= answer && answer <=39) ? 4 :
        (40 <= answer && answer <=59) ? 5 :
        (60 <= answer && answer <=130) ? 6 : 7) {
        case 0:
        System.out.println("You're a baby."); // if between age 0 and 1, then it prints you're a baby
        break;
        case 1:
        System.out.println("You're a toddler.");
        break;
        case 2:
        System.out.println("You're a child.");
        break;
        case 3:
        System.out.println("You're a teen.");
        break;
        case 4:
        System.out.println("You're a adult.");
        break;
        case 5:
        System.out.println("You're a middle-aged adult.");
        break;
        case 6:
        System.out.println("You're old.");
        break;
        case 7:
        System.out.println("How? I think you made a mistake."); //if not in the range of any of the previous cases
        break;
        default: 
        System.out.println("Try again."); //if user input bypasses all cases
        break;
}
How old are you?: 
---------------------------------------------------------------------------
java.lang.NumberFormatException: For input string: ""
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
	at java.base/java.lang.Integer.parseInt(Integer.java:662)
	at java.base/java.lang.Integer.parseInt(Integer.java:770)
	at .(#18:1)
import java.util.Scanner;


public class GradeCalculator {
    public static void main(String args[]) {
        
        Scanner input = new Scanner(System.in);
        System.out.println("Seperate category?: ")
        boolean category = input.nextBoolean();
        
        if (category == true) {
            System.out.println("current grade?: ")
            double a = input.nextDouble();
            System.out.println("% final: ")
            double perc1 = input.nextDouble();
            System.out.println("desired grade: ")
            double b input.nextDouble();
    

            double cgrade = a/100 * (100-perc1)/100;
            double bgrade = b/100-cgrade; 
            double perc2 = bgrade/perc1;

            System.out.println("needed grade: " + perc2);

        }

    }
}

De Morgan's Law

  • negate an and statement
  • negate an or statement
  • uses ! operator (not)
import java.util.Scanner;


public class GradeCalculator {
    public static void main(String args[]) {
        
        Scanner input = new Scanner(System.in);
        System.out.println("Seperate category?: ");
        boolean category = input.nextBoolean();
        
        if (category == true) {
            System.out.println("current grade?: ");
            double a = input.nextDouble();
            System.out.println("% final: ");
            double perc1 = input.nextDouble();
            System.out.println("desired grade: ");
            double b = input.nextDouble();
            double cgrade = a/100 * (100-perc1)/100;
            double bgrade = b/100-cgrade; 
            double perc2 = bgrade/perc1;

            System.out.println("needed grade: " + perc2*10000 + "%");

        }
        else {
            System.out.println("current grade?: ");
            double a = input.nextDouble();
            System.out.println("desired grade: ");
            double b = input.nextDouble();
            System.out.println("test category %: ");
            double c = input.nextDouble();
            System.out.println("points in test category: ");
            double d = input.nextDouble();
            System.out.println("points in the final: ");
            double e = input.nextDouble();   
            a = a/100;
            b = b/100;
            double total = (e/(d+e))*c; //final is this percent of the total grade
            double needed = (e*(b-a))/total;
            System.out.println("points needed: " + needed * 10000);


        }

    }
}
GradeCalculator.main(null);
Seperate category?: 
current grade?: 
% final: 
desired grade: 
needed grade: 113.33333333333333%
Scanner input = new Scanner(System.in);
/*/
System.out.println("current grade?: ");
double a = input.nextDouble();
System.out.println("desired grade: ");
double b = input.nextDouble();
System.out.println("test category %: ");
double c = input.nextDouble();
System.out.println("current grade in test category: ");
double curr = input.nextDouble();
System.out.println("points in test category: ");
double d = input.nextDouble();
System.out.println("points in the final: ");
double e = input.nextDouble();
/*/

double a = 89;
System.out.println("desired grade: ");
double b = 90;
System.out.println("test category %: ");
double c = 80;
System.out.println("current grade in test category: ");
double curr = 90;
System.out.println("points in test category: ");
double d = 100;
System.out.println("points in the final: ");
double e = 100;

double other = a - (c * curr/100); //other category percent
System.out.println(other);
double needed = b - other;
System.out.println(needed);
double rneeded = needed/c; //needed in test category
System.out.println(rneeded);
// double current = ((fin + d)/(d + e))*100 = rneeded
double fin = ((rneeded)*(d+e))/100-d/100;
System.out.print(fin);
desired grade: 
test category %: 
current grade in test category: 
points in test category: 
points in the final: 
17.0
73.0
0.9125
0.825