Lab 04 - Loops, Arrays and Strings: Comparison For Loop While Loop Do While Loop
Lab 04 - Loops, Arrays and Strings: Comparison For Loop While Loop Do While Loop
Introduction The Java for loop is a The Java while loop The Java do while
control flow is a control flow loop is a control
statement that statement that flow statement
iterates a part of executes a part of that executes a
the programs multipl the programs part of the
e times. repeatedly on the programs at least
basis of given once and the
boolean condition. further execution
depends upon the
given boolean
condition.
Java Arrays
Normally, an array is a collection of similar type of elements which has contiguous memory
location.
Java array is an object which contains elements of a similar data type. Additionally, The
elements of an array are stored in a contiguous memory location. It is a data structure where we
store similar elements. We can store only a fixed set of elements in a Java array.
Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd
element is stored on 1st index and so on.
Unlike C/C++, we can get the length of the array using the length member. In C/C++, we need
to use the sizeof operator.
In Java, array is an object of a dynamically generated class. Java array inherits the Object
class, and implements the Serializable as well as Cloneable interfaces. We can store primitive
values or objects in an array in Java. Like C/C++, we can also create single dimentional or
multidimentional arrays in Java.
Advantages
o Code Optimization: It makes the code optimized, we can retrieve or sort the data
efficiently.
o Random access: We can get any data located at an index position.
Disadvantages
o Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its
size at runtime. To solve this problem, collection framework is used in Java which grows
automatically.
Java String
In Java, string is basically an object that represents sequence of char values. An array of
characters works same as Java string. For example:
char[] ch={'j','a','v','a','t','p','o','i','n','t'};
is same as:
String s="javatpoint";
Java String class provides a lot of methods to perform operations on strings such as compare(),
concat(), equals(), split(), length(), replace(), compareTo(), intern(), substring() etc.`
Example
The java.lang.String class provides many useful methods to perform operations on sequence of
char values.
17 String[] split(String regex, int limit) returns a split string matching regex
and limit.
20 int indexOf(int ch, int fromIndex) returns the specified char value index
starting with given index.
package helloworld;
import java.util.Scanner;
public class testclass {
Lab Tasks:
1. Write java program to produce an n times multiplication table, where n is entered by the
user.
2. Declare and initialize array of 10 elements and assign it value 10 20 ---- 100.
3. Repeat question 2 and take elements from user.
4. Write a program that allow the user to enter age of five individual where age range from
25 to 60 using a ‘for’ loop, and using another loop find how many individuals are elder
than 30.
Post Lab
*
* *
* * *
* * * *
* * * * *
2. Define a 1-D array. A company pays its salespeople on a commission basis. The salespeople
receive Rs 200 per week plus 9% of their gross sales for that week. For example, a salesperson
who grosses Rs 5000 in sales in a week receives 200 plus 9 % of 5000, or a total of Rs 650. Write
a program (using an array of counters) that determines how many of the salespersons earned
salaries in each of the following ranges.
• 200-299
• 300-399
• 400-499
• 500-599
• 600-699
• 700-799
• 800-899
• 900-999
• 1000 -over
3. Read a string and a word from the user. Write a program to find the frequency of occurrence of
a word in the string