Skip to the content.

Unit 2 Hacks

PRACTICE: Objects

Now, we’ll see if you understand the concept of classes and objects.

Try and create a class in the following code space to represent a dog.

class Dog {
    String name;
    String breed;
    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!
class Movie {
    String title;

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

    public void printTitle() {
        System.out.println("Movie Title: " + this.title);
    }
}

public class Main {
    public static void main(String[] args) {
        Movie myMovie = new Movie("Iron Man");
        myMovie.printTitle(); 
    }
}
Main.main(null);
Movie Title: Iron Man

Practice 2

Fix the code below!


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));
    }
}

Fixed code below


public class Main {
    public static void main(String[] args) {
        Integer num1 = 50;
        Integer num2 = 75;

        Double d1 = 3.14;
        double d2 = 2.718;

        System.out.println("Sum of integers: " + (num1 + num2));
        System.out.println("Product of doubles: " + (d1 * d2));
    }
}

Now, complete the exercise below without any extra help.

public class Main {
    public static void main(String[] args) {
        Integer num1 = 100;
        
        Double doubleObj = 50.25;
        double num2 = doubleObj;

        System.out.println("Autoboxed Integer: " + num1); 
        System.out.println("Unboxed double: " + num2);
    }
}
Main.main(null);
Autoboxed Integer: 100
Unboxed double: 50.25

Quick, let’s do an exercise for practice! What will the following code segment return?

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));
    }
}

SubstringOfDemo.main(null)
What is printed if we only pass one parameter into the substring method?
ibidi
public class CompareToDemo {
    public static void main(String[] args) {
        String word = new String("skibidi");
        String word2 = new String("skibidi1");
        String word3 = new String("skibidi");

        System.out.println("\nIf word is < word2, a negative value will be printed. If they are equal, 0 will be printed, and if word > word2, a positive value is printed");
        System.out.println(word.compareTo(word2));

        System.out.println("\nComparison between word and word3");
        System.out.println(word.compareTo(word3));
    }
}

CompareToDemo.main(null)
If word is < word2, a negative value will be printed. If they are equal, 0 will be printed, and if word > word2, a positive value is printed
-1

Comparison between word and word3
0
public class EqualToDemo {
    public static void main(String[] args) {
        String word = new String("skibidi");
        String word2 = new String("skibidi1");
        String word3 = new String("skibidi");

        System.out.println("\nThis displays if word1 = word2, if false it returns false, if true it returns true");
        System.out.println(word.equals((word2)));

        System.out.println("\nThis displays if word1 = word3, if false it returns false, if true it returns true");
        System.out.println(word.equals((word3)));
    }
}

EqualToDemo.main(null)
This displays if word1 = word2, if false it returns false, if true it returns true
false

This displays if word1 = word3, if false it returns false, if true it returns true
true

PRACTICE

Let’s try a practice! Fill in the function below, randomize, following the steps below:

Take the absolute value of both numbers Return a random number in between those two numbers, inclusive

import java.util.*;

public class Main {
    public static double randomize(double a, double b) {
        a = Math.abs(a);
        b = Math.abs(b);

        double min = Math.min(a, b);
        double max = Math.max(a, b);

        return min + (Math.random() * (max - min));
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter first number: ");
        double a = scan.nextDouble();
        System.out.println("Enter second number: ");
        double b = scan.nextDouble();

        System.out.println("Random number: " + randomize(a, b));
    }
}
Main.main(null);
Enter first number: 
Enter second number: 
Random number: 6.348515218955287

Method Golf

methodgolf

Homework

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
public class Main {
    
    // Circle Class: Represents a geometric circle with a radius and methods to calculate its circumference and area
    public static class Circle {
        private double radius; // Class variable to store the radius of the circle

        // Constructor: Initializes the circle with a given radius
        public Circle(double radius) {
            this.radius = radius;
        }

        // Method to calculate and return the circumference of the circle
        public double circumference() {
            return 2 * Math.PI * radius;
        }

        // Method to calculate and return the area of the circle
        public double area() {
            return Math.PI * Math.pow(radius, 2);
        }
    }

    // Student Class: Represents a student with a name and grade, and provides methods to retrieve related information
    public static class Student {
        private String name; // Class variable to store the student's name
        private Integer grade; // Class variable to store the student's grade (Integer)

        // Constructor: Initializes the student with a given name and grade
        public Student(String name, Integer grade) {
            this.name = name;
            this.grade = grade;
        }

        // Method to return the length of the student's name
        public int nameLength() {
            return name.length();
        }

        // Method to return the student's grade as a Double
        public Double getGradeAsDouble() {
            return grade.doubleValue();
        }

        // Method to return the student's grade scaled down by half (divided by 2)
        public double getScaledGrade() {
            return grade / 2.0;
        }
    }

    // Main method: Entry point of the program, used for testing Circle and Student classes
    public static void main(String[] args) {
        // Create two Circle objects with different radii
        Circle circle1 = new Circle(5.0);
        Circle circle2 = new Circle(7.0);

        // Print details of the first circle (circumference and area)
        System.out.println("Circle 1:");
        System.out.println("Radius: " + circle1.circumference()); // Prints the circumference of circle1
        System.out.println("Circumference: " + circle1.circumference());
        System.out.println("Area: " + circle1.area()); // Prints the area of circle1

        // Print details of the second circle (circumference and area)
        System.out.println("\nCircle 2:");
        System.out.println("Radius: " + circle2.circumference()); // Prints the circumference of circle2
        System.out.println("Circumference: " + circle2.circumference());
        System.out.println("Area: " + circle2.area()); // Prints the area of circle2

        // Create two Student objects with different names and grades
        Student student1 = new Student("Srini", 75);
        Student student2 = new Student("Akshay", 45);

        // Print details of the first student (name, name length, grade as double, and scaled grade)
        System.out.println("\nStudent 1:");
        System.out.println("Name: " + student1.name);
        System.out.println("Name Length: " + student1.nameLength()); // Prints the length of student1's name
        System.out.println("Grade: " + student1.getGradeAsDouble()); // Prints the grade of student1 as a Double
        System.out.println("Scaled Grade: " + student1.getScaledGrade()); // Prints the grade scaled down by 2

        // Print details of the second student (name, name length, grade as double, and scaled grade)
        System.out.println("\nStudent 2:");
        System.out.println("Name: " + student2.name);
        System.out.println("Name Length: " + student2.nameLength()); // Prints the length of student2's name
        System.out.println("Grade: " + student2.getGradeAsDouble()); // Prints the grade of student2 as a Double
        System.out.println("Scaled Grade: " + student2.getScaledGrade()); // Prints the grade scaled down by 2
    }
}

// Call the main method to run the program
Main.main(null);
Circle 1:
Radius: 31.41592653589793
Circumference: 31.41592653589793
Area: 78.53981633974483

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

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

Student 2:
Name: Akshay
Name Length: 6
Grade: 45.0
Scaled Grade: 22.5