JAVA Coding Flashcards
Swap values of two variables
public class Swap {
public static void main(String[] args) {
int j = 15;
int i = 10;
j = j - i; // j = 15 - 10; j = 5
i = i + j; // i = 10 + 5; i = 15
j = i - j; // j = 15 - 5; j = 10
System.out.println(i); // 15
System.out.println(j); // 10
}
}
Two String Anagram
public static void main(String[] args)
System.out.println(isAnagtarm(“abc”, “cba”);
System.out.println(IsAnagram(“asd”, “zxc”);
public static boolean isAnagram(String str, String str1) {
char ch[] = str.toCharArray();
char ch1[] = str1.toCharArray();
Arrays.sort(ch);
Arrays.sort(ch1);
return Arrays.equals(ch, ch1);
}
}
Prime Number
public static void main(String [] args) {
System.out.println(primeNum(7));
System.out.println(primeNum(24));
}
public static boolean primeNum(int num) {
for (int i = 2; i < num/2; i++) {
if (num % i == 0) {
return false;
}
return true;
}
}
String reverse using StringBuilder
public String reverseStr(String str) {
// create variable to store reversed version of str
StringBuilder reverse = new StringBuilder();
// iterate over input string from the back with charAt for(int i = str.length() - 1; i >= 0; i--) { // add chars to reversed variable reverse.append(String.valueOf(str.charAt(i))); } // convert to string and return reversed version return reverse.toString(); }
Array reverse
public void revArr(int[] arrNum) {
// we will use two ‘pointers’. One pointer will start from the beginning
// another one from the back and we will swap their values
// pointer that will start from the back int j = arrNum.length - 1; // our loop will go till half of our input array // 'i' is a pointer that will start from the beginning for(int i = 0; i < arrNum.length / 2; i++) { // swap elements using positions of i and (j - i) int tmp = arrNum[i]; arrNum[i] = arrNum[j - i]; arrNum[j - i] = tmp; } }
String palindrome
// isPal(“anna”) -> true
// isPal(“civic”) -> true
// isPal(“apple”) -> false
// isPal(“level”) -> true
public boolean isPal(String str) {
// we will use two ‘pointers’. One pointer will start looking from beginning
// another from the back. If values of pointers are not equal, we can return false
int j = str.length() - 1; // pointer for the back // loop will go till half because we have two pointers for(int i = 0; i < str.length() / 2; i++) { // if pointers values are not equal return false if(str.charAt(i) != str.charAt(j - i)){ return false; } } // if program reach here, it means all values were equal so it's palindrome return true; }
Reverse string
public class ReverseStr {
public static void main(String[] args) {
// Implement revStr(String str) method so
// it will return the reverse version of its str argument.
String res = revStr(“apple”);
System.out.println(res); // elppa
}
public static String revStr(String str) { String res = ""; for (int k = str.length() - 1; k >= 0; k--) { str += str.charAt(k); } return str; } }
Number palindrome
// isPalNum(545) -> true
// isPalNum(1001) -> true
// isPalNum(13) -> false
// isPalNum(33) -> true
public boolean isPalNum(int num) {
// there are two most important things to remember
// to get most right number, we can do ‘num % 10’
// to remove most right number, we can do ‘num / 10’
// both will work for any numbers
int copyOfOriginal = num; int reversedNumber = 0; int remainder; while(num > 0) { // get most right number remainder = num % 10; // multiply by 10 to concat, not to add(plus) reversedNumber = (reversedNumber * 10) + remainder; // remove most right number from num. num = num / 10; } // if reversed version and original are equal so it's palindrome return reversedNumber == copyOfOriginal; }
Max number from an array
// max({4, 781, 8, 99, 103}) -> 781
// max({1, 2, 3, 4, 5}) -> 5
// max({3, 4}) -> 4
// max({100}) -> 100
public int max(int[] arrNum) {
// assume first element of array is biggest number
int max = arrNum[0];
// loop over the array and test our above assumption for(int i = 0; i < arrNum.length; i++) { // if max was not the biggest number, update it if(max < arrNum[i]) { max = arrNum[i]; } } // after the loop max variable will hold the biggest number return max; }
Min number from an array
// min({4, 781, 8, 99, 103}) -> 4
// min({1, 2, 3, 4, 5}) -> 1
// min({3, 4}) -> 3
// min({100}) -> 100
public int min(int[] arrNum) {
// assume first element of array is the smallest number
int min = arrNum[0];
// loop over the array and test assumption for(int i = 0; i < arrNum.length; i++) { // if min was not smallest, update it if(min > arrNum[i]) { min = arrNum[i]; } } return min; }
String reverse
public class Practice {
public static void main(String[] args) {
System.out.println(revStr(“apple”));
}
public static String revStr(String str) {
String rev = “”;
for (int i = str.length() - 1; i >= 0; i–) {
rev += str.charAt(i);
}
return rev;
}
}