Skip to content

Commit f2a5561

Browse files
authored
Merge pull request #7 from MicrosoftLearning/lp2-m1-eric
Lp2 m1 eric - Clean-up of Lp2 M01 interfaces instructions/code
2 parents 80b6dc2 + b5ba951 commit f2a5561

File tree

3 files changed

+37
-60
lines changed

3 files changed

+37
-60
lines changed

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/ImplementInterfaces/IPerson.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,4 @@ public interface IPerson
66
int Age { get; set; }
77
void DisplayInfo();
88
}
9-
10-
public class Student : IPerson
11-
{
12-
public string Name { get; set; } = string.Empty;
13-
public int Age { get; set; } = 0;
14-
15-
public void DisplayInfo()
16-
{
17-
Console.WriteLine($"Student Name: {Name}, Age: {Age}");
18-
}
19-
}
209
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace ImplementInterfaces
2+
{
3+
public class Student : IPerson
4+
{
5+
public string Name { get; set; } = string.Empty;
6+
public int Age { get; set; } = 0;
7+
8+
public void DisplayInfo()
9+
{
10+
Console.WriteLine($"Student Name: {Name}, Age: {Age}");
11+
}
12+
}
13+
}

Instructions/Labs/l2p2-lp2-m1-exercise-implement-interfaces-project.md

Lines changed: 24 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,6 @@ This exercise includes the following tasks:
4545

4646
To start, you need to create a new C# project in your development environment. This project will serve as the foundation for implementing interfaces and their respective classes.
4747

48-
### What you'll learn
49-
50-
- How to create a new C# console application using the .NET CLI.
51-
52-
### Steps
53-
5448
1. Open Visual Studio Code.
5549
1. Ensure that the C# Dev Kit extension is installed.
5650
1. Open the terminal in Visual Studio Code by selecting `View > Terminal`.
@@ -79,13 +73,11 @@ Ensure that the project has been created successfully by verifying the presence
7973

8074
---
8175

82-
## Task 2: Define an interface with method signatures and properties
76+
## Task 2: Define the `IPerson` interface with method signatures and properties
8377

8478
Next, you will define an interface that includes method signatures and properties. This interface will be used to enforce a contract for any class that implements it. The code defines an interface in C# which shows interfaces enforcing consistent behavior across classes.
8579

86-
### Steps
87-
88-
1. In the `ImplementInterfaces` project, create a new file named `IPerson.cs`.
80+
1. Create a new file named `IPerson.cs` in the `ImplementInterfaces` project.
8981
1. Add the following code to define the `IPerson` interface:
9082

9183
```csharp
@@ -100,18 +92,14 @@ Next, you will define an interface that includes method signatures and propertie
10092
}
10193
```
10294

103-
### Check your work
104-
105-
Verify that the `IPerson` interface is correctly defined by checking the `IPerson.cs` file. The interface should include the `Name` and `Age` properties, as well as the `DisplayInfo` method signature.
95+
*The `IPerson` interface has been defined to include the `Name` and `Age` properties, along with the `DisplayInfo` method signature, ensuring a consistent contract for implementing classes.*
10696

10797
---
10898

109-
## Task 3: Implement the defined interface in a class
99+
## Task 3: Implement the defined `IPerson` interface in a class
110100

111101
Now, you will create a class that implements the `IPerson` interface. This class will provide concrete implementations for the interface members. The code in this step implements an interface in a class and provides concrete implementations for the interface.
112102

113-
### Steps
114-
115103
1. In the `ImplementInterfaces` project, create a new file named `Student.cs`.
116104
1. Add the following code to implement the `IPerson` interface in the `Student` class:
117105

@@ -131,18 +119,15 @@ Now, you will create a class that implements the `IPerson` interface. This class
131119
}
132120
```
133121

134-
### Check your work
135-
Ensure that the `Student` class correctly implements the `IPerson` interface by checking the `Student.cs` file. The class should provide implementations for the `Name` and `Age` properties, as well as the `DisplayInfo` method.
122+
*The `Student` class has been implemented to adhere to the `IPerson` interface. It provides concrete implementations for the `Name` and `Age` properties, as well as the `DisplayInfo` method, ensuring compliance with the interface contract.*
136123

137124
---
138125

139-
## Task 4: Create another class that implements different behavior
126+
## Task 4: Create a second class to implement `IPerson` interface with different behavior
140127

141128
You will now create another class that implements the `IPerson` interface but with different behavior. Task 4 implements the same interface in multiple classes. This demonstrates how to provide unique behavior for each class using an interface.
142129

143-
### Steps
144-
145-
1. In the `ImplementInterfaces` project, create a new file named `Teacher.cs`.
130+
1. Create a new file named `Teacher.cs` in the `ImplementInterfaces` project.
146131
1. Add the following code to implement the `IPerson` interface in the `Teacher` class:
147132

148133
```csharp
@@ -161,18 +146,14 @@ You will now create another class that implements the `IPerson` interface but wi
161146
}
162147
```
163148

164-
### Check your work
165-
166-
Verify that the `Teacher` class correctly implements the `IPerson` interface by checking the `Teacher.cs` file. The class should provide implementations for the `Name` and `Age` properties, as well as the `DisplayInfo` method.
149+
*You have created the `Teacher` class, which implements the `IPerson` interface. This class provides its own implementation for the `Name` and `Age` properties, as well as the `DisplayInfo` method, ensuring it adheres to the interface contract.*
167150

168151
---
169152

170-
## Task 5: Demonstrate interface implementation
153+
## Task 5: Demonstrate the interface implementation
171154

172155
In this task, you will demonstrate the use of the interface by creating instances of the `Student` and `Teacher` classes and calling their methods. This demonstrates how to use polymorphism to treat objects of different classes as the same interface type.
173156

174-
### Steps
175-
176157
1. Open the `Program.cs` file in the `ImplementInterfaces` project.
177158
1. Replace the existing code with the following:
178159

@@ -195,41 +176,35 @@ In this task, you will demonstrate the use of the interface by creating instance
195176
}
196177
```
197178

198-
### Check your work
199-
200-
Run the application using the following command:
201-
202-
```bash
203-
dotnet run
204-
```
205-
206-
You should see the output displaying the information for both the student and the teacher, demonstrating the interface implementation.
207-
208-
---
179+
*The `Program.cs` file demonstrates how to use the `IPerson` interface to create polymorphic behavior. By creating instances of the `Student` and `Teacher` classes, you can observe how each class adheres to the interface contract while providing its own unique implementation of the `DisplayInfo` method.*
209180

210181
## Task 6: Test the implemented interfaces
211182

212183
Next, you will test the implemented interfaces and their respective classes to ensure they function correctly. This section of code demonstrates how to test interface implementations in a C# application.
213184

214-
### Steps
185+
1. Ensure that the `Program.cs` file contains the code to create instances of `Student` and `Teacher` and calls their `DisplayInfo` methods and all files are saved.
186+
1. Build the application using the following command:
187+
188+
```bash
189+
dotnet build
190+
```
191+
192+
*Ensure that the build completes successfully without any errors.*
215193

216-
1. Ensure that the `Program.cs` file contains the code to create instances of `Student` and `Teacher` and calls their `DisplayInfo` methods.
217194
1. Run the application again using the following command:
218195

219196
```bash
220197
dotnet run
221198
```
222199

223-
1. Verify the output to ensure that the information for both the student and the teacher is displayed correctly.
224-
225-
### Check your work
200+
1. Confirm that the application runs without errors and displays the correct information for both the student and the teacher. The output should look similar to the following:
226201

227-
Confirm that the application runs without errors and displays the correct information for both the student and the teacher. The output should look similar to the following:
202+
```console
203+
Student Name: Eric Solomon, Age: 20
204+
Teacher Name: Kayla Lewis, Age: 35
205+
```
228206

229-
```console
230-
Student Name: Eric Solomon, Age: 20
231-
Teacher Name: Kayla Lewis, Age: 35
232-
```
207+
*Observe that the output confirms the successful implementation of the `IPerson` interface in both the `Student` and `Teacher` classes. Each class adheres to the interface contract while providing its unique implementation of the `DisplayInfo` method, demonstrating polymorphism in action.*
233208

234209
---
235210

0 commit comments

Comments
 (0)