[go: up one dir, main page]

0% found this document useful (0 votes)
76 views30 pages

Oop 12

The document discusses various ways to add more sophisticated behavior to a technical support system program. It covers using library classes like HashMap, HashSet, ArrayList and Random to improve the program. It also discusses comparing strings, reading documentation, adding random responses, and using Maps to associate keys with values. The document provides code examples for generating random numbers and responses. It describes improvements made to the program to make it more tolerant of input variations and mimic a more human-like technical support agent.

Uploaded by

mohammad bilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views30 pages

Oop 12

The document discusses various ways to add more sophisticated behavior to a technical support system program. It covers using library classes like HashMap, HashSet, ArrayList and Random to improve the program. It also discusses comparing strings, reading documentation, adding random responses, and using Maps to associate keys with values. The document provides code examples for generating random numbers and responses. It describes improvements made to the program to make it more tolerant of input variations and mimic a more human-like technical support agent.

Uploaded by

mohammad bilal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 30

More-Sophisticated Behavior

Lecture-12

1
Lecture Outline

1. Using library classes:


 HashMap, HashSet, ArrayList and Random
 Comparing Strings vs. comparing other
2. Objects
Reading documentation
Writing documentation (javadoc)
Information hiding

2
The Java class library

Thousands of classes
Tens of thousands of methods
 Many useful classes that make life much Easier
A competent Java programmer must be able to work
with the libraries

3
The Java class library

You should:
• know some important classes by name
• know how to find out about other classes
Remember:
• We only need to know the interface, not the implementation
 The interface of a class describes what a class does and how it
can be used without showing the implementation.
 The complete source code that defines a class is called the
implementation of that class.

4
A Technical Support System
 TechSupport is a program intended to provide technical support
for customers.
 DodgySoft software company., had a technical support
department with people sitting at telephones.
 Customers could call to get advice and help with their
technical problems with the DodgySoft software products.
Recently, business has not been going so well, and dodgySoft
decided to get rid of the technical support department to save
money.

5
A Technical Support System

They now want to develop the TechSupport system to give


the impression that support is still provided.
The system is supposed to mimic the responses a
technical-support person might give. Customers can
communicate with the technical-support system online.

6
A Technical Support System

7
A Technical Support System

Improve the processing of input a little.


our system is not very tolerant: if we type “Bye” or“ bye”
instead of “bye”, for instance, the word is not recognized.
 We want to change that by adjusting the text read in from a
user so that these variations are all recognized as “bye”.
The documentation of the String class tells us that it has a
method called trim to remove spaces at the beginning and the
end of the string.
We can use that method to handle the second problem case.
8
A Technical Support System

String objects are immutable


An object is said to be immutable if its contents or state cannot be changed once it
has been created. Strings are an example of immutable objects
trim method, for example, returns a new string;
input.toUpperCase();
Use the String class’s toLowerCase method to do this
Pitfall
It is a common error in Java to try to modify a string—for example by writing
input.toUpperCase();
This is incorrect (strings cannot be modified), but this unfortunately does not
produce an error. The statement simply has no effect, and the input string remains

unchanged.
9
Checking string equality

if(input == "bye")
{ // does not always work!
...
}

The problem here is that it is possible for several independent


String objects to exist that all represent the same text.
Two String objects, for example, could both contain the
characters ‘bye’. The equality operator (==) checks whether each
side of the operator refers to the same object, not whether they
have the same value! That is an important difference.
10
Checking string equality

11
Checking string equality

12
Checking string equality

13
Checking string equality

14
Checking string equality

15
Adding Random Behavior

Main problems with Technical Support-1 is that it always


gives the same response, independent of the user’s input.
To do this, we will use an ArrayList to store some response
strings, generate a random integer number, and use the
random number as an index into the response list to pick
one of our phrases.
We need a version that will response with different
responses. So the requester will feel look like the original
program and feel better.
16
The Random class
To generate a random number, we have to:
■ create an instance of class Random and
■ make a call to a method of that instance to get a number.

Looking at the documentation, we see that there are various


methods called nextSomething for generating random
values of various types. The one that generates a random
integer number is called nextInt.

17
The Random class
The following illustrates the code needed to generate and
print a random integer number:

Random randomGenerator;
randomGenerator = new Random();
int index = randomGenerator.nextInt();
System.out.println(index);

18
The Random class
The Random class also offers a method to support this.
It is again called nextInt, but it has a parameter to specify
the range of numbers that we would like to use.
 Find the nextInt method in class Random that allows
the target range of random numbers to be specified. What
are the possible random numbers that are generated
when you call this method with 100 as its parameter?
Write a method in your RandomTester class called
throwDice that returns a random number between 1 and 6
(inclusive).
19
The Random class

Write a method called getResponse that randomly returns


one of the strings "yes", "no", or "maybe".

Add a method to your RandomTester class that takes a


parameter max and generates a random number in the
range 1 to max (inclusive).

Add a method to your RandomTester class that takes two


parameters, min and max, and generates a random
number in the range min to max (inclusive).

20
Generating random responses

We shall now add code to this first version to:


■ declare a field of type Random to hold the random
number generator;
■ declare a field of type ArrayList to hold our possible
responses;
■ create the Random and ArrayList objects in the
Responder constructor;
■ fill the responses list with some phrases;
■ select and return a random phrase when
generateResponse is called.
shows a version of the Responder source code with these
additions.

21
Maps

Maps are collections that contain pairs of objects


Pairs consist of a key and a value
Lookup works by supplying a key, and retrieving a value
• An example: a telephone book

22
Maps

23
Maps

24
Maps

25
Maps

26
Maps

27
Maps

28
Maps

29
Maps

30

You might also like