Dilip
(Methods)
Complete Source
Code
/*
In this class, I have created a list of static methods that I will use
frequently in
many of my programs. They are here so that I would be able to call them at
anytime
without having to rewrite the code. NOTE: This
class does not have a "main" method. */
public class Dilip {
// This method will repatedly ask the user to enter
a password until he/she
// gets it right. The subroutine will terminate once the password has been
accepted.
public static void password()
{
String password,
userInput;
password =
"elephant";
System.out.println("\nThis program is password protected!");
System.out.print("\nPlease
enter the password: ");
do {
userInput = TextIO.getln();
if (!password.equals(userInput)) System.out.print("Access denied! Try
again: ");
} while (!password.equals(userInput));
System.out.println("\nAccess granted!");
} // end of
password()
// This method will determine the primality of a
given integer. Integers that are
// less than 2 are automatically determined to be composite.
public static boolean prime(int
N) {
int n, m, r;
boolean primality;
primality = false;
if (N < 2)
primality = false;
if (N == 2)
primality = true;
if (N > 2 && N %
2 == 0) primality = false;
if (N > 2 && N % 2 != 0) {
n = 2;
m = 3;
r = 1;
while (m <= N/2 & r > 0) {
r = N % m;
m = 2*n + 1;
n = n + 1;
}
if (r > 0) primality = true;
else primality = false;
}
return primality;
} // end of
prime()
// This method will compute the divisors of a given
positive integer and display them
// on standard output.
public static void divisors(int
N) {
int n = 1;
System.out.println();
while (n <= N/2)
{
if (N % n == 0) System.out.print(n + ", ");
n = n + 1;
}
System.out.println(N);
} // end of
divisors()
// This method will compute the sum of the digits of a given integer.
public static int sumDigits(int
N) {
int M, digit, sum;
sum = 0;
M = Math.abs(N);
do {
digit = M % 10;
sum = sum + digit;
M = M/10;
} while (M >=
10);
sum = sum + M;
return sum;
} // end of
sumDigits()
// This method will reverse the digits of a given positive integer.
Trailing zeros
// will not affect the result in any way. So the numbers 43 and 430, for
example, will
// have the same reverse (namely 34).
public static int reverseDigits(int
N) {
int digit, revN;
revN = 0;
if (N < 10) revN
= N;
else {
do {
digit = N % 10;
revN = 10*(revN + digit);
N = N/10;
} while (N >= 10);
revN = revN + N;
}
return revN;
} // end of
reverseDigits()
// This method will concatenate any string into a single word without any
spaces.
public static String joinString(String
s1) {
int N, M;
s1 = s1.trim();
do {
N = s1.indexOf(' ');
M = s1.length();
if (N != -1) s1 = s1.substring(0,N) + s1.substring(N+1,M);
} while (N !=
-1);
return s1;
} // end of
joinString()
// This method will determine the Nth prime number.
public static int NthPrime(int
N) {
int n, count;
n = 1;
count = 0;
do {
n++;
if (prime(n)) count++;
} while (count <
N);
return n;
} // end of
NthPrime()
// This method will organize an array of integers into columns for display
on
// standard output.
public static void intColumns(int[]
list, int columnSize) {
int n, m;
if (list.length >
0) {
System.out.println();
n = 0;
while (n < columnSize) {
m = n;
while (m < list.length) {
System.out.print(list[m] + " ");
m = m + columnSize;
}
System.out.println();
n++;
}
}
} // end of
intColumns()
// This method will sort an array of integers in ascending or descending
order and then
// return the sorted array back to the call statement.
public static int[] intSort(int[]
list, String order) {
int n, m, temp;
if (order.equals("ascending"))
{
for (n = 0; n < list.length; n++) {
for (m = n + 1; m < list.length; m++) {
if (list[m] < list[n]) {
temp = list[m];
list[m] = list[n];
list[n] = temp;
}
}
}
}
if (order.equals("descending"))
{
for (n = 0; n < list.length; n++) {
for (m = n + 1; m < list.length; m++) {
if (list[m] > list[n]) {
temp = list[m];
list[m] = list[n];
list[n] = temp;
}
}
}
}
return list;
} // end of
intSort()
// This method will create a time lag of a specified number of
milliseconds whenever
// it is invoked inside a program. It can be used to deliberately slow
down the
// normal flow of a program.
public static void timeLag(long
duration) {
long start,
timer;
start =
System.currentTimeMillis();
timer = 0;
while (timer <=
duration) {
timer = System.currentTimeMillis() - start;
}
} // end of
timeLag()
// This methods prints out a string by sliding each of its characters into
place through
// an animation sequence. The parameter t represents the sliding speed.
public static void slideLetters(String
s1, long t) {
int i, j, k;
System.out.println();
for (j = 60; j >
60 - s1.length(); j--) {
for (i = 1; i <= j; i++) System.out.print(" ");
for (k = 1; k <= j; k++) {
System.out.print(s1.charAt(60-j));
System.out.print(" ");
Dilip.timeLag(t);
System.out.print("\b\b\b");
}
System.out.print(s1.charAt(60-j));
if (60-j == s1.length()-1) System.out.print(" ");
}
System.out.println();
} // end of
slideLetters()
// This method scrolls a line of text from the right edge of the window to
the left edge
// through an animation sequence. The program is divided into three main
for loops (with
// loop control variable j). In the first loop, the text is fed into the
screen from the right
// edge of the window. In the second loop, the text is scrolled across the
screen to the
// left edge. In the third loop, it will be fed out of the screen. Time
lags are in place to
// ensure continuous movement and transition between the three code
segments. There is also
// an outer loop that contains the entire body of the program which
repeats the animation a
// specified number of times. The parameter t represents the scrolling
speed.
public static void marquee(String
s1, long t, int N) {
int i, j, k;
System.out.println();
for (k = 1; k <=
N; k++) {
for (i = 1; i <= 77; i++) System.out.print(" ");
for (j = 0; j < s1.length(); j++) {
for (i = 0; i <= j; i++) System.out.print(s1.charAt(i));
for (i = 0; i <= j + 1; i++) System.out.print("\b");
Dilip.timeLag(t);
}
for (j = 0; j <= 77 - s1.length(); j++) {
for (i = 0; i < s1.length(); i++) System.out.print(s1.charAt(i));
System.out.print(" ");
for (i = 0; i < s1.length() + 2; i++) System.out.print("\b");
Dilip.timeLag(t);
}
for (j = 1; j <= s1.length(); j++) {
for (i = j; i < s1.length(); i++) System.out.print(s1.charAt(i));
System.out.print(" ");
for (i = 0; i < s1.length(); i++) System.out.print("\b");
Dilip.timeLag(t);
}
}
System.out.println();
} // end of
marquee()
} // end of class Dilip |