You are on page 1of 12

Basecamp Capability 2 Assignments

Introduction:
This document covers list of assignments/exercises related to second capability of
Basecamp program:

 Use arrays to structure the raw data and to perform data comparison &
operations

Observations:
KO: 1. Ability to work with multi-dimensional array - 1D and 2D arrays (primitive type
arrays)
KO: 2. Ability to work on string operations - search, concatenation, replace characters
3. Ability to manipulate data with the given requirement - modifying, update data in
arrays
KO: 4. Ability to pass array as argument to a method and return an array from a method

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Exercise 1: Sum of elements of an array


Write a program which creates an integer array and displays sum of its elements.

Exercise 2: Handling multiple arrays


Write a program which performs addition of elements which are stored in two arrays of
type double. Arrays lengths may be variable in size. The resultant values must be
stored in an integer array. Display the resultant integer array in a formatted way.

Example:
Input:

dInputArray1[] 10.0 20.0 30.0


dInputArray2[] 20.0 50.0 30.0 70.0 80.0
Basecamp Capability 2 Assignments

Output:

iSumArray[] 30 70 60 70 80

Exercise 3: Maximum of 3 numbers


Create a method GetMax(int a, int b, int c), that returns maximal of three numbers.
Write a program that reads three numbers from the console and prints the biggest of
them.
Example
Input Output Input Output
1 3 -100 -100
2 -101
3 -102

Exercise 4: Digits in reverse Order


Write a method that prints the digits of a given decimal number in a reversed order.
Example
Input Output
256 652

Exercise 5: Prime Number


Write a Boolean method IsPrime(n) that check whether a given integer number n is
prime.
Basecamp Capability 2 Assignments

Example

n IsPrime(n)
0 false
1 false
2 true
3 true
4 false
5 true
323 false
337 true
6737626471 true
117342557809 false

Exercise 6: Computing Initials


Write a program that computes your initials from your full name and displays them.
Example: If name = “Kavi Arasu V”, it must record the initials as “KAV”

Exercise 7: Generate password


Write a program which generates a password for a student using his initials and age.
Each student will have first name, middle name and last name.

Exercise 8: Exchanging Names


We have 2 student names Ali Al-Ali and Ahmed Al-Ahmed. Design and implement a
program that will exchange the last name of the two students in such a way that the new
names are going to be

Ali Al-Ahmed and Ahmed Al-Ali.


NOTE: Your program should work for any names NOT ONLY these given names in the
exercise.
Basecamp Capability 2 Assignments

Exercise 9: Compare two strings

Analyze the below code and invoke the method using main method.

Exercise 10: String Validations


In an online application, a university would like to validate the university seat number
(USN) entered by its student.
Example: A sample USN looks like: 1DS19CS010
Following are the constraints to enter registration number:
 Each USN must be length of 10 characters
 1st character must be digit and have value either '1' or '2'
 2nd and 3rd characters must be upper case letters
Basecamp Capability 2 Assignments

 4th and 5th characters must be digits and can have values between 0-9
 6th and 7th characters must be upper case letters and can have combination any
of following substring: CS, IS,EC and ME
 8th,9th and 10th characters must be digits and can have values between 0-9
 Create class called TestUSN which has main() method to:
 Initialize USN variable with string value
 Validate USN against above constraints
 Print "Success" or "Failure" message based on result of validation

Exercise 11: Reversing words


Given a sentence which consists of alphabets [a-zA-Z], digits [0-9], “,” and “.”.
Write a method which returns a string after reversing only the alphabets [a-zA-Z] in a
sentence which is passed as an argument the method.
Note: reversing has to be done word-by-word. Example: if sentence contains “Hello
World”, the reversed sentence will be “dlroW olleH”

Example Input Output


1 1 cup of hot coffee costs 8.00, 1 puc fo toh eeffoc stsoc 8.00, saerehw dloc
whereas cold coffee costs 45.00. eeffoc stsoc 45.00.
2 It Costs 25000rs for 1 LCD tI stsoC 25000sr rof 1 DCL rotcejorP.
Projector.
3 8990.33 8990.33

Exercise 12: String compression


Let us design a simple compression algorithm where only the frequency of individual
letters is used to compress the data. For e.g., the string Aabcccccaaa would become
a2b1c5a3. The compression logic should be applied only when the total length of the
compressed string is less than the original string. For the purpose of compression logic,
the case sensitiveness is not considered. For e.g. A and a are considered the same.

Input
A String that needs to be compressed is given. Given string always contains characters.
The string may contain characters in upper as well as lower case.
Basecamp Capability 2 Assignments

Output
Output is the compressed string or the same string if the compressed string length is
more than or equal to the length of the original string. Output contains string in
lowercase always.
Input: aAbcccccaaA output: a2b1c5a3
Input: BBBBbbb output: b7

Exercise 13: Unique Elements


Write a method which accepts two integer arrays and returns an array of unique
elements.
Example:
Array 1 = { 10, 5, 20, 15, 25, 30}
Array 2 = {50, 12, 5, 30, 15, 70}
Result_Array = {10, 20, 25, 50, 12, 70}

Int [] uniqElements(int array1[], int array2[]);

Exercise 14: Predict the Output

Analyze below given code and predict the output.


Basecamp Capability 2 Assignments

Exercise 15: Addition of two Matrix


Write a method which accepts two matrices of Size N X N and returns summation of
resultant Matrix.
Example:
Matrix A: [1,2,3] [4,5,6]
Matrix B: [4,5,6] [7,8,9]
Matrix C = A + B = [5,7,9] [11,13,15]

Exercise 16: Row Magic


Write a method
public static boolean isRowMagic(int[][] a)
that checks if the array is row-magic (this means that every row has the same row sum).
Basecamp Capability 2 Assignments

Exercise 17: Magic Array


Write a method
public static boolean isMagic(int[][] a)
that checks if the array is a magic square. This means that it must be square, and that
all row sums, all column sums, and the two diagonal-sums must all be equal.

Exercise 18: Student Marks


Create an array that stores the marks scored by students in a subject. You can assume
the size of the student array to be 10. The range of the marks must be 0 to 25. Write a
program to accept the marks of the students and count the number of students who
have scored each marks in the range, i.e you need to count how many students have
scored 0, how many students scored 1, how many students scored 2 ... how many
students scored 25. Implement the above logic using function/methods.

Exercise 19: LEADERS


Write a Java program to print all the LEADERS in the array.
Note: An element is leader if it is greater than all the elements to its right side.

Example:
Basecamp Capability 2 Assignments

Source: https://www.w3resource.com/java-exercises/array/java-array-exercise-39.php
Basecamp Capability 2 Assignments

Exercise 20: Find the output


Execute the program and understand the problems, if you find any error then correct it!

Exercise 21: Find the output


Execute the program and understand the problems, if you find any error then correct it!
Basecamp Capability 2 Assignments

Exercise 22: Find the output


Execute the program and understand the problems, if you find any error then correct it!

Exercise 23: Find the output

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Basecamp Capability 2 Assignments

Summary:
You must have learnt:
 How to declare and initialize array elements?
 How to work with 1D and 2D arrays?
 How to handle strings and perform search and other operations?
 How to process array elements?
 How to pass arguments to a method/functions and return a value?

You might also like