0% found this document useful (0 votes)
11 views5 pages

C# Object-Oriented Programming Examples

The document contains C# code for two classes, Character and Student, within the 'darklter' namespace. The Character class includes methods for introducing the character and displaying their stats, while the Student class provides methods for introducing the student and evaluating their grades based on midterm and final scores. Each class is instantiated in a Program class to demonstrate their functionality.

Uploaded by

Lesunter KLter
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views5 pages

C# Object-Oriented Programming Examples

The document contains C# code for two classes, Character and Student, within the 'darklter' namespace. The Character class includes methods for introducing the character and displaying their stats, while the Student class provides methods for introducing the student and evaluating their grades based on midterm and final scores. Each class is instantiated in a Program class to demonstrate their functionality.

Uploaded by

Lesunter KLter
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

17# Object Methods Object Oriented Programming

/////////////////
//[Link]//
//\\\\\\\\\\\\\\

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace darklter
{
internal class Character
{
public string name, dialog;
public int hp, mp, lvl;

public Character(string name, string dialog, int hp, int mp, int lvl)
{
[Link] = name;
[Link] = dialog;
[Link] = hp;
[Link] = mp;
[Link] = lvl;
}

public void introduce()


{
[Link]("I'am " + name);
}

public void sayDialog()


{
[Link](name + " : " + dialog);
}
}
}

///////////////
//[Link]//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Character c = new Character("David", "Hello World", 100, 50, 1);
[Link]();
[Link]();
}
}
}
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

/////////////////
//[Link]//
//\\\\\\\\\\\\\\

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace darklter
{
internal class Character
{
public string name, dialog;
public int hp, mp, lvl;

public Character(string name, string dialog, int hp, int mp, int lvl)
{
[Link] = name;
[Link] = dialog;
[Link] = hp;
[Link] = mp;
[Link] = lvl;
}

public string introduce()


{
return "I'am " + name;
}

public string sayDialog()


{
return name + " : " + dialog;
}

public void checkStats()


{
[Link]("Name : " + name);
[Link]("Lvl : " + lvl);
[Link]("HP : " + hp);
[Link]("MP : " + mp);
}
}
}

///////////////
//[Link]//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Character c = new Character("David", "Hello World", 100, 50, 1);
[Link]();
}
}
}

|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|||||||||||||||||||||||||||||||||||||||||||

/////////////////
// [Link] //
//\\\\\\\\\\\\\\

using System;
using [Link];
using [Link];
using [Link];
using [Link];

namespace darklter
{
internal class Student
{
public string firstName, lastName;
public string course, year, section;
public float midtermGrade, finalGrade;

public Student(string firstName, string lastName, string course, string


year, string section, float midtermGrade, float finalGrade)
{
[Link] = firstName;
[Link] = lastName;
[Link] = course;
[Link] = year;
[Link] = section;
[Link] = midtermGrade;
[Link] = finalGrade;
}

public void introduceSelf()


{
[Link]("Full Name : " + firstName + " " + lastName);
[Link]();
[Link]("Course : " + course);
[Link]("Year : " + year);
[Link]("Section : " + section);
[Link]();
}

public void evaluateGrade()


{
float average = (midtermGrade + finalGrade) / 2;

[Link]("Average : " + average);

if (average > 100) [Link]("Invalid Grade");


else if (average >= 98) [Link]("With Highest Honors");
else if (average >= 95) [Link]("With High Honors");
else if (average >= 90) [Link]("With Honors");
else if (average >= 75) [Link]("Passed");
else [Link]("Failed");
}
}
}

///////////////
//[Link]//
//\\\\\\\\\\\\

using System;
namespace darklter
{
class Program
{
static void Main(string[] args)
{
Student s1 = new Student("David", "Sdpt", "BSIT", "2", "E", 90, 90);
[Link]();
[Link]();

[Link]();

Student s2 = new Student("Alenere", "Sdpt", "BSCS", "2", "A", 95, 98);


[Link]();
[Link]();
}
}
}

Common questions

Powered by AI

Using public fields in the 'Character' and 'Student' classes compromises encapsulation and data hiding principles by exposing internal state directly to any outside code. This can lead to unintended modifications and difficulties in maintaining class invariants. Ideally, fields should be private, with public properties providing controlled access through 'get' and 'set' methods, adding validation and logic as needed. This encapsulates data, promoting class stability and integrity .

The methods 'introduce' and 'sayDialog' in the 'Character' class contribute to encapsulation by allowing external classes or methods to interact with the 'Character' object's properties indirectly. They encapsulate the behavior of printing the character's name and dialog, respectively, without exposing the underlying data fields directly. This helps in maintaining control over modification of the object's state, adhering to one of the core principles of encapsulation in C# .

The constructor method in the 'Student' class facilitates initialization by setting the object properties for 'firstName', 'lastName', 'course', 'year', 'section', 'midtermGrade', and 'finalGrade' at the time of the object's creation. This ensures the object starts with a valid and intended state, reducing the likelihood of errors or undefined behavior later. Constructors enforce consistency and integrity by defining mandatory parameters and initializing default values when needed .

Not handling exceptions in methods could lead to ungraceful failures and crashes when invalid data is processed or unexpected states occur, especially in production environments. This affects user experience and software reliability. For example, accessing properties that aren't properly initialized could trigger exceptions. Implementing structured exception handling ensures robustness by catching and managing errors gracefully, allowing the program to continue operation or terminate safely while providing clear feedback .

Using classes to represent characters and students encapsulates data and behavior relevant to these entities, promoting reusability and modularity in C#. Classes such as 'Character' and 'Student' allow the representation of attributes (e.g., 'name', 'dialog', 'hp' for 'Character'; 'firstName', 'lastName' for 'Student') and behaviors (e.g., 'introduce()', 'sayDialog()' for 'Character'; 'introduceSelf()', 'evaluateGrade()' for 'Student') in a structured manner. This approach adheres to object-oriented principles, making the code more organized and maintainable .

Polymorphism could be applied to the classes by introducing a base class or interface representing a general entity, such as 'Person', with methods like 'introduce' defined. Both 'Character' and 'Student' classes could inherit from this base class or implement the interface. Each class would then provide its specific implementation of 'introduce', allowing different behaviors while treating objects of these classes as instances of the 'Person' type. This demonstrates polymorphism by allowing code to be written at an abstract level for flexibility and extensibility .

The 'checkStats' method can provide immediate, detailed feedback to the user on the character's current stats (level, HP, MP), which is critical for decision-making in a game's UI. By offering easy access to vital statistics, it helps players strategize their gameplay, adjust tactics, or prepare for challenges based on their character's strengths and weaknesses. This aligns with best practices in UI design that emphasize transparency and informed user actions .

The 'evaluateGrade' method in the 'Student' class includes data validation by checking if the calculated average exceeds 100, which is deemed an 'Invalid Grade'. This condition helps ensure that the inputs (midterm and final grades) produce a reasonable average. By setting this logical boundary, the method implicitly validates the data to maintain meaningful and valid outputs .

To enable reuse across different application types, the 'introduceSelf' and 'evaluateGrade' methods should be modified to return strings instead of directly outputting to the console. The responsibility for displaying the output can be shifted to the calling context, allowing these methods' results to be formatted or logged differently depending on the application's needs (e.g., GUI, web, mobile). This change promotes separation of concerns, ensuring logic is decoupled from presentation .

The use of 'Console.WriteLine' in the methods directly ties the business logic to a specific output mechanism (console), reducing flexibility in software design. This design choice makes it challenging to reuse these methods in contexts other than console applications, such as web or mobile applications where console output is irrelevant. To improve, methods could return strings which are then output using 'Console.WriteLine' in the main program or a dedicated UI layer, practicing separation of concerns and making the codebase more modular and adaptive .

You might also like