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 {
    // 1. Class variable: radius (double)
    public double radius;

    // 2. Make a constructor that takes in the radius as a parameter, and sets the radius property
    public Circle(double radius) {
        this.radius = radius;
    }

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

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

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

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

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

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

    // 5. getScaledGrade() method: Return grade divided by 2
    public Double getScaledGrade() {
        return Double.valueOf(getGradeAsDouble() / 2);
    }
}

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