## Word Counter

0 likes • Nov 19, 2022 • 0 views
Java

## More Java Posts

#### Factorial

0 likes • Nov 19, 2022 • 0 views
Java
public class Factorial
{
public static void main(String[] args)
{ final int NUM_FACTS = 100;
for(int i = 0; i < NUM_FACTS; i++)
System.out.println( i + "! is " + factorial(i));
}
public static int factorial(int n)
{ int result = 1;
for(int i = 2; i <= n; i++)
result *= i;
return result;
}
}

#### Heap sort

0 likes • Nov 19, 2022 • 0 views
Java
import java.util.*;
public class HeapSortMain {
public static void buildheap(int []arr) {
/*
* As last non leaf node will be at (arr.length-1)/2
* so we will start from this location for heapifying the elements
* */
for(int i=(arr.length-1)/2; i>=0; i--){
heapify(arr,i,arr.length-1);
}
}
public static void heapify(int[] arr, int i,int size) {
int left = 2*i+1;
int right = 2*i+2;
int max;
if(left <= size && arr[left] > arr[i]){
max=left;
} else {
max=i;
}
if(right <= size && arr[right] > arr[max]) {
max=right;
}
// If max is not current node, exchange it with max of left and right child
if(max!=i) {
exchange(arr,i, max);
heapify(arr, max,size);
}
}
public static void exchange(int[] arr,int i, int j) {
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
}
public static int[] heapSort(int[] arr) {
buildheap(arr);
int sizeOfHeap=arr.length-1;
for(int i=sizeOfHeap; i>0; i--) {
exchange(arr,0, i);
sizeOfHeap=sizeOfHeap-1;
heapify(arr, 0,sizeOfHeap);
}
return arr;
}
public static void main(String[] args) {
int[] arr={1,10,16,19,3,5};
System.out.println("Before Heap Sort : ");
System.out.println(Arrays.toString(arr));
arr=heapSort(arr);
System.out.println("=====================");
System.out.println("After Heap Sort : ");
System.out.println(Arrays.toString(arr));
}
}

#### Puzzle 1

0 likes • Feb 6, 2021 • 0 views
Java
import java.util.Scanner;
/* In Heaven, the seed is tall and the oak small / It's input vital
* tinyurl.com/17snqkfv
* email answer to [email protected]
**/
public class Main
{
public static void main(String args[])
{
int x = 0;
int a, c, m = 10000;
Scanner in = new Scanner(System.in);
System.out.println("Please enter a seed, maximum 3000000:");
x = in.nextInt();
System.out.println("Please input a number, maximum 29:");
a = in.nextInt();
System.out.println("Please input a number, maximum 999999:");
c = in.nextInt();
{
x = (a*x + c) % m;
System.out.println("The next number is " + x + "\nWould you like another? (y/n)");
}
}
}
//██╗  ░█████╗░███╗░░░███╗  ██████╗░░█████╗░███████╗██████╗░░█████╗░██╗░░░░░██╗░░░██╗░██████╗
//██║  ██╔══██╗████╗░████║  ██╔══██╗██╔══██╗██╔════╝██╔══██╗██╔══██╗██║░░░░░██║░░░██║██╔════╝
//██║  ███████║██╔████╔██║  ██║░░██║███████║█████╗░░██║░░██║███████║██║░░░░░██║░░░██║╚█████╗░
//██║  ██╔══██║██║╚██╔╝██║  ██║░░██║██╔══██║██╔══╝░░██║░░██║██╔══██║██║░░░░░██║░░░██║░╚═══██╗
//██║  ██║░░██║██║░╚═╝░██║  ██████╔╝██║░░██║███████╗██████╔╝██║░░██║███████╗╚██████╔╝██████╔╝
//╚═╝  ╚═╝░░╚═╝╚═╝░░░░░╚═╝  ╚═════╝░╚═╝░░╚═╝╚══════╝╚═════╝░╚═╝░░╚═╝╚══════╝░╚═════╝░╚═════╝░

#### Quine-McCluskey Tabular Method

CHS
0 likes • Feb 3, 2021 • 0 views
Java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
char[] variables = setVariables(in);
int[] minTerms = setMinTerms(in);
String[] binaryMinTerms = decimalToBinary(variables, minTerms);
ArrayList<Group> groups = computeSuccessiveGroups(minTerms, binaryMinTerms);
displayInput(variables, minTerms);
printPIs(groups, variables);
}
private static char[] setVariables(Scanner in) {
System.out.print("Enter the number of variables: ");
int numVariables = in.nextInt();
char[] variables = new char[numVariables];
System.out.print("Enter each variable separated by spaces: ");
for(int i = 0; i < numVariables; i++) {
variables[i] = in.next().charAt(0);
}
return variables;
}
private static int[] setMinTerms(Scanner in) {
System.out.print("Enter the number of min terms: ");
int numMinTerms = in.nextInt();
int[] minTerms = new int[numMinTerms];
System.out.print("Enter each min term separated by spaces: ");
for(int i = 0; i < numMinTerms; i++) {
minTerms[i] = in.nextInt();
}
return minTerms;
}
private static String[] decimalToBinary(char[] variables, int[] minTerms) {
String[] binaryMinTerms = new String[minTerms.length];
for(int i = 0; i < minTerms.length; i++) {
binaryMinTerms[i] = Integer.toBinaryString(minTerms[i]);
if(binaryMinTerms[i].length() < variables.length) {
String zeros = "";
int len = binaryMinTerms[i].length();
while(len < variables.length) {
zeros = zeros.concat("0");
len++;
}
binaryMinTerms[i] = zeros + binaryMinTerms[i];
}
}
return binaryMinTerms;
}
private static void displayInput(char[] variables, int[] minTerms) {
System.out.print("Function: f(");
for(int i = 0; i < variables.length; i++) {
if(i == variables.length-1)
System.out.print(variables[i]);
else
System.out.print(variables[i] + ", ");
}
System.out.print(") = SIGMAm(");
for(int i = 0; i < minTerms.length; i++) {
if(i == minTerms.length-1)
System.out.print(minTerms[i]);
else
System.out.print(minTerms[i] + ", ");
}
System.out.println(")");
}
private static ArrayList<Group> computeSuccessiveGroups(int[] minTerms, String[] binaryMinTerms) {
ArrayList<Group> groups = new ArrayList<Group>();
for(int i = 0; i < minTerms.length; i++)
int sizeBeforeRemoval = 0, currentSize;
while(sizeBeforeRemoval != groups.size()) {
currentSize = groups.size();
for(int i = 0; i < currentSize; i++) {
for(int j = i+1; j < currentSize; j++) {
if(differences(groups.get(i).binary, groups.get(j).binary) == 1) {
String combinedBin = combineBinary(groups.get(i).binary, groups.get(j).binary);
groups.get(i).grouped = true;
groups.get(j).grouped = true;
if(isUniqueBin(groups, combinedBin)) {
String newTuple = groups.get(i).decimal + "," + groups.get(j).decimal;
}
else break;
}
}
}
sizeBeforeRemoval = groups.size();
Iterator<Group> iter = groups.iterator();
while(iter.hasNext()) {
Group next = iter.next();
if(next.grouped)
iter.remove();
}
}
return groups;
}
private static int differences(String a, String b) {
int diff = 0;
for(int i = 0; i < a.length(); i++)
if(a.charAt(i) != b.charAt(i))
diff++;
return diff;
}
private static String combineBinary(String a, String b) {
String combined = "";
for(int i = 0; i < a.length(); i++) {
if(a.charAt(i) != b.charAt(i))
combined = combined.concat("-");
else
combined = combined.concat(Character.toString(a.charAt(i)));
}
return combined;
}
private static boolean isUniqueBin(ArrayList<Group> groups, String s) {
for(int i = 0; i < groups.size(); i++)
if(groups.get(i).binary.equals(s))
return false;
return true;
}
private static void printPIs(ArrayList<Group> groups, char[] variables) {
System.out.println("Prime Implicants: ");
for(int i = 0; i < groups.size(); i++)
System.out.println(groups.get(i).decimal + " | " + binaryToMinTerm(variables, groups.get(i).binary));
}
private static String binaryToMinTerm(char[] variables, String s) {
String minTerm = "";
for(int i = 0; i < s.length(); i++) {
if(s.charAt(i) != '-') {
if(s.charAt(i) == '0')
minTerm = minTerm.concat(Character.toString(variables[i]) + "'");
else
minTerm = minTerm.concat(Character.toString(variables[i]));
}
}
return minTerm;
}
}

#### merge sort

0 likes • Nov 18, 2022 • 0 views
Java
import static java.lang.System.*;
import java.util.Arrays;
public class MergeSort{
private static int passCount;
public static void mergeSort(int[] list)
{
passCount=0;
mergeSort(list, 0, list.length);
}
private static void mergeSort(int[] list, int front, int back) //O( Log N )
{
int mid = (front+back)/2;
if(mid==front) return;
mergeSort(list, front, mid);
mergeSort(list, mid, back);
merge(list, front, back);
}
private static void merge(int[] list, int front, int back) //O(N)
{
int dif = back-front;
int[] temp = new int[dif];
int beg = front, mid = (front+back)/2;
int saveMid = mid;
int spot = 0;
while(beg < saveMid && mid < back) {
if(list[beg] < list[mid]) {
temp[spot++] = list[beg++];
} else {
temp[spot++] = list[mid++];
}
}
while(beg < saveMid)
temp[spot++] = list[beg++];
while(mid < back)
temp[spot++] = list[mid++];
for(int i = 0; i < back-front; i++) {
list[front+i] = temp[i];
}
System.out.println("pass " + passCount++ + " " + Arrays.toString(list) + "\n");
}
public static void main(String args[])
{
mergeSort(new Comparable[]{ 9, 5, 3, 2 });
System.out.println("\n");
mergeSort(new Comparable[]{ 19, 52, 3, 2, 7, 21 });
System.out.println("\n");
mergeSort(new Comparable[]{ 68, 66, 11, 2, 42, 31});
System.out.println("\n");
}
}

#### Me

0 likes • Feb 6, 2021 • 0 views
Java
public class Daedalus extends Athenian
{
private Story story;
public ArrayList<Skill> skills = new ArrayList<Skill>();
public ArrayList<Athenian> children = new ArrayList<Athenian>();
public Daedalus(Story story, ArrayList<Skill> inherentSkills)
{
if(story != null)
System.out.println("ERROR No one is created with a story.");