Sie sind auf Seite 1von 22

Object Oriented

Programming:
String, Random & Loops
PARAHYANGAN CATHOLIC UNIVERSITY
BANDUNG, 2017
String
o In computer programming, a String is a sequence of
characters.
o In Java, there is a predifned class called String class that
can be used to store and process strings of characters.
o Objects of type String are strings of characters that are
written within double quotes.
o Example: String motto = "Wombats are kind-
hearted.";
o We can also use String constructor to create a String.
o Example: String motto = new String("Wombats
are kind-hearted.");
String Concatenation
o Concatenation: connecting strings to get a longer string.
o Concatenation operator (+) is used to concatenate two
strings.
String Concatenation
String Methods
o These are several important String’s methods:
o int length()
o String trim()
o boolean equals(String otherString) & boolean
equalsIgnoreCase(String otherString)
o String toLowerCase() & String toUpperCase()
o char charAt(int position) & int indexOf(int character) & int
indexOf(String otherString)
o String substring(int beginIndex) & String substring(int
beginIndex,int endIndex)
o int compareTo(String otherString) : which comes first in
lexicographical order
Random Numbers
o Games and simulation programs often require the
computer to generate random numbers.
o For example, a card game might need a way to randomly
shuffle the cards in the deck or to roll a pair of dice.
o Java generates pseudorandom numbers (sequence of
numbers that looks random but this sequence of numbers
is initialized by a “seed” value)
o The same seed values will generate the exact same
sequence of numbers.
Generating Random Numbers
o Java provides two ways to generate random numbers:
o Random class that have several methods that can be used to
generate many different types of random numbers(int, float,
double, boolean).
o The method Math.random() that returns a random double that is
greater than or equal to 0.0 but less than 1.0.
Generating Random Numbers
Loops
o With a loop, a part of a program is repeated over and
over, until a specific goal is reached.
o Two types of loops in computer programming:
o You know exactly how many times you have to repeat
o You know the specific condition when to stop the repetition, but
you do not know how many times to repeat the action to achieve
that particular condition
Loops

o How about eats 10 times?


o Eats N times?
o Eats until weight >=100.0?
o Java provides several type of loops: while, do-while, for
While
o Syntax: while(conditions){
statement;
statement;
statement;

}

o As long as the condition is true, the statements will always be


repeated.
o while is the best choice to perform repetition when it does
not know how many times it should be repeated (but do know
what the condition that should make the repetition to stop)
While
While (More Examples)
Exercise
o Wombat needs to write all numbers between 1 to n, in
reversed order!
o Wombat needs to find the smallest prime factor of a
given integer!
do-while
o Syntax: do{
statement;
statement;
statement;

}while(conditions);

o The statements inside the block will always be repeated


as long as the condition is true.
o The do-while loop is appropriate when the loop body
must be executed at least once.
do-while
do-while
do-while
for
Syntax:

◦ initialization: one or more statements which are executed only one time in
the beginning of the iteration. The counter/iterator usually initialize in the
initialization
◦ condition: decide whether the block statement will be executed or not. If
the expression returns false, then the whole for statement is terminated
and the program will evaluate the next statement.
◦ updater: statement which is executed after the block statement is executed.
The counter/iterator’s value usually updated in the updater
for
Common Mistakes
o Forget the block statement
o Add a semicolon right behind the for statement
o The expression never returns a false value
o Modify the counter inside the block statement
Exercise
o  Wombats need to know how many vocal character are
there in their names. Write a program to help wombats to
solve this problem! Assume that wombat’s name only
consist of lowercase letters!
o Help the wombats to make a method to calculate the n
factorial!
o Given the value of n (positive integer), help the wombats
to make a method to aproximate the value of

Das könnte Ihnen auch gefallen