Skip to content

Commit 01760b6

Browse files
committed
Generate random characters
1 parent 0418bcb commit 01760b6

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

Chapter_06/Problem$38/Problem$38.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package chapter_six;
2+
3+
/**
4+
* *6.38 (Generate random characters) Use the methods in RandomCharacter in Listing
5+
* 6.10 to print 200 uppercase letters and then 200 single digits, printing ten per line.
6+
*
7+
* @author Sharaf Qeshta
8+
* */
9+
10+
11+
public class Problem$38
12+
{
13+
public static void main(String[] args)
14+
{
15+
for (int i = 0; i < 400; i++)
16+
{
17+
if (i % 10 == 0)
18+
System.out.println();
19+
20+
if (i > 199)
21+
System.out.print(getRandomDigitCharacter());
22+
else
23+
System.out.print(getRandomUpperCaseLetter());
24+
}
25+
}
26+
27+
public static char getRandomCharacter(char ch1, char ch2)
28+
{
29+
return (char) (ch1 + Math.random() * (ch2 - ch1 + 1));
30+
}
31+
32+
public static char getRandomLowerCaseLetter()
33+
{
34+
return getRandomCharacter('a', 'z');
35+
}
36+
37+
38+
public static char getRandomUpperCaseLetter()
39+
{
40+
return getRandomCharacter('A', 'Z');
41+
}
42+
43+
public static char getRandomDigitCharacter()
44+
{
45+
return getRandomCharacter('0', '9');
46+
}
47+
48+
public static char getRandomCharacter()
49+
{
50+
return getRandomCharacter('\u0000', '\uFFFF');
51+
}
52+
53+
}

0 commit comments

Comments
 (0)