P. 1
C Sharp Exercises

# C Sharp Exercises

5.0

|Views: 5,862|Likes:

Published by: kumar.rajuu5519 on Apr 22, 2009

### Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

09/10/2015

pdf

text

original

# 1.

Write a program that randomly fills a 3 by 4 by 6 array, then prints the largest and smallest values in the array. 2. Sales tax in New York City is 8.25%. Write a program that accepts a price on the command line and prints out the appropriate tax and total purchase price. 3. Modify the sales tax program to accept an arbitrary number of prices, total them, calculate the sales tax and print the total amount. 4. Write a program that reads two numbers from the command line, the number of hours worked by an employee and their base pay rate. Then output the total pay due. 5. Modify the previous program to meet the Dept. of Labor's requirement for time and a half pay for hours over forty worked in a given week. 6. Add warning messages to the payroll program if the pay rate is less than the minimum wage (\$4.35 an hour as of mid-1996) or if the employee worked more than the number of hours in a week. 7. There are exactly 2.54 centimeters to an inch. Write a program that takes a number of inches from the command line and converts it to centimeters. 8. Write the inverse program that reads a number of centimeters from the command line and converts it to inches. 9. There are 454 grams in a pound and 1000 grams in a kilogram. Write programs that convert pounds to kilograms and kilograms to pounds. Read the number to be converted from the command line. Can you make this one program instead of two? 10. The equivalent resistance of resistors connected in series is calculated by adding the resistances of the individual resistors. Write a program that accepts a series of resistances from the command line and outputs the equivalent resistance. 11. The formula for resistors connected in parallel is a little more complex. Given two resistors with resistances R1 and R2 connected in parallel the equivalent resistance is given by the inverse of the sum of the inverses, i.e. * If there are more than two resistors you continue to invert the sum of their inverses; e.g. for four resistors you have: * Write a program that calculates the resistance of a a group of resistors arranged in parallel. 12. Write a ForexDeal class in CSharp with the following characteristics: Data members: PortfolioD PortfolioManager CustomerD) BuyiSell BuyingCurrency SellingCurrency Amount Operations: Appropriate constructors PromsRequest CancelRequest

13. Write a class with a set of static methods to perform foreign exchange conversions. For e2, it could do conversion from INR to USID (Indian Rupee to US Dollar). 14. Write a program to print the various data types in CSharp language. Also include a non-String object reference in the list. 15. Write a program to parse a bunch of user-specified command line options for running a program. 16. Write programs to test some fundamental language elements like conditionals, loops, labels etc. 17. Write a class Person with FirstName and LastName as data members. Override the toString() method to return the full name of the person. Define constructors to take appropriate parameters. This class does not have a default constructor. Use the this() to invoke other constructors. 18. Write a class SurveyOperator derived from Person. This class has the following additional members: Data members: NumberOfCalls NumberOfSuccessfulCalls TotalTime Available Operations: GetNumberOfCalls GetNumber-OfSuccessfulCalls SetAvailable MakeCall Define appropriate constructors for this class. Remember the fact that the base class Person does not have a default constructor and needs an explicit invocation using super(). 19. Write a program to study the constructor calling sequence in case of inheritance. 20. Write a program to print the list of system properties to the console. 21. Write a program to copy elements of one array into another 22. Write a program to reverse a string and test it. 23. Write a program to count the number of tokens, given a string and a separator. 24. Use the numeric wrapper class to do conversion between the types and also to convert strings to primitive data types. 25. Write a program to use some the APIs provided by the Math class. 26. Write a program that takes a filename and reads the contents of the file by lines. Also catch the required exceptions.

27. Write an exception class NotAvailableException that just contains a custom message printed.

28. Write a program that writes data into file. The data to be written are the CSharp data types and not just bytes.

29. Write a program to count the numbers of characters entered through stdin. The program exits upon entering Ctrl+Z.

30. Accept a filename. Write a program to print the properties of the file

31. Write a program to accept a filename and print the contents of the file along with the line numbers. It could be the source code itself.

32. We have two variables, int a=-7; ja int i=8; Find out the values of i and a after each assignment. Each line is to be processed distinct, a) does not affect to latter ones and so on... Nothing else is done between definition and assignment.
a) b) c) a = ++i; a = i++; a = --i;

d) a = i--; e) a += i++; f) a *= ++i + i++; g) a -= i++ - --i; h) a -= (i++ - --a) + (++a + i--); i) a += (i += ++a) - (a -= i--);

39. Construct a program Wc ("word count"), which counts number of chars, words and lines of the text file. Space is counted as a character. Empty rows are counted as lines. "Word" will represent a string. Note: Actually, the end of line is represented as a char too. , Unix/Linux uses one char, Windows two chars. Method Lue.rivi() ignores end of line! Also class Wc ignores it. 40. In programming it might be useful to have an array-like data structure, which is always ordered and have varying number number of cells. So let's construct one! Defining of the class AscendingListOfNumbers begins: public class AscendingListOfNumbers {

private int[] array; private int length; // Compare to exercise 1 / 1!

public AscendingListOfNumbers(int maxsize) { length = 0; if (maxsize > 0) array = new int[maxsize]; else // consider how the errors are handled!! }

... Program at least these accessors to the class add(int newInt) adds to AscendingListOfNumbers-instance a new integer whichIndex(int number) returns the index of the number (don't forget to use the binary search!) whichInt(int index) returns integer from the index removeInt(int index) removes integer from the given index length() number of integers in toString()-method creates an elegant String-representation of AscendingListOfNumbers-instance ... (more useful accessors) Construct error-handling procedures to accessors and define the type of accessors, that is, void, boolean, int, ... Example of using the class: ... AscendingListOfNumbers x = new AscendingListOfNumbers(1000); x.add(57); x.add(-32); x.add(60); x.add(12); System.out.println(x); // prints (-32, 12, 57, 60) System.out.println(x.length()); // prints 4

System.out.println(x.whichIndex(57); // prints 2 System.out.println(x.whichIndex(43); // prints -1

x.removeInt(2); System.out.println(x); // prints (-32, 12, 60) System.out.println(x.whichInt(1)); // prints 12 ...

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->