Share
Explore

F23 MO2033 G1 CSharp Assignment 1 Due September 29 Hand In Via GitHUB

ASSIGNMENT 1 - INTRODUCTION TO C# PROGRAMMING
Course: MO2033 - Object Oriented Programming
DUE September 29
Total Points: 100

To submit Assignment 1: Make a text file, named as StudentName_StudentID.txt
Introduction:
Welcome to your first assignment in Object Oriented Programming!
This assignment is designed to help you become familiar with the C# language, a powerful tool in the Microsoft family.
Data
Primitives
Object
Methods (all code must be wrapped up in a METHOD)

By completing this assignment, you'll gain hands-on experience and build a strong foundation in programming.
Instructions:

You will create five programs.

Start by setting up a GitHub repository named "MO2033_Assignment1".

As you complete each exercise, push your code to this repository.
Detailed instructions on setting up a GitHub repository and pushing code can be found
.

EXERCISES
Simple If-Then Statement (10 points)
Description: Create a program that asks the user for their age. If they're 18 or older, display "You are an adult". Otherwise, display "You are a minor".
For Loop Implementation (20 points)
Description: Create a program that prints numbers 1 through 10 in ascending order using a for loop.
While Loop and User Input (20 points)
Description: Create a program that continuously asks the user for their favorite color until they type "stop". For each color they enter, display "Your favorite color is [color]".
Method Calls (20 points)
Description: Design a program with a method named "GreetUser". This method should take in a user's name as a parameter and print out "Hello, [name]". Call this method three times in your main program with different names.
Guessing Game (30 points)
Description: Create an exciting guessing game. The program randomly selects a number between 1 and 100, and the user must guess the number. After each guess, inform the user whether their guess was too high, too low, or correct. Once the user guesses the number, congratulate them and display the number of attempts it took.
GRADING RUBRIC
Simple If-Then Statement:
Correct user prompt: 5 points
Correctly implemented if-then statement: 5 points
megaphone

Here's a sample solution code for the exercise:

using System;
namespace MO2033_Assignment1 { class Program { static void Main(string[] args) { // Numeric Comparison Console.WriteLine("Please enter your age:"); int age; // Ensure the input is a valid integer while (!int.TryParse(Console.ReadLine(), out age)) { Console.WriteLine("Invalid input. Please enter a valid number for your age:"); }
if (age >= 18) { Console.WriteLine("You are an adult."); } else { Console.WriteLine("You are a minor."); }
// String Comparison Console.WriteLine("\nPlease enter your favorite fruit (e.g., apple, banana):"); string favoriteFruit = Console.ReadLine().ToLower(); // Convert input to lowercase for easy comparison
if (favoriteFruit == "apple") { Console.WriteLine("You love apples!"); } else if (favoriteFruit == "banana") { Console.WriteLine("Bananas are great!"); } else { Console.WriteLine($"So you like {favoriteFruit}? Interesting choice!"); } } } }
In this sample solution:
- For the numeric comparison, we're using an `int` variable type for the age. We've also added an input validation check to ensure the user enters a valid number. - For the string comparison, we're using a `string` variable type for the favorite fruit. We convert the user's input to lowercase to ensure a case-insensitive comparison. This helps in handling cases where a user might enter "Apple", "apple", "APPLE", etc.
For Loop Implementation:
Correct use of for loop: 10 points
Correctly prints numbers 1-10: 10 points
megaphone

Here's a sample solution for the for-loop implementation using an array of fruits:


using System;

namespace MO2033_Assignment1
{
class Program
{
static void Main(string[] args)
{
string[] fruits = {"apple", "banana", "cherry", "date", "elderberry"};

Console.WriteLine("List of fruits:");
for (int i = 0; i < fruits.Length; i++)
{
Console.WriteLine($"{i + 1}. {fruits[i]}");
}

// Let's do something interesting: ask the user for their favorite fruit and inform if it's on the list
Console.WriteLine("\nWhich of the above fruits is your favorite?");
string userFruit = Console.ReadLine().ToLower();

bool isFruitFound = false;
for (int i = 0; i < fruits.Length; i++)
{
if (fruits[i] == userFruit)
{
isFruitFound = true;
break;
}
}

if (isFruitFound)
{
Console.WriteLine($"Great choice! {userFruit} is on the list.");
}
else
{
Console.WriteLine($"Sorry, {userFruit} is not on our list. You have a unique taste!");
}
}
}
}

In this solution:
We've first defined an array of fruits and used a for-loop to display them.
We then ask the user for their favorite fruit.
Using another for-loop, we search for the user's choice in our fruits array.
Depending on whether the fruit is found or not, we display an appropriate message to the user.
While Loop and User Input:
Continuously prompts user until "stop": 10 points
Correctly displays user's favorite color: 10 points
megaphone

Here's a fun example:

Scenario: A user is prompted to input characters, and the program will construct a word from the entered characters. The word-building process will stop when the user enters the exclamation mark (!).

using System;

namespace MO2033_Assignment1
{
class Program
{
static void Main(string[] args)
{
string constructedWord = "";
Console.WriteLine("Let's build a word! Enter characters one by one.");
Console.WriteLine("Enter '!' to stop and see the constructed word.");

while (true)
{
Console.WriteLine("Enter a character:");
char userInput = Console.ReadKey().KeyChar; // Read a single character from user input
if (userInput == '!')
{
Console.WriteLine("\nStopping word construction.");
break;
}
constructedWord += userInput; // Add the character to our word

Console.WriteLine($"\nCurrent word: {constructedWord}");
}

if (constructedWord.Length > 0)
{
Console.WriteLine($"Your constructed word is: {constructedWord}");
}
else
{
Console.WriteLine("You didn't add any characters to the word.");
}
}
}
}

In this example:
The user is repeatedly prompted to enter a character to add to the constructed word.
If the user enters an exclamation mark (!), the word construction stops, and the final word is displayed.
If the user doesn't add any characters and stops immediately, a message indicates that no characters were added.
Method Calls:
Correct implementation of GreetUser method: 10 points
Correctly calls method with different names: 10 points
megaphone

Here's a solution example for the Method Calls section:

csharpCopy code
using System;

namespace MO2033_Assignment1
{
class Calculator
{
static void Main(string[] args)
{
GreetUser("John"); // Test the greeting method with different names.
GreetUser("Alice");

Console.WriteLine("Welcome to the Calculator!");

while (true)
{
Console.WriteLine("\nSelect operation:\n1. Add\n2. Subtract\n3. Multiply\n4. Divide\n5. Exit");
int choice = Convert.ToInt32(Console.ReadLine());

if (choice == 5)
{
Console.WriteLine("Thank you for using the Calculator. Goodbye!");
break;
}

Console.Write("Enter first number: ");
double num1 = Convert.ToDouble(Console.ReadLine());

Console.Write("Enter second number: ");
double num2 = Convert.ToDouble(Console.ReadLine());

switch (choice)
{
case 1:
Console.WriteLine($"Result: {Add(num1, num2)}");
break;
case 2:
Console.WriteLine($"Result: {Subtract(num1, num2)}");
break;
case 3:
Console.WriteLine($"Result: {Multiply(num1, num2)}");
break;
case 4:
try
{
Console.WriteLine($"Result: {Divide(num1, num2)}");
}
catch (DivideByZeroException)
{
Console.WriteLine("Error: Division by zero is not allowed.");
}
break;
default:
Console.WriteLine("Invalid choice.");
break;
}
}
}

static void GreetUser(string name)
{
Console.WriteLine($"Hello, {name}!");
}

static double Add(double a, double b)
{
return a + b;
}

static double Subtract(double a, double b)
{
return a - b;
}

static double Multiply(double a, double b)
{
return a * b;
}

static double Divide(double a, double b)
{
if (b == 0) throw new DivideByZeroException();
return a / b;
}
}
}

In this example:
The GreetUser method greets a user based on the name passed to it.
Four methods (Add, Subtract, Multiply, and Divide) perform the calculator operations.
Exception handling is in place for division by zero using the DivideByZeroException.
Guessing Game:
Randomly selects a number between 1 and 100: 10 points
Correctly informs user if their guess is too high/low: 10 points
Correctly identifies when the user has guessed correctly: 5 points
Displays the number of attempts: 5 points
Total: 100 points

megaphone

Here's a solution for the guessing game using multiple classes and objects:

csharpCopy code
using System;

namespace GuessingGame
{
class Program
{
static void Main(string[] args)
{
Game game = new Game();
game.Start();
}
}

class Game
{
private int target;
private int attempts;

public Game()
{
Random rand = new Random();
this.target = rand.Next(1, 101);
this.attempts = 0;
}

public void Start()
{
Console.WriteLine("Welcome to the Guessing Game! Try to guess the number between 1 and 100.");
while (true)
{
Console.Write("Enter your guess: ");
int guess = Convert.ToInt32(Console.ReadLine());
this.attempts++;

GuessResult result = CheckGuess(guess);
if (result == GuessResult.Correct)
{
Console.WriteLine($"Congratulations! You've guessed the number {this.target} in {this.attempts} attempts.");
break;
}
else if (result == GuessResult.TooHigh)
{
Console.WriteLine("Your guess is too high. Try again!");
}
else
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.