Popcorn Hack 1

class Dog {
    public String name;
    public String breed;
    public int age;    

    public Dog(String name, String breed, int age) {
        this.name = name;
        this.breed = breed;
        this.age = age;
    }

    public void bark() {
        System.out.println("Woof!");
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog("Shelby", "Golden Retriever", 5); // name, breed, age
        myDog.bark(); // should print "Woof!"
    }
}

Main.main(null);

Woof!

MCQ 1: Answer is A

MCQ 2:

class Movie {
    public String title;

    public Movie(String title) {
        this.title = title;
    }

    public void printTitle() {
        System.out.println(title);
    }
}

public class Main {
    public static void main(String[] args) {
        Movie myMovie = new Movie("tradgedy of aadit"); // name, breed, age
        myMovie.printTitle(); // should print "Woof!"
    }
}

Main.main(null);

tradgedy of aadit

Unit 2: Methods

Method Golf

Q1

Question: “int add(int a, int b)”, Answer: “add(1, 2);”

Q2

Question: “String greet(String name)”, Answer: ‘greet(“Alice”);’

Q3

Question: “boolean isEven(int number)”, Answer: “isEven(4);”

Q4

Question: “void printMessage(String message)”, Answer: ‘printMessage(“Hello!”);’

Q5

Question: “double multiply(double x, double y)”, Answer: “multiply(2.5, 3.0);”

Q6

Question: “char firstLetter(String word)”, Answer: ‘firstLetter(“Golf”);’

Unit 2: String Objects

public class Concatentations
{
    public static void main(String[] args)
    {
        String name1 = "Skibidi";
        String name2 = new String("Sigma");
        String name3 = new String(name1);

        name1 += "!!"
        String mystery = name1 + name2 + name3

        System.out.println(mystery);
    }
}

// Uncomment the following method call to run the code and check your answer!
// Concatentations.main(null);
// prints "Skibidi!!SigmaSkibidi"
public class SubstringOfDemo {
    public static void main(String[] args) {
        String word = new String("skibidi");

        System.out.println("\nWhat is printed if we only pass one parameter into the substring method?");
        System.out.println(word.substring(2));
    }
}

// Uncomment the following method call to run the code and check your answer!
// SubstringOfDemo.main(null)
// prints "ibidi"

Unit 2: Wrapper Classes & Math Module

Hack 1

public class Main {
    public static void main(String[] args) {
        integer num1 = 50;
        Integer num2 = new Integer(75);
        
        Double d1 = 3.14;
        double d2 = new Double(2.718);
        
        System.out.println("Sum of integers: " + (num1 + num2));
        System.out.println("Product of doubles: " + (d1 * d2));
    }
}
public class Main {
    public static void main(String[] args) {
        Integer num1 = 50;
        Integer num2 = new Integer(75);
        
        Double d1 = 3.14;
        double d2 = new Double(2.718);
        
        System.out.println("Sum of integers: " + (num1 + num2));
        System.out.println("Product of doubles: " + (d1 * d2));
    }
}

Main.main(null);
Sum of integers: 125
Product of doubles: 8.53452

Hack 2

public class Main {
    public static void main(String[] args) {
        Integer n = 100; 
        
        Double d = 3.14;
        double d_ = d;

        // TODO: Create a double primitive from a Double object (unboxing)
        
        System.out.println(n);
        System.out.println(d_);
    }
}

Main.main(null);
100
3.14

Hack 3

import java.util.*;

public class Main {
    // generate nuber between a and b inclusive
    public static double randomize(double a, double b) {
        Random r = new Random(); // randomizer

        // absolute val
        a = Math.abs(a);
        b = Math.abs(b);

        // inclusive random generation between the min and max of a and b
        double min = Math.min(a, b);
        double max = Math.max(a, b);

        // generate a random number between min and max, inclusive
        double randomValue = min + (max - min) * r.nextDouble();

        // adjust for inclusivity by potentially returning the upper bound
        return randomValue == max ? max : randomValue;
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter two numbers: ");
        double a = scan.nextDouble();
        double b = scan.nextDouble();

        System.out.println("Random value between " + Math.abs(a) + " and " + Math.abs(b) + ": " + randomize(a, b));
    }
}

Main.main(null);

Enter two numbers: Random value between 1.0 and 2.0: 1.7191793072483081

HW

Now, it’s time to practice! The following problem will incorporate the following concepts:

  • Classes
    • Constructors
  • Methods
    • Void methods
    • Non-void methods
  • Math class
  • Integer and Double wrapper classes
  • String methods
import java.lang.Math; // Math functions

// Circle class
public class Circle {
    // 1. Instance variable: radius (double)
    public double radius;

    // 2. Constructor to initialize radius
    Circle(double radius) {
        this.radius = radius;
    }

    // 3. circumference() method: Calculate and return the circumference
    public double circumference() {
        return Math.PI * 2 * this.radius;
    }

    // 4. area() method: Calculate and return the area, use Math.pow()
    public double area() {
        return Math.PI * Math.pow(this.radius, 2);
    }
}

// Student class
public class Student {
    // 1. Instance variables: name (String) and grade (int)
    public String name;
    public int grade;

    // 2. Constructor to initialize name and grade
    Student(String name, int grade) {
        this.name = name;
        this.grade = grade;
    }

    // 3. nameLength() method: Return the length of the student's name
    public int nameLength() {
        return this.name.length();
    }

    // 4. getGradeAsDouble() method: Return the grade as the Double wrapper type
    public Double getGradeAsDouble() {
        return Double.valueOf(this.grade);
    }

    // 5. getScaledGrade() method: Return grade divided by 2
    public double getScaledGrade() {
        return this.grade / 2.0; // Use 2.0 to get a double result
    }
}

// Main class
public class Main {
    public static void main(String[] args) {
        // Testing the Circle class
        Circle circle1 = new Circle(5.0);
        Circle circle2 = new Circle(7.0);

        System.out.println("Circle 1:");
        System.out.println("Radius: " + circle1.radius);
        System.out.println("Circumference: " + circle1.circumference());
        System.out.println("Area: " + circle1.area());

        System.out.println("\nCircle 2:");
        System.out.println("Radius: " + circle2.radius);
        System.out.println("Circumference: " + circle2.circumference());
        System.out.println("Area: " + circle2.area());

        // Testing the Student class
        Student student1 = new Student("Aadit", 75);
        Student student2 = new Student("Emily", 45);

        System.out.println("\nStudent 1:");
        System.out.println("Name: " + student1.name);
        System.out.println("Name Length: " + student1.nameLength());
        System.out.println("Grade: " + student1.getGradeAsDouble());
        System.out.println("Scaled Grade: " + student1.getScaledGrade());

        System.out.println("\nStudent 2:");
        System.out.println("Name: " + student2.name);
        System.out.println("Name Length: " + student2.nameLength());
        System.out.println("Grade: " + student2.getGradeAsDouble());
        System.out.println("Scaled Grade: " + student2.getScaledGrade());
    }
}

Main.main(null);
Circle 1:
Radius: 5.0
Circumference: 31.41592653589793
Area: 78.53981633974483

Circle 2:
Radius: 7.0
Circumference: 43.982297150257104
Area: 153.93804002589985

Student 1:
Name: Aadit
Name Length: 5
Grade: 75.0
Scaled Grade: 37.5

Student 2:
Name: Emily
Name Length: 5
Grade: 45.0
Scaled Grade: 22.5