Videos to Watch - Week 2 : Part 1

  1. Understanding Arrays
  2. Creating and Calling Methods
  3. While Iterations statement
  4. Working with Strings
  5. Working with Date and time
  6. Understanding Classes
  7. More About Classes and Methods

Problems - Week 2 : Part 1

For problems 1 to 7, you will write a method within a class called program and call those methods with predetermined values within the main function. e.g.

using System;

namespace MyProgram	
{
    public class Program	
    {	
        static void Main(string[] args)
        {
            string	value = MyMethod();//	You	will call the method in main
            Console.WriteLine(value);	
        }	

        //You will define the method in the class. 
        //For now, it doesn't matter if it is public or private

        private static string MyMethod()
        {
            return "Hello World!";
        }
    }	
}
                      

Problem 1

Write a method that takes 2 integers as its parameters (arguments) and returns (not print) the sum of them. Here is template:

public class Program	
{	
    static void Main(string[] args)
    {
        int number = Sum(2,3);	
        Console.WriteLine(number);	
        Console.ReadLine();
    }	

    public static int Sum(int a, int b)
    {
        //code goes here
    }
}	
                      

Problem 2

Write a method that takes 2 strings and returns a boolean value depending on whether they are equal or not.

Problem 3

Write a method that takes one string (someone's name), and appends it to the string: "Hello " and prints it. Example: if the name passed in is Coding Temple. It will print "Hello Coding Temple"

Problem 4

Write a method that takes an array and returns the sum.

Problem 5

Write a method that that takes 3 parameters (string, int, char), it will return a new string with new letter at position given by parameter.

ChangeChar("Hello", 1, 'a'); // will return "Hallo"

Problem 6

Write a method that will take two integers and will return the first integer to the power of the second integer.

Power(2,3); //this should return 8

Problem 7

Write a method that will take an array of integers and return the greatest value in that array.

int[] numbers = {24, 13, 27, 8};
Greatest(numbers); //this should return 27

Videos to Watch - Week 2 : Part 2

  1. Working with Date and time
  2. Understanding Classes
  3. More About Classes and Methods

Problems - Week 2 : Part 2

Problem 8

Create a class called Person. Give it the following properties:

  • Age (int)
  • Name (string)
  • Gender (string)

Give it a method called Birthday, which causes the Age to increase by 1.

In your Main method, create an instance of Person, assign a value to each of the properties, and invoke the Birthday method.

public class Program
{
    static void	Main(string[] args)
    {
        Person p1 = new	Person();
        p1.Name	= "Frank";
        p1.Age = 30;
        p1.Gender = "Male";
        p1.Birthday();
        Console.WriteLine(p1.Age);  //Should print out 31;
    }	
}
                      

Problem 9

Create a class called Vehicle. Give it the following properties:

  • Make (string)
  • Model (string)
  • Year (int)
  • Color (string)
  • Miles (decimal)

Give it the following methods:

Drive
Takes a parameter of type decimal, and adds that value to the Miles property. Returns void.
IsNew
Takes no parameters, and returns a True if the Year property is equal to the current year, false otherwise.

Create an instance of Vehicle in the Main method (in Program class) and assign a value to each of the properties of that instance a value. Invoke both methods to see the result

public class Program
{
    static void	Main(string[] args)
    {
        Vehicle v1 = new Vehicle();
        v1.Make	= "Ford";
        v1.Model = "T";
        v1.Color = "Brown";
        v1.Miles = 1000m;
        v1.Year = 1925;
        
        Console.WriteLine(v1.IsNew());  //Should print out false;
        v1.Drive(100);
        Console.WriteLine(v1.Miles); //Should print out 1100;
    }	
}
                    

Problem 10

Create a class called Point. Give it the following properties:

  • X (decimal)
  • Y (decimal)

Give it the following methods:

Point
Constructor which takes two arguments, x and y, of type decimal, and assigns them to the X and Y properties
Distance
non-static method which takes a Point as a parameter, and returns a decimal for the distance

public class Program
{
    static void	Main(string[] args)
    {
        Point a = new Point(22.5, -6.2);
        Point b = new Point(0.0, 10);
        decimal calculatedDistance = a.Distance(b);
        
        Console.WriteLine(calculatedDistance); 
    }	
}

public class Point
{
    public Point(decimal x, decimal y)
    {
        //Assign properties here
    }
    
    public decimal Distance(Point that)
    {
        //Use the distance formula here. 
        //You can access one point using "that" parameter, and the other point using the "this" object.
    }
}

                    

Problem 11

Create a class called Employee. Give it the following properties:

  • Name (string)
  • DateOfBirth (DateTime)
  • JobTitle (string)
  • Salary(decimal)
  • Count (int, mark this as static)

Create the following special methods, referred to as Overloaded Constructors

Employee(string name, DateTime dateOfBirth)
Assigns the name and dataOfBirth parameters to the properties, assigns JobTitle the value of "", and Salary the value of -1;
Employee(string name, DateTime dateOfBirth, string jobTitle, decimal salary)
Assigns all four parameter values to the appropriate properties
Employee()
Defaults the property values to "" for name and jobTitle, -1 for salary, and DateTime.Now for dateOfBirth.

Every time an Employee is created, increment the Employee.Count property by 1.

Create three instances of an employee object using the different constructors

public class Program
{
    static void	Main(string[] args)
    {
        Employee a = new Employee("Sally", new DateTime(1972, 3, 15), "CEO", 120000m);
        Employee b = new Employee("Bill", new DateTime(1981, 1, 16));
        Employee c = new Employee();
        Console.WriteLine(Employee.Count);  //Should print 3 
    }	
}



                    

Problem 12

We're going to expand on Problem 9. In addition to the Vehicle class you had previously created, create a new Class, Car, which Inherits from Vehicle, by defining the class as follows:

public class Car : Vehicle
{

}

In addition to the Car class, create a Truck class which also inherits from Vehicle. Create an instance of each new class inside your main method, and assign properties to the Make, Model, Year, Color, and Miles of each. Notice that you don't need to re-define these properties on Car and Truck. As these classes inherit from Vehicle, they already have all of the public methods and properties that vehicle has.

public class Program
{
    static void	Main(string[] args)
    {
        Car c = new Car();
        c.Color = "Yellow";
        
        Truck t = new Truck();
        t.Miles = 100;
    }	
}
                  

Problem 13

Create a class called Animal. Give it the following properties:

  • Name (string)
  • Dirty (bool)

Give it the following methods:

Animal(string name)
A constructor which takes the name and assigns it to the Name property.
Speak()
A method which returns a string

Create two additional classes, Cat and Dog which inherit from Animal. Notice that because Animal's constructor takes a parameter, Dog and Cat need constructors which can call the Animal constructor. e.g.

public class Dog
{
public Dog(string name) : base(name) {}
}

Create a static method in your program class called WashTheAnimal, which takes a parameter of type Animal. In your Main method, create an instance of Dog and Cat, and pass each of them to the WashTheAnimal method

public class Program
{
    static void	Main(string[] args)
    {
        Dog d = new Dog("Harry");
        d.Dirty = true;
        
        Cat c = new Cat("Larry");
        c.Dirty = true;
        
        WashTheAnimal(d);
        WashTheAnimal(c);
    }	
    
    private static void WashTheAnimal(Animal a)
    {
        a.Dirty = false;
        Console.WriteLine(a.Name + " is clean");
    }
}
                  

Notice that I don't need to write two versions of "WashTheAnimal" for cats and dogs. Since both classes inherit from Animal, instances of dogs and cats are valid arguments for methods which expect Animals. This technique is important, because it saves you from having to write duplicate code for each inheriting class. This concept is referred to as Polymorphism

Problem 14

Create a class called BankAccount, give it the following properties:

  • AccountNumber (string)
  • Owner (string)
  • Balance (decimal)

Give it the following Methods

BankAccount(string accountNumber, string owner, decimal balance)
A constructor which assigns each parameter to the appropriate property.
BankAccount(string accountNumber, string owner)
A constructor which assigns the accountNumber and owner properties, but default balance to 0
Withdraw(decimal amount)
Updates balance by subtracing the amount parameter to the balance.
Deposit(decimal amount)
Updates the balance by adding the amount parameter to the balance.
AddInterest()
This will be used to update the balance with a compound interest rate. In the "BankAccount" base class, we'll leave this method empty

We'll be using a new C# keyword, virtual, as a modifier on the Withdraw, Deposit, and AddInterest methods. The method signature will look like this:

public virtual void AddInterest()

Create two classes, CheckingAccount and SavingsAccount, which inherit from BankAccount. CheckingAccount and SavingsAccount need two constructors each, which in turn will call the BankAccount constructors, as we did in Problem 13.

While BankAccount provides an implementation for Deposit, Withdraw, and AddInterest, by marking them as virtual, you are allowed to override the default implementations in derived classes. Use the override keyword to do so:

public override void Deposit()

Override the withdraw method in both classes, and make sure that someone cannot withdraw more money than is present in their account. Futhermore, in the SavingsAccount, make that someone cannot withdraw more than 10% of their current balance.

if(amount > (this.Balance * 0.10))

In the SavingsAccount, override the AddInterest method so that the balance is increased by 0.2% when the method is called.

Continue to Week 3