Skip to the content.

Unit2 Notes_ipynb_2_

class Dog {
    private String name;
    private String breed;
    private int age;
    public String noise = "Woof!";

    // Constructor to initialize the dog's name, breed, and age
    public Dog(String name, String breed, int age) {
        this.name = name;
        this.breed = breed;
        this.age = age;
    }

    // Instance method to make the dog bark
    public void bark() {
        System.out.println(noise);
    }
}

public class Main {
    public static void main(String[] args) {
        // Create a Dog object with name, breed, and age
        Dog myDog = new Dog("Shelby", "Golden Retriever", 5);
        // Call the bark method to print "Woof!"
        myDog.bark();
    }
}

Main.main(new String[0]);
Woof!
public class Movies
{
    public void Movies(String title)
    {
        System.out.println("The title of the movie is: " + title);
    }
}

public class Main
{
    public static void main(String[] args)
    {
        Movies movie = new Movies();
        movie.Movies("Iron Man");
    }
}

Main.main(new String[0]);
The title of the movie is: Iron Man
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);
    }
}


Concatentations.main(null);
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));
    }
}


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

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 Circle {
    private double radius;

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

    // Method to calculate and print the circumference
    public void circumference() {
        double circumference = 2 * Math.PI * radius;
        System.out.println("The circumference of the circle is: " + circumference);
    }

    // Method to calculate and print the area
    public void area() {
        double area = Math.PI * Math.pow(radius, 2);
        System.out.println("The area of the circle is: " + area);
    }
}

public class Main {
    public static void main(String[] args) {
        // Create an instance of the Circle class
        Circle myCircle = new Circle(5.0);

        // Call the methods to calculate and print the circumference and area
        myCircle.circumference();
        myCircle.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.getName());
        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.getName());
        System.out.println("Name Length: " + student2.nameLength());
        System.out.println("Grade: " + student2.getGradeAsDouble());
        System.out.println("Scaled Grade: " + student2.getScaledGrade());
    }
}

public class Student {
    private String name;
    private Integer grade;

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

    // Getter method for name
    public String getName() {
        return name;
    }

    // Getter method for grade
    public Integer getGrade() {
        return grade;
    }

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

    // Method to return the grade as a Double wrapper type
    public Double getGradeAsDouble() {
        return grade.doubleValue();
    }

    // Method to return the grade divided by 2
    public double getScaledGrade() {
        return grade / 2.0;
    }
}

// Execute the main method to see the output
Main.main(new String[0]);
The circumference of the circle is: 31.41592653589793
The area of the circle is: 78.53981633974483

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