String/Arrays in Java
String/Arrays in Java
Objective
After completing this lab, the students should be able to
- Understand the difference between primitive data types and user-
defined/reference type data types
- Understand the conditional statements
- Understand the usage of mathematic functions
- Understand the for, while and do while loops
- Learn how to take user’s input
Arrays
An array stores a sequence of values that are all of the same type. We want
not just to store values but also to be able to quickly access each individual
value. The length of an array is established when the array is created. After
creation, its length is fixed. Each item in an array is called an element,
and each element is accessed by its numerical index. The method that we use
to refer to individual values in an array is to number and then index them—
if we have n values, we think of them as being numbered from 0 to n−1.
elementType[] arrayRefVar;
The elementType can be any data type, and all elements in the array will
have the same data type.
The Java programming language supports basic arithmetic with its arithmetic
operators: +, -, *, /, and %. The Math class provides methods and constants
for doing more advanced mathematical computation.
The Math is located in the java.lang package, and not in the java.math package.
Thus, the fully qualified class name of the Math class is java.lang.Math .
The methods in the Math class are all static, so you call them directly from
the class, like this:
Math.cos(angle);
Note: Using the static import language feature, you don't have to
write Math in front of every math function:
import static java.lang.Math.*;
This allows you to invoke the Math class methods by their simple names. For
example:
cos(angle);
Constants
The Math class includes more than 40 static methods. They can be categorized as
trigonometric methods, exponent methods, and service methods. Service methods
include the rounding, min, max, absolute, and random methods.
Trigonometric Methods
The Math class contains the following methods.
The parameter for sin, cos, and tan is an angle in radians. The return value
for asin, acos, and atan is a degree in radians in the range between -pi/2
and pi/2. One degree is equal to pi/180 in radians, 90 degrees is equal to
pi/2 in radians, and 30 degrees is equal to pi/6 in radians.
Exponent Methods
There are five methods related to exponents in the Math class.
The Rounding Methods
The Math class contains five rounding methods
The abs method returns the absolute value of the number (int, long, float,
or double).
This method generates a random double value greater than or equal to 0.0 and
less than 1.0 (0 <= Math.random() < 1.0). You can use it to write a simple
expression to generate random numbers in any range.
Converting Strings
The toLowerCase() method returns a new string with all lowercase letters and
the toUpperCase() method returns a new string with all uppercase letters.
For example,
"Welcome".toLowerCase() returns a new string welcome.
"Welcome".toUpperCase() returns a new string WELCOME.
The trim() method returns a new string by eliminating whitespace characters
from both ends of the string. The characters ' ', \t, \f, \r, or \n are known
as whitespace characters.
Your application should then display a boarding pass indicating the person’s
seat number and whether it’s in the first-class or economy section of the
plane. Use a one-dimensional array of primitive type boolean to represent
the seating chart of the plane. Initialize all the elements of the array to
false to indicate that all the seats are empty. As each seat is assigned,
set the corresponding element of the array to true to indicate that the seat
is no longer available.
Your application should never assign a seat that has already been assigned.
When the economy section is full, your application should ask the person if
it’s acceptable to be placed in the first-class section (and vice versa). If
yes, make the appropriate seat assignment. If no, display the message "Next
flight leaves in 3 hours."
The article array should contain the articles "the", "a", "one", "some" and
"any"; the noun array should contain the nouns "boy", "girl", "dog", "town"
and "car"; the verb array should contain the verbs "drove", "jumped", "ran",
"walked" and "skipped"; the preposition array should contain the prepositions
"to", "from", "over", "under" and "on".