Skip to the content.

Unit3hacks_ipynb_2_

// whats wrong with this code? (below)
String Anika = "Anika";
String Alisha = "Alisha";
String myName = Alisha;

myName != Anika;
myName == Alisha ;
   String myName = Alisha;

// the code is not working because the variable Alisha is not defined.

// cannot find symbol

//   symbol:   variable Alisha

homework question: the num is set to a number inclusive between 1 and 6

// popcorn hack
// create test cases that do not satisy the condition above. you can copy/paste the code into the new code cell


public static void main(String[] args) {
    int myAge = 16;
    System.out.println("Current age: " + myAge);
    
    if (myAge >= 16) {
        System.out.println("You can start learning to drive!");
    }

    System.out.println("On your next birthday, you will be " + (myAge + 1) + " years old!");
}


// test case
// when myAge is 15 or less

3.4 Hacks

if you printed 19 you would be able to vote and are old for a drivers license. if you tested 13 you would not be able to do anything cause you satisfy none of the requirements.

int balance = 10;

if (balance > 10){
    System.out.println("This is out of your budget");
}
else if (balance <= 10){
    System.out.println("This is within your budget");
}
This is within your budget

3.5 hacks if they are a student then they get discounts, using specific variables returns specific values. Output for the program will be your eligible for a student discount.

public class Main {
    public static void main(String[] args) {
        int age = 18; // Change this value for testing
        boolean isStudent = false; // Change this value for testing

        if (age >= 18 && isStudent == true){
            System.out.println("Discount approved");
            if (age >= 30 && isStudent == true){
                System.out.println("Full ride scholarship since you are over 30");
            }
        }
        else if (age < 18 && isStudent == false){
            System.out.println("Discount not approved");
        }
        else if (age > 18 && isStudent == false){
            System.out.println("Discount not approved");
        }
        else{
            System.out.println("Nothing approved");
        }
    }
}

Main.main(null);
Nothing approved

3.6

  • !(x==0) is the same as (x != 0)
  • (x < -5 || x > 10) is equivalent to !(x >= -5 && x <= 10)

3.7

  • sharons and myhouse are the same cause they have the same input values.
import java.util.Scanner;

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

        // Prompt user for inputs
        System.out.print("Enter your age: ");
        int age = scanner.nextInt();

        System.out.print("Enter your annual income: ");
        double income = scanner.nextDouble();

        System.out.print("Are you a student? (yes or no): ");
        String studentStatus = scanner.next().toLowerCase();

        System.out.print("Enter your employment type (full-time, part-time, unemployed): ");
        String employmentType = scanner.next().toLowerCase();

        // Validate inputs
        if (age <= 0 || income <= 0 || (!studentStatus.equals("yes") && !studentStatus.equals("no")) ||
                (!employmentType.equals("full-time") && !employmentType.equals("part-time") && !employmentType.equals("unemployed"))) {
            System.out.println("Invalid input. Please enter valid details.");
            return;
        }

        // Determine membership eligibility
        boolean qualifiesForPremium = income >= 50000;
        boolean qualifiesForSeniorDiscount = age >= 65;
        boolean qualifiesForStudentDiscount = studentStatus.equals("yes");
        boolean qualifiesForBasic = !qualifiesForPremium && !qualifiesForSeniorDiscount && !qualifiesForStudentDiscount;

        // Print all qualifying memberships
        if (qualifiesForPremium) {
            System.out.println("You qualify for Premium Membership.");
        }
        if (qualifiesForSeniorDiscount) {
            System.out.println("You qualify for Senior Discount.");
        }
        if (qualifiesForStudentDiscount) {
            System.out.println("You qualify for Student Discount.");
        }
        if (qualifiesForBasic) {
            System.out.println("You qualify for Basic Membership.");
        }

        // Print final recommendation
        if (qualifiesForPremium) {
            System.out.println("Final Recommendation: Premium Membership");
        } else if (qualifiesForSeniorDiscount) {
            System.out.println("Final Recommendation: Senior Discount");
        } else if (qualifiesForStudentDiscount) {
            System.out.println("Final Recommendation: Student Discount");
        } else if (qualifiesForBasic) {
            System.out.println("Final Recommendation: Basic Membership");
        } else {
            System.out.println("You do not qualify for any memberships or discounts.");
        }

        scanner.close();
    }
}

PrimeClubMembership.main(null);
Enter your age: Enter your annual income: Are you a student? (yes or no): Enter your employment type (full-time, part-time, unemployed): You qualify for Premium Membership.
You qualify for Student Discount.
Final Recommendation: Premium Membership