5.1* | ( Converting an uppercase letter to lowercase ) Write a method that converts an uppercase letter to a lowercase letter. Use the following method header: public static char upperCaseToLowerCase(char ch) For example, upperCaseToLowerCase('B') returns b . See Exercise 2.7 on how to convert an uppercase letter to lowercase. | ||
5.2* | ( Summing the digits in an integer ) 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 2 + 3 + 4 = 9. Hint
| ||
5.3* | ( Displaying an integer reversed ) Write the following method to display an integer in reverse order: public static void reverse( int number) For example, reverse(3456) displays 6543 . | ||
5.4** | ( Returning an integer reversed ) Write the following method to return an integer reversed: public static int reverse( int number) For example, reverse(3456) returns 6543 . | ||
5.5* | ( Sorting three numbers ) Write the following method to display three numbers in increasing order: public static void sort( double num1, double num2, double num3) | ||
5.6* | ( Displaying patterns ) Write a method to display a pattern as follows : 1 2 1 3 2 1 ... n n-1 ... 3 2 1 The method header is public static void displayPattern( int n) | ||
5.7* | ( Computing the future investment value ) Write a method that computes future investment value at a given interest rate for a specified number of years . The future investment is determined using the formula in Exercise 2.9. Use the following method header: public static double futureInvestmentValue( double investmentAmount, double monthlyInterestRate, int years) For example, futureInvestmentValue(10000, 0.05/12, 5) returns 12833.59 . Write a test program that prompts the user to enter the investment amount (e.g., 1000 ) and the interest rate (e.g., 9% ), and print a table that displays future value for the years from 1 to 30 , as shown below: The amount invested: 1000 Annual interest rate: 9% Years Future Value 1 1093.8 2 1196.41 ... 29 13467.25 30 14730.57 | ||
5.8 | ( Conversions between Celsius and Fahrenheit ) Write a class that contains the following two methods : /** Converts from Celsius to Fahrenheit */ public static double celsiusToFahrenheit( double celsius) /** Converts from Fahrenheit to Celsius */ public static double fahrenheitToCelsius( double fahrenheit) The formula for the conversion is: fahrenheit = (9.0 / 5) * celsius + 32 Write a test program that invokes these methods to display the following tables: Celsius Fahrenheit Fahrenheit Celsius 40.0 105.0 120.0 48.89 39.0 102.2 110.0 43.33 ... 32.0 89.6 40.0 5.44 31.0 87.8 30.0 -1.11 | ||
5.9 | ( Conversions between feet and meters ) Write a class that contains the following two methods: /** Converts from feet to meters */ public static double footToMeter( double foot) /** Converts from meters to feet */ public static double meterToFoot( double meter) The formula for the conversion is: meter = 0.305 * foot Write a test program that invokes these methods to display the following tables: Feet Meters Meters Feet 1.0 0.305 20.0 65.574 2.0 0.61 25.0 81.967 ... 9.0 2.745 60.0 195.721 10.0 3.05 65.0 213.115 | ||
| |||
5.10 | ( Computing GCD ) Write a method that returns the greatest common divisor between two positive integers, using the following header: public static int gcd( int m, int n) Write a test program that computes gcd(24, 16) and gcd(255, 25) . | ||
5.11 | ( Computing commissions ) Write a method that computes the commission, using the scheme in §4.8.2 "Example: Finding the Sales Amount." The header of the method is: public static double computeCommission( double salesAmount) Write a test program that displays the following table: SalesAmount Commission 10000 900.0 15000 1500.0 ... 95000 11100.0 100000 11700.0 | ||
5.12 | ( Displaying characters ) Write a method that prints characters using the following header: public static void printChars( char ch1, char ch2, int numberPerLine) This method prints the characters between ch1 and ch2 with the specified numbers per line. Write a test program that prints ten characters per line from ' 1 ' and ' Z .' | ||
5.13* | ( Summing series ) Write a method to compute the following series:
Write a test program that displays the following table: i m(i) 2 0.5 3 1.1667 ... 19 15.4523 20 16.4023 | ||
5.14* | ( Computing series ) Write a method to compute the following series:
| ||
5.15* | ( Printing a tax table ) Use the computeTax methods in Listing 5.5, ComputeTaxWithMethod.java, to write a program that prints a 2002 tax table for taxable income from $50,000 to $60,000 with intervals of $50 for all four statuses, as follows: Taxable Single Married Married Head of Income Joint Separate a House 50000 9846 7296 10398 8506 50050 9859 7309 10411 8519 ... 59950 12532 9982 13190 11192 60000 12546 9996 13205 11206 | ||
| |||
5.16* | ( Revising Listing 4.11, PrimeNumber.java ) Write a program that meets the following requirements:
|
5.17* | ( Displaying matrix of 0s and 1s ) Write a method that displays an n by n matrix using the following header: public static void printMatrix( int n) Each element is or 1 , which is generated randomly . Write a test program that prints a 3 by 3 matrix that may look like this: 0 1 0 0 0 0 1 1 1 |
5.18 | ( Using the Math.sqrt method ) Write a program that prints the following table using the sqrt method in the Math class. Number SquareRoot 0 0.0000 2 1.4142 ... 18 5.2426 20 5.4721 |
5.19* | ( The MyTriangle class ) Create a class named MyTriangle that contains the following two methods: /** Returns true if the sum of any two sides is * greater than the third side. */ public static boolean isValid( double side1, double side2, double side3) /** Returns the area of the triangle. */ public static double area( double side1, double side2, double side3) The formula for computing the area is
Write a test program that reads three sides for a triangle and computes the area if the input is valid. Otherwise, it displays that the input is invalid. |
| |
5.20 | ( Using trigonometric methods ) Print the following table to display the sin value and cos value of degrees from to 360 with increments of 10 degrees. Round the value to keep four digits after the decimal point. Degree Sin Cos 0 0.0 1.0 10 0.1736 0.9848 ... 350 -0.1736 0.9848 360 0.0 1.0 |
5.21** | ( Computing mean and standard deviation ) In business applications, you are often asked to compute the mean and standard deviation of data. The mean is simply the average of the numbers. The standard deviation is a statistic that tells you how tightly all the various data are clustered around the mean in a set of data. For example, what is the average age of the students in a class? How close are the ages? If all the students are the same age, the deviation is . Write a program that generates ten random numbers between and 1000 , and computes the mean and standard deviations of these numbers using the following formula:
|
5.22** | ( Approximating the square root ) Implement the sqrt method. The square root of a number, num , can be approximated by repeatedly performing a calculation using the following formula: nextGuess = (lastGuess + (num / lastGuess)) / 2 When nextGuess and lastGuess are almost identical, nextGuess is the approximated square root. The initial guess will be the starting value of lastGuess . If the difference between nextGuess and lastGuess is less than a very small number, such as 0.0001, you can claim that nextGuess is the approximated square root of num . |
5.23* | ( Generating random characters ) Use the methods in RandomCharacter in Listing 5.6 to print one hundred uppercase letters and then one hundred single digits, and print ten per line. |
5.24** | ( Displaying current date and time ) Listing 2.8, ShowCurrentTime.java, displays the current time. Improve this example to display the current date and time. The calendar example in §5.11, "Method Abstraction and Stepwise Refinement," should give you some ideas on how to find year, month, and day. |
5.25** | ( Converting milliseconds to hours, minutes, and seconds) Write a method that converts milliseconds to hours, minutes, and seconds using the following header: public static String convertMillis( long millis) The method returns a string as hours:minutes:seconds . For example, convertMillis(5500) returns a string 0:0:5 , convertMillis(100000) returns a string 0:1:40 , and convertMillis(555550000) returns a string 154:19:10 . |