Docsity

CSIS 110 Lecture 19: Generating Random Numbers and String Manipulation in Java - Prof. Bri, Study notes of Javascript programming

Toggle sidebar
bg1
CSIS 110 - Lecture 19
Quiz: Wednesday, March 1
Read: Section 5.5, 5.6, 5.8, and 5.9 (last time: 5.1 to 5.4)
Last Time: Random Numbers
Java includes a class for generating random numbers. What is it called?
What package is it contained in? (That is, where do you need to tell Java to look for the
Random class in an import statement?)
How do you create an instance of a Random?
How do you generate a random number?
- create an instance of class Random
- call a method on that object to get a number
The class Random has an overloaded constructor:
public Random()
public Random( long seed )
Both create a random number generator. The second version takes a seed value as a
parameter. This "primes" the generator. Two random number generators created using the
same seed value will generate the same sequence of 'random' numbers.
The nextInt method returns a random number. This method is also overloaded:
public int nextInt() โ€“ return a random int in the range -2,147,484,648 to
2,147,484,647.
public int nextInt( int max ) โ€“ return a random int in the range 0 to
max-1.
Look at the 'random' project. Construct 2 random number generators using the same seed.
Let's look at the random DNA string method (the last lab).
Tech-Support
Let's look at an application that does some String manipulation. Open the tech-support
application.
Go ahead and create a SupportSystem object. Tell it about your computing problems. It's
not a very good system, is it?
pf3

Partial preview of the text

Download CSIS 110 Lecture 19: Generating Random Numbers and String Manipulation in Java - Prof. Bri and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 19

Quiz: Wednesday, March 1 Read: Section 5.5, 5.6, 5.8, and 5.9 (last time: 5.1 to 5.4) Last Time: Random Numbers Java includes a class for generating random numbers. What is it called? What package is it contained in? (That is, where do you need to tell Java to look for the Random class in an import statement?) How do you create an instance of a Random? How do you generate a random number?

  • create an instance of class Random
  • call a method on that object to get a number The class Random has an overloaded constructor: public Random() public Random( long seed ) Both create a random number generator. The second version takes a seed value as a parameter. This "primes" the generator. Two random number generators created using the same seed value will generate the same sequence of 'random' numbers. The nextInt method returns a random number. This method is also overloaded: public int nextInt() โ€“ return a random int in the range -2,147,484,648 to 2,147,484,647. public int nextInt( int max ) โ€“ return a random int in the range 0 to max-1. Look at the 'random' project. Construct 2 random number generators using the same seed. Let's look at the random DNA string method (the last lab). Tech-Support Let's look at an application that does some String manipulation. Open the tech-support application. Go ahead and create a SupportSystem object. Tell it about your computing problems. It's not a very good system, is it?

There are 3 classes: SupportSystem, InputReader, and Responder. The InputReader class gets user input. The Responder class generates a response. Let's look at the Responder class. Let's examine the code of class SupportSystem: The constructor creates an InputReader and a Responder. There are a couple of methods that print messages. The start method does the most interesting work. It has a while loop - What does this loop do normally?

  • read some user input
  • get a response from the Responder object
  • print that response The loop terminates when the input begins with the String "bye". String method startsWith โ€“ returns true if String begins with the given parameter, false otherwise. You can think of this as looking for a prefix. [Give some examples] What about finding a suffix? Look it up in the Java documentation. There is an endsWith method. So, if we run the start method on a SupportSystem object, we notice that we can enter any String that begins with the sequence "bye". What if we enter a space first? It doesn't work. How can we fix this? Use the trim method. What is the signature of this method? What does that mean? public String trim() โ€“ returns a String with the whitespace trimmed off of the ends. It doesn't remove white space from the middle. It doesn't modify the original String. Modify your SupportSystem start method to use trim. OK, so now it works with whitespace at the beginning. What if I type "Bye" or "BYE"? What method could we use to make this work? Modify your SupportSystem start method to use toLowerCase. Remember, this method returns a new String โ€“ it doesn't modify the original.