🤔

# Practice

💡

Note: Assume all methods that you write are public static.

## Average

Code a method called average that takes two double parameters, a and b. The method should return the average of the two doubles. Test if your average method works in the main method by calling it at least 3 times with different arguments and printing the results. Bonus points if you write a JavaDoc comment for the average method.

## Temperature

Write a class that contains the following two methods:

Convert from Celsius to Fahrenheit `public static double celsiusToFahrenheit(double celsius)`

Convert from Fahrenheit to Celsius `public static double fahrenheitToCelsius(double fahrenheit)`

The formula for the conversion is: fahrenheit = (9.0 / 5) * celsius + 32 celsius = (5.0 / 9) * (fahrenheit – 32)

Write a test program that asks the user for a temperature in Fahrenheit and prints out the Celsius conversion. Then ask the user for a temperature in Celsius and print out the Fahrenheit conversion.

Adapted from Exercise 6.8, Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang

## Print Evens

Code a method printEvens that takes an integer parameter n. The method should print the first n even numbers to the console. (Note that 0 is considered an even number.) Test if your printEvens method works in the main method by calling it at least 3 times with different arguments. Bonus points if you write a JavaDoc comment for the printEvens method.

## Triangle

Create a class named Triangle that contains the following two methods:

Return true if the sum of any two sides is greater than the third side. (This is the definition of a valid triangle.) `public static boolean isValid(double side1, double side2, double side3)`

Return the area of the triangle. `public static double area(double side1, double side2, double side3)`

Write a test program that reads three sides for a triangle and computes the area if the sides form a valid triangle. Otherwise, it displays that the input is invalid. Use Heron's Formula to calculate the area of a triangle given 3 sides.

Adapted from Exercise 6.19, Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang

## Sum Digits

Write a method that computes the sum of the digits in an integer. Use the following method header:

`public static int sumDigits(long n)`

For example, sumDigits(234) returns 9 (because 2 + 3 + 4).

💡

Hint: Use the % operator to extract digits, and the / operator to remove the extracted digit. For instance, to extract 4 from 234, use 234 % 10 (= 4). To remove 4 from 234, use 234 / 10 (= 23).

Use a loop to repeatedly extract and remove the digit until all the digits are extracted.

Write a test program that prompts the user to enter an integer and displays the sum of all its digits.

Bonus points for writing a JavaDoc comment for the sumDigits method.

Adapted from Exercise 6.2, Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang

## Is Prime

Write a method called isPrime which returns true if the given integer is prime and false otherwise. Bonus points for writing JavaDoc comments for the method.

Test your isPrime method in the main method by calling it at least 3 times and printing the result.

Solution from Listing 6.7, Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang

Some websites impose certain rules for passwords. Write a method that checks whether a string is a valid password.

Suppose the password rules are as follows: ■ A password must have at least eight characters. ■ A password consists of only letters and digits. ■ A password must contain at least two digits.

Write a program that prompts the user to enter a password and displays Valid Password if the rules are followed or Invalid Password otherwise. To do this, use a method called isValid which returns true if the password is valid and false otherwise.

You may also find it helpful to write a method called isAlphanumeric which returns true if the given string is only made up of letters and digits.

You may also find it helpful to write a method called getNumberOfDigits which returns the number of digits in a given string.

You may also find it helpful to use the Character.isDigit(char) and Character.isDigit(char) methods. You can read more about them here.

If you don't want to use those methods, you can also use regular expressions (aka regex) and the matches(regex) String method. You can read about that here and here.