Skip to the content.

Unit 3 Hacks

Popcorn hack

whats wrong with this code? (below)

String myName = Alisha;

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

// Check if myName is not equal to "Anika"
if (!myName.equals("Anika")) {
    System.out.println("myName is not Anika");
}

// Check if myName is equal to "Alisha"
if (myName.equals("Alisha")) {
    System.out.println("myName is Alisha");
}
myName is not Anika
myName is Alisha

Homework question

unit3pic

What is the precondition for num?

public static int getCheck(int num) {
    // Ensure the number of digits in num is between 1 and 6
    if (num < 0 || num > 999999 || num == 0) {
        throw new IllegalArgumentException("num must have between 1 and 6 digits and be positive.");
    }

    // Rest of the implementation
    return -1;  // Placeholder return value
}

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 where age is less than 16 (does not satisfy the driving condition)
    int underAge = 15;
    System.out.println("\nTest case: underAge = " + underAge);
    if (underAge >= 16) {
        System.out.println("You can start learning to drive!");
    } else {
        System.out.println("You cannot start learning to drive.");
    }

    // Test case where age is far below the driving age
    int toddlerAge = 5;
    System.out.println("\nTest case: toddlerAge = " + toddlerAge);
    if (toddlerAge >= 16) {
        System.out.println("You can start learning to drive!");
    } else {
        System.out.println("You cannot start learning to drive.");
    }

    // Test case where age is equal to 16 (boundary condition)
    int boundaryAge = 16;
    System.out.println("\nTest case: boundaryAge = " + boundaryAge);
    if (boundaryAge >= 16) {
        System.out.println("You can start learning to drive!");
    }
}
main(null);
Current age: 16
You can start learning to drive!
On your next birthday, you will be 17 years old!

Test case: underAge = 15
You cannot start learning to drive.

Test case: toddlerAge = 5
You cannot start learning to drive.

Test case: boundaryAge = 16
You can start learning to drive!

Popcorn hack

  • explain the purpose of this algorithm, and what each if condition is used for
  • what would be output if input is
    • age 20
    • anual income 1500
    • student status: yes

The checkMembershipEligibility function checks if a user qualifies for various types of memberships or discounts based on their age, annual income, and student status.

The output would be You are eligible for a Student Discount.

Popcorn Hack #2

  • Write a program that checks if a person can get a discount based on their age and student status. You can define your own discount criteria! Use compound conditionals to determine the output.
public class Main {
    public static void main(String[] args) {
        int age = 30; // Change this value for testing
        boolean isStudent = true; // Change this value for testing

        // Compound conditional logic
        if (age < 18 || isStudent) {
            System.out.println("You qualify for a Student or Youth Discount.");
        } else if (age >= 65) {
            System.out.println("You qualify for a Senior Discount.");
        } else {
            System.out.println("No discounts available.");
        }
    }
}
Main.main(null);
You qualify for a Student or Youth Discount.

Popcorn Hack

Challenge Questions

  1. What is !(x == 0) equivalent to?
    • Apply De Morgan’s Law to find an equivalent expression.

Answer: x != 0

  1. Negate the expression (x < -5 || x > 10).
    • Use De Morgan’s Law to rewrite this expression in a different form.

Answer: x >= -5 && x <= 10

Popcorn hack

Would the sharons house and my house be the same?

class House {
    private String color;
    private int size;

    public House(String color, int size) {
        this.color = color;
        this.size = size;
    }

    // Override equals method to compare House objects by content
    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;  // Check if same object reference
        if (obj == null || getClass() != obj.getClass()) return false;  // Check if same class
        House house = (House) obj;
        return size == house.size && color.equals(house.color);  // Compare attributes
    }
}

public class Main {
    public static void main(String[] args) {
        House myHouse = new House("green", 150);
        House anikasHouse = new House("green", 150);
        House sharonsHouse = new House("green", 150);

        // Correct comparison using equals()
        System.out.println(myHouse.equals(sharonsHouse));  // This should return true
    }
}
Main.main(null);

true

Answer = true

Homework: Membership Recommendation System

Problem Overview

You are building a membership recommendation system for a fictional company called “Prime Club.” This system will suggest the most suitable membership tier (or tiers) for a user based on their age, annual income, student status, and employment type. There are 4 types of memberships:

Membership Types

  1. Basic Membership:
    • Requirements:
      • Age ≥ 18
      • Annual income ≥ $20,000
  2. Premium Membership:
    • Requirements:
      • Age ≥ 25
      • Annual income ≥ $50,000
  3. Student Discount:
    • Requirements:
      • Must be a student.
  4. Senior Discount:
    • Requirements:
      • Age ≥ 65

Task Description

Using conditional statements, Boolean expressions, and comparison techniques you’ve learned, write a Java program that:

  1. Prompts the user to input the following:
    • Age (as an integer)
    • Annual income (as a double)
    • Student status (yes or no)
    • Employment type (full-time, part-time, unemployed)
  2. Based on these inputs, your program should print out all membership tiers and discounts the user qualifies for. If the user does not qualify for any membership or discount, the program should print:
    “You do not qualify for any memberships or discounts.”

Additional Challenge

Add a final recommendation where your program prioritizes the “best” membership for the user if they qualify for multiple memberships (use an else-if structure).

The best membership should be decided based on the following priorities:

  1. Premium Membership (highest priority)
  2. Senior Discount
  3. Student Discount
  4. Basic Membership (lowest priority)
Hints (click to reveal) 1. **Input Validation:** - Ensure that age is a positive integer, and annual income is a positive double. - Student status input should be case-insensitive ("yes" or "no"). - Employment type should be one of: "full-time", "part-time", or "unemployed." 2. **Conditions to Consider:** - Use `if-else` statements to check for membership qualifications. - Remember to handle multiple conditions where a user qualifies for more than one membership. 3. **Recommendation Logic:** - Use `else-if` statements to prioritize the memberships based on the provided hierarchy.

Constraints

  • Age must be a positive integer.
  • Annual income must be a positive double.
  • Student status should only accept “yes” or “no” (case-insensitive).
  • Employment type should be one of: “full-time”, “part-time”, or “unemployed.”

Senior Discount:

Requirements: Age ≥ 65 Task Description: Using conditional statements, Boolean expressions, and comparison techniques you’ve learned, write a Java program that:

Prompts the user to input the following:

Age (as an integer) Annual income (as a double) Student status (yes or no) Employment type (full-time, part-time, unemployed) Based on these inputs, your program should print out all membership tiers and discounts the user qualifies for. If the user does not qualify for any membership or discount, the program should print out: “You do not qualify for any memberships or discounts.”

Additional Challenge: Add a final recommendation where your program prioritizes the “best” membership for the user if they qualify for multiple memberships (use an else-if structure).

The best membership should be decided based on the following priorities: Premium Membership (highest priority) Senior Discount Student Discount Basic Membership (lowest priority) Constraints: Age must be a positive integer. Annual income must be a positive double. Student status should only accept “yes” or “no” (case-insensitive). Employment type should be one of: “full-time”, “part-time”, or “unemployed.”

import java.util.Scanner;

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

        // Collect user inputs
        System.out.println("Enter your age: ");
        int age = sc.nextInt();

        System.out.println("Enter your annual income: ");
        double income = sc.nextDouble();

        System.out.println("Are you a student? (yes/no): ");
        String studentStatus = sc.next().toLowerCase();
        boolean isStudent = studentStatus.equals("yes");

        System.out.println("Employment type (full-time, part-time, unemployed): ");
        String employment = sc.next().toLowerCase();

        // Variable to track whether the user qualifies for a membership
        boolean hasMembership = false;

        // Membership and Discount checks
        if (age >= 25 && income >= 50000) {
            System.out.println("You qualify for Premium Membership.");
            hasMembership = true;
        } else if (age >= 65) {
            System.out.println("You qualify for Senior Discount.");
            hasMembership = true;
        } else if (isStudent) {
            System.out.println("You qualify for a Student Discount.");
            hasMembership = true;
        } else if (age >= 18 && income >= 20000) {
            System.out.println("You qualify for Basic Membership.");
            hasMembership = true;
        }

        // If no memberships are applicable
        if (!hasMembership) {
            System.out.println("You do not qualify for any memberships or discounts.");
        }
    }
}
MembershipSystem.main(null);
Enter your age: 
Enter your annual income: 
Are you a student? (yes/no): 
Employment type (full-time, part-time, unemployed): 
You qualify for a Student Discount.