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)

    // 2. Make a constructor that takes in the radius as a parameter, and sets the radius property

    // 3. cirumference() method: Calculate and return the circumference

    // 4. area() method: Calculate and return the area, use Math.pow()
    public double radius;
    Circle(double radius){
        this.radius=radius;

    }

    public double circumference(){
        return this.radius*2*3.14;
    }
    public double area(){
        return 3.14*Math.pow(this.radius,2);
    }
}

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

    // 2. Constructor to initialize name and grade

    // 3. nameLength() method: Return the length of the student's name

    // 4. getGradeAsDouble() method: Return the grade as the Double wrapper type

    // 5. getScaledGrade() method: Return grade divided by 2
    public String name;
    public int grade;
    Student(String name, int grade){
        this.name=name;
        this.grade=grade;
    }
    public int nameLength(){return this.name.length();}
    public double getGradeAsDouble(){return (double)this.grade;}
    public int getScaledGrade(){return grade/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.400000000000002
Area: 78.5

Circle 2:
Radius: 7.0
Circumference: 43.96
Area: 153.86

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

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