Creating Class and methods – Java OOP Exercise

Java OOP exercise to learn how to create class and methods by reading requirements.

Exercise-1:

Create a class Dog, that has 3 properties (class fields) breed, age and colour with behaviours (class method) bark and sleep.

Exercise-2:

There is a car, which has attributes model and price, and the car has functionalities start, stop and move. Also, there is a driver, having attributes name and age, and the behaviour drive.

Model the classes Car and Driver. You need to take care of the accessibility of the attributes from outside the class for the best design.

( You can read about class concepts in java here)


SOLUTION

Sol-1:

class Dog {

	// Properties
	String breed;
	int age;
	String colour;

	// Behaviors ( methods)

	public void bark() {
	}

	public void sleep() {

	}
}

Sol-2:

Following is the Car and Driver class implementations. The Car class contains public start, stop and move methods and the Driver contains public drive method.

The methods of the classes are made public, so they can be accessed from outside of the class. The class variables are made private to prevent their accessibility from outside. We should provide their accessibility through public methods as the best design.

Car class:

class Car {
	private int price;
	private String model;

	public void start() {
		System.out.println("Car Start");
	}

	public void stop() {
		System.out.println("Car stop");
	}

	public void move() {
		System.out.println("Car move");
	}

	public int getPrice() {
		return price;
	}

	public void setPrice(int price) {
		this.price = price;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

}

Driver Class

class Driver {
	private String name;
	private int age;

	public void drive() {

		System.out.println("Driver drive car");
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}
}

INTENT: The intent of the exercise is to deal with modelling a class, fields, and methods.

SPECIAL NOTES

Wondering, why a class variable should be private? In fact, there are two primary reasons to make them private as the best design.

  1. To prevent unauthorized access to them.
  2. To avoid maintenance issues.

Read the elaborations given below for both the points a) and b).

a) If you make the class variable public, the user classes can directly access them as below and can modify their value accidentally or intentionally.

For example, ASSUME, the Driver class is designed as below, which must have only the drive and getName method and the name variable is public.

class Driver {
	public String name;
	
	public void drive() {

		System.out.println("Driver drive car");
	}

	public String getName() {
		return name;
	}	
}

Since the name class variable of the Driver class is public, the License user class has accessed and modified the value of the name variable of the Driver class, which might not be expected.

class License {
	
	public void drivingLicense(){

		Driver d = new Driver();

		d.name ="Peter";		
	}
}

b) If multiple classes in program access the class variable directly, then on modifying the variable name for some reasons at a later point of time (maybe coding standard change requirement) will cause an error and you need to fix then all places where you have used it.



Leave a Comment