Upgrade the Java built-in class ‘Random’ by using the inheritance concept

profilesmartstudent12
week1-3Assignment1Discussion.pptx

Assignment 1. Upgrade the Java built-in Random Class

Mission

The Java Random class is a part of the java.util package and contains built-in methods to generate random numbers. Some important methods are:

Your mission is to upgrade the Random class by adding the following six methods.

method functionality example
nextInt(int low, int high) Returns a random number which is an integer value between the range of ‘low’ to ‘high’. nextInt(5, 9) -> one of 5, 6, 7, 8, 9 (assume low < high)
nextEven(int low, int high) Returns a random number which is an even number between the range of ‘low’ to ‘high’. nextEven(5, 9) -> one of 6, 8 (assume low < high)
nextOdd(int low, int high) Returns a random number which is an odd number between the range of ‘low’ to ‘high’. nextOdd(5, 9) -> one of 5, 7, 9 (assume low < high)
nextChar() Returns an uppercase character. nextChar() -> one of ‘A’ ~ ‘Z’
nextChar(char from, char to) Returns an uppercase letter between the range of ‘from’ to ‘to’. nextChar(‘C’ , ‘F’) -> one of ‘C’, ‘D’, ‘E’, ‘F’ nextCharCap(‘X’ , ‘C’) -> one of ‘X’, ‘Y’, ‘Z’, ‘A’, ‘B’, ‘C’
nextSpecialChar() Returns a special character nextSpecialChar() -> one of any special character like ‘%’

Current (built-in)Random Class

NewRandom Class

Upgrade

+

3

What you need to know:

how to use built-in class

how to create a user-define class

concepts of “extends” (inheritance)

how to initiate an object (instance)

random number generation

concepts of ascii

character calculation

while loop

data type cast

Step by step development

public class NewRandom extends Random {

}

public class NewRandom extends Random {

public int nextInt(int low, int high) {

. . . }

public int nextEven(int low, int high) {

. . . }

public int nextOdd(int low, int high) {

. . . }

. . .

}

public class MyRandomTest {

public static void main(String[] args) {

NewRandom rand = new NewRandom();

for (int i = 0; i < 6; i++) { // test each method 6 times

int a = rand.nextInt(4, 10);

System.out.println(a); }

} }

Submit these two files

Create NewRandom class. Apply inheritance to get the methods in Random class

add(implement) new methods in NewRandom class

create a main program to test NewRandom class

(See slide #12)

// overloads Random's nextInt() to return a random number between low and high, both inclusive. [low, high]

public int nextInt(int low, int high) {

int n = nextInt(high - low + 1) + low;

return n;

}

How to implement each method (1)

CS 210 Note (Ch. 5)

6

How to implement each method (2)

CS 210 Note (Ch. 5)

// returns an even random number between low and high, both inclusive. [low, high]

public int nextEven(int low, int high) {

int n = 0;

boolean even = false;

while (!even) {

n = nextInt(low, high); // uses method from this class

if n is an even number {

even = true;

}

}

return n;

}

CS 210 Note (Ch. 2)

7

How to implement each method (3)

CS 210 Note (Ch. 5)

// returns an odd random number between low and high, both inclusive. [low, high]

public int nextOdd(int low, int high) {

int n = 0;

boolean odd = false;

while (!odd) {

n = nextInt(low, high); // uses method from this class

.

.

}

return n;

}

CS 210 Note (Ch. 2)

8

How to implement each method (4)

CS 210 Note (Ch. 4)

CS 210 Note (Ch. 4)

// returns a random upper case "capital" character.

public char nextChar() { int n = nextInt('A', 'Z’); // int n = nextInt(65, 90); // 65 = 'A', 90 = ‘Z’

return (char) n;

}

9

How to implement each method (5)

// returns a random upper case "capital" character.

public char nextChar(char from, char to) {

if (from < to) {

int n = nextInt(from, to); return (char) n;

}

else

if (from > to) {

. . .

}

else // from == to

{

} }

10

How to implement each method (6)

// returns a random special character.

public char nextSpecialChar() {

int low = 33; int high = 126;

char c=' '; //note: c=''(x), c=' '(O) -> need a space to assign initial value

boolean special = false;

while (!special) {

int n = nextInt(low, high);

c= (char) n;

// if c is not a number, not a letter, c is a special character.

. . .

} return c;

}

CS 210 Note (Ch. 4)

11

Sample structure of ‘MyNewRandomTest’

public class MyNewRandomTest{

public static void main(String[] args) {

NewRandom rand = new NewRandom();

System.out.println("\n1) Test: nextInt(low, high)");

for (int i = 0; i < 6; i++) {

int a = rand.nextInt(3, 9);

System.out.println(a);

}

System.out.println("\n2) Test: nextEven(low, high)");

for (int i = 0; i < 6; i++) {

int a = rand.nextEven(3, 9);

System.out.println(a);

}

System.out.println("\n3) Test: nextOdd(low, high)");

for (int i = 0; i < 6; i++) {

int a = rand.nextOdd(3, 9);

System.out.println(a);

}

. . . . .

test all the 6 methods by calling 6 times each.

1) Test: nextInt(low, high)

8

4

6

7

7

9

2) Test: nextEven(low, high)

4

6

8

4

6

6

3) Test: nextOdd(low, high)

5

5

7

3

9

7

Sample output

4) Test: nextChar()

C

K

T

Y

Z A

5) Test: nextChar(low, high)

F B S T K M

6) Test: nextSpecialChar()

* ! ~ & ) /

5) Test: nextChar(low, high)

U B X T A Z

if nextChar(B, T)

if nextChar(T, B)

Include appropriate program comments and formatting including:

1) Your first and last name

2) Your student ID

3) The date

4) A short description of the program’s function

5) Comments necessary to explain the operation of your program

6) Proper indentation

Due: Tuesday 04/20/2021 11:59pm, 30 points

You have to upload two .java files: NewRandom.java and MyNewRandomTest.java