Tillämpade Övningar 1
The main focus in this part is on the following concepts:
Loops (Task 1-4) and A
ays(Task 5, 6).
In all tasks make sure that you apply exception handling, when needed. For example, when
eading an integer from keyboard or dividing by a number.
Task 1
A special lottery consists of a ring with tickets that are (invisibly) numbered with 2, 4, 6, 8, 10,
12,…, 100. The drawn number determines the price of the ticket! If you draw the lottery with
number 26, in other words that particular lottery ticket costs SEK 26.
Write a program that summarizes and prints how much income you get if you sell all the tickets in
the ring.
Algorithm:
Task 2
Write a program that, using a loop, calculate the product of all integers between 1 and N (the
entered number). If you read 20 from the console window, the program should therefore calculate
and show the result of 1 × 2 × 3 × 4 ×… × 20.
Task 3
The application shall ask the user to input an integer, take the integer and save it in a variable
called radius, and then compute the volume of spheres with their radius from 1 to the input value.
Write a separate method to do the calculation and call your method from the Main method.
For example if the input value is 10, the output shall display sphere’s volume with radius from
1 to 10, like this:
“Sphere’s volume with radius 1 is ……”
“Sphere’s volume with radius 2 is ……”
…………..
“Sphere’s volume with radius 10 is ……”
Sphere’s volume =
Task 4
Write an application that prompts a student to enter his/her grade (‘A’ to ‘F’) on a course and
displays the grade’s definition in the output screen (Based on the following ECTS Definitions).
The student should be able to enter a grade many times until ‘G’ is entered to end the
program.
ECTS Definition
A Excellent – outstanding performance with only minor e
ors
B Very good – above the average standard but with some e
ors
C Good– generally sound work with a number of notable e
ors
D Satisfactory – fair but with significant shortcomings
E Sufficient – performance meets the minimum criteria
Fx Fail – some more work required before the credit can be awarded
F Fail - considerable further work is required
Hint: use a switch-statement embedded in a while-statement.
Task 5
Develop a Console application that displays ten student grades together with a histogram. Use an
a
ay to hold the grades. Use the random class to generate the grades.
Note: No user input is needed. The grades shall be created randomly. No need to create any
additional method. Everything can be done in Main method.
The output may look like:
Task 6
Your program contains an a
ay with ten random numbers between 0 and 19. Write methods
accordingly to the specification below. Make sure your methods work co
ectly by testing
them. Testing your methods is always easier if you know what the output should be, given a
specific input. A suggestion is to work with pen and paper first to figure out what the output
should be.
a) Write a method that takes an a
ay as parameter and returns the index of the smallest numbe
in the a
ay.
) Write a method that takes an a
ay as parameter and returns the index of the largest numbe
in the a
ay.
c) Write a method that switch position of two numbers in the a
ay. The method shall
have two input parameters, the indexes of the numbers to switch.