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();
}
}