Programming Exercises

These exercises are designed to improve your skills as a programmer. They are meant to complement the following of our courses: Visual Basic .NET, Visual C# .NET and Java. Complete the exercise in the language you are studying. The first exercise should be fairly simple. But an exercise like exercise 10 will really challenge your programming skills!

 

Exercise 1 - Difficulty Level: Beginner

Set up a string variable to hold the letters of the alphabet, A to G:

C# NET
string notes = "ABCDEFG";

Java
String notes = "ABCDEFG";

VB NET
Dim notes As String = "ABCDEFG"

Use string methods to extract the key of C Major (CDEFGAB) from the variable called notes. Display your answer in a textbox or a listbox for VB NET and C#. For Java, you can output to a console window. When you have finished this exercise, you should have the following displayed:

The key of C is: CDEFGAB

Here's some useful links from our site to help you solve this problem:

C# NET: Substring

VB Net: Substring

Java: Substring

Answer to Exercise 1

Exercise 2 - Difficulty Level: Beginner

Set up an array to hold the following string values:

Beyonce (f)
David Bowie (m)
Elvis Costello (m)
Madonna (f)
Elton John (m)
Charles Aznavour (m)

Write a program to loop round and count how many are male vocalists and how many are female. Display your answer in a textbox, listbox or console.

Useful reference links if you get stuck:

C# NET: Loops, Arrays, Contains

VB NET: Loops, Arrays, InStr

Java: Loops, Arrays, indexOf

Answer to Exercise 2

Exercise 3 - Difficulty Level: Intermediate

Set up an array of the following musical instruments:

cello
guitar
violin
double bass

Loop round and remove the vowels. Display the new words in a textbox, listbox or console.

Useful reference links: Again, loops and arrays. But also the Replace string method. Hint for this exercise: you can have one loop inside another.

Answer to Exercise 3

 

Exercise 4 - Difficulty Level: Beginner

Write a program to count how many numbers between 1 and 1000 are divisible by 3 with no remainder. Display the answer in a message box for VB NET, C#, and Java.

Answer to Exercise 4

 

Exercise 5 - Difficulty Level: Beginner

For every number on a dartboard (1 to 20), work out the possible single, double and treble scores. (For example, the number 20 has the possible score 20, 40 and 60.)

Answer to Exercise 5

Exercise 6 - Difficulty Level: Intermediate to Advanced

Write code to create a checkerboard pattern with the words "black" and "white" standing in for colours. Display the new words in a textbox, listbox or console. When you have finshed this exercise, it should look something like this:

Checkerboard pattern

This is a tricky exercise, so here's some reference sections to help you out:

 

C# NET: Loops, Multi Dimensional Arrays, Contains

VB NET: Loops, Multi Dimensional Arrays, InStr

Java: Loops, Multi Dimensional Arrays, indexOf

 

Answer to Exercise 6

 

Exercise 7 - Difficulty Level: Intermediate

Write a program to work out if a series of 5 digits are consecutive numbers. To make this easier, assumes the digits are a string:

string numbers = "10-9-8-7-6";

Make sure your code works for the following sequence, as well:

string numbers = "1-2-3-4-5";

The two string variables above are from C# .NET. In VB NET, the variable declarations would be:

Dim numbers As String = "10-9-8-7-6"

Dim numbers As String = "1-2-3-4-5"

In Java they would be:

String numbers = "10-9-8-7-6";

String numbers = "1-2-3-4-5";

Answer to Exercise 7

 

Exercise 8 - Difficulty Level: Intermediate

Following on from the exercise above, work out if a series of 5 integers contain the same number. So the sequence 10-9-8-7-7 would yield the answer "Pair of numbers found".

Answer to Exercise 8

 

Exercise 9 - Difficulty Level: Easy to Intermediate

Print the word diagonally, diagonally from left to right, top to bottom. When you display your answer it should look something like this:

Answer to Exercise 9

Exercise 10 - Difficulty Level: Intermediate to Advanced

Set up a string variable to hold the following results:

C# NET
string results = "Manchester United 1 Chelsea 0, Arsenal 1 Manchester United 1, Manchester United 3 Fulham 1, Liverpool 2 Manchester United 1, Swansea 2 Manchester United 4";

Java
String results = "Manchester United 1 Chelsea 0, Arsenal 1 Manchester United 1, Manchester United 3 Fulham 1, Liverpool 2 Manchester United 1, Swansea 2 Manchester United 4";

VB NET
Dim results As String = "Manchester United 1 Chelsea 0, Arsenal 1 Manchester United 1, Manchester United 3 Fulham 1, Liverpool 2 Manchester United 1, Swansea 2 Manchester United 4"

Write a programme to work out how many wins Manchester United had, how many games they drew, and how many Manchester United lost.

Extend the programme to work out how many goals Manchester United scored and how many they conceded.

Suppose a win gains you 3 points, a draw 1 point, and a loss no points. Have your programme work out how many points in total Manchester United have acquired.

When you have finished this exercise, the output in your textbox, listbox or console should be as follows:

Answer to Exercise 10