Exercises involving methods


Exercises about methos, control statements, loopings and flux control structuresFeel free to practice your creativity, using what ever you learned you want (while, for, do ... while) commands (switch, break and continue), if else conditionals tests or even nothing ;)



However, as we are in the methods section, it is important to train and use methods, the return statement, and parameters and arguments on all issues below:







0. Create a method that takes a value and indicate whether it is positive or negative by a return to boolean.
Declare as: boolean isPositive (float a)

1. Create a method that takes a value and tell whether it is null or not.
Declare as: boolean isZero (float a)

2. Create a method that takes three values, 'a', 'b' and 'c', which are the coefficients of a quadratic equation and returns the value of the delta, which is given by 'b ² - 4ac'

3. Using the 3 methods above, create an application that calculates the roots of an equation of second degree:
ax ² + bx + c = 0
For it exists, the coefficient 'a' must be nonzero.
If delta is greater than or equal to zero, the roots are real. If the delta is negative, the real will be complex
and form: x + iy

4. Create a method that takes two numbers and returns the largest value.

5. Create a method that takes two numbers and returns the lowest value.

6. Create a method that takes three numbers and returns the largest value, use the Math.max().

7. Create a method that takes 3 numbers and return the lowest value, use the Math.min ().

8. As a method called rollDice() which returns through draw a number from 1 to 6.

9. Use the method of question and toss the dice past 1 million times. Count how many times each number came out.
The probability worked? That is, the percentage of numbers was about the same?

10. Create an application for conversion between Fahrenheit and Celsius temperatures.
First the user must choose whether to enter the temperature in Celsius or Fahrenheit, chosen after the conversion is performed through a command switch.
If C is the temperature in Celsius and F farenheit, the conversion formulas are:
C = 5. (F-32) / 9
F = (9.C / 5) + 32

11. A teacher, very cool, did 3 tests during a semester but will only take into account the two highest grades to average.
Write a Java application that asks the value of 3 notes, show how it would mean to these 3 tests, average with the 2 highest grades, as well as his highest score and your lowest note. These averages must be calculated using the same method, because you're a Java programmer and will not stand idly creating methods.
Create a method that receives all 3 grades and returns the largest. And another that returns the smallest.

Challenge: Find all prime numbers up to 1000
Prime number is one that is divisible only by 1 and itself.

Challenge: Write a Java program that takes two integers and returns the GCD, greatest common divisor.

Challenge: Find all perfect numbers up to 1000.
Perfect number is one that is the sum of its factors. For example, 6 is divisible by 1, 2 and 3 while 6 = 1 + 2 + 3.

Challenge: Create a Java program that receives a number and print it in reverse order.
That is, if the whole received 123, should print the integer 321.

No comments: