Java Technical Tasks

Ace your homework & exams now with Quizwiz!

Swap two numbers without using temp value

// one line methods a = a ^ b ^ (b = a); //method 1 b = (a + b) - (a = b); //method 2 a += b - (b = a); //method 3 // temporary variable int temp = a; a = b; b = temp;

Reverse String

String reverse = arrList.stream() // Stream .map(String::valueOf) // Stream .collect(Collectors.joining());

Medriven

for(int x = 1; x<=5; x++) { for(int y=1; y<=x; y++) { System.out.print(y+""); } System.out.println(); }

Largest in Array using Arrays

int [] arr = {5, 6, 76, 31, 43, 1}; Arrays.sort(arr); System.out.println(arr[arr.length-1]);

Fibonacci

int a = 0; int b = 1; System.out.print(a + " " + b + " "); for(int i = 2; i <=10; i++) { int c = a+b; a = b; b = c; System.out.print(c + " + "); }

Division without divide or mod

public static String division(int divident, int divisor) { int quotient = 0; int remainder = 0; while(divident >= divisor) { divident = divident - divisor; quotient++; remainder = divident; } return "quotient = " + quotient + " remainder = " + remainder;}

Factorial

int number = 10; int factorialSum = 1; for(int i = 1 ; i <=number; i++) { factorialSum = factorialSum * i; } System.out.println("Factorial of " + number + " is " + factorialSum); }

Sum of Digits

int number = 1346; int sum = 0; while(number > 0) { sum += number%10; number = number/10; } System.out.println(sum); }

Reverse String (for loop)

public static String reverseString(String str) { String reverse= ""; for(int i = str.length()-1; i >=0; i--) { reverse += str.charAt(i); } return reverse; }

Prime Number

public static boolean checkPrime(int n) { if (n <= 1) { return false; } for (int i = 2; i < Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; }

Palindrome (charAt)

public static boolean isPalindrome(String s) { int head = 0; int tail = s.length() - 1; while (head < tail) { if (s.charAt(head) != s.charAt(tail)) { return false; } head++; tail--; } return true; }

Palindrome (String Builder)

public static boolean isPalindrome(String str) { if(str == null) {return false;} StringBuilder sb = new StringBuilder(str); return sb.reverse().toString().equals(str); }

Largest in Array using Collections

public static int getLargest(int[] a, int total){ int temp; for (int i = 0; i < total; i++) { for (int j = i + 1; j < total; j++) { if (a[i] > a[j]) { temp = a[i]; a[i] = a[j]; a[j] = temp; }}} return a[total-1]; }

Reverse Number

public static int reverse(int number){ int reverse = 0; int remainder = 0; do{ remainder = number%10; reverse = reverse*10 + remainder; number = number/10; } while(number > 0); return reverse; }

Reverse String (String Builder)

public static void main(String[] args) { String str = "Hello World"; StringBuilder sb = new StringBuilder(str); System.out.println(sb.reverse()); }

Find First two Max Numbers in Array

public void GetTwoMaxValues(int[] nums) { int maxOne = 0; int maxTwo = 0; Arrays.sort(nums); System.out.println("Max1 - " + (nums[nums.length-1])); System.out.println("Max2 - " + (nums[nums.length-2]));

Reverse String (char Array)

public void printReverse(char[] letters, int size){ for (int i = letters.length-1; i >= 0 ; i--){ System.out.print(letters[i]); } }


Related study sets

Chapter 9 - Book and Quiz Questions

View Set

Fin 453 Chapter 17: The Central Bank Balance Sheet and Money Supply

View Set

ST for the ST - Chapter 1- Organizations and their acronyms

View Set

3. Mucho gusto. Encantado vs. Encantada (Pleased to meet you)

View Set

Abnormal Psych - Final (Case Studies)

View Set

Security Creating Strong Passwords

View Set

Ch 11 Cardiovascular System PRACTICE

View Set