06-Methods-Exercises
06-Methods-Exercises
1. Grades
Write a method that receives a grade between 2.00 and 6.00 and prints the corresponding grade in words
2.00 – 2.99 - "Fail"
3.00 – 3.49 - "Poor"
3.50 – 4.49 - "Good"
4.50 – 5.49 - "Very good"
5.50 – 6.00 - "Excellent"
Examples
Input Output
3.33 Poor
4.50 Very
good
2.99 Fail
Hints
1. Read the grade from the console and pass it to a method
2. Then create the method and make the if statements for each case
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
3. Calculations
Write a program that receives a string on the first line (“add”, “multiply”, “subtract” or “divide”) and on
the next two lines receives two numbers. Create four methods (for each calculation) and invoke the right one
depending on the command. The method should also print the result (needs to be void).
Example
Input Output
subtract 1
5
4
divide 2
8
4
Hints
1. Read the command on the first line and the two numbers, and then make an if/switch statement for each
type of calculation:
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Examples
Input Output
3 1
1 2
1 2 3
1 2
1
4 1
1 2
1 2 3
1 2 3 4
1 2 3
1 2
1
Hints
1. After you read the input
2. Start by creating a method for printing a single line from a given start to a given end. Choose a meaningful
name for it, describing its purpose:
3. Create another method for printing the whole triangle. Again choose a meaningful name for it, describing
its purpose.
4. Think how you can use the PrintLine() method to solve the problem
5. After you spent some time thinking, you should have come to the conclusion that you will need two loops
6. In the first loop you can print the first half of the triangle:
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Examples
Input Output
3 12
4
6 12
2
Hints
1. Read the input
2. Create a method, but this time instead of typing "static void" before its name, type "static double" as this
will make it to return a value of type double:
3. Invoke the method in the main and save the return value in a new variable:
6. Repeat String
Write a method that receives a string and a repeat count n (integer). The method should return a new string (the
old one repeated n times).
Example
Input Output
abc abcabcabc
3
String StringStrin
g
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Hints
1. Firstly read the string and the repeat count n
2. Then create the method and pass it the variables
7. Math Power
Create a method that calculates and returns the value of a number raised to a given power:
Examples
Input Output
2 256
8
3 81
4
Hints
1. As usual, read the input
2. Create a method which will have two parameters - the number and the power, and will return a result of
type double:
Examples
Input Output
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Examples
Input Output Comment
-12345 54 Evens: 2 4
Odds: 1 3
5
Even sum:
6
Odd sum: 9
6 * 9 = 54
Example
Input Output
5 25
*
5
4 12
+
8
Hint
1. Read the input and create a method that returns a double (the result of the operation)
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Examples
Input Output
2 2
5
3
600 123
342
123
25 4
21
4
Examples
Input Outpu
t
SoftUn 3
i
Cats 1
JS 0
Examples
Input Output
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
# $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9
:
Examples
Input Output
Hints
Write a method for each rule.
Examples
Input Output
aString r
someText eT
3245 24
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Examples
Input Output Input Output
5 60.00 6 360.0
2 2 0
Examples
Input Output Input Output
Examples
Input Output Input Output
50 17 100 17
35 35
53
71
79
97
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Input
The input data should be read from the console.
On the first line, the initial array is received as a line of integers, separated by a single space
On the next lines, until the command “end” is received, you will receive the array manipulation commands
The input data will always be valid and in the format described. There is no need to check it explicitly.
Output
The output should be printed on the console.
On a separate line, print the output of the corresponding command
On the last line, print the final array in square brackets with its elements separated by a comma and a space
See the examples below to get a better understanding of your task
Constraints
The number of input lines will be in the range [2 … 50].
The array elements will be integers in the range [0 … 1000].
The number of elements will be in the range [1 .. 50]
The split index will be an integer in the range [-231 … 231 – 1]
first/last count will be an integer in the range [1 … 231 – 1]
There will not be redundant whitespace anywhere in the input
Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.
Examples
Input Output
1 3 5 7 9 2
exchange 1 No matches
max odd [5, 7]
min even []
first 2 odd [3, 5, 7, 9, 1]
last 2 even
exchange 3
end
Input Output
1 10 100 1000 3
max even Invalid count
first 5 even Invalid index
exchange 10 0
min odd 2
exchange 0 0
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.
Input Output
© SoftUni – https://softuni.org. Copyrighted document. Unauthorized copy, reproduction or use is not permitted.