šŸ¤”

11.11 Practice

Ā 

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)
Ā 
Bonus points for writing JavaDoc comments for both methods.
Ā 
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
Ā 

Check Password

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.
Ā 
Bonus points for writing JavaDoc comments for the methods.
Ā 
Adapted from Exercise 6.18, Introduction to Java Programming (Comprehensive), 10th ed. by Y. Daniel Liang
Ā 
āš–ļø
Copyright Ā© 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.