Skip to content

Commit

Permalink
Merge pull request #5 from MLSA-Mehran-UET/main
Browse files Browse the repository at this point in the history
adddddd
  • Loading branch information
jmrchelani authored Oct 20, 2020
2 parents ef50d3f + 555fef1 commit 80b5f3e
Show file tree
Hide file tree
Showing 54 changed files with 1,683 additions and 0 deletions.
27 changes: 27 additions & 0 deletions BarcodeGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
*/

import java.util.Random; // make sure to write the import for Random class

/**
*
* @author ahsan
*/
public class BarcodeGenerator
{
public String generateBarcode()
{
String barcode;
String[] numbers={"0","1","2","3","4","5","6","7","8","9"};
String[] Alphabets ={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};
Random random = new Random(); //creating instance of Random class
int n1=random.nextInt(10); // generating a random number
int n2=random.nextInt(10);
int n3=random.nextInt(10);
int n4=random.nextInt(10);
// now creating barcode using random numbers i-e n1, n2
barcode= numbers[n1]+numbers[n2]+numbers[n3]+numbers[n4];
return barcode;
}
}

35 changes: 35 additions & 0 deletions CreatingStrings
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
this files helps you understand string and shows different ways of creating string
String in java is simply an object that refers to the array pf characters

*/
package strings;

/**
*
* @author Ahsan Qadeer
*/
public class CreatingString {

// ch and ahsan are same
char ch[]={'a','h','s','a','n'};
String name="ahsan";// this is called string literal
//
String name2="ahsan";//this does'nt create a new literal
// name and name2 are refering to same array of characters in the stringConstantPool within memory

String name3="ahsanqadeer";//new literal refering to a new string in string contant pool which resides in heap memory

//other ways of creating strings
String s=new String("ahsan");
// JVM will create a new string object in normal (non-pool) heap memory, and the literal "ahsan" will be placed in the string constant pool.

String s2=new String("ahsan");
// s1 and s2 don't have same reference;
public static void main(String[] args) {
// TODO code application logic here
CreatingString ct=new CreatingString();
System.out.println(ct.name2==ct.name);
}

}
84 changes: 84 additions & 0 deletions Data Structures & Algorithm/Sorting/Bitonic Sort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
public class BitonicSort
{
/* The parameter dir indicates the sorting direction,
ASCENDING or DESCENDING; if (a[i] > a[j]) agrees
with the direction, then a[i] and a[j] are
interchanged. */
void compAndSwap(int a[], int i, int j, int dir)
{
if ( (a[i] > a[j] && dir == 1) ||
(a[i] < a[j] && dir == 0))
{
// Swapping elements
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}

/* It recursively sorts a bitonic sequence in ascending
order, if dir = 1, and in descending order otherwise
(means dir=0). The sequence to be sorted starts at
index position low, the parameter cnt is the number
of elements to be sorted.*/
void bitonicMerge(int a[], int low, int cnt, int dir)
{
if (cnt>1)
{
int k = cnt/2;
for (int i=low; i<low+k; i++)
compAndSwap(a,i, i+k, dir);
bitonicMerge(a,low, k, dir);
bitonicMerge(a,low+k, k, dir);
}
}

/* This funcion first produces a bitonic sequence by
recursively sorting its two halves in opposite sorting
orders, and then calls bitonicMerge to make them in
the same order */
void bitonicSort(int a[], int low, int cnt, int dir)
{
if (cnt>1)
{
int k = cnt/2;

// sort in ascending order since dir here is 1
bitonicSort(a, low, k, 1);

// sort in descending order since dir here is 0
bitonicSort(a,low+k, k, 0);

// Will merge wole sequence in ascending order
// since dir=1.
bitonicMerge(a, low, cnt, dir);
}
}

/*Caller of bitonicSort for sorting the entire array
of length N in ASCENDING order */
void sort(int a[], int N, int up)
{
bitonicSort(a, 0, N, up);
}

/* A utility function to print array of size n */
static void printArray(int arr[])
{
int n = arr.length;
for (int i=0; i<n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// Driver method
public static void main(String args[])
{
int a[] = {3, 7, 4, 8, 6, 2, 1, 5};
int up = 1;
BitonicSort ob = new BitonicSort();
ob.sort(a, a.length,up);
System.out.println("\nSorted array");
printArray(a);
}
}
70 changes: 70 additions & 0 deletions Data Structures & Algorithm/Sorting/HeapSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
public class HeapSort {
public void sort(int arr[]) {
int n = arr.length;

// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);

// One by one extract an element from heap
for (int i = n - 1; i > 0; i--) {
// Move current root to end
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;

// call max heapify on the reduced heap
heapify(arr, i, 0);
}
}

// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], int n, int i) {
int largest = i; // Initialize largest as root
int l = 2 * i + 1; // left = 2*i + 1
int r = 2 * i + 2; // right = 2*i + 2
// If left child is larger than root
if (l < n && arr[l] > arr[largest]) largest = l;

// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest]) largest = r;

// If largest is not root
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;

// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}

/* A utility function to print array of size n */
static void printArray(int arr[]) {
int n = arr.length;
for (int i = 0; i < n; ++i)
System.out.print(arr[i] + " ");
System.out.println();
}

// Driver program
public static void main(String args[]) {
int arr[] = {
12,
11,
13,
5,
6,
7
};
int n = arr.length;

HeapSort ob = new HeapSort();
ob.sort(arr);

System.out.println("Sorted array is");
printArray(arr);
}
}
120 changes: 120 additions & 0 deletions Data Structures & Algorithm/Sorting/Radix.Java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import java.io. * ;

import java.util. * ;

class Radix {

// A utility function to get maximum value in arr[]
static int getMax(int arr[], int n)

{

int mx = arr[0];

for (int i = 1; i < n; i++)

if (arr[i] > mx)

mx = arr[i];

return mx;

}

// A function to do counting sort of arr[] according to
// the digit represented by exp.
static void countSort(int arr[], int n, int exp)

{

int output[] = new int[n]; // output array
int i;

int count[] = new int[10];

Arrays.fill(count, 0);

// Store count of occurrences in count[]
for (i = 0; i < n; i++)

count[(arr[i] / exp) % 10]++;

// Change count[i] so that count[i] now contains
// actual position of this digit in output[]
for (i = 1; i < 10; i++)

count[i] += count[i - 1];

// Build the output array
for (i = n - 1; i >= 0; i--) {

output[count[(arr[i] / exp) % 10] - 1] = arr[i];

count[(arr[i] / exp) % 10]--;

}

// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to curent digit
for (i = 0; i < n; i++)

arr[i] = output[i];

}

// The main function to that sorts arr[] of size n using
// Radix Sort
static void radixsort(int arr[], int n)

{

// Find the maximum number to know number of digits
int m = getMax(arr, n);

// Do counting sort for every digit. Note that
// instead of passing digit number, exp is passed.
// exp is 10^i where i is current digit number
for (int exp = 1; m / exp > 0; exp *= 10)

countSort(arr, n, exp);

}

// A utility function to print an array
static void print(int arr[], int n)

{

for (int i = 0; i < n; i++)

System.out.print(arr[i] + " ");

}

/*Driver Code*/

public static void main(String[] args)

{

int arr[] = {
170,
45,
75,
90,
802,
24,
2,
66
};

int n = arr.length;

// Function Call
radixsort(arr, n);

print(arr, n);

}

}
34 changes: 34 additions & 0 deletions Downcasting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

// Downcasting is when a child class type reference variable point to/references
// to a parent object or parentclass type
package downcasting;

class Animal
{

}
public class Downcasting extends Animal{


static void method(Animal a) {
if(a instanceof Downcasting){
Downcasting d=(Downcasting)a;//downcasting
System.out.println("ok downcasting performed");
}
}

public static void main(String[] args) {

//downcasting is not possible in this way will give compile-time error
// Downcasting d=new Animal();

//Compiles successfully but ClassCastException is thrown at runtime if we use type casting as shown under :
// Dog d=(Dog)new Animal();

//but we can do it as under
Animal a=new Downcasting();
Downcasting.method(a);

}

}
8 changes: 8 additions & 0 deletions Enum/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Enum/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 80b5f3e

Please sign in to comment.