Java Interview Coding

¡Supera tus tareas y exámenes ahora con Quizwiz!

String -- Reverse Write a return method that can reverse String Ex: Reverse("ABCD"); ==> DCBA

public static String Reverse(String str) { return new StringBuffer(str).reverse().toString()); }

ArrayList -- Remove some values Given a list of Integers 1, 2, 3, 4, 5, 6 ....etc. remove all values greater than 100.

ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300)); list.removeIf(p -> p>100); System.out.println(list);

Map -- Unique character from String

public static void main(String[] args) { String str = "accabbcd"; Map<Character, Integer> unique = new HashMap<>(); for( int j = 0; j < str.length(); j++) { int count =0; for (int i = 0; i < str.length(); i++) { if(str.charAt(i) == str.charAt(j)) count++; } if( count == 1){ unique.put(str.charAt(j), count); } } System.out.println(unique);}

Numbers -- Swap Numbers Swap two variable' values without using a third variable

int a = 10; int b = 20; a = a +b; b = a - b; a = a - b;

Map -- Sort the map by values Write a method that can sort the Map by values

public static Map<String, Integer> sortByValue(Map<String, Integer> map){ List<Entry<String, Integer>> list = new ArrayList(map.entrySet()); list.sort(Entry.comparingByValue()); map = new LinkedHashMap(); for(Entry<String, Integer> each : list) { map.put(each.getKey(), each.getValue()); } return map; }

String -- Frequency of Characters(2)

public static String FrequencyOfChars(String str) { String expectedResult = ""; int j=0; while( j < str.length()) { int count = 0; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == str.charAt(j)) { count++; } } expectedResult +=str.charAt(j)+"" + count; str = str.replace(""+str.charAt(j) , "" ); } return expectedResult; }

String -- Frequency of Characters(1)

public static String FrequencyOfChars(String str) { String nonDup = ""; for(int i=0; i < str.length(); i++) if(!nonDup.contains(""+str.charAt(i))) nonDup+= ""+str.charAt(i); String expectedResult = ""; for( int j=0;j < nonDup.length(); j++) { int count = 0; for(int i=0; i < str.length(); i++) { if(str.charAt(i) == nonDup.charAt(j)) count++; } expectedResult +=nonDup.charAt(j)+"" + count; } return expectedResult; }

String -- Reverse Write a return method that can reverse String Ex: Reverse("ABCD"); ==> DCBA

public static String StrReverse(String str) { String reverse=""; for(int i=str.length()-1; i >= 0; i--) reverse += str.toCharArray()[i]; return reverse; }

String -- Remove Duplicates Write a return method that can remove the duplicated values from String Ex: removeDup("AAABBBCCC") ==> ABC

public static String removeDup( String str) { String result = ""; for (int i = 0; i < str.length(); i++) if (!result.contains("" + str.charAt(i))) result += "" + str.charAt(i); return result; }

String -- Remove Duplicates Write a return method that can remove the duplicated values from String Ex: removeDup("AAABBBCCC") ==> ABC

public static String removeDup(String str) { str = new LinkedHashSet<String>(Arrays.asList(str.split(""))).toString(); str = str.replace(", " , "" ).replace("[","").replace("]",""); return str; }

Numbers -- Armstrong numbers Write a method that can check if a number is Armstrong number

public static boolean ArmStrongNumber (int num) { int a = 0, b =0, c= num; while(num>0){ a=num%10; num/=10; b=b+(a*a*a); } if(c==b) { return true; } return false; }

String -- Password Validation Task 1. Write a return method that can verify if a password is valid or not. requirements: 1. Password MUST be at least have 6 characters and should not contain space 2. PassWord should at least contain one upper case letter 3. PassWord should at least contain one lowercase letter 4. Password should at least contain one special characters 5. Password should at least contain a digit if all requirements above are met, the method returns true, otherwise returns false

public static boolean PassWordvalidation(String password) { String lowercase="(.*[a-z].*)"; String uppercase="(.*[A-Z].*)"; String numbers="(.*[0-9].*)"; String specialchars="(.*[ -/, :-@].*)"; boolean HasLower = password.matches(lowercase), HasUppere = password.matches(uppercase), HasDigits = password.matches(numbers), HasSpecial = password.matches(specialchars), Valid = false; if(password.length() >= 6 && !password.contains(" ")) if( HasLower && HasUppere && HasDigits && HasSpecial) Valid = true; return Valid; }

Numbers -- Prime Number Write a method that can check if a number is prime or not

public static boolean primeNumber(int num) { boolean result = false; if(num <= 1) { return result; } for(int i = 2; i < num; i++) { if(num % i == 0 ) { return true; } } return result; }

String -- Same letters Write a return method that check if a string is build out of the same letters as another string. Ex: same("abc", "cab"); -> true same("abc", "abb"); -> false:

public static boolean same(String a, String b) { char[] ch1 = a.toCharArray(), char[] ch2 = b.toCharArray(); Arrays.sort(ch1); Arrays.sort(ch2); String a1="", a2=""; for(char each: ch1) a1 +=each; for(char each: ch2) a2 +=each; return a1.equals(a2) ; }

Array -- Find Minimum Write a method that can find the maximum number from an int Array

public static int maxValue( int[] n ) { Arrays.sort( n ); return n [ 0 ]; }

Array -- Find Maximum Write a method that can find the maximum number from an int Array

public static int maxValue( int[] n ) { Arrays.sort( n ); return n [ n.lenth-1 ]; }

Array -- Find Maximum Write a method that can find the maximum number from an int Array

public static int maxValue( int[] n ) { int max = Integer.MIN_VALUE; for(int each: n) if(each > max) max = each; return max; }

Array -- Find Minimum Write a method that can find the maximum number from an int Array

public static int maxValue( int[] n ) { int min = Integer.MAX_VALUE; for(int each: n) if(each < min) min = each; return min; }

Map -- Min value Write a method that can return the minimum value from ta map (DO NOT use sort method)

public static int minValue( Map<String,Integer> map ) { SortedSet<Integer> sm = new TreeSet<>(map.values()); return sm.first( ); }

Number -- Reverse negative number Write a return method that can reverse negative number and return it as int

public static int reverseNum(int num) { String str = new StringBuilder(""+num).reverse().toString(); if(num < 0) { str = "-"+str.substring(0, str.length()-1); } return Integer.valueOf(str); }

String -- sum of digits in a string Write a method that can return the sum of the digits in a string

public static int sumOfDigits(String s) { int total = 0; char[] ch = s.toCharArray(); for(char each: ch) { if(Character.isDigit(each)) { total += Integer.valueOf(""+each); } } return total; }

Array -- N unique integers that sum up to 0 Write a function: that, given an integer N (1 < N < 100), returns an array containing N unique integers that sum up to 0. The function can return any such array. For example, given N = 4, the function could return [1,0, -3,2] or [-2,1, -4,5]. The answer [1, -1,1,3] would be incorrect (because value 1 occurs twice). For N = 3 one of the possible answers is [-1,0,1] (but there are many more correct answers).

public static int[] Solution(int N){ int[] result = new int[N]; int sum = 0; for(int i=0; i < N-1; i++) { result[i] =i; sum += i; } result[N-1] = -sum; return result; }

Numbers -- Divide without / operator Write a method that can divide two numbers without using division operator

public static void divides(int num1, int num2) { if(num2==0) { System.out.println("Invalid Number"); return; } System.out.print(num1 +" divided by "+num2 +" is: "); int count =0; while(num1 >= num2) { num1 -= num2; count++; } System.out.println(count+" and remainder is "+num1); }

Numbers -- Divisible by 3, 5, 15 Write a program that can print the numbers between 1 ~ 100 that can be divisible by 3, 5, and 15. if the number can be divisible by 3, 5 and 15, then it should only be displayed in DivisibelBy15' section if the number can be divisible by 3 but cannot be divisible by 15, then it should only be displayed in DivisibelBy3' section if the number can be divisible by 5 but cannot be divisible by 15, then it should only be displayed in DivisibelBy5' section ex: OutPut: Divisible By 15 15 30 45 60 75 90 Divisible By 5 5 10 20 25 35 40 50 55 65 70 80 85 95 100 Divisible By 3 3 6 9 12 18 21 24 27 33 36 39 42 48 51 54 57 63 66 69 72 78 81 84 87 93 96 99

public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); for(int i=0; i < 100; i++) list.add(i+1); System.out.print("Divisible By 15: "); list.forEach( p -> { if(p%15==0 && p%3==0) System.out.print(p+" "); } ) ; System.out.print("\nDivisible By 5: "); list.forEach( p -> { if(p%5==0 && p%15!=0) System.out.print(p+" "); } ) ; System.out.print("\nDivisible By 3: "); list.forEach( p -> { if(p%3==0 && p%15!=0) System.out.print(p+" "); } ) ; }

Numbers -- Divisible by 3, 5, 15 Write a program that can print the numbers between 1 ~ 100 that can be divisible by 3, 5, and 15. if the number can be divisible by 3, 5 and 15, then it should only be displayed in DivisibelBy15' section if the number can be divisible by 3 but cannot be divisible by 15, then it should only be displayed in DivisibelBy3' section if the number can be divisible by 5 but cannot be divisible by 15, then it should only be displayed in DivisibelBy5' section ex: OutPut: Divisible By 15 15 30 45 60 75 90 Divisible By 5 5 10 20 25 35 40 50 55 65 70 80 85 95 100 Divisible By 3 3 6 9 12 18 21 24 27 33 36 39 42 48 51 54 57 63 66 69 72 78 81 84 87 93 96 99

public static void main(String[] args) { String divisibleBy15 =""; String divisibleBy5 =""; String divisibleBy3=""; List<Integer> list = new ArrayList<Integer>(); for(int i=0; i < 100; i++) list.add(i+1); for(int each: list) { if(each %15==0 && each %3==0) divisibleBy15+= each+" "; if(each %5==0 && each % 15!=0) divisibleBy5 += each+" "; if(each%3==0 && each %15!=0) divisibleBy3 += each+" "; } System.out.println("Divisible By 15: "+divisibleBy15); System.out.println("Divisible By 5: "+divisibleBy5); System.out.println("Divisible By 3: "+divisibleBy3); }

Numbers -- Divisible by 3, 5, 15 Write a program that can print the numbers between 1 ~ 100 that can be divisible by 3, 5, and 15. if the number can be divisible by 3, 5 and 15, then it should only be displayed in DivisibelBy15' section if the number can be divisible by 3 but cannot be divisible by 15, then it should only be displayed in DivisibelBy3' section if the number can be divisible by 5 but cannot be divisible by 15, then it should only be displayed in DivisibelBy5' section ex: OutPut: Divisible By 15 15 30 45 60 75 90 Divisible By 5 5 10 20 25 35 40 50 55 65 70 80 85 95 100 Divisible By 3 3 6 9 12 18 21 24 27 33 36 39 42 48 51 54 57 63 66 69 72 78 81 84 87 93 96 99

public static void main(String[] args) { String divisibleBy15 =""; String divisibleBy5 =""; String divisibleBy3=""; int[] arr = new int[100]; for(int i=0; i < arr.length; i++) arr[i] = i+1; for(int each: arr) { if(each %15==0 && each %3==0) divisibleBy15+= each+" "; if(each %5==0 && each % 15!=0) divisibleBy5 += each+" "; if(each%3==0 && each %15!=0) divisibleBy3 += each+" "; } System.out.println("Divisible By 15: "+divisibleBy15); System.out.println("Divisible By 5: "+divisibleBy5); System.out.println("Divisible By 3: "+divisibleBy3); }

Array -- permutation combinations Given an array of 3 characters print all permutation combinations from the given characters

public static void printPermutation(char[] ch) { for(String s: permutation(ch)) System.out.println(Arrays.toString( s.toCharArray( ) ) ); }public static Set<String> permutation(char[] ch) { String str = Arrays.toString(ch).replace(", ", "").replace("[", "").replace("]", ""); Set<String> set = new LinkedHashSet<>(); if (str.length() == 1) set.add(str); else for (int i=0; i<str.length(); i++){ String a3 = str.substring(0, i)+ str.substring(i+1); char[] ch2 = a3.toCharArray(); for (String permutation : permutation(ch2)) set.add(str.charAt(i) + permutation); } return set; }

Numbers -- print consecutive numbers Write a function: that, given a positive integer N, prints the consecutive numbers from 1 to N, each on a separate line. However, any number divisible by 2, 3 or 5 should be replaced by the word Codility, Test or Coders respectively. If a number is divisible by more than one of the numbers: 2,3 or 5, it should be replaced by a concatenation of the respective words Codility, Test and Coders in this given order. For example, numbers divisible by both 2 and 3 should be replacée by CodilityTest and numbers divisible by all three numbers: 2,3 and 5, should be replaced by CodilityTestCoders. For example, here is the output for N = 24: 1 Codility Test Codility Coders CodilityTest 7 Codility Test Codi1ityCoders 11 CodilityTest 13 Codility TestCoders Codility 17 ....

public static void solution(int N) { String result = ""; for (int i = 1; i <= N; i++) { if(i %2 ==0 && i%3 == 0 && i %5==0) result += "CodilityTestCoders\n"; else if(i %2 ==0 && i%3 == 0) result += "CodilityTest\n"; else if(i % 2==0 && i %5==0) result += "CodilityCoders\n"; else if(i % 3 == 0 && i % 5 ==0) result +="TestCoders\n"; else if(i % 2 ==0) result += "Codility\n"; else if (i % 5 == 0) result += "Coders\n"; else if (i % 3 == 0) result += "Test\n"; else result += i + "\n"; } System.out.println(result); }

ArrayList -- Remove "Ahmed" Given a list of people' names: "Ahmed", "John", Eric", "Ahmed"..... Write a java operation to remove all the names named Ahmed

ArrayList<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", Eric", "Ahmed".)); names.removeAll( Arrays.asList("Ahmed")); System.out.println(names);

ArrayList -- Remove some values Given a list of Integers 1, 2, 3, 4, 5, 6 ....etc. remove all values greater than 100.

List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300)); Iterator<Integer> it = list.iterator(); while(it.hasNext()) if(it.next()>100) it.remove(); System.out.println(list);

ArrayList -- Remove some values Given a list of Integers 1, 2, 3, 4, 5, 6 ....etc. remove all values greater than 100.

List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300)); for(ListIterator<Integer> il = list.listIterator(); il.hasNext();) if(il.next()>100) il.remove(); System.out.println(list);

ArrayList -- Remove "Ahmed" Given a list of people' names: "Ahmed", "John", Eric", "Ahmed"..... Write a java operation to remove all the names named Ahmed

List<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", Eric", "Ahmed".)); Iterator<String> it = names.iterator(); while(it.hasNext()) { if(it.next().equals("Ahmed")) it.remove(); } System.out.println( names );

Array -- Sort Descending Write a return method that can sort an int array in descending order without using the sort method of the Arrays class Ex: int[] arr = {10,20,7, 8, 90}; arr = Sort(arr); ==> {90, 20, 10, 8, 7};

public static void SortingArrayDesc(int[] arr) { ArrayList<Integer> list = new ArrayList(); for(int each: arr) { list.add(each); } for (int i = 0; i < list.size(); i++) { for (int j = 0; j < list.size(); j++) { if (list.get(i) > list.get(j)) { Integer temp = list.get(i); list.set(i, list.get(j)); list.set(j, temp); } } } for(int i=0; i < list.size(); i++) { arr[i] = list.get(i); } }

ArrayList -- sorting in ascending Write a method that can sort the ArrayList in Ascending order without using the sort method

public static void SortingArrayListAsc(List<Integer> list) { for (int i = 0; i < list.size(); i++) { for (int j = 0; j < list.size(); j++) { if (list.get(i) < list.get(j)) { Integer temp = list.get(i); list.set(i, list.get(j)); list.set(j, temp); } } } }

ArrayList -- sorting in descending order Write a method that can sort the ArrayList in descending order without using the sort method

public static void SortingArrayListDesc(List<Integer> list) { for (int i = 0; i < list.size(); i++) { for (int j = 0; j < list.size(); j++) { if (list.get(i) > list.get(j)) { Integer temp = list.get(i); list.set(i, list.get(j)); list.set(j, temp); } } } }

ArrayList -- Remove some values Given a list of Integers 1, 2, 3, 4, 5, 6 ....etc. remove all values greater than 100.

ArrayList<Integer> list1 = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,101,200,300)); ArrayList<Integer> list2 = new ArrayList<>(); for(int each : lists1) if( each < 100) lists2.add(each); list1=list2; System.out.println(list1);

ArrayList -- Remove "Ahmed" Given a list of people' names: "Ahmed", "John", Eric", "Ahmed"..... Write a java operation to remove all the names named Ahmed

List<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", Eric", "Ahmed".)); List<String> names2 = new ArrayList<>(); names.forEach(p-> {if(p.equals("Ahmed")) names2.add(p);} ); System.out.println(names2);

ArrayList -- Remove "Ahmed" Given a list of people' names: "Ahmed", "John", Eric", "Ahmed"..... Write a java operation to remove all the names named Ahmed

List<String> names = new ArrayList<>(Arrays.asList("Ahmed", "John", Eric", "Ahmed".)); for(ListIterator<String> it=names.listIterator(); it.hasNext();) if(it.next().equals("Ahmed")) it.remove(); System.out.println(names);

String -- Find the unique Write a return method that can find the unique characters from the String Ex: unique("AAABBBCCCDEF") ==> "DEF";

Solution 1: public static String unique(String str) { String[ ] arr=str.split(""); String unique1=""; for(int j=0; j<arr.length; j++) { int num=0; for(int i=0; i<arr.length; i++ ) { if(arr[i].equals(arr[j]) ) num++; } if(num==1) unique1+=arr[j]; } return unique1; }

Numbers -- odd & even Write a method which can identifies given number is even or odd EX: identify(5) -> "Odd" identify(6) -> "Even"

public static String identifyOddEven( int n ) { return n%2==0 ? "Even" : "odd" ; }

String -- Frequency of Characters(4)

public static String frequency(String str) { String nonDup="", result=""; for(int i=0; i < str.length(); i++) if(! nonDup.contains(""+str.charAt(i))) nonDup += ""+str.charAt(i); for(int i=0; i < nonDup.length(); i++) { int num = Collections.frequency( Arrays.asList(str.split("") ) , ""+nonDup.charAt( i ) ); result += ""+nonDup.charAt(i) + num; } return result; }

Numbers -- Swap Numbers Swap two variable' values without using a third variable

int a = 10; int b = 20; a = a^b; b = a^b; a = a^b;

String -- Frequency of Characters(3) Write a return method that can find the frequency of characters Ex: FrequencyOfChars("AAABBCDD") ==> A3B2C1D2

public static String FrequencyOfChars(String str) {String b=new LinkedHashSet<>(Arrays.asList(str.split(""))).toString(); b = b.replace(", ","").replace("[","").replace("]",""); String result=""; for(int j=0; j<b.length();j++) { int count=0; for(int i=0; i<str.length(); i++) { if (str.substring(i, i + 1).equals("" + b.charAt(j))) { count++; } } result+=b.substring(j, j+1)+count; }return result; }

String -- Find the unique Write a return method that can find the unique characters from the String Ex: unique("AAABBBCCCDEF") ==> "DEF";

public static String Unique(String str) { String result =""; for(String each : str.split("")) result +=( (Collections.frequency(Arrays.asList(str.split("")), each)) ==1 ) ? each : ""; return result; }

String -- Same letters Write a return method that check if a string is build out of the same letters as another string. Ex: same("abc", "cab"); -> true same("abc", "abb"); -> false:

public static boolean Same(String str1, String str2) { str1 = new TreeSet<String>(Arrays.asList( str1.split("") ) ).toString( ); str2 = new TreeSet<String>(Arrays.asList( str2.split("") ) ).toString( ); return str1.equals(str2); }

String -- Sort Letters and Numbers from alphanumeric String Given alphanumeric String, we need to split the string into substrings of consecutive letters or numbers, sort the individual string and append them back together Ex: Input: "DC501GCCCA098911" OutPut: "CD015ACCCG011899"

public static void SortLettersAndNumbersFromString(String str) { String str2 = ""; for(int i = 0; i < str.length(); i++) { str2 += ""+str.charAt(i); if(Character.isAlphabetic(str.charAt(i)) && i < str.length()-1 ) { if(Character.isDigit(str.charAt(i+1)) ) { str2 +=","; } } if(Character.isDigit(str.charAt(i)) && i < str.length()-1) { if(Character.isAlphabetic(str.charAt(i+1))) { str2 +=","; } } } String[] arr = str2.split(","); str =""; for(String each: arr) { char[] chars=each.toCharArray(); Arrays.sort(chars); for(char eachChar: chars){ str +=""+eachChar; } } System.out.println(str); }

Array -- Sort Descending Write a return method that can sort an int array in descending order without using the sort method of the Arrays class Ex: int[] arr = {10,20,7, 8, 90}; arr = Sort(arr); ==> {90, 20, 10, 8, 7};

public static int[] Sort(int[] a) { ArrayList<Integer> list=new ArrayList<Integer>(); for(int each: a) list.add(each); for(int i=0; i < a.length; i++) { a[i] = findMax(list); list.remove(Integer.valueOf(a[i])); } return a; } public static int findMax(ArrayList<Integer> a) { int max=Integer.MIN_VALUE; for(int each: a) max = Math.max(max, each); return max; }

Array -- Sort Ascending Write a return method that can sort an int array in Ascending order without using the sort method of the Arrays class Ex: int[] arr = {10, 9, 8, 7}; arr = Sort(arr); ==>{ 7, 8, 9, 10};

public static int[] Sort(int[] a) { ArrayList<Integer> list=new ArrayList<Integer>(); for(int each: a) list.add(each); for(int i=0; i < a.length; i++) { a[i] = findMin(list); list.remove(Integer.valueOf(a[i])); } return a; } public static int findMin(ArrayList<Integer> a) { int min =Integer.MAX_VALUE; for(int each: a) min = Math.min(min, each); return min; }

Array -- Concat two arrays Write a return method that can concatenate two arrays

public static int[] concatTwoArrays(int[] arr1 , int[] arr2) { int[] result = new int[arr1.length + arr2.length]; int i = 0; for(int each: arr1) { result[i] = each; i++; } for(int each: arr2) { result[i] =each; i++; } return result; }

Array -- Move Zeros to the End Write a method that can move all the zeros to last indexes of the array (Do Not Use Sort Method) Ex: input: {1,0,2,0,3,0,4,0}; output: [1, 2, 3, 4, 0, 0, 0, 0]

public static int[] moveZeros( int[] arr ) { ArrayList<Integer> list = new ArrayList<>(); int countZero = 0; for(int each: arr) { list.add(each); countZero+= (each==0)?1:0; } list.removeAll(Arrays.asList(0)); arr = new int[arr.length]; for(int i=0; i <arr.length-countZero; i++ ) { arr[i] = list.get(i); } return arr; }

Numbers -- FINRA Write a method which prints out the numbers from 1 to 30 but for numbers which are a multiple of 3, print "FIN" instead of the number and for numbers which are a multiple of 5, print "RA" instead of the number. for numbers which are a multiple of both 3 and 5, print "FINRA" instead of the number.

public static void FINRA() { String result = ""; for(int i=1; i <= 30; i++) { if(i % 5==0 && i %3 ==0) result += "FINRA "; else if(i%5 == 0) result += "RA "; else if(i%3==0) result+="FIN "; else result += i+" "; } System.out.println(result); }

Numbers -- FINRA Write a method which prints out the numbers from 1 to 30 but for numbers which are a multiple of 3, print "FIN" instead of the number and for numbers which are a multiple of 5, print "RA" instead of the number. for numbers which are a multiple of both 3 and 5, print "FINRA" instead of the number.

public static void FINRA() { String result = ""; for(int i=1; i <= 30; i++) { result += (i % 5 ==0 && i %3 ==0)? "FINRA " : (i%5 == 0) ? "RA " :(i%3 == 0) ? "FIN " : i+" "; } System.out.println(result); }

Numbers -- FINRA Write a method which prints out the numbers from 1 to 30 but for numbers which are a multiple of 3, print "FIN" instead of the number and for numbers which are a multiple of 5, print "RA" instead of the number. for numbers which are a multiple of both 3 and 5, print "FINRA" instead of the number.

public static void FINRA() { String[] myarr= new String[30]; for( int i=0; i <= 29; i++ ) myarr[i] = ""+(i+1); for(int j=0; j<myarr.length; j++) if(Integer.valueOf(myarr[j])%3==0 && new Integer(myarr[j])%5==0) myarr[j]="FINRA"; else if (Integer.valueOf(myarr[j])%3==0) myarr[j]="FIN"; else if (Integer.valueOf(myarr[j])%5==0) myarr[j]="RA"; System.out.println(Arrays.toString(myarr)); }

Map -- Frequency of Characters Write a method that prints the frequency of each character from a String

public static void FrequencyTest(String str ) { Map<Character, Integer> map = new LinkedHashMap<>(); for (Character each : str.toCharArray()) { if (map.containsKey(each)) { map.put(each, map.get(each) + 1); } else { map.put(each, 1); } } System.out.println(map); }

Array -- Sort Ascending Write a return method that can sort an int array in Ascending order without using the sort method of the Arrays class Ex: int[] arr = {10, 9, 8, 7}; arr = Sort(arr); ==>{ 7, 8, 9, 10};

public static void SortingArrayAsc(int[] arr) { ArrayList<Integer> list = new ArrayList(); for(int each: arr) { list.add(each); } for (int i = 0; i < list.size(); i++) { for (int j = 0; j < list.size(); j++) { if (list.get(i) < list.get(j)) { Integer temp = list.get(i); list.set(i, list.get(j)); list.set(j, temp); } } } for(int i=0; i < list.size(); i++) { arr[i] = list.get(i); } }


Conjuntos de estudio relacionados

Med Surg Success: Dysrhythmias and Conduction Problems

View Set

Solar System Final (Mercury, Venus, Mars, Asteroids, Jupiter & Saturn, Uranus & Neptune, Comets, Exoplanets, Earth's Moon, Earth as a Terrestrial Planet, The Origin of the Solar System)

View Set

A.P. Biology 2nd Trimester Exam (MC)

View Set

Health Assessment Chapter 1 Practice Questions

View Set

OB Chapter 20: Postpartum Adaptations

View Set