Questions and Exercises

Estimated reading: 4 minutes 186 Views

Build a class to find the area and circumference of a circle

Circle class:

package com.mycompany.javaoop;

public class Circle {
    private double radius;
    private String color;
    
    public double getRadius() {
        return radius;
    }
    
    public void setRadius(double radius) {
        this.radius = radius;
    }
    
    public String getColor() {
        return color;
    }
    
    public void setColor(String color) {
        this.color = color;
    }
    
    public double getArea() {
        return Math.PI * radius * radius;
    }
    
    public double getCircumference() {
        return Math.PI * 2 * radius;
    }
    
    public String toString() {
        return "Circle{" + "radius=" + radius + ", color=" + color + "}";
    }
}

Attributes:

  • radius: Private double attribute to store the circle’s radius.
  • color: Private String attribute to store the circle’s color (optional, for additional information).

Methods:

– Getters and Setters:

  • getRadius(): Returns the current value of the radius attribute.
  • setRadius(double radius): Sets the value of the radius attribute, potentially with validation (not included in this example).
  • getColor(): Returns the current value of the color attribute (if present).
  • setColor(String color): Sets the value of the color attribute (if present).

– Area and Circumference:

  • getArea(): Calculates and returns the area of the circle using Math.PI * radius * radius.
  • getCircumference(): Calculates and returns the circumference of the circle using Math.PI * 2 * radius.
  • toString(): Overrides the default toString() method to provide a more descriptive representation of the Circle object, including its radius and color (if present).

Main Class:

package com.mycompany.javaoop;

public class Main
{
    public static void main(String args[]) {
        Circle c1 = new Circle(), c2 = new Circle();
        c1.setRadius(3.0);
        c1.setColor("Black");
        System.out.println("c1 area = " + c1.getArea());
        System.out.println(c1.toString());
        
        c2.setRadius(2.0);
        c2.setColor("Blaue");
        System.out.println("c2 Circumference = " + c2.getCircumference());
    }
}
  • Creates two Circle objects: c1 and c2.
  • Sets the radius and color (if applicable) for each object.
  • Calculates and prints the area of c1 and the circumference of c2.
  • Optionally, you could call c1.toString() and c2.toString() to print their complete representations.

Build a simple banking system

Account class:

package com.mycompany.javaoop;

public class Account {
    private int accountNo;
    private String name;
    private float amount;
    
    public void insert(int a, String n, float amt) {
        this.accountNo = a;
        this.name = n;
        this.amount = amt;
    }
    
    public void deposit(float amt) {
        this.amount = this.amount + amt;
        System.out.println(amt + "deposited");
    }
    
    public void withdraw(float amt) {
        if(amount < amt) {
            System.out.println("Insufficient Balance")
        } else {
            this.amount = this.amount - amt;
            System.out.println(amt + "withdrawn");
        }
    }
    
    public void checkBalance() {
        System.out.println("Balance = " + this.amount);
    }
    
    public String toString() {
        return "Account{" + "accountNo=" + accountNo + ", name =" + name + ", amount=" + amount; 
    }
}package com.mycompany.javaoop;

public class Account {
    private int accountNo;
    private String name;
    private float amount;
    
    public void insert(int a, String n, float amt) {
        this.accountNo = a;
        this.name = n;
        this.amount = amt;
    }
    
    public void deposit(float amt) {
        this.amount = this.amount + amt;
        System.out.println(amt + "deposited");
    }
    
    public void withdraw(float amt) {
        if(amount < amt) {
            System.out.println("Insufficient Balance")
        } else {
            this.amount = this.amount - amt;
            System.out.println(amt + "withdrawn");
        }
    }
    
    public void checkBalance() {
        System.out.println("Balance = " + this.amount);
    }
    
    public String toString() {
        return "Account{" + "accountNo=" + accountNo + ", name =" + name + ", amount=" + amount; 
    }
}

Attributes:

  • accountNo: Private int stores the account number.
  • name: Private String stores the account holder’s name.
  • amount: Private float stores the account balance.

Methods:

  • insert(int a, String n, float amt):
    Initializes an account with the provided account number, name, and initial amount.
  • deposit(float amt):
    Deposits the specified amount into the account.
    Prints a confirmation message with the deposited amount.
  • withdraw(float amt):
    Withdraws the specified amount from the account, but only if there’s sufficient balance.
    Prints a confirmation message if successful, or an “Insufficient Balance” message otherwise.
  • checkBalance():
    Displays the current account balance.
  • toString():
    Overrides the default toString() method to provide a custom string representation of the account, including its account number, name, and balance.

Now complete this code as an exercise by creating the appropriate instances. For further information please watch this video: Questions and Exercises – Classes and Objects

Share this

Questions and Exercises

Or copy link

CONTENTS
English