Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA Test Create NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

JAVA

Test Create NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOdd(int low, int high) nextCharl) nextChar(char from, char to) nextChar(char from, int i) nextSpecial Charl) (New RandomTest) public class NewRandom extends Random } 9 Test Create NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOddint low, int high) next Charl) nextChar(char from, charto) nextChar(char from, int i) nextSpecialCharl) (NewRandomTest) Method nextDouble method Il overloads Random's nextInt() to return a random number between low and high, both inclusive. [low, high] nextBooleano nextInt(int low, int high) nextFloati) nextEven(int low, int high) call public int nextInt(int low, int high) { int n = nextInt(high - low + 1) + low; return n; } Reference: CS 210 (Ch. 5) Generating random numbers nextInt() nextOddfint low, int high) nextInt(int n) nextChar() nextBytes(byte[] bytes) nextChar(char from, char to) Longso Common usage: to get a random number from 1 to N int - rand, nextInt (20) + 1; 1/ 1-20 inclusive nextChar(char c, inti) nextGaussian nextSpecialChar() To get a number in arbitrary range [min, max) inclusive: name.nextInt (size of range) + min Where (size of range) is (max min + 1) Example: A random integer between 4 and 10 inclusive: int - rand.nextInt (7) 4 mamine 10-41 9 Test 1 2 3 4 5 6 7 8 Create Implement Implement Implement Implement Implement Implement Implement Write a test NewRandom new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program class nextInt(int low, int high) nextEventint low, int high) nextOdd(int low, int high) next Charl) nextChar(char from, charto) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) (Apply inheritance) // returns an even random number between low and high, both inclusive. [low, high) Method nextDouble method public int nextEven(int low, int high) { int n = 0; nextBoolean) nextInt(int low, int high) + 1 nextEven(int low, int high) nextFloato) nextinto nextOdd(int low, int high) nextInt(int n) nextChar() boolean even = false; while (leven) { n = nextInt(low, high); // uses method from this class if n is an even number { even = true; } } return n; } CS 210 Note (Ch.5) CS 210 Note (Ch. 2) Integer remainder with % nextBytes(byte[] bytes) nextChar(char from, char to) Longso nextChar(charc, inti) nextGaussian nextSpecialChar() The mother Randon and wanden int te - 0 Boolean redelse while inade roll Shed int rell -rand next16) + 1 . int co2 and next16) ist rolli 12 System.out.printin roll + roll2 + + tre. 10 Osteoaren 1108537 See whether herid 1420 0 1 2 3 4 5 6 7 8 9 Create Implement Implement Implement Implement Implement Implement Implement Write a test Test NewRandom new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program class nextInt(int low, int high) nextEven(int low, int high) nextOddfint low, int high) nextCharl) nextChar(char from, char to) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) (Apply inheritance) // returns an odd random number between low and high, both inclusive. [low, high] Method public int nextOdd(int low, int high) { nextDouble method int n = 0; nextBoolean() nextInt(int low, int high) . nextEven(int low, int high) (1 nextOddint low, int high) boolean odd = false; while (!odd) { n = nextInt(low, high); // uses method from this class next Floato) nextInt() nextInt(int n) nextChar() } return n; n } nextBytes(byte[] bytes) nextChar(char from, char to) Longso nextChar(char c, inti) CS 210 Note (Ch. 2) Integer remainder with % nextGaussian nextSpecialChar() Bar Thertor come the remainder from CS 210 Note (Ch.5) Random rand new Random() int tries - 03 Boolean madetales while made 1/ roll the dice once int rolli - rand.nextInt (6) + 1: int ro112 = rand.nextInt (6) + 1 introlli 12 System.out.printineroll + roll2) tries. if (sum) made System.out.println("Yo watter tries + - tries! 20 11.0 Create NewRandom class (Apply inheritance) 4 Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOddint low, int high) nextCharl) nextChar(char from, charto) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) Test // returns a random upper case "capital" character. Method nextDouble method nextBooleano nextInt(int low, int high) 4 nextEven(int low, int high) public char nextChar() { int n = nextInt('A', 'Z'); // int n = nextInt(65, 90); // 65 = 'A', 90 = 'Z return (char) n; } nextFloato) nextinto nextOdd(int low, int high) CS 210 Note (Ch.4) nextInt(int n) nextChar() Each char is mapped to an integer value internally - Called an ASCII value nextBytes(byte[] bytes) nextChar(char from, char to) 'A' is 65 a' is 97 'B' i 66 'b' is 98 ..is 32 **! is 42 Longso nextChar(char c, inti) CS 210 Note (Ch. 4) nextGaussian nextSpecialChar() Mixing char and int causes automatic conversion to int. al + 10 is 107, 'A' is 130 // prints the alphabet for (char c = 'a'; c to) { nextinto nextOdd(int low, int high) nextint(int n) nextChar() (1 nextBytes(byte[] bytes) nextChar(char from, char to) } else // from == to { Longso nextChar(charc, inti) nextGaussian nextSpecialChar() } } Test Create NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOdd(int low, int high) nextCharl) nextChar(char from, char to) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) // returns a random upper case "capital character. Method nextDouble method public char nextChar(char c, int i) { nextBoolean() nextInt(int low, int high) nextFloato) nextEven(int low, int high) nextinto nextOdd(int low, int high) } nextint(int n) nextChar() ! nextBytes(byte[] bytes) nextChar(char-from, char to) Longso nextChar(char c, inti) nextGaussian nextSpecialChar() eg) nextChar('C', 3) returns one of C, D, E, F nextChar('X', 5) returns one of X, Y, Z, A, B, C nextChar('C', -3) returns one of C, B, A, Z nextChar('X', -3) returns one of X, W, V, U nextChar('K', 0) returns K Create NewRandom class (Apply inheritance) 4 Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOddint low, int high) nextCharl) nextChar(char from, charto) nextChar(char from, int i) nextSpecial Charl) (New RandomTest) Test // returns a random special character. Method nextDouble method nextBoolean() nextInt(int low, int high) + nextEven(int low, int high) next Floato) 2 public char nextSpecialChar() { int low = 33; int high = 126; char c=''; /ote: 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 AND not a letter, c is a special character. nextinto nextOddint low, int high) nextInt(int n) nextChar() ! nextBytes(byte[] bytes) nextChar(char from, char to) return c; Longso nextChar(charointi) CS 210 Note (Ch. 4) nextGaussian nextSpecialChar() How to check if a character is a digit (number) / letter Character.isDigit(char) // number CharacterisLetter(char) // letter Never do this way (Silly idea) String specialCharacters="#$%&'()*+,-./:;?@[]^{1}"; int n = nextInt (27); c= special characters.charAt(n); Create Test NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement new method 1 Write a test new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEvenint low, int high) nextOddint low, int high) nextCharl) nextChar(char from, char to) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) Sample structure of 'NewRandom Test'. Test all the 7 methods by calling 6 times each. public class NewRandomTest public static void main(String[] args) { NewRandom rand = new NewRandom(); int no_of_tests - 6; System.out.printin(" 1) Test: nextInt(low, high)"); for (int i 0; CONX 6) Test: nextChar(ci) Y A if nextChar("X', 5) 9 2) Test: nextEven(low, high) 5) Test: nextChar(low, high) 6) Test: nextCharc, i) B S T M w U X X 4 if nextChar(BT) if nextChar('X', -3) 3) Test: nextOdd(low, high) 5) Test: nextCharlow, high) 7) Test: nextSpecialCharo) U B X T if nextChar(T, B) A z I will use different values to check all the 7 requirements 7 Test Create NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOdd(int low, int high) nextCharl) nextChar(char from, char to) nextChar(char from, int i) nextSpecial Charl) (New RandomTest) public class NewRandom extends Random } 9 Test Create NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOddint low, int high) next Charl) nextChar(char from, charto) nextChar(char from, int i) nextSpecialCharl) (NewRandomTest) Method nextDouble method Il overloads Random's nextInt() to return a random number between low and high, both inclusive. [low, high] nextBooleano nextInt(int low, int high) nextFloati) nextEven(int low, int high) call public int nextInt(int low, int high) { int n = nextInt(high - low + 1) + low; return n; } Reference: CS 210 (Ch. 5) Generating random numbers nextInt() nextOddfint low, int high) nextInt(int n) nextChar() nextBytes(byte[] bytes) nextChar(char from, char to) Longso Common usage: to get a random number from 1 to N int - rand, nextInt (20) + 1; 1/ 1-20 inclusive nextChar(char c, inti) nextGaussian nextSpecialChar() To get a number in arbitrary range [min, max) inclusive: name.nextInt (size of range) + min Where (size of range) is (max min + 1) Example: A random integer between 4 and 10 inclusive: int - rand.nextInt (7) 4 mamine 10-41 9 Test 1 2 3 4 5 6 7 8 Create Implement Implement Implement Implement Implement Implement Implement Write a test NewRandom new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program class nextInt(int low, int high) nextEventint low, int high) nextOdd(int low, int high) next Charl) nextChar(char from, charto) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) (Apply inheritance) // returns an even random number between low and high, both inclusive. [low, high) Method nextDouble method public int nextEven(int low, int high) { int n = 0; nextBoolean) nextInt(int low, int high) + 1 nextEven(int low, int high) nextFloato) nextinto nextOdd(int low, int high) nextInt(int n) nextChar() boolean even = false; while (leven) { n = nextInt(low, high); // uses method from this class if n is an even number { even = true; } } return n; } CS 210 Note (Ch.5) CS 210 Note (Ch. 2) Integer remainder with % nextBytes(byte[] bytes) nextChar(char from, char to) Longso nextChar(charc, inti) nextGaussian nextSpecialChar() The mother Randon and wanden int te - 0 Boolean redelse while inade roll Shed int rell -rand next16) + 1 . int co2 and next16) ist rolli 12 System.out.printin roll + roll2 + + tre. 10 Osteoaren 1108537 See whether herid 1420 0 1 2 3 4 5 6 7 8 9 Create Implement Implement Implement Implement Implement Implement Implement Write a test Test NewRandom new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program class nextInt(int low, int high) nextEven(int low, int high) nextOddfint low, int high) nextCharl) nextChar(char from, char to) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) (Apply inheritance) // returns an odd random number between low and high, both inclusive. [low, high] Method public int nextOdd(int low, int high) { nextDouble method int n = 0; nextBoolean() nextInt(int low, int high) . nextEven(int low, int high) (1 nextOddint low, int high) boolean odd = false; while (!odd) { n = nextInt(low, high); // uses method from this class next Floato) nextInt() nextInt(int n) nextChar() } return n; n } nextBytes(byte[] bytes) nextChar(char from, char to) Longso nextChar(char c, inti) CS 210 Note (Ch. 2) Integer remainder with % nextGaussian nextSpecialChar() Bar Thertor come the remainder from CS 210 Note (Ch.5) Random rand new Random() int tries - 03 Boolean madetales while made 1/ roll the dice once int rolli - rand.nextInt (6) + 1: int ro112 = rand.nextInt (6) + 1 introlli 12 System.out.printineroll + roll2) tries. if (sum) made System.out.println("Yo watter tries + - tries! 20 11.0 Create NewRandom class (Apply inheritance) 4 Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOddint low, int high) nextCharl) nextChar(char from, charto) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) Test // returns a random upper case "capital" character. Method nextDouble method nextBooleano nextInt(int low, int high) 4 nextEven(int low, int high) public char nextChar() { int n = nextInt('A', 'Z'); // int n = nextInt(65, 90); // 65 = 'A', 90 = 'Z return (char) n; } nextFloato) nextinto nextOdd(int low, int high) CS 210 Note (Ch.4) nextInt(int n) nextChar() Each char is mapped to an integer value internally - Called an ASCII value nextBytes(byte[] bytes) nextChar(char from, char to) 'A' is 65 a' is 97 'B' i 66 'b' is 98 ..is 32 **! is 42 Longso nextChar(char c, inti) CS 210 Note (Ch. 4) nextGaussian nextSpecialChar() Mixing char and int causes automatic conversion to int. al + 10 is 107, 'A' is 130 // prints the alphabet for (char c = 'a'; c to) { nextinto nextOdd(int low, int high) nextint(int n) nextChar() (1 nextBytes(byte[] bytes) nextChar(char from, char to) } else // from == to { Longso nextChar(charc, inti) nextGaussian nextSpecialChar() } } Test Create NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOdd(int low, int high) nextCharl) nextChar(char from, char to) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) // returns a random upper case "capital character. Method nextDouble method public char nextChar(char c, int i) { nextBoolean() nextInt(int low, int high) nextFloato) nextEven(int low, int high) nextinto nextOdd(int low, int high) } nextint(int n) nextChar() ! nextBytes(byte[] bytes) nextChar(char-from, char to) Longso nextChar(char c, inti) nextGaussian nextSpecialChar() eg) nextChar('C', 3) returns one of C, D, E, F nextChar('X', 5) returns one of X, Y, Z, A, B, C nextChar('C', -3) returns one of C, B, A, Z nextChar('X', -3) returns one of X, W, V, U nextChar('K', 0) returns K Create NewRandom class (Apply inheritance) 4 Implement Implement Implement Implement Implement Implement Implement Write a test new method 1 new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEven(int low, int high) nextOddint low, int high) nextCharl) nextChar(char from, charto) nextChar(char from, int i) nextSpecial Charl) (New RandomTest) Test // returns a random special character. Method nextDouble method nextBoolean() nextInt(int low, int high) + nextEven(int low, int high) next Floato) 2 public char nextSpecialChar() { int low = 33; int high = 126; char c=''; /ote: 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 AND not a letter, c is a special character. nextinto nextOddint low, int high) nextInt(int n) nextChar() ! nextBytes(byte[] bytes) nextChar(char from, char to) return c; Longso nextChar(charointi) CS 210 Note (Ch. 4) nextGaussian nextSpecialChar() How to check if a character is a digit (number) / letter Character.isDigit(char) // number CharacterisLetter(char) // letter Never do this way (Silly idea) String specialCharacters="#$%&'()*+,-./:;?@[]^{1}"; int n = nextInt (27); c= special characters.charAt(n); Create Test NewRandom class (Apply inheritance) Implement Implement Implement Implement Implement Implement Implement new method 1 Write a test new method 2 new method 3 new method 4 new method 5 new method 6 new method 7 Program nextInt(int low, int high) nextEvenint low, int high) nextOddint low, int high) nextCharl) nextChar(char from, char to) nextChar(char from, int i) nextSpecialCharl) (NewRandom Test) Sample structure of 'NewRandom Test'. Test all the 7 methods by calling 6 times each. public class NewRandomTest public static void main(String[] args) { NewRandom rand = new NewRandom(); int no_of_tests - 6; System.out.printin(" 1) Test: nextInt(low, high)"); for (int i 0; CONX 6) Test: nextChar(ci) Y A if nextChar("X', 5) 9 2) Test: nextEven(low, high) 5) Test: nextChar(low, high) 6) Test: nextCharc, i) B S T M w U X X 4 if nextChar(BT) if nextChar('X', -3) 3) Test: nextOdd(low, high) 5) Test: nextCharlow, high) 7) Test: nextSpecialCharo) U B X T if nextChar(T, B) A z I will use different values to check all the 7 requirements 7

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Liquidity Risk Management In Banks Economic And Regulatory Issues

Authors: Roberto Ruozi, Pierpaolo Ferrari

1st Edition

3642295800, 978-3642295805

More Books

Students also viewed these Finance questions