|
9 | 9 |
|
10 | 10 | * **Finish Features**
|
11 | 11 | * Add 5 different fields to the `Person` class.
|
12 |
| - * Ensure each of the methods for manipulating and accessing these fields have appropriate testing in the `TestPerson`. |
| 12 | + * Ensure each of the methods for manipulating and accessing these fields have appropriate testing in the `TestPerson`. |
| 13 | + |
| 14 | +## Some background |
| 15 | + |
| 16 | +In Java, an object is an instance of a class that represents a real-world entity or concept. A class is a blueprint or template that defines the properties and behaviors of an object. |
| 17 | + |
| 18 | +For example, let's say we want to model a car in Java. We can create a Car class that defines the properties and behaviors of a car, such as its make, model, color, and speed. We can then create Car objects that represent individual cars, each with its own set of values for these properties. |
| 19 | + |
| 20 | +Here is an example of a Car class in Java: |
| 21 | + |
| 22 | +```java |
| 23 | +public class Car { |
| 24 | + private String make; |
| 25 | + private String model; |
| 26 | + private String color; |
| 27 | + private int speed; |
| 28 | + |
| 29 | + public Car(String make, String model, String color) { |
| 30 | + this.make = make; |
| 31 | + this.model = model; |
| 32 | + this.color = color; |
| 33 | + this.speed = 0; |
| 34 | + } |
| 35 | + |
| 36 | + public void accelerate(int amount) { |
| 37 | + speed += amount; |
| 38 | + } |
| 39 | + |
| 40 | + public void brake(int amount) { |
| 41 | + speed -= amount; |
| 42 | + } |
| 43 | + |
| 44 | + public String getMake() { |
| 45 | + return make; |
| 46 | + } |
| 47 | + |
| 48 | + public String getModel() { |
| 49 | + return model; |
| 50 | + } |
| 51 | + |
| 52 | + public String getColor() { |
| 53 | + return color; |
| 54 | + } |
| 55 | + |
| 56 | + public int getSpeed() { |
| 57 | + return speed; |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
| 61 | + |
| 62 | +In this example, the Car class has four properties: make, model, color, and speed. The class also has three methods: accelerate(), brake(), and getters for each property. The accelerate() and brake() methods modify the speed property of the Car object. |
| 63 | + |
| 64 | +To create a Car object, we can use the following code: |
| 65 | + |
| 66 | +```java |
| 67 | +Car myCar = new Car("Toyota", "Corolla", "Red"); |
| 68 | +``` |
| 69 | + |
| 70 | +This creates a new Car object with the make "Toyota", model "Corolla", and color "Red". The speed property is initialized to 0. |
| 71 | + |
| 72 | +We can then use the methods of the Car class to modify the speed of the car: |
| 73 | + |
| 74 | +```java |
| 75 | +myCar.accelerate(10); |
| 76 | +myCar.brake(5); |
| 77 | +``` |
| 78 | + |
| 79 | +This increases the speed of the car by 10 and then decreases it by 5. |
| 80 | + |
| 81 | +Thus objects and classes are fundamental concepts in Java that allow developers to model real-world entities and concepts in their programs. By defining the properties and behaviors of an object in a class, developers can create instances of that object and manipulate them in their programs. |
0 commit comments