1. Tutorial - 04
Arrays
1. Write a program that declares an Array of size 5 and asks the user to enter different words
and store them into the array. The program will display all the words in the array.
Output :
Enter a word : Lorem
Enter a word : Ipsum
Enter a word : Dolor
Enter a word : Sit
Enter a word : Amet
Lorem
Ipsum
Dolor
Sit
Amet
2. Write the Java code to declare an integer array of 50 slots and assign the value of index * 3
to each slot. Then write the code to print the 5th, 12th and 48th element of the array.
Output :
5th
Element : 15
12th
Element : 36
48th
Element : 144
3. Write a program that requests a user to input an array of strings and then write a method to
remove duplicates from a given array and reprint the array to show that the duplications have
been removed.
Output :
Enter a String : Hello
Enter a String : Hello
Enter a String : Java
Enter a String : Hello
1st
String : Hello
2nd
String : Java
2. 4. Develop a method to count the number of vowels in a given array of characters.
Output :
Enter a String : abcdefgh
Number of vowels in String : 2
5. Read in 5 ints from the keyboard, and store them in an array. Find and print the position (or
index) of the maximum and minimum values in the array, and swap them (move the biggest
element to the position of the smallest, and move the smallest element to the position of the
biggest) and then print the same index to show swapping succeeded.
Output :
Enter a number : 8
Enter a number : 2
Enter a number : 9
Enter a number : 0
Enter a number : 10
Largest Element Position : 5
Smallest Element Position : 4
5th
Element in Array : 0
4th
Element in Array : 10
6. Set up an array to hold number of values that the user specifies, then the program randomly
assigns integer values to the array between 0 and 100. Write a program with three methods to :
a- Sort the array in ascending order and print them.
b- Sort the array in descending order and print them.
c- Search an element’s position in the array and print the position.
Output :
Elements in Array : 50, 0, 98, 75, 62
Array in Ascending Order : 0, 50, 62, 75, 98
Array in Descending Order : 98, 75, 62, 50, 0
Enter a number : 98
Element is in index : 2
3. 7. An anagram is a word or a phrase that can be created by rearranging the letters of another
given word or phrase. We ignore white spaces and letter case. All of the letters in "Desperation"
can be rearranged to the phrase "A Rope Ends It".
Implement a Java program that checks to given Strings whether one is an anagram of the other.
Output :
Enter 1st
String : Desperation
Enter 2nd
String : A Rope Ends It
The 2nd
String is an anagram of the 1st
String.