0% found this document useful (0 votes)
24 views2 pages

Java Ex 04

The document provides tutorials on using arrays and ArrayLists in Java programs. It includes examples of programs that read data into arrays, perform operations on array elements like reducing marks by 10% or swapping elements, find the largest or smallest element, shift elements, and sort an ArrayList. It also asks challenge questions about finding all pairs of elements from an array that sum to a given number, and removing "bad pairs" from an ArrayList where the left element is larger than the right.

Uploaded by

Nithin Kaneshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views2 pages

Java Ex 04

The document provides tutorials on using arrays and ArrayLists in Java programs. It includes examples of programs that read data into arrays, perform operations on array elements like reducing marks by 10% or swapping elements, find the largest or smallest element, shift elements, and sort an ArrayList. It also asks challenge questions about finding all pairs of elements from an array that sum to a given number, and removing "bad pairs" from an ArrayList where the left element is larger than the right.

Uploaded by

Nithin Kaneshan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Tutorial 5 : Arrays & ArrayLists

1. Write a program that reads in 10 student marks into an array called marks and computes the
number of students who have failed (got a mark less than 40). The program should then output
this number.

2. Write a program that reads in and stores 10 student marks and then reduces each mark by 10%
(because it was 24 hours late). Use a loop to reduce each mark. Each reduced mark is then
stored in the same element of the array. You should then output the contents of the array.

3. Write a program that reads in 100 doubles and stores them in an array called nums. The program
should then print out the largest number.

4. Write a program that reads in a sequence of ints into an array. The program should then shift
each value on one position in the array. For example, whatever is in array position 4 should be
moved into position 5. Whatever is in the last should be moved to the start. The program should
then print out the contents of the array.

5. Write a program where an array of length 20 say is filled with doubles. The user then inputs any
two integer values i and j and the values in the array positions i and j are swapped around. The
program should then print out the contents of the array.

6. Extend your answer above. You need to write a method (function) called swap(). swap() accepts
two integer parameters and swaps the contents of an array and those locations. This method
does not return a value. Implement this method into a suitable java program by demonstrating
how the method changes the contents of the array.

7. Write a program to get 10 Integers from the user and store them in an array called
myNamesArray. then clone the Integers in the myNames into an ArrayList called
myNamesArrayList. Sort the myNamesArrayList using the sort method.

8. Are arrays passed by reference or passed by value? Justify your answer with an example code
snippet.

Challenge Questions : Try on your own :

9. How do you find all pairs whose sum is equal to a given number from an integer array in
Java? You have given an array of int primitives and a number, you need to find all pairs in an
array whose sum is equal to given number e.g. if an array is {1, 2, 3,  4, 5} and given sum is 6
then your program should return {2, 4} and {1, 5}.

Software Development II
10. Write a method removeBadPairs that accepts an ArrayList of integers and removes any adjacent
pair of integers in the list if the left element of the pair is larger than the right element of the
pair. Every pair's left element is an even-numbered index in the list, and every pair's right
element is an odd index in the list.

For example, suppose a variable called list stores the following element values: [3, 7, 9, 2, 5, 5, 8,
5, 6, 3, 4, 7, 3, 1] We can think of this list as a sequence of pairs: (3, 7), (9, 2), (5, 5), (8, 5), (6, 3),
(4, 7), (3, 1). The pairs (9, 2), (8, 5), (6, 3), and (3, 1) are "bad" because the left element is larger
than the right one, so these pairs should be removed. So the call of removeBadPairs(list); would
change the list to store the following element values: [3, 7, 5, 5, 4, 7] If the list has an odd length,
the last element is not part of a pair and is also considered "bad;" it should therefore be
removed by your method. If an empty list is passed in, the list should still be empty at the end of
the call. You may assume that the list passed is not null. You may not use any other arrays, lists,
or other data structures to help you solve this problem, though you can create as many simple
variables as you like.

Software Development II

You might also like