1 package chapter5方法; 2 //求最大公约数; 3 public class GreatestCommonDivisor { 4 public static void main(String[] args){ 5 java.util.Scanner input = new java.util.Scanner(System.in); 6 7 System.out.print("Enter first integer: "); 8 int n1 = input.nextInt(); 9 System.out.print("Enter second integer: "); 10 int n2 = input.nextInt(); 11 System.out.println("The greatest common divisor for " + n1 + 12 " and " + n2 + " is " + gcd(n1, n2)); 13 } 14 15 public static int gcd(int n1, int n2){ 16 int gcd = 1; 17 int k = 2; 18 while(k <= n1 && k <= n2){ 19 if (n1 % k == 0 && n2 % k ==0) //能同时被n1和n2整除; 20 gcd = k; 21 k++; 22 } 23 return gcd; 24 } 25 }
1 package chapter5方法; 2 3 public class Increment { 4 public static void main(String[] args){ 5 int x = 1; 6 System.out.println("Befre the call, x is " + x); 7 increment(x); 8 System.out.println("After the call, x is " + x); 9 } 10 11 public static void increment(int n) { 12 n++; 13 System.out.println("n inside the method is " + n); 14 } 15 16 }
1 package chapter5方法; 2 3 public class PrimeNumberMethod { 4 public static void main(String[] args){ 5 System.out.println("The first 50 prime numbers are \n"); 6 printPrimeNumbers(50); 7 } 8 9 public static void printPrimeNumbers(int numberOfPrimes){ 10 final int NUMBER_OF_PRIMES_PER_LINE = 10; 11 int count = 0; 12 int number = 2; 13 14 while (count < numberOfPrimes) { 15 if (isPrime(number)) { 16 count++; 17 if (count % NUMBER_OF_PRIMES_PER_LINE == 0) { 18 System.out.printf("%-5s\n", number); 19 } 20 else 21 System.out.printf("%-5s", number); 22 } 23 number++; 24 } 25 } 26 27 public static boolean isPrime(int number){ 28 for(int divisor = 2; divisor <= number /2; divisor++) { 29 if (number % divisor == 0){ 30 return false; 31 } 32 } 33 return true; 34 } 35 36 }
1 package chapter5方法; 2 3 public class TestMax { 4 public static void main(String[] args){ 5 int i = 5; 6 int j = 2; 7 int k = max(i, j); 8 System.out.println("The maximum between " + i + 9 " and " + j + " is " + k); 10 } 11 public static int max(int num1, int num2){ 12 int result; 13 if (num1 > num2) 14 result = num1; 15 else 16 result = num2; 17 return result; 18 } 19 }
1 package chapter5方法; 2 3 public class TestPassByValue { 4 public static void main(String[] args){ 5 int num1 = 1; 6 int num2 = 2; 7 System.out.println("Before invoking the swap method, num1 is " + 8 num1 + " and num2 is " + num2); 9 swap(num1, num2); 10 System.out.println("After invoking the swap method, num1 is " + 11 num1 + " and num2 is " + num2); 12 } 13 14 public static void swap(int n1, int n2){ 15 System.out.println("\tInside the swap method"); 16 System.out.println("\t\tBefore swapping n1 is " + 17 n1 + " n2 is " + n2); 18 19 int temp = n1; 20 n1 = n2; 21 n2 = temp; 22 23 System.out.println("\t\tAfter swapping n1 is " + 24 n1 + " n2 is " + n2); 25 } 26 }
1 package chapter5方法; 2 3 public class TestReturnGradeMethod { 4 public static void main(String[]args){ 5 System.out.print("The grade is " + getGrade(78.6)); 6 System.out.print("\nThe grade is " + getGrade(59.8)); 7 } 8 9 public static char getGrade(double score) { 10 if (score >= 90.0) 11 return 'A'; 12 else if (score >= 80.0) 13 return 'B'; 14 else if (score >= 70.0) 15 return 'C'; 16 else if (score >= 60.0) 17 return 'D'; 18 else 19 return 'F'; 20 } 21 22 }
1 package chapter5方法; 2 3 public class TestViodMethod { 4 public static void main(String[] args){ 5 System.out.print("The grade is "); 6 printGrade(78.5); 7 8 System.out.print("The grade is "); 9 printGrade(59.7); 10 } 11 12 public static void printGrade(double score) { 13 if (score >= 90.0){ 14 System.out.println('A'); 15 } 16 else if (score >= 80.0){ 17 System.out.println('B'); 18 } 19 else if (score >= 70.0){ 20 System.out.println('C'); 21 } 22 else if (score >= 60.0){ 23 System.out.println('D'); 24 } 25 else{ 26 System.out.println('F'); 27 } 28 } 29 }
1 package chapter5方法; 2 import java.util.*;//十进制转换为十六进制! 3 public class Decimal2HexConversion { 4 public static void main(String[] args){ 5 Scanner input = new Scanner(System.in); 6 System.out.print("Enter a decimal number: "); 7 int decimal = input.nextInt(); 8 9 System.out.println("The hex number for decimal " + 10 decimal + " is " + decimalToHex(decimal)); 11 } 12 13 public static String decimalToHex(int decimal) { 14 String hex = ""; 15 16 while (decimal != 0) { 17 int hexValue = decimal % 16; 18 hex = toHexChar(hexValue) + hex; 19 decimal = decimal / 16; 20 } 21 return hex; 22 } 23 24 public static char toHexChar(int hexValue) { 25 if (hexValue <= 9 && hexValue >= 0) 26 return (char)(hexValue + '0'); 27 else 28 return (char)(hexValue - 10 + 'A'); 29 } 30 }
1 package chapter5方法; 2 import java.util.*; 3 public class PrintCalendar { 4 5 public static void main(String[] args) { 6 Scanner input = new Scanner(System.in); 7 System.out.print("Eneter full year (e.g.,2001): "); 8 int year = input.nextInt(); 9 System.out.print("Eneter nonth in number between 1 an 12: "); 10 int month = input.nextInt(); 11 12 printMonth(year, month); 13 } 14 15 public static void printMonth(int year, int month){ 16 printMonthTitle(year, month); 17 printMonthBody(year, month); 18 } 19 20 public static void printMonthTitle(int year, int month){ 21 System.out.println(" " + getMonthName(month) + " " + year); 22 System.out.println("----------------------------"); 23 System.out.println(" Sun Mon Tue Wed Thu Fri Sat"); 24 } 25 26 public static String getMonthName(int month){ 27 String monthName = ""; 28 switch (month) { 29 case 1:monthName = "January"; break; 30 case 2:monthName = "February"; break; 31 case 3:monthName = "March"; break; 32 case 4:monthName = "April"; break; 33 case 5:monthName = "May"; break; 34 case 6:monthName = "June"; break; 35 case 7:monthName = "July"; break; 36 case 8:monthName = "August"; break; 37 case 9:monthName = "Septemper"; break; 38 case 10:monthName = "October";break; 39 case 11:monthName = "November"; break; 40 case 12:monthName = "December"; 41 } 42 return monthName; 43 } 44 45 public static void printMonthBody(int year, int month){ 46 int startDay = getStartDay(year, month); 47 int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month); 48 49 int i = 0; 50 for (i = 0; i < startDay; i++) 51 System.out.print(" "); 52 53 for (i = 1; i <= numberOfDaysInMonth; i++){ 54 System.out.printf("%4d", i); 55 if ((i + startDay) % 7 == 0) 56 System.out.println(); 57 } 58 System.out.println(); 59 } 60 61 public static int getStartDay(int year, int month){ 62 final int START_DAY_FOR_JAN_1_1800 = 3; 63 int totalNumberOfDays = getTotalNumberOfDays(year, month); 64 return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7; 65 } 66 67 public static int getTotalNumberOfDays(int year, int month){ 68 int total = 0; 69 for (int i = 1800; i < year; i++) 70 if(isLeapYear(i)) 71 total = total + 366; 72 else 73 total = total + 365; 74 75 for (int i = 1; i < month; i++) 76 total = total + getNumberOfDaysInMonth(year, i); 77 78 return total; 79 } 80 81 public static int getNumberOfDaysInMonth(int year, int month){ 82 if (month == 1 || month == 3 || month == 5 || month == 7 || 83 month == 8 || month == 10 || month == 12) 84 return 31; 85 if (month == 4 || month == 6 ||month == 9 || month == 11) 86 return 30; 87 if (month == 2) return isLeapYear(year) ? 29 : 28; 88 return 0; 89 } 90 91 public static boolean isLeapYear(int year){ 92 return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0); 93 } 94 }
1 package chapter5方法; 2 3 public class RandomCharacter { 4 public static char getRandomCharacter(char ch1, char ch2){ 5 return (char)(ch1 + Math.random() * (ch2 - ch1 + 1)); 6 } 7 8 9 public static char getRandomLowerCaseLetter(){ 10 return getRandomCharacter('a', 'z'); 11 } 12 public static char getRandomUpperCaseLetter(){ 13 return getRandomCharacter('A', 'Z'); 14 } 15 public static char getRandomDigitCharacter(){ 16 return getRandomCharacter('0', '9'); 17 } 18 public static char getRandomCharcter(){ 19 return getRandomCharacter('\u0000', '\uFFFF'); 20 } 21 22 }
1 package chapter5方法; 2 3 public class TestMethodOverloading { 4 public static void main(String[] args){ 5 System.out.println("The maximum between 3 and 4 is " + max(3, 4)); 6 System.out.println("The maximun between 3.0 and 5.4 is " + max(3.0, 5.4)); 7 System.out.println("The maximum between 3.0, 5.4, and 10.14 is " + max(3.0, 5.4, 10.14)); 8 } 9 10 public static int max(int num1, int num2){ 11 if (num1 > num2) 12 return num1; 13 else 14 return num2; 15 } 16 public static double max(double num1, double num2){ 17 if (num1 > num2) 18 return num1; 19 else 20 return num2; 21 } 22 23 public static double max(double num1, double num2, double num3){ 24 return max(max(num1, num2), num3); 25 } 26 }
1 package chapter5方法; 2 3 public class TestRandomCharacter { 4 public static void main(String args[]){ 5 final int NUMBER_OF_CHARS = 50; //打印的字符数,循环次数; 6 final int CHARS_PER_LINE = 25; 7 8 for (int i = 0; i < NUMBER_OF_CHARS; i++){ 9 char ch = RandomCharacter.getRandomLowerCaseLetter(); 10 if ((i + 1) % CHARS_PER_LINE == 0) 11 System.out.println(ch); //打印并换行; 12 else 13 System.out.print(ch + " "); //打印不换行; 14 15 } 16 } 17 }