commit
eca7a0ee28
65
Conversions/DecimalToAnyBase.java
Normal file
65
Conversions/DecimalToAnyBase.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Driver Program
|
||||||
|
public class DecimalToAnyBase {
|
||||||
|
public static void main (String[] args) throws Exception{
|
||||||
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||||
|
System.out.println("Enter the decimal input below: ");
|
||||||
|
int decInput = Integer.parseInt(br.readLine());
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
System.out.println("Enter the base below: ");
|
||||||
|
int base = Integer.parseInt(br.readLine());
|
||||||
|
System.out.println();
|
||||||
|
|
||||||
|
System.out.println("Decimal Input" + " is: " + decInput);
|
||||||
|
System.out.println("Value of " + decInput + " in base " + base + " is: " + convertToAnyBase(decInput, base));
|
||||||
|
|
||||||
|
br.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method produces a String value of any given input decimal in any base
|
||||||
|
* @param inp Decimal of which we need the value in base in String format
|
||||||
|
* @return string format of the converted value in the given base
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static String convertToAnyBase(int inp, int base) {
|
||||||
|
ArrayList<Character> charArr = new ArrayList<>();
|
||||||
|
|
||||||
|
while (inp > 0) {
|
||||||
|
charArr.add(reVal(inp%base));
|
||||||
|
inp /= base;
|
||||||
|
}
|
||||||
|
|
||||||
|
StringBuilder str = new StringBuilder(charArr.size());
|
||||||
|
|
||||||
|
for(Character ch: charArr)
|
||||||
|
{
|
||||||
|
str.append(ch);
|
||||||
|
}
|
||||||
|
|
||||||
|
return str.reverse().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This method produces character value of the input integer and returns it
|
||||||
|
* @param num integer of which we need the character value of
|
||||||
|
* @return character value of input integer
|
||||||
|
*/
|
||||||
|
|
||||||
|
public static char reVal(int num) {
|
||||||
|
if (num >= 0 && num <= 9)
|
||||||
|
return (char)(num + '0');
|
||||||
|
else
|
||||||
|
return (char)(num - 10 + 'A');
|
||||||
|
}
|
||||||
|
}
|
@ -1,62 +1,77 @@
|
|||||||
import java.util.Scanner;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author Varun Upadhyay (https://github.com/varunu28)
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
public class LinearSearch{
|
public class LinearSearch{
|
||||||
/**
|
/**
|
||||||
* The main method
|
* The main method
|
||||||
* @param args Command line arguments
|
* @param args Command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args){
|
public static void main(String[] args) throws Exception {
|
||||||
|
|
||||||
Scanner input = new Scanner(System.in);
|
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||||
int[] myArray;
|
|
||||||
|
// Test for Integer inputs
|
||||||
|
Integer[] myArray;
|
||||||
int size = 0;
|
int size = 0;
|
||||||
|
|
||||||
//Prompt user to create array and its elements
|
//Prompt user to create array and its elements
|
||||||
System.out.print("Enter the array size: ");
|
System.out.print("Enter the array size: ");
|
||||||
size = input.nextInt();
|
size = Integer.parseInt(br.readLine());
|
||||||
myArray = new int[size];
|
myArray = new Integer[size];
|
||||||
for (int i = 0; i < size; i++){
|
for (int i = 0; i < size; i++){
|
||||||
System.out.print("For index " + i + ", enter an integer: ");
|
System.out.print("For index " + i + ", enter an integer: ");
|
||||||
myArray[i] = input.nextInt();
|
myArray[i] = Integer.parseInt(br.readLine());
|
||||||
}
|
}
|
||||||
|
|
||||||
//Prompt user to search for particular element
|
//Prompt user to search for particular element
|
||||||
System.out.print("Enter integer to search for: ");
|
System.out.print("Enter integer to search for: ");
|
||||||
int key = input.nextInt();
|
Integer key = Integer.parseInt(br.readLine());
|
||||||
|
|
||||||
//Output array and index of target element, if found
|
//Output array and index of target element, if found
|
||||||
printarray(myArray);
|
System.out.printf("The integer %d is found in index %d\n", key, linearSearch(myArray, key));
|
||||||
System.out.printf("The integer %d is found in index %d\n", key, linearsearch(myArray, key));
|
|
||||||
|
|
||||||
input.close();
|
// Test for String inputs
|
||||||
|
String[] myArray1;
|
||||||
|
int size1 = 0;
|
||||||
|
|
||||||
|
//Prompt user to create array and its elements
|
||||||
|
System.out.print("Enter the array size: ");
|
||||||
|
size1 = Integer.parseInt(br.readLine());
|
||||||
|
myArray1 = new String[size];
|
||||||
|
for (int i = 0; i < size1; i++){
|
||||||
|
System.out.print("For index " + i + ", enter a String: ");
|
||||||
|
myArray1[i] = br.readLine();
|
||||||
|
}
|
||||||
|
|
||||||
|
//Prompt user to search for particular element
|
||||||
|
System.out.print("Enter String to search for: ");
|
||||||
|
String key1 = br.readLine();
|
||||||
|
|
||||||
|
//Output array and index of target element, if found
|
||||||
|
System.out.printf("The string %s is found in index %d\n", key1, linearSearch(myArray1, key1));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Linear search method
|
* Generic Linear search method
|
||||||
*
|
*
|
||||||
* @param list List to be searched
|
* @param array List to be searched
|
||||||
* @param key Key being searched for
|
* @param value Key being searched for
|
||||||
* @return Location of the key
|
* @return Location of the key
|
||||||
*/
|
*/
|
||||||
public static int linearsearch(int[] list, int key){
|
public static <T extends Comparable<T>> int linearSearch(T[] array, T value) {
|
||||||
|
int lo = 0;
|
||||||
for (int i = 0; i< list.length; i++){
|
int hi = array.length - 1;
|
||||||
if (list[i] == key){
|
for (int i = lo; i <= hi; i++) {
|
||||||
|
if (array[i].compareTo(value) == 0) {
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
} return -1;
|
|
||||||
}
|
}
|
||||||
/**
|
return -1;
|
||||||
* Helper Method
|
|
||||||
*
|
|
||||||
* @param a array to be printed
|
|
||||||
*/
|
|
||||||
public static void printarray(int[] a){
|
|
||||||
System.out.print("The array is: ");
|
|
||||||
for( int d: a){
|
|
||||||
System.out.print(d+" ");
|
|
||||||
}
|
|
||||||
System.out.println();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user