Skip to main content

Factorial

0 likes • Nov 19, 2022 • 0 views
Java
Loading...

More Java Posts

longest sequence

0 likes • Oct 7, 2023 • 5 views
Java
import java.util.Arrays;
public class LongestIncreasingSubsequence {
public int lengthOfLIS(int[] nums) {
if (nums.length == 0) {
return 0;
}
int[] dp = new int[nums.length];
Arrays.fill(dp, 1); // Initialize all elements in dp array to 1, as each element itself is a valid subsequence of length 1.
int maxLength = 1; // Initialize the maximum length to 1, considering a single element as the sequence.
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] > nums[j]) {
dp[i] = Math.max(dp[i], dp[j] + 1); // If nums[i] is greater than nums[j], update the longest subsequence length ending at i.
}
}
maxLength = Math.max(maxLength, dp[i]); // Update the maximum length if needed.
}
return maxLength;
}
public static void main(String[] args) {
LongestIncreasingSubsequence lis = new LongestIncreasingSubsequence();
int[] nums = {10, 9, 2, 5, 3, 7, 101, 18};
int result = lis.lengthOfLIS(nums);
System.out.println("Length of the Longest Increasing Subsequence: " + result); // Output: 4
}
}

Random Integers -> file

0 likes • Nov 19, 2022 • 0 views
Java
//sample code to write 100 random ints to a file, 1 per line
import java.io.PrintStream;
import java.io.IOException;
import java.io.File;
import java.util.Random;
public class WriteToFile {
public static void main(String[] args) {
try {
PrintStream writer = new PrintStream(new File("randInts.txt"));
Random r = new Random();
final int LIMIT = 100;
for (int i = 0; i < LIMIT; i++) {
writer.println(r.nextInt());
}
writer.close();
} catch (IOException e) {
System.out.println("An error occured while trying to write to the file");
}
}
}

quick sort

0 likes • Nov 19, 2022 • 0 views
Java
import java.util.Arrays;
public class QuickSortMain {
private static int array[];
public static void sort(int[] arr) {
if (arr == null || arr.length == 0) {
return;
}
array = arr;
quickSort(0, array.length-1);
}
private static void quickSort(int left, int right) {
int i = left;
int j = right;
// find pivot number, we will take it as mid
int pivot = array[left+(right-left)/2];
while (i <= j) {
/**
* In each iteration, we will increment left until we find element greater than pivot
* We will decrement right until we find element less than pivot
*/
while (array[i] < pivot) { i++; } while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchange(i, j);
//move index to next position on both sides
i++;
j--;
}
}
// call quickSort() method recursively
if (left < j)
quickSort(left, j);
if (i < right)
quickSort(i, right);
}
private static void exchange(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
public static void main(String a[]){
int[] input = {33,21,45,64,55,34,11,8,3,5,1};
System.out.println("Before Sorting : ");
System.out.println(Arrays.toString(input));
sort(input);
System.out.println("==================");
System.out.println("After Sorting : ");
System.out.println(Arrays.toString(array));
}
}

Send Email from Java

0 likes • Nov 19, 2022 • 3 views
Java
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingException
{
boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.example.com");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo[i] = new InternetAddress(recipients[i]);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}

6x6 Covariance Matrix Conversion from ENU to ECEF

0 likes • Jan 28, 2023 • 40 views
Java
public static void enu2ecefCov(GMatrix ecefCov, GMatrix enuCov, LLA refLLA) {
double lambda = refLLA.longitude;
double phi = refLLA.latitude;
GMatrix R = new GMatrix(6, 6);
GMatrix Rt = new GMatrix(6, 6);
GMatrix tmp1 = new GMatrix(6, 6);
GMatrix tmp2 = new GMatrix(6, 6);
R.setElement(0, 0, -Math.sin(lambda));
R.setElement(0, 1, -Math.sin(phi)*Math.cos(lambda));
R.setElement(0, 2, Math.cos(phi)*Math.cos(lambda));
R.setElement(1, 0, Math.cos(lambda));
R.setElement(1, 1, -Math.sin(phi)*Math.sin(lambda));
R.setElement(1, 2, Math.cos(phi)*Math.sin(lambda));
R.setElement(2, 0, 0);
R.setElement(2, 1, Math.cos(phi));
R.setElement(2, 2, Math.sin(phi));
R.setElement(3, 3, -Math.sin(lambda));
R.setElement(3, 4, -Math.sin(phi)*Math.cos(lambda));
R.setElement(3, 5, Math.cos(phi)*Math.cos(lambda));
R.setElement(4, 3, Math.cos(lambda));
R.setElement(4, 4, -Math.sin(phi)*Math.sin(lambda));
R.setElement(4, 5, Math.cos(phi)*Math.sin(lambda));
R.setElement(5, 3, 0);
R.setElement(5, 4, Math.cos(phi));
R.setElement(5, 5, Math.sin(phi));
Rt.transpose(R);
tmp1.mul(enuCov, R);
ecefCov.mul(Rt, tmp1);
}

Add Binary Numbers

0 likes • Oct 15, 2022 • 0 views
Java
public static String addBinary(){
// The two input Strings, containing the binary representation of the two values:
String input0 = "1010";
String input1 = "10";
// Use as radix 2 because it's binary
int number0 = Integer.parseInt(input0, 2);
int number1 = Integer.parseInt(input1, 2);
int sum = number0 + number1;
return Integer.toBinaryString(sum); //returns the answer as a binary value;
}