Skip to content

Commit 198f7a2

Browse files
mikedamaymirkoperillo
authored andcommitted
classes - new exercise
* classes initial commit * classes initial commit * classes code * classes proofing * classes proofing classes - addressed review points * classes - address missed review point * classes updated config.sys etc.
1 parent abdff59 commit 198f7a2

10 files changed

Lines changed: 482 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
The primary object-oriented construct in Java is the _class_, which is a combination of data ([_fields_][fields]), also known as instance variables, and behavior ([_methods_][methods]). The fields and methods of a class are known as its _members_.
2+
3+
Access to members can be controlled through access modifiers, the two most common ones being:
4+
5+
- [`public`][public]: the member can be accessed by any code (no restrictions).
6+
- [`private`][private]: the member can only be accessed by code in the same class.
7+
8+
In Java if no access modifier is specified, the default is _package visibility_. In this case, the member is visible to all classes defined into the same package.
9+
10+
The above-mentioned grouping of related data and behavior plus restricting access to members is known as [_encapsulation_][encapsulation], which is one of the core object-oriented concepts.
11+
12+
You can think of a class as a template for creating instances of that class. To [create an instance of a class][creating-objects] (also known as an _object_), the [`new` keyword][new] is used:
13+
14+
```java
15+
class Car {
16+
}
17+
18+
// Create two car instances
19+
Car myCar = new Car();
20+
Car yourCar = new Car();
21+
```
22+
23+
[Fields][fields] have a type and a name (cased in [camelCase][camel-case]) and can be defined anywhere in a class (cased in [PascalCase][pascal-case]).
24+
25+
```java
26+
class Car {
27+
// Accessible by anyone
28+
public int weight;
29+
30+
// Only accessible by code in this class
31+
private String color;
32+
}
33+
```
34+
35+
One can optionally assign an initial value to a field. If a field does _not_ specify an initial value, it will be set to its type's [default value][default-values]. An instance's field values can be accessed and updated using dot-notation.
36+
37+
```java
38+
class Car {
39+
// Will be set to specified value
40+
public int weight = 2500;
41+
42+
// Will be set to default value (0)
43+
public int year;
44+
}
45+
46+
Car newCar = new Car();
47+
newCar.weight; // => 2500
48+
newCar.year; // => 0
49+
50+
// Update value of the field
51+
newCar.year = 2018;
52+
```
53+
54+
Private fields are usually updated as a side effect of calling a method. Such methods usually don't return any value, in which case the return type should be [`void`][void]:
55+
56+
```java
57+
class CarImporter {
58+
private int carsImported;
59+
60+
public void ImportCars(int numberOfCars)
61+
{
62+
// Update private field
63+
carsImported = carsImported + numberOfCars;
64+
}
65+
}
66+
```
67+
68+
Note that is not customary to use public fields in Java classes. Private fields are typically used which are accessed through [_getters_ and _setters_][so-getters-setters].
69+
70+
Within a class, the [`this` keyword][this] will refer to the current class. This is especially useful if a parameter has the same name as a field:
71+
72+
```java
73+
class CarImporter {
74+
private int carsImported;
75+
76+
public void SetImportedCars(int carsImported)
77+
{
78+
// Update private field from public method
79+
this.carsImported = carsImported;
80+
}
81+
}
82+
```
83+
84+
[fields]: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
85+
[methods]: https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
86+
[this]: https://docs.oracle.com/javase/tutorial/java/javaOO/thiskey.html
87+
[new]: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
88+
[void]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/void
89+
[creating-objects]: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
90+
[public]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/public
91+
[private]: https://en.wikibooks.org/wiki/Java_Programming/Keywords/private
92+
[default-values]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
93+
[camel-case]: https://techterms.com/definition/camelcase
94+
[pascal-case]: https://techterms.com/definition/pascalcase
95+
[encapsulation]: https://en.wikipedia.org/wiki/Encapsulation_(computer_programming)
96+
[so-getters-setters]: https://stackoverflow.com/questions/2036970/how-do-getters-and-setters-work
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## General
2+
3+
## 1. Buy a brand-new remote controlled car
4+
5+
- [This page shows how to create a new instance of a class][creating-objects].
6+
7+
## 2. Display the distance driven
8+
9+
- Keep track of the distance driven in a [field][fields].
10+
- Consider what visibility to use for the field (does it need to be used outside the class?).
11+
12+
## 3. Display the battery percentage
13+
14+
- Keep track of the distance driven in a [field][fields].
15+
- Initialize the field to a specific value to correspond to the initial battery charge.
16+
- Consider what visibility to use for the field (does it need to be used outside the class?).
17+
18+
## 4. Update the number of meters driven when driving
19+
20+
- Update the field representing the distance driven.
21+
22+
## 5. Update the battery percentage when driving
23+
24+
- Update the field representing the battery percentage driven.
25+
26+
## 6. Prevent driving when the battery is drained
27+
28+
- Add a conditional to only update the distance and battery if the battery is not already drained.
29+
- Add a conditional to display the empty battery message if the battery is drained.
30+
31+
[creating-objects]: https://docs.oracle.com/javase/tutorial/java/javaOO/objectcreation.html
32+
[fields]: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
In this exercise you'll be playing around with a remote controlled car, which you've finally saved enough money for to buy.
2+
3+
Cars start with full (100%) batteries. Each time you drive the car using the remote control, it covers 20 meters and drains one percent of the battery.
4+
5+
The remote controlled car has a fancy LED display that shows two bits of information:
6+
7+
- The total distance it has driven, displayed as: `"Driven <METERS> meters"`.
8+
- The remaining battery charge, displayed as: `"Battery at <PERCENTAGE>%"`.
9+
10+
If the battery is at 0%, you can't drive the car anymore and the battery display will show `"Battery empty"`.
11+
12+
You have six tasks, each of which will work with remote controlled car instances.
13+
14+
## 1. Buy a brand-new remote controlled car
15+
16+
Implement the (_static_) `RemoteControlCar.buy()` method to return a brand-new remote controlled car instance:
17+
18+
```java
19+
RemoteControlCar car = RemoteControlCar.buy();
20+
```
21+
22+
## 2. Display the distance driven
23+
24+
Implement the `RemoteControlCar.distanceDisplay()` method to return the distance as displayed on the LED display:
25+
26+
```java
27+
RemoteControlCar car = RemoteControlCar.buy();
28+
car.distanceDisplay();
29+
// => "Driven 0 meters"
30+
```
31+
32+
## 3. Display the battery percentage
33+
34+
Implement the `RemoteControlCar.batteryDisplay()` method to return the distance as displayed on the LED display:
35+
36+
```java
37+
RemoteControlCar car = RemoteControlCar.buy();
38+
car.batteryDisplay();
39+
// => "Battery at 100%"
40+
```
41+
42+
## 4. Update the number of meters driven when driving
43+
44+
Implement the `RemoteControlCar.drive()` method that updates the number of meters driven:
45+
46+
```java
47+
RemoteControlCar car = RemoteControlCar.buy();
48+
car.drive();
49+
car.drive();
50+
car.distanceDisplay();
51+
// => "Driven 40 meters"
52+
```
53+
54+
## 5. Update the battery percentage when driving
55+
56+
Update the `RemoteControlCar.drive()` method to update the battery percentage:
57+
58+
```java
59+
RemoteControlCar car = RemoteControlCar.buy();
60+
car.drive();
61+
car.drive();
62+
car.batteryDisplay();
63+
// => "Battery at 98%"
64+
```
65+
66+
## 6. Prevent driving when the battery is drained
67+
68+
Update the `RemoteControlCar.drive()` method to not increase the distance driven nor decrease the battery percentage when the battery is drained (at 0%):
69+
70+
```java
71+
RemoteControlCar car = RemoteControlCar.buy();
72+
73+
// Drain the battery
74+
// ...
75+
76+
car.distanceDisplay();
77+
// => "Driven 2000 meters"
78+
79+
car.batteryDisplay();
80+
// => "Battery empty"
81+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
The primary object-oriented construct in Java is the _class_, which is a combination of data (_fields_) and behavior (_methods_). The fields and methods of a class are known as its _members_.
2+
3+
Access to members can be controlled through access modifiers, the two most common ones being:
4+
5+
- `public`: the member can be accessed by any code (no restrictions).
6+
- `private`: the member can only be accessed by code in the same class.
7+
8+
You can think of a class as a template for creating instances of that class. To create an instance of a class (also known as an _object_), the `new` keyword is used:
9+
10+
```java
11+
class Car {
12+
}
13+
14+
// Create two car instances
15+
Car myCar = new Car();
16+
Car yourCar = new Car();
17+
```
18+
19+
Fields have a type and a name (defined in camelCase) and can be defined anywhere in a class (by convention cased in PascalCase):
20+
21+
```java
22+
class Car {
23+
// Accessible by anyone
24+
public int weight;
25+
26+
// Only accessible by code in this class
27+
private String color;
28+
}
29+
```
30+
31+
One can optionally assign an initial value to a field. If a field does _not_ specify an initial value, it wll be set to its type's default value. An instance's field values can be accessed and updated using dot-notation.
32+
33+
```java
34+
class Car {
35+
// Will be set to specified value
36+
public int weight = 2500;
37+
38+
// Will be set to default value (0)
39+
public int year;
40+
}
41+
42+
Car newCar = new Car();
43+
newCar.weight; // => 2500
44+
newCar.year; // => 0
45+
46+
// Update value of the field
47+
newCar.year = 2018;
48+
```
49+
50+
Private fields are usually updated as a side effect of calling a method. Such methods usually don't return any value, in which case the return type should be `void`:
51+
52+
```java
53+
class CarImporter {
54+
private int carsImported;
55+
56+
public void ImportCars(int numberOfCars)
57+
{
58+
// Update private field from public method
59+
carsImported = carsImported + numberOfCars;
60+
}
61+
}
62+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"contributors": [
3+
{
4+
"github_username": "mirkoperillo",
5+
"exercism_username": "mirkoperillo"
6+
}
7+
],
8+
"authors": [
9+
{
10+
"github_username": "mikedamay",
11+
"exercism_username": "mikedamay"
12+
}
13+
]
14+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Learning objectives
2+
3+
- Know what classes are.
4+
- Know what encapsulation is.
5+
- Know what fields are.
6+
- Know how to create an object.
7+
- Know how to update state through methods.
8+
- Know about the `void` type.
9+
10+
## Out of scope
11+
12+
- Reference equality.
13+
- Constructors.
14+
- Interfaces.
15+
- Inheritance.
16+
- Finalizers.
17+
- Method overloading.
18+
19+
## Concepts
20+
21+
- `classes`: know what classes are; know what encapsulation is; know what fields are; know how to create an object; know how to update state through methods; know about the `void` type.
22+
23+
## Prerequisites
24+
25+
- `basics`: know how to define a basic class with basic methods.
26+
- `strings`: know how to do basic string interpolation.
27+
- `numbers`: know how to compare numbers.
28+
- `conditionals`: know how to do conditional logic.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class RemoteControlCar {
2+
private int batteryPercentage = 100;
3+
private int distanceDrivenInMeters = 0;
4+
5+
public void drive() {
6+
if (batteryPercentage > 0) {
7+
batteryPercentage--;
8+
distanceDrivenInMeters += 20;
9+
}
10+
}
11+
12+
public String distanceDisplay() {
13+
return "Driven " + distanceDrivenInMeters + " meters";
14+
}
15+
16+
public String batteryDisplay() {
17+
if (batteryPercentage == 0) {
18+
return "Battery empty";
19+
}
20+
21+
return "Battery at " + batteryPercentage + "%";
22+
}
23+
24+
public static RemoteControlCar buy() {
25+
return new RemoteControlCar();
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apply plugin: "java"
2+
apply plugin: "eclipse"
3+
apply plugin: "idea"
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testCompile "junit:junit:4.13"
11+
testImplementation "org.assertj:assertj-core:3.15.0"
12+
}
13+
14+
test {
15+
testLogging {
16+
exceptionFormat = 'short'
17+
showStandardStreams = true
18+
events = ["passed", "failed", "skipped"]
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class RemoteControlCar {
2+
public static RemoteControlCar buy() {
3+
throw new UnsupportedOperationException("Please implement the (static) RemoteControlCar.buy() method");
4+
}
5+
6+
public String distanceDisplay() {
7+
throw new UnsupportedOperationException("Please implement the (static) RemoteControlCar.distanceDisplay() method");
8+
}
9+
10+
public String batteryDisplay() {
11+
throw new UnsupportedOperationException("Please implement the (static) RemoteControlCar.batteryDisplay() method");
12+
}
13+
14+
public void drive() {
15+
throw new UnsupportedOperationException("Please implement the (static) RemoteControlCar.drive() method");
16+
}
17+
}

0 commit comments

Comments
 (0)