Delphi Programming Exercises

  1. (Type integer, arithmetic operators, integer operators, TEdit, TLabel, TButton, TEdit.Text, StrToInt, IntToStr). Write a program which reads two integers and then displays their sum. Solution
     
  2. (Type integer, arithmetic operators, integer operators, TEdit, TLabel, TButton, TEdit.Text, StrToInt, IntToStr). Extend exercise 1 so that the product and quotient (first div second) are displayed. Solution
     
  3. (Constants, FloatToStr) Write a program which takes the radius of a circle as input and returns its area and circumference (pi = 3.14159). Solution
     
  4. (FloatToStrF) Write a program that inputs three real numbers, computes their sum and outputs the result to a specified number of decimal places. Solution
     
  5. (Constants) Write a program which sets up constants for base rate and higher rate tax (try 20% and 40%) and the tax thresholds for basic and higher rates (try 5,000 and 40,000). After inputting the gross pay the program should return the net pay according to the thresholds and rates. Provide a suitable interface for the user.
     
  6. (If, else, and, showmessage) Amend program 1 so that only three-digit positive integers are accepted.
     
  7. (If, Repeat..until, and) Amend program 2 so that only values in the range -100 to +100 are accepted.
     
  8. (If..then, showmessage) Write a program which accepts a temperature value and then offers the user a choice between converting from C to F or from F to C. Provide a suitable interface for the user.
     
  9. (if, repeat..until, global variables, onCreate) Write a program that accepts a sequence of numbers and calculates their sum and average.  Provide a suitable interface for the user.
     
  10. (for, listbox) Write a program that outputs triangle numbers - 1, 3, 6, 10, 15, etc.
     
  11. (for, listbox, memo) Write a program that outputs the multiplication table of the user's choice. Each line should read, for example, "3 x 13 = 39", where 13 was the number entered by the user.
     
  12. (for loop, list box) Write a program that outputs the Fibonacci sequence up to a user-supplied value and displays the 'golden ratio' each time. The output should be displayed as pairs of Fibonacci numbers followed by the golden ratio between them, eg f1, f2, f1/f2; f2, f3, f2/f3, etc.
     
  13. (case, ComboBox) Extend the first program so that the user can choose the operation on the two variables from a drop-down list (ComboBox).
     
  14. (case, ComboBox, onChange) Write a program that presents a list of the planets in a ComboBox and displays a brief message in response to a selection.
     
  15. (Image control, multiple forms and units) Write a program that includes a start form with an image and buttons that link to a second form. Provide a link from the second form back to the first.
     
  16. (screen.fonts, listbox, memo) Write a program that provides a list of fonts from the current system list, displays each font in its actual face and applies the font to text in a memo box.
     
  17. (Array) Write a program that fills an array with a sequence of numbers such as a multiplication table. The user should then be able to specify a value from the table by its index value. For example, if the table contains 5, 10, 15...50, the user should be able to input 6 and obtain the result 30 from the table (6x5).
     
  18. (Array) Write a program which reverses a sequence of numbers. Solution
     
  19. (Array) Write a program that creates an array of numbers and then allows the user to see if particular values are in it (linear search).
     
  20. (sort) Write a program that defines a list of numbers in random order and outputs them in ascending order.
     
  21. (spinner, printing) Write a program that presents a menu of items in a fast food restaurant, provides spinners for input of the number required and produces a total bill for the customer. Print a receipt. (Chapter 4)
     
  22. (record) Write a program which that defines a record for pupils with fields for name, form and year of entry. Input data from edit boxes into the fields of the record.
     
  23. (record, array) Write a program that reads a series of pupil records into an array. Add a routine to sort the array by a specific field.
     
  24. (record, file) Write a program that reads a record pupil and saves it into a file.
     
  25. (Finite state machines) Imagine a door that opens and closes automatically. There is a sensor outside the building to detect when someone is near the door and one inside with a similar purpose. When the door is closed and someone approaches from either inside or outside the door will open. When the door is open and there is no one on either sensor then it will close. There are six further states (eight in total) based on the combinations of the state of the door and the sensors. List all possible states for the door. Now create a simulation of the door using suitable techniques. A console program would start in state closed and change according to the input. A GUI program would be preferable as you could model the sensors with buttons and the door itself could be drawn in an animated graphic.
     
  26. (Finite state machines) Write a program that accepts strings in alphabet Σ={a,b} of the form a, b, aa, bb, bab, aba, but not ab, ba or bbba, etc. You will need to draw the fsm for this first. The machine begins in state S when a letter is read. If the symbol is a the machine proceeds to acceptance state q1, where it may read further characters from the alphabet: if it reads a it stays in state q1; if it reads b it moves to non-acceptance state q2. The pattern for reading symbol b is the same as for a. Provide a suitable interface for the user as the letters are read.
     
  27. Write a program that allows a person to play noughts and crosses against the computer.

     

OOP Problems

Answer these problems in the Delphi GUI. Use your own classes, properties and methods to define the objects, data items and methods needed in each solution.

  1. Write a program in OOP style which accepts a temperature value for water and then offers the user a choice between converting from C to F or from F to C. Solution

  2. Write a program in OOP style that accepts input for number of hours worked in a week and hourly rate of pay. Hours worked over 40 should be paid at 1.5 times the base rate. Solution

  3. Write a program in OOP style which sets up a constant for base rate tax, inputs the gross pay and returns the net pay..

  4. Write a program in OOP style which accepts a sequence of numbers and calculates their sum and mean average. Ask the user to enter the number of values in the sequence.

  5. Write a program in OOP style that takes a string such as a name and encrypts it by finding the numerical value of each character and adding 1 to this (or any other value). The code should convert the altered numeric value to the corresponding character and build a copy of the encrypted string.

  6. Write a program in OOP style which includes one function to calculate a Fibonacci number from the previous two numbers and another function to calculate the golden ratio from two Fibonacci numbers. The program should take a user-supplied value and display the Fibonacci sequence up to that value, along with the golden ratio for the current value divided by the previous one.

  7. Write a program in OOP style which takes a date as three numbers and returns output in the form number/string/number eg 12th November 1992. Include error checking on the range of acceptable numbers for the day and month, and check that the numbers entered for the day are valid for the month (eg 30/2/99, or 31/6/02).

  8. Write a program in OOP style which inputs some strings (such as names) and outputs them in the same sequence, in reverse and in ascending order.

  9. Write a program in OOP style which accepts an integer from the user and outputs the equivalent in Roman numerals. Your program should allow input of numbers up to 3,999.

  10. Write a program in OOP style that plays noughts and crosses with the user.

  11. Write a program in OOP style that calculates the values of m ^ n mod p for n = 1, 2, 4, 8, 16... Store the results in an array. e.g. 18 ^ 1 mod 55, 18^2 mod 55, 18^4 mod 55, 18^8 mod 55. Hence, using the previous program about sums of powers of 2, find a way to calculate e.g. 18^7 mod 55, which is 18^1 mod 55 x 18^2 mod 55 x 18^4 mod 55.

  12. Write a program in OOP style that inputs 9 digits of an ISBN code and calculates the check digit. This is a good point at which to include a try..except structure to check that each character entered is a digit.

  13. Write a program in OOP style which inputs a string (such as a name) and outputs it in normal and reverse. Add a routine to test whether the string is a palindrome ('madam', etc.

  14. Write a program in OOP style that sets up a base class 'employee' with properties and methods for calculating pay and tax. Set up two sub-classes to store information on employees who are paid by monthly salary on the one hand and hours per week on the other. The amount of tax paid should be based on approximate national rates: the first £8000 is tax free, the next £30000 is at 20% and earning over £38,000 are taxed at 40%.

Travel tickets

Saving records

Loops and arrays

Back