Intro to Programming in Java
Intro to Programming in Java CSIS 110
Popular in Course
Popular in Computer Information Systems
This 3 page Class Notes was uploaded by Laurianne White MD on Monday October 12, 2015. The Class Notes belongs to CSIS 110 at Fort Lewis College taught by Brian Hanks in Fall. Since its upload, it has received 28 views. For similar materials see /class/221848/csis-110-fort-lewis-college in Computer Information Systems at Fort Lewis College.
Reviews for Intro to Programming in Java
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/12/15
CSIS 110 Lecture 23 Announcements Read section 55 56 58 and 59 last time 51 to 54 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 quotprimesquot the generator Two random number generators created using the same seed value will generate the same sequence of 39random numbers The nextlnt method returns a random number This method is also overloaded public int nextlnt iretum a random int in the range 2l47484648 to 2147484647 public int nextlnt int max iretum a random int in the range 0 to max l 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 techsupportl application in chapter 5 folder Go ahead and create a SupportSystem object Tell it about your computing problems It39s 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 Let39s look at the Responder class Not much here yet is there Let s examine the code of class SupportSys tem 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 wni l e loop What does this loop do normally read some user input get a response from the Re sponde r object print that response The loop terminates when the input begins with the String quotbyequot String method s t a rt sWi tn 7 returns true if St ri ng begins with the given parameter false otherwise You can think of this as looking for a pre x Give some examples What about nding a suf x Look it up in the Java documentation There is an endsWi tn method So ifwe run the start method on a SupportSystem object we notice that we can enter any String that begins with the sequence quotbyequot What if we enter a space rst It doesn39t work How can we x this Use the trim method What is the signature ofthis 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 quotByequot or quotBYEquot What method could we use to make this work Modify your SupportSystem start method to use toLowerCas e Remember this method returns a new String 7 it doesn39t modify the original Lab Exercise Modify your Tech Support System so that it generates a random response instead of always responding in the same way Modify the Responder class as follows 1 Declare an instance variable of type Random 2 Declare an instance variable to hold the possible responses 7 use an ArrayLi st of S t r i ng s 3 Create the Random and ArrayLi s t objects in the Re sponde r constructor 4 Fill the response ArrayLi st with some messages 7 write a new method call it fi 1 1 Re spons e s that does this and call this method from the constructor 5 Use the random number generator the variable you declared in step 1 above to select and return a random message from the ArrayLi s t when generateRespons e is called Here are some example messages quotThat sounds odd Could you give me more detailsquot quotWe know about that problem To fix it simply turn off your computer and go watch TVquot quotYour description is rather vague I need more precise information about your problemquot quotDo you have your user manual handy That problem is described on page 43quot You can use these messages plus some of your own in your Tech Support System Email the file Responder j ava to me Use the subject quotLab 18quot