Task #9: Using Arrays and Methods
In this task, you are being asked to write methods that manipulate arrays in Java.
Write a method that swaps the elements of the array as follows:
Swap the value of n-1’th index with the value of 0th index.
Swap the value of i’th index with the value of i+1’th index.
You may use the following header for this method:
static int[] arraySwapValues(int[] array)
For example, suppose that the array array has 10 values: 1 2 3 4 5 6 7 8 9 10
The method will return an array which would contain the values: 2 3 4 5 6 7 8 9 10 1
NOTE: Declare and initialize the array without taking the input from user.
1. Create a program called ArraySwapValuesLab1.java.
2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.
Page 8
Task #8: Using Arrays and Methods
In this task, you are being asked to write methods that manipulate arrays in Java.
Write a method called fillArray() which will take an integer array as input and the fills the array
with the user entered numbers.
You may use the following header for this method:
static void fillArray(int[] array)
Write a method called absoluteArray() which will take an integer array as input and takes the
absolute of all the array elements.
You may use the following header for this method:
static void absoluteArray(int[] array)
Take a number from the user as the size of the array, call the method fillArray(), then call the
method absoluteArray().
NOTE: Perform input validation so that the array size must be greater than 0.
1. Create a program called ArrayAbsoluteLab1.java.
2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.
Task #7: Using Arrays and Methods
In this task, you are being asked to write methods that manipulate arrays in Java.
Write a method called fillArray() which will take an integer array as input and the fills the array
with the user entered numbers.
You may use the following header for this method:
static void fillArray(int[] array)
Write another method called printSumAverage() which takes an integer array as input and prints
the sum and average of the array elements.
You may use the following header for this method:
static void printSumAverage(int[] array)
Take a number from the user as the size of the array, call the method fillArray(), then call the
method printSumAverage().
NOTE: Perform input validation so that the size must be greater than 0.
1. Create a program called ArraySumAverageLab1.java.
2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.
Task #5: Writing value returning Method
In this task, you are being asked to write value returning method in Java.
Your job is to write a program which will take two integer numbers as input and returns:
0, if both numbers are equal.
1, if first number is greater than second number.
-1, if first number is less than second number.
You may use the following header for this method:
static int compare(int firstNumber, int secondNumber)
Take input from the user, call the method and print the appropriate messages according to the
result.
1. Create a program called CompareNumberLab1.java.
2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.s
Task #4: Writing value returning Methods
In this task, you are being asked to write value returning methods in Java.
Write a method called charAtPosition() that accepts two arguments: a String object, and a
positive integer. It then returns the character located at position given by the integer argument.
You may use the following header for this method:
static char charAtPosition(String word, int position)
For example, if the method is called with the arguments, "GIFT University" and 5, the method
returns the character ‘U’ and a sample output should be:
Character at position 5 is: U
NOTE: A String starts from character position 0. Also, perform input validation so that the
position must be greater than or equal to 0.
HINT: Use the charAt(index) method of the String to find the character in a String.
1. Create a program called CharacterPositionLab1.java.
2. Correctly call the method with appropriate arguments.
3. Correctly display appropriate messages.
Task #3: Writing void Methods
In this task, you are being asked to write void methods in Java.
Write a method called printTable() that accepts three positive integer arguments, and then prints
the table of the number from the starting number to the ending number.
You may use the following header for this method:
static void printTable(int number, int start, int end)
NOTE: Perform input validation so that all numbers must be greater than 0 and the “start” number
should be less than “end” number.
1. Create a program called TablesLab1.java.
2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.
In this task, you are being asked to write void methods in Java.
Write a method called wordsInfo() that accepts one String argument, and prints how many
characters are there in that argument, as well as the number of vowels.
You may use the following header for this method:
static void wordsInfo(String word)
For this exercise, assume that a e i o u y are vowels. For example, if the method is called with an
argument "Harry", the method prints:
wordsInfo(“Harry”);
//method prints
5 characters.
2 vowels.
1. Create a program called CharactersLab1.java
2. Create appropriate variables and assign values using a Scanner object.
3. Correctly display appropriate messages.
You will need to create a class FamilyMember with the required attributes i.e. names (String), role (String), gender (char), and age (int). Provide the constructor and getters and setters.
You will also need to create a class Family that will contain a collection (ArrayList) for family member instances. Provide appropriate constructor. Provide methods addFamilyMember (name, role, gender and age) that adds a member to the collection. You will call this method at an appropriate place in the GUI layer
Provide another method getAllMembers() that returns an array containing all instances of family members in this collection. You will call this method at an appropriate place in the GUI
Write a program to store 15 numbers in an array. Then display all two digits number in ascending order using sorting and also display how many such numbers found.
Write a program to store 15 numbers in an array. Then display all negative numbers in ascending order using sorting.