Unit 2 — Recap & Homework

Objects and Void Methods Methods String Objects Wrapper Classes HW

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

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

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

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

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

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

    // 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) this.grade;
    }

    // 5. getScaledGrade() method: Return grade divided by 2
    public double getScaledGrade() {
        return ((double) this.grade) / 2;
    }
}

// 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());
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

Comments

You are seeing this because your Disqus shortname is not properly set. To configure Disqus, you should edit your _config.yml to include either a disqus.shortname variable.

If you do not wish to use Disqus, override the comments.html partial for this theme.