Skip to content

Commit 0ead8fa

Browse files
committed
Methods
1 parent 8c033c4 commit 0ead8fa

12 files changed

+570
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package chapter_six.samples;
2+
3+
import java.util.Scanner;
4+
5+
/** Listing 6.6 GreatestCommonDivisorMethod.java */
6+
public class GreatestCommonDivisorMethod
7+
{
8+
/** Main method */
9+
public static void main(String[] args)
10+
{
11+
// Create a Scanner
12+
Scanner input = new Scanner(System.in);
13+
14+
// Prompt the user to enter two integers
15+
System.out.print("Enter first integer: ");
16+
int n1 = input.nextInt();
17+
System.out.print("Enter second integer: ");
18+
int n2 = input.nextInt();
19+
20+
System.out.println("The greatest common divisor for " + n1 +
21+
" and " + n2 + " is " + gcd(n1, n2));
22+
}
23+
24+
/** Return the gcd of two integers */
25+
public static int gcd(int n1,int n2)
26+
{
27+
int gcd = 1; // Initial gcd is 1
28+
int k = 2; // Possible gcd
29+
while (k <= n1 && k <= n2)
30+
{
31+
if (n1 % k == 0 && n2 % k == 0)
32+
gcd = k; // Update gcd
33+
k++;
34+
}
35+
return gcd; // Return gcd
36+
}
37+
}

Chapter_06/samples/Hex2Dec.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package chapter_six.samples;
2+
3+
import java.util.Scanner;
4+
5+
/** Listing 6.8 Hex2Dec.java */
6+
public class Hex2Dec
7+
{
8+
/** Main method */
9+
public static void main(String[] args)
10+
{
11+
// Create a Scanner
12+
Scanner input = new Scanner(System.in);
13+
14+
// Prompt the user to enter a string
15+
System.out.print("Enter a hex number: ");
16+
String hex = input.nextLine();
17+
18+
System.out.println("The decimal value for hex number "
19+
+ hex + " is " + hexToDecimal(hex.toUpperCase()));
20+
}
21+
22+
public static int hexToDecimal(String hex)
23+
{
24+
int decimalValue = 0;
25+
for (int i = 0; i < hex.length(); i++)
26+
{
27+
char hexChar = hex.charAt(i);
28+
decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
29+
}
30+
return decimalValue;
31+
}
32+
33+
34+
public static int hexCharToDecimal(char ch)
35+
{
36+
if (ch >= 'A' && ch <= 'F')
37+
return 10 + ch - 'A';
38+
else // ch is '0', '1', ..., or '9'
39+
return ch - '0';
40+
}
41+
}

Chapter_06/samples/Increment.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package chapter_six.samples;
2+
3+
/** Listing 6.4 Increment.java */
4+
public class Increment
5+
{
6+
public static void main(String[] args)
7+
{
8+
int x = 1;
9+
System.out.println("Before the call, x is " + x);
10+
increment(x);
11+
System.out.println("After the call, x is " + x);
12+
}
13+
14+
public static void increment(int n)
15+
{
16+
n++;
17+
System.out.println("n inside the method is " + n);
18+
}
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package chapter_six.samples;
2+
3+
/** Listing 6.7 PrimeNumberMethod.java */
4+
public class PrimeNumberMethod
5+
{
6+
public static void main(String[] args)
7+
{
8+
System.out.println("The first 50 prime numbers are \n");
9+
printPrimeNumbers(50);
10+
}
11+
12+
public static void printPrimeNumbers(int numberOfPrimes)
13+
{
14+
final int NUMBER_OF_PRIMES_PER_LINE = 10; // Display 10 per line
15+
int count = 0; // Count the number of prime numbers
16+
int number = 2; // A number to be tested for primeness
17+
18+
// Repeatedly find prime numbers
19+
while (count < numberOfPrimes)
20+
{
21+
// Print the prime number and increase the count
22+
if (isPrime(number))
23+
{
24+
count++; // Increase the count
25+
if (count % NUMBER_OF_PRIMES_PER_LINE == 0)
26+
// Print the number and advance to the new line
27+
System.out.printf("%−5d\n", number);
28+
else
29+
System.out.printf("%−5d", number);
30+
}
31+
// Check whether the next number is prime
32+
number++;
33+
}
34+
}
35+
36+
37+
/** Check whether number is prime */
38+
public static boolean isPrime(int number)
39+
{
40+
for (int divisor = 2; divisor <= number / 2; divisor++)
41+
{
42+
if (number % divisor == 0) // If true, number is not prime
43+
return false; // Number is not a prime
44+
}
45+
return true;
46+
}
47+
}

Chapter_06/samples/PrintCalendar.java

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package chapter_six.samples;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Listing 6.12 PrintCalendar.java
7+
*/
8+
public class PrintCalendar {
9+
/**
10+
* Main method
11+
*/
12+
public static void main(String[] args)
13+
{
14+
Scanner input = new Scanner(System.in);
15+
16+
// Prompt the user to enter year
17+
System.out.print("Enter full year (e.g., 2012): ");
18+
int year = input.nextInt();
19+
20+
// Prompt the user to enter month
21+
System.out.print("Enter month as a number between 1 and 12: ");
22+
int month = input.nextInt();
23+
24+
// Print calendar for the month of the year
25+
printMonth(year, month);
26+
}
27+
28+
/**
29+
* Print the calendar for a month in a year
30+
*/
31+
public static void printMonth(int year, int month)
32+
{
33+
// Print the headings of the calendar
34+
printMonthTitle(year, month);
35+
36+
// Print the body of the calendar
37+
printMonthBody(year, month);
38+
}
39+
40+
41+
/**
42+
* Print the month title, e.g., March 2012
43+
*/
44+
public static void printMonthTitle(int year, int month)
45+
{
46+
System.out.println(" " + getMonthName(month)
47+
+ " " + year);
48+
System.out.println("−−−−−−−−−−−−−−−−−−−−−−−−−−−−−");
49+
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
50+
}
51+
52+
/**
53+
* Get the English name for the month
54+
*/
55+
public static String getMonthName(int month)
56+
{
57+
String monthName = "";
58+
switch (month)
59+
{
60+
case 1:
61+
monthName = "January";
62+
break;
63+
case 2:
64+
monthName = "February";
65+
break;
66+
case 3:
67+
monthName = "March";
68+
break;
69+
case 4:
70+
monthName = "April";
71+
break;
72+
case 5:
73+
monthName = "May";
74+
break;
75+
case 6:
76+
monthName = "June";
77+
break;
78+
case 7:
79+
monthName = "July";
80+
break;
81+
case 8:
82+
monthName = "August";
83+
break;
84+
case 9:
85+
monthName = "September";
86+
break;
87+
case 10:
88+
monthName = "October";
89+
break;
90+
case 11:
91+
monthName = "November";
92+
break;
93+
case 12:
94+
monthName = "December";
95+
}
96+
return monthName;
97+
}
98+
99+
100+
/**
101+
* Print month body
102+
*/
103+
public static void printMonthBody(int year, int month)
104+
{
105+
// Get start day of the week for the first date in the month
106+
int startDay = getStartDay(year, month);
107+
108+
// Get number of days in the month
109+
int numberOfDaysInMonth = getNumberOfDaysInMonth(year, month);
110+
111+
// Pad space before the first day of the month
112+
int i = 0;
113+
for (i = 0; i < startDay; i++)
114+
System.out.print(" ");
115+
116+
for (i = 1; i <= numberOfDaysInMonth; i++)
117+
{
118+
System.out.printf("%4d", i);
119+
120+
if ((i + startDay) % 7 == 0)
121+
System.out.println();
122+
}
123+
System.out.println();
124+
}
125+
126+
/**
127+
* Get the start day of month/1/year
128+
*/
129+
public static int getStartDay(int year, int month)
130+
{
131+
final int START_DAY_FOR_JAN_1_1800 = 3;
132+
// Get total number of days from 1/1/1800 to month/1/year
133+
int totalNumberOfDays = getTotalNumberOfDays(year, month);
134+
135+
// Return the start day for month/1/year
136+
return (totalNumberOfDays + START_DAY_FOR_JAN_1_1800) % 7;
137+
}
138+
139+
/**
140+
* Get the total number of days since January 1, 1800
141+
*/
142+
public static int getTotalNumberOfDays(int year, int month)
143+
{
144+
int total = 0;
145+
146+
// Get the total days from 1800 to 1/1/year
147+
for (int i = 1800; i < year; i++)
148+
if (isLeapYear(i))
149+
total = total + 366;
150+
else
151+
total = total + 365;
152+
153+
// Add days from Jan to the month prior to the calendar month
154+
for (int i = 1; i < month; i++)
155+
total = total + getNumberOfDaysInMonth(year, i);
156+
return total;
157+
}
158+
159+
/**
160+
* Get the number of days in a month
161+
*/
162+
public static int getNumberOfDaysInMonth(int year, int month)
163+
{
164+
if (month == 1 || month == 3 || month == 5 || month == 7 ||
165+
month == 8 || month == 10 || month == 12)
166+
return 31;
167+
168+
if (month == 4 || month == 6 || month == 9 || month == 11)
169+
return 30;
170+
171+
if (month == 2) return isLeapYear(year) ? 29 : 28;
172+
return 0; // If month is incorrect
173+
}
174+
175+
/**
176+
* Determine if it is a leap year
177+
*/
178+
public static boolean isLeapYear(int year)
179+
{
180+
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
181+
}
182+
183+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package chapter_six.samples;
2+
3+
/**
4+
* Listing 6.10 RandomCharacter.java
5+
*/
6+
public class RandomCharacter
7+
{
8+
/**
9+
* Generate a random character between ch1 and ch2
10+
*/
11+
public static char getRandomCharacter(char ch1, char ch2)
12+
{
13+
return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
14+
}
15+
16+
/**
17+
* Generate a random lowercase letter
18+
*/
19+
public static char getRandomLowerCaseLetter()
20+
{
21+
return getRandomCharacter('a', 'z');
22+
}
23+
24+
/**
25+
* Generate a random uppercase letter
26+
*/
27+
public static char getRandomUpperCaseLetter()
28+
{
29+
return getRandomCharacter('A', 'Z');
30+
}
31+
32+
/**
33+
* Generate a random digit character
34+
*/
35+
public static char getRandomDigitCharacter()
36+
{
37+
return getRandomCharacter('0', '9');
38+
}
39+
40+
/**
41+
* Generate a random character
42+
*/
43+
public static char getRandomCharacter()
44+
{
45+
return getRandomCharacter('\u0000', '\uFFFF');
46+
}
47+
}

0 commit comments

Comments
 (0)