c sharp exercises

8
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

Upload: kumarrajuu5519

Post on 10-Apr-2015

5.792 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: C Sharp Exercises

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

Page 2: C Sharp Exercises

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.

Page 3: C Sharp Exercises

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) a = ++i; b) a = i++; c) a = --i;

Page 4: C Sharp Exercises

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!!

}

Page 5: C Sharp Exercises

...

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

Page 6: C Sharp Exercises

x.removeInt(2);

System.out.println(x); // prints (-32, 12, 60)

System.out.println(x.whichInt(1)); // prints 12

...