CSIS 110 Lecture 23: Generating Random Numbers and String Manipulation - Prof. Brian F. Ha, Study notes of Javascript programming

In this lecture, students are introduced to the java random class for generating random numbers and learn how to use it to simulate rolling dice. The lecture also covers string manipulation, specifically focusing on the startswith and endswith methods, and demonstrates how to modify the tech-support application to generate random responses. Students are asked to modify the responder class to use a random object and an arraylist of possible responses, and to email the updated file to the instructor.

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-8vo-1
koofers-user-8vo-1 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 - Lecture 23
Announcements:
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
-
Discuss ways that students used random numbers to select DNA characters.
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.
So, how would we use a Random number generator to simulate rolling a single die? How
about two dice?
pf3

Partial preview of the text

Download CSIS 110 Lecture 23: Generating Random Numbers and String Manipulation - Prof. Brian F. Ha and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 - Lecture 23

Announcements: 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

Discuss ways that students used random numbers to select DNA characters. 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. So, how would we use a Random number generator to simulate rolling a single die? How about two dice?

Tech-Support Let's look at an application that does some String manipulation. Open the tech-support application (in chapter 5 folder) 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. Not much here yet, is there? 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? (public String trim() ). What does that mean? 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.