统计信息

  • 访问数:12110
  • 博客数:142
  • 建立时间:2008-01-05
  • 更新时间:2008-05-21
我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!

Java Programming Practical 5

2008-04-07 13:18:20

天气: 晴朗 心情: 高兴

Java Programming Practical 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.

 
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: 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.
 
 
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.
 

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 (Sorting three numbers)
Write the following method to display three numbers in increasing order:

public static void sort(double num1, double num2, double num3)


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)


7 (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



8 (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



9 (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).


10 (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.'
 

11 (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


12 (Computing series)
Write a method to compute the following series:


 
13 (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


14 (Prime number)
Write a program that meets the following requirements:
Declare a method to determine whether an integer is a prime number. Use the following method header:

public static boolean isPrime(int num)

An integer greater than 1 is a prime number if its only divisor is 1 or itself. For example, isPrime(11) returns true, and isPrime(9) returns false.

Use the isPrime method to find the first thousand prime numbers and display every ten prime numbers in a row, as follows:

2   3   5   7   11   13   17   19   23   29
31  37  41  43  47   53   59   61   67   71
73  79  83  89  97  ...
...


15 (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 0 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
 

16 (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


17 (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.
 
 
18 (Using trigonometric methods)
Print the following table to display the sin value and cos value of degrees from 0 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

 
19 (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 0. Write a program that generates ten random numbers between 0 and 1000, and computes the mean and standard deviations of these numbers using the following formula:


20 (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.


21 (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.  

22 (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.  


23 (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.

加入收藏 编辑 审核

TAG: computing

我来说两句

OPEN

Powered by X-Space 1.2 © 2001-2006 Comsenz Technology Ltd