Showing posts with label basics. Show all posts
Showing posts with label basics. Show all posts

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

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