Saturday, July 28, 2007

Sample Codes: Arrays and Collections

Here are the sample codes demonstrating the concepts discussed for arrays and collections

Wednesday, July 25, 2007

Programming Exercise (Ungraded): Inheritance

Implement the following class hierarchy:


Description of each class and its members:
  • Phone - an abstract class
    • Type - an abstract readonly property that identifies the type of phone. Implemented by subclasses
    • Number - an abstract property that sets and gets the phone number. Implemented by subclasses
    • Ring - an abstract method that will output the message "Ringing phone-type" when invoked. Implemented by subclasses
  • Telephone - an implementation of the Phone abstract class that represents all landline-based phones
    • Telephone - constructor that accepts a phone number as parameter
    • Type - returns "Telephone"
    • Number - gets or sets the phone number. This property should also validate the assigned phone number using this format: nnn-nnnn
    • Ring - displays the message "Ringing Telephone" when invoked
  • CellularPhone - an implementation of the Phone abstract class that represents all mobile-based phones
    • CellularPhone - constructor that accepts a phone number parameter
    • Type - returns "CellularPhone"
    • Number - gets or sets the phone number. This property should also validate the assigned phone number using this format: nnnn-nnnnnnn
    • Ring - displays the message "Ringing CellularPhone" when invoked
After implementing, you should be able to use your classes as follows:

class Program
{
static void Main(string[] args)
{
Telephone telephone = new Telephone("233-1234");
CellularPhone cellphone = new CellularPhone("0920-1234567");

Console.WriteLine(telephone.Type); // displays "Telephone"
Console.WriteLine(cellphone.Type); // displays "CellularPhone"

telephone.Number = "233-123"; // displays error
Console.WriteLine(telephone.Number); // displays "233-1234"
cellphone.Number = "0920-123x567"; // displays error
Console.WriteLine(cellphone.Number); // displays
"0920-1234567"
Phone phone = cellphone;
Console.WriteLine(phone.Number); // displays
// "0920-1234567"

phone = telephone;
phone.Ring(); // displays "Ringing Telephone"

Console.ReadLine();
}
}

Monday, July 23, 2007

Exam 1: Solution

This solution contains all implementation for the exam problems. Download it here.

Tuesday, July 17, 2007

Exam 1: C# Basics and Classes

Instruction: Zip all your project files for this exam and send them to my e-mail address. Name your zip file as section_name.zip. Project folders inside the zip file should be organized as follows:
/Prob1
/Prob2
/Prob3
/Prob4 (if present)

Ganbatte ne.

Problem #1 (10pts): Create a console application that accepts 3 float values and displays these numbers in ascending order.

Expected program output:
Enter float 1: 3.6
Enter float 2: 1.15
Enter float 3: 3.45

Order:
1.15
3.45
3.6

Problem #2 (15pts):
Create a console application that computes the Fahrenheit equivalent of a Celsius value and vice versa. The application should provide a menu that allows the user to choose the conversion to perform. In addition, the menu should also have an option to exit the application. In other words, the application will only terminate if the user chooses this option.

You can use these conversion formulas:

F = (9/5) C + 32
C = (F - 32)(5/9)

Expected program output:
Menu:
[C] Convert from Fahrenheit to Celsius
[F] Convert from Celsius to Fahrenheit
[X] Exit application
Choice: C

Enter temperature in Fahrenheit: 86
Temperature in Celsius: 30

Menu:
[C] Convert from Fahrenheit to Celsius
[F] Convert from Celsius to Fahrenheit
[X] Exit application
Choice: F

Enter temperature in Celsius:
Temperature in Fahrenheit:

Menu:
[C] Convert from Fahrenheit to Celsius
[F] Convert from Celsius to Fahrenheit
[X] Exit application
Choice: X

Problem #3 (25pts): Download this project and complete the Book class:

Define constructors that can accept the following parameters:
  • All parameters: book type, title, author, pages
  • Book type, title and author only
  • Title and author only
  • No parameters
Note: The book status should be initialized to Available upon creation.

Define the following properties:
  • Title - sets and gets the the book title
  • Author - sets and gets the author of the book.
    • The set implementation should not accept values with digits and should only update the field if the given value is valid
    • The get implementation return "Unknown" if it the author field has no value
  • Pages - sets and gets the number of pages in the book.
  • Type - sets and gets the book type
  • IsBorrowed (readonly) - returns a bool value indicating whether the book is borrowed or not
Define the following methods:
  • Borrow - sets the book status to borrowed. This method will return an error if the book's status is already borrowed
  • Return - sets the book status to available. This method should return an error if the book's status is already available.
  • ToString - returns a string representation of the object, formatted as follows:
    • "Type: booktype, Title: title, Author: author, Pages: pages"
After implementing these requirements, you should be able to compile and execute the project. You can use the defined Main method for testing your implementation.

Bonus (Extra 10pts):
Create a ConsoleUtil class that defines the following static methods:
  • ReadString - takes a string parameter that is used as a message prompt and returns a string entered by the user
  • WriteString - takes a string parameter and displays it
Sample usage:

class Program
{
static void Main(string[] args)
{
string name = ConsoleUtil.ReadString("Enter name:" ); // Prompts user to enter a name
ConsoleUtil.WriteString("Hello " + name);

Console.ReadLine();
}
}

Monday, July 16, 2007

Review Exercises

Problem #1: Data Types and Variables
Create a console application that accepts 4 int values from the user, and compute and output the following
  • The sum of the 4 integers
  • The product of the 4 integers
  • The difference between integers 1 and 2
  • The quotient between integers 3 and 4 expressed as a float type
Hint: Recall the Convert.Totype-name() or type-name.Parse() methods for converting a string to a numeric type. Also recall how to cast one data type to another to get the desired value.

Sample program output:

Enter integer 1: 10
Enter integer 2: 5
Enter integer 3: 8
Enter integer 4: 14

Sum: 37
Product: 5600
Difference (1 and 2): 5
Quotient (3 and 4): 0.5714286

Problem #2: Control Structures (Conditionals, Loops)
Create a console application that accepts a number of student grades to process. The application should loop through this number to get the grade for each student.

For each grade input, the application should check if the grade is within 60 to 100. If the grade is invalid, the application should display an error and ask for input again.

If the grade is valid, the application should check if the student passed or failed (failing grade is between 60 to 74) and should increment the appropriate count for passed or failed students (declared as variables). The application should also be able to compute the percentage of passed and failed students.

Sample program output:

Enter number of students to process: 10

Enter grade for student 1 (60-100): 80
Enter grade for student 2 (60-100): 10
Error: Grade must be within 60 to 100
Enter grade for student 2 (60-100): 74
...
Enter grade for student 10 (60-100): 90

Total no. of students: 10
No. of students who passed: 6
No. of students who failed: 4

Percentage of students who passed: 60%
Percentage of students who failed: 40%


Problem #3: Classes - Constructors, Fields, Properties and Methods
Create a class called MathUtil which implements the basic arithmetic functions defined in Problem #1. The choice on how to define the constructors, fields, properties or methods is totally up to you.

Slides: C# Basics and Classes

I updated the PowerPoint presentations used in our discussions. You can download them here:
Review these concepts for the prelim exam

Wednesday, July 11, 2007

Programming Exercise 4: Classes - Methods

Define a Deactivate() method in your existing User class. Calling this method will set its status field to Inactive.

This is the expected output when using your class:


class Program
{
static void Main(string[] args)
{
User user = new User("jtamad", "jtamad");
Console.WriteLine(user.IsActive); // Outputs "True" (default value)
user.Deactivate();
Console.WriteLine(user.IsActive); // Outputs "False"
Console.ReadLine();
}
}


Define another method, GetCopy(), that allows the class to return a copy of itself. Hint: Use the MemberwiseClone() method inherited from System.Object. You also need to perform a cast to the appropriate User object as this method returns a System.Object type.

After implementing this, you should be able to use your class as follows:


class Program
{
static void Main(string[] args)
{
User user1 = new User("jtamad", "jtamad");
Console.WriteLine(user1.UserID); // Outputs "jtamad"

User user2 = user1; // user2 references the
// same object as user1
user2.UserID = "juantamad"; // Modifies user1 also
// since user2 references
// the same object as user1
Console.WriteLine(user1.UserID); // Outputs "juantamad

user2 = user1.GetCopy(); // user2 has its own copy
// of a User object copied
// from user1
user2.UserID = "jtamad"; // Does not affect user1
// since this is a new object
Console.WriteLine(user1.UserID); // Outputs "juantamad"
Console.WriteLine(user2.UserID); // Outputs "jtamad"

Console.ReadLine();
}
}

Tuesday, July 10, 2007

Sample Code: Class Inheritance

This sample implements the inheritance hierarchy shown below

Download Code
Note: If clicking on this link doesn't work, copy the link location to a new browser window

Monday, July 9, 2007

Programming Exercise 3: Classes - Static Fields and Static Constructors

Define additional fields in your existing User class.
  • _dateCreated (static readonly) - Date of object creation
  • ClassesCreated (static) - Number of objects instantiated
Define a static constructor that initializes the ClassesCreated field to 0.

Modify your existing constructor to set values for the additional fields:
  • The _dateCreated field is set to the current date
  • The ClassesCreated field is incremented
Define a read-only property for the _dateCreated field

After implementing these, you should be able to use your class as follows:


class Program
{
static void Main(string[] args)
{
User user1 = new User("user1", "password1");
Console.WriteLine(user1.DateCreated); // Outputs the creation date for user1
Console.WriteLine(User.ClassesCreated); // Outputs "1" since this is the first object

User user2 = new User("user2", "password2");
Console.WriteLine(user2.DateCreated); // Outputs the creation date for user2
Console.WriteLine(User.ClassesCreated); // Outputs "2" since this is the second object
Console.ReadLine();
}
}

Thursday, July 5, 2007

Sample Codes: Classes

Here are a couple of sample codes available for download:
Note: If clicking on these links don't work, copy the link location to a new browser window

Wednesday, July 4, 2007

Programming Exercise 2: Classes - Fields, Properties and Constructors

Create a User class. This class should at least contain the following fields:
  • User ID - this is equivalent to a login name
  • Password
  • First Name
  • Last Name
  • E-mail Address
  • Status - valid values are 'Inactive', 'Active', 'Deleted'
Define constructors that accept the following parameter lists:
  • All parameters: User ID, Password, First Name, Last Name, E-mail Address
  • User ID, Password and E-mail Address
  • User ID and Password Only
** The status field should be initialized to 'Active' upon creation
** Constructors 2 and 3 should use a constructor initializer

Define read-only properties that return the following:
  • IsActive – whether or not the account's status is active
  • FullName – a concatenation of the account's first and last names
Define additional properties that behave as follows:
  • UserID
    • Set operation should ensure the user id does not start with a digit and should only update the user id field if the given value is valid
    • Get operation just returns the user id
  • Password
    • Set operation should validate the password length is within 8 to 20 characters and should only update the password field if the given value is valid
    • Get operation just returns the password
** You can reuse elements from your first programming exercise.
** Refer to the string class in the MSDN for methods in processing strings

After implementing these, you should be able to use your class as follows:


class Program
{
static void Main(string[] args)
{
User user = new User("jtamad", "jtamad",
"Juan", "Tamad", "jtamad@tapulan.com");

Console.WriteLine(user.IsActive); // Outputs "True"
Console.WriteLine(user.FullName); // Outputs "Juan Tamad"

user.UserID = "1jtamad"; // Outputs an error
user.Password = "1jta"; // Outputs an error
Console.WriteLine(user.UserID); // Outputs "jtamad", no changes
Console.WriteLine(user.Password); // Outputs "jtamad", no changes

user.UserID = "juantamad";
user.Password = "juantamad";
Console.WriteLine(user.UserID); // Outputs "juantamad"
Console.WriteLine(user.Password); // Outputs "juantamad"
}
}