Posts

Showing posts with the label for loop

mathajax

Vector - magnitude

Image
The java code finds magnitude of a vector which can be 2 Dimension or 3 Dimension or any number of dimension. A java class Vector defined with two member functions to calculate magnitude of a vector in double or integer datatype. As the both member function declared with keyword static,  we can access the member function by class class name.  we do not need to create object to the class. Magnitude of 2D vector Equation Magnitude of N-Dimensional vector Equation Example Calculation This is an example calculation shown below that explains how to find a vector magnitude . A vector,array of elements declared and initialized in java using one dimensional array. int A[] = { 2 ,3 }; // 2D vector A find a magnitude for vector A |A| = sqrt ( A[0]^2 + A[1]^2 ) = sqrt ( 2^2 + 3^2 ); = sqrt ( 4 + 9 ); = sqrt ( 13 ); |A| = 3.605 m...

Chebyshev distance between two points

Image
The java program finds distance between two points using chebyshev distance equation . The points can be a scalar or a vector . 1D distance metric It measures distance between two points by absolute difference between them. Each point has one element. 2-Dimensional distance metric It measures distance between two points (2D vector) by selecting maximum absolute difference on 2D vector element absolute difference. N-Dimensional distance metric It measures distance between two N-Dimensional vector by selecting maximum absolute difference on the two N-D vector element. Example Calculation on 2D vectors This is an example calculation shown below explain how to find the distance between two vectors using Chebyshev distance formula. A vector,array of elements declared and initialized in Java using one dimensional array. int A[] = { 2 ,6 }; // 2D vector A int B[] = { 4,1 }; // 2D vector B max - operators finds greatest val...

Metric - Minkowski distance between two points

Image
The java program finds distance between two points using minkowski distance equation . when power is set P=1, minkowski metric results as same as manhattan distance equation and when set P=2, minkowski metric results as same as euclidean distance equation. 1-Dimensional distance It measures the distance between two points . Each point lies on a line and has one element. 2-Dimensional distance It measures the distance between two points. Each point lies on 2D cartesian plane and has 2 elements. N-Dimensional distance It measures the distance between two points and each point has N elements. Example Calculation This is an example calculation shown below explain how to find the distance between two vectors using Minkowski distance formula . A vector,array of elements declared and initialized in java using one dimensional array. int A[] = { 5 ,3 }; // 2D vector A int B[] = { 2,1 }; // 2D vector B int P =3; P th ...

Metric - Manhattan Distance between two points

Image
The java program finds distance between two points using manhattan distance equation.  The points can be a scalar or vector and the passed to function as arguments can be integer or double datatype. 1D distance between two points It measures distance between two points on a line (1D) by absolute difference between them and the points are scalar value. Two points on 2D cartesian space It measures distance between two points on a plane. The points are vectors and each has two elements. The resulting distance equals sum of the difference of each element on the vectors . Two points on N-Dimensional cartesian space It measures d istance between two points or vectors on N-dimensional space . The points are vectors and each has N elements.The resulting distance equals sum of the difference of each element on the vectors. This is an example calculation shown below explain how to find the distance between two vectors using Manhat...

Arithmetic and Geometric Sequence

Image
Arithmetic Sequence The program generates arithmetic sequence of K items.  on executing the program, user asked to the following input values  a (start value) , d (common difference) and K (number of elements in the sequence) to generate the sequence.   a - starting value  d - common difference import java.util.Scanner; public class ArithSequence { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println( "Generate Arithmetic Sequence n" ); Scanner sc = new Scanner(System.in); System.out.println( "starting value for a " ); int a = sc.nextInt(); System.out.println( "Enter common deference for d " ); int d = sc.nextInt(); System.out.println( "Enter K items in sequence" ); int K = sc.nextInt(); sc.close(); System.out.println( "nSequence " ); int s=0; for ( int n=0; n < K ; n++ ) { s = n*d + a; System.out.pr int ( ...

Java program String char into Ascii and Generate multiplication tables.

 String char to ASCII value Conversion  The program read s string from user and converts each of the character from string into ascii value using for loop statement and byte type casting. On beginning of the program execution, a string input read from user to convert into ascii char by char.    import java.util.Scanner; public class String2Ascii { public static void main(String[] args) { System.out.println( "string char to Ascii value n" ); Scanner sc = new Scanner(System.in); System.out.println( "Enter a string" ); String str =sc.nextLine(); sc.close(); System.out.println( "\n char \t Ascii" ); for ( int n=0;n<str.length() ; n++ ) { char c = str.charAt(n); System.out.println(c + " -t" + ( byte )c); } } } Output string char to Ascii value Enter a string hello world char - Ascii h - 104 e - 101 l - 108 l - 108 o - 111 ...

Java program Sum of Array Integers

Image
The java program find sum of  integers.       Pseudocode of sum of integer  set sum =0 Read an integer N initialize integer array size of N for n= 0  to N-1       Read an integer data      store data into array end for      for each E  in  array      sum = sum + E end each print sum Java keywords and objects usage in the program BufferedReader class object : provides stream mechanism to read inputs from user/keyboard Integer class object : converts string object into java primitive datatype int for-loop :- repeats reading inputs from user upto N times for-each  : to read element from array in sequential order  import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class SumofArrayNumber { public static void main(String[] args) throws IOException   ...

Java program Factorial Number

A result of series of multiplication from 1 to factorial F is called factorial number F Factorial Number - Example for example, a factorial number denoted by 5! (postfix exclamation symbol with digits) find equivalent decimal value for given a factorial number 5! 5! = 1 x 2 x 3 x 4 x 5 5! = 120 Factorial Number - Pseudocode Read a positive integer F set sf = 1 For n= 2 to F sf = sf * n End for print sf Factorial number - Java code The Java program finds factorial value for a number . The program reads a positive integer from user while running and returns factorial value as result of the it. import java.io.IOException; import java.util.Scanner; public class Factorial { public static void main(String[] args) throws IOException { ...