Skip to content

Commit 64c72f6

Browse files
author
Eric Camplin
committed
LP2 M1 - rework fo lab and solution code
1 parent a2dfc37 commit 64c72f6

8 files changed

Lines changed: 136 additions & 80 deletions

File tree

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/IPerson.cs

Lines changed: 0 additions & 6 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace ImplementInterfaces
2+
{
3+
public interface IPerson
4+
{
5+
string Name { get; set; }
6+
int Age { get; set; }
7+
void DisplayInfo();
8+
}
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+
}
20+
}

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/ImplementInterfaces.csproj renamed to DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/ImplementInterfaces/ImplementInterfaces.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/Program.cs renamed to DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/ImplementInterfaces/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
using System;
2+
13
namespace ImplementInterfaces
24
{
35
class Program
46
{
57
static void Main(string[] args)
68
{
7-
IPerson student = new Student { Name = "John Doe", Age = 20 };
8-
IPerson teacher = new Teacher { Name = "Jane Smith", Age = 35 };
9+
IPerson student = new Student { Name = "Eric Solomon", Age = 20 };
10+
IPerson teacher = new Teacher { Name = "Kayla Lewis", Age = 35 };
911

1012
student.DisplayInfo();
1113
teacher.DisplayInfo();
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 Teacher : 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($"Teacher Name: {Name}, Age: {Age}");
11+
}
12+
}
13+
}

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/Student.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

DownloadableCodeProjects/LP2_implement_interfaces/Interfaces_M1/Solution/Teacher.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

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

Lines changed: 98 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,19 @@ In this exercise, you will create a console app to define and implement interfac
1212

1313
This exercise takes approximately **20** minutes to complete.
1414

15+
---
16+
1517
## Before you start
1618

1719
Before you can start this exercise, you need to:
1820

19-
1. Ensure that you have the latest short term support (STS) version of the .NET SDK installed on your computer. You can download the latest versions of the .NET SDK using the following URL: Download .NET
20-
1. Ensure that you have Visual Studio Code installed on your computer. You can download Visual Studio Code using the following URL: Download Visual Studio Code
21+
1. Ensure that you have the latest short term support (STS) version of the .NET SDK installed on your computer. You can download the latest versions of the .NET SDK using the following URL: [Download .NET](https://dotnet.microsoft.com/download).
22+
1. Ensure that you have Visual Studio Code installed on your computer. You can download Visual Studio Code using the following URL: [Download Visual Studio Code](https://code.visualstudio.com/).
2123
1. Ensure that you have the C# Dev Kit configured in Visual Studio Code.
2224

23-
For additional help configuring the Visual Studio Code environment, see Install and configure Visual Studio Code for C# development
25+
For additional help configuring the Visual Studio Code environment, see [Install and configure Visual Studio Code for C# development](https://learn.microsoft.com/en-us/dotnet/core/tutorials/with-visual-studio-code).
26+
27+
---
2428

2529
## Exercise scenario
2630

@@ -35,10 +39,18 @@ This exercise includes the following tasks:
3539
1. Demonstrate interface implementation by creating instances of the classes and calling their methods.
3640
1. Test the implemented interfaces and their implementations to ensure they work as expected.
3741

42+
---
43+
3844
## Task 1: Create a new C# project
3945

4046
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.
4147

48+
### What you'll learn
49+
50+
- How to create a new C# console application using the .NET CLI.
51+
52+
### Steps
53+
4254
1. Open Visual Studio Code.
4355
1. Ensure that the C# Dev Kit extension is installed.
4456
1. Open the terminal in Visual Studio Code by selecting `View > Terminal`.
@@ -61,86 +73,105 @@ To start, you need to create a new C# project in your development environment. T
6173
code .
6274
```
6375

64-
### Check your work: Create a new C# project
76+
### Check your work
6577

6678
Ensure that the project has been created successfully by verifying the presence of the `Program.cs` file in the project directory. You should also see the project structure in the Visual Studio Code Explorer pane.
6779

80+
---
81+
6882
## Task 2: Define an interface with method signatures and properties
6983

70-
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.
84+
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.
85+
86+
### Steps
7187

7288
1. In the `ImplementInterfaces` project, create a new file named `IPerson.cs`.
7389
1. Add the following code to define the `IPerson` interface:
7490

7591
```csharp
76-
public interface IPerson
92+
namespace ImplementInterfaces
7793
{
78-
string Name { get; set; }
79-
int Age { get; set; }
80-
void DisplayInfo();
94+
public interface IPerson
95+
{
96+
string Name { get; set; }
97+
int Age { get; set; }
98+
void DisplayInfo();
99+
}
81100
}
82101
```
83102

84-
### Check your work: Define an interface
103+
### Check your work
85104

86105
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.
87106

107+
---
108+
88109
## Task 3: Implement the defined interface in a class
89110

90-
Now, you will create a class that implements the `IPerson` interface. This class will provide concrete implementations for the interface members.
111+
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.
91112

92-
1. In the `ImplementInterfaces` project, create a new file named `Student.cs`.
93-
1. Add the following code to implement the `IPerson` interface in the `Student` class. Note that we initialize the properties with default values to avoid nullable warnings:
113+
### Steps
94114

95-
```csharp
96-
public class Student : IPerson
97-
{
98-
public string Name { get; set; } = string.Empty;
99-
public int Age { get; set; } = 0;
100-
101-
public void DisplayInfo()
102-
{
103-
Console.WriteLine($"Student Name: {Name}, Age: {Age}");
104-
}
105-
}
106-
```
115+
1. In the `ImplementInterfaces` project, create a new file named `Student.cs`.
116+
1. Add the following code to implement the `IPerson` interface in the `Student` class:
107117

108-
> **Note:**
109-
> In C#, the code `public string Name { get; set; } = string.Empty;` and `public int Age { get; set; } = 0;` sets default values for the properties (`Name` starts as an empty string, and `Age` starts as 0). This helps avoid warnings from the compiler about "nullable" issues. If you dont set these default values (e.g., just use `public string Name { get; set; }`), your code will still work, but the compiler will warn you that these properties might not be initialized before being used.
118+
```csharp
119+
namespace ImplementInterfaces
120+
{
121+
public class Student : IPerson
122+
{
123+
public string Name { get; set; } = string.Empty;
124+
public int Age { get; set; } = 0;
110125

111-
### Check your work: Implement the defined interface
126+
public void DisplayInfo()
127+
{
128+
Console.WriteLine($"Student Name: {Name}, Age: {Age}");
129+
}
130+
}
131+
}
132+
```
112133

134+
### Check your work
113135
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.
114136

137+
---
138+
115139
## Task 4: Create another class that implements different behavior
116140

117-
You will now create another class that implements the `IPerson` interface but with different behavior.
141+
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.
142+
143+
### Steps
118144

119145
1. In the `ImplementInterfaces` project, create a new file named `Teacher.cs`.
120146
1. Add the following code to implement the `IPerson` interface in the `Teacher` class:
121147

122148
```csharp
123-
public class Teacher : IPerson
149+
namespace ImplementInterfaces
124150
{
125-
public string Name { get; set; } = string.Empty;
126-
public int Age { get; set; } = 0;
127-
128-
public void DisplayInfo()
151+
public class Teacher : IPerson
129152
{
130-
Console.WriteLine($"Teacher Name: {Name}, Age: {Age}");
153+
public string Name { get; set; } = string.Empty;
154+
public int Age { get; set; } = 0;
155+
156+
public void DisplayInfo()
157+
{
158+
Console.WriteLine($"Teacher Name: {Name}, Age: {Age}");
159+
}
131160
}
132161
}
133162
```
134163

135-
In the teacher class the `DisplayInfo` method differs from the version in the student class with teh console output starting with "Teacher Name."
136-
137-
### Check your work: Create another class that implements the same interface
164+
### Check your work
138165

139166
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.
140167

168+
---
169+
141170
## Task 5: Demonstrate interface implementation
142171

143-
In this task, you will demonstrate the use of the interface by creating instances of the `Student` and `Teacher` classes and calling their methods.
172+
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.
173+
174+
### Steps
144175

145176
1. Open the `Program.cs` file in the `ImplementInterfaces` project.
146177
1. Replace the existing code with the following:
@@ -154,8 +185,8 @@ In this task, you will demonstrate the use of the interface by creating instance
154185
{
155186
static void Main(string[] args)
156187
{
157-
IPerson student = new Student { Name = "John Doe", Age = 20 };
158-
IPerson teacher = new Teacher { Name = "Jane Smith", Age = 35 };
188+
IPerson student = new Student { Name = "Eric Solomon", Age = 20 };
189+
IPerson teacher = new Teacher { Name = "Kayla Lewis", Age = 35 };
159190

160191
student.DisplayInfo();
161192
teacher.DisplayInfo();
@@ -164,33 +195,49 @@ In this task, you will demonstrate the use of the interface by creating instance
164195
}
165196
```
166197

167-
### Check your work: Demonstrate interface implementation
198+
### Check your work
168199

169-
You should see the `dotnet run` output displaying the information for both the student and the teacher, demonstrating the interface implementation.
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+
---
170209

171210
## Task 6: Test the implemented interfaces
172211

173-
Finally, you will test the implemented interfaces and their respective classes to ensure they function correctly.
212+
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.
213+
214+
### Steps
174215

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

178-
```bash
179-
dotnet run
180-
```
219+
```bash
220+
dotnet run
221+
```
181222

182223
1. Verify the output to ensure that the information for both the student and the teacher is displayed correctly.
183224

184-
### Check your work: Test the implemented interfaces
225+
### Check your work
185226

186227
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:
187228

188229
```console
189-
Student Name: John Doe, Age: 20
190-
Teacher Name: Jane Smith, Age: 35
230+
Student Name: Eric Solomon, Age: 20
231+
Teacher Name: Kayla Lewis, Age: 35
191232
```
192233

193-
In this exercise, you learned how to define and implement interfaces in C# to enforce consistent behavior across multiple classes. By creating a shared interface and implementing it in different classes, you ensured that each class adhered to a common contract while allowing for unique implementations. You also demonstrated the use of interfaces by creating instances of these classes and testing their functionality. This approach is a powerful way to design flexible and maintainable applications in object-oriented programming.
234+
---
235+
236+
## Key learnings
237+
238+
This exercise demonstrated how to use interfaces in C# to enforce consistent behavior across multiple classes. By implementing the `IPerson` interface in both `Student` and `Teacher` classes, you explored how interfaces enable polymorphism and allow different classes to share a common contract while maintaining unique behavior. This approach is essential for building scalable and maintainable applications.
239+
240+
---
194241

195242
## Clean up
196243

0 commit comments

Comments
 (0)