public: It can be called (method) or accessed (field) from any class in any package.
static: It is declared on the class rather than the object. If a method, you do not need an object to call it, it can be called directly on the class. If a field, there is only one variable for the class, not one per object.
final: If a method, the method cannot be overridden. If a field, the value cannot be changed (a constant).
int: If a method, the return type. If a field, the field type (it can only hold values of type 'int'). int is the primitive integer type.
public static final int getSum(final int n) { int sum = 0; for(int i = 1; i <= n; ++i) { sum += i; } return sum; }
public static final double getAverage(final int[] ns) { if (ns.length == 0) { return 0.0; } int sum = 0; for (int n : ns) { sum += n; } return ((double) sum) / ns.length; }
//with a function: int exponent(int a, int b){ int returnValue; if (x==0) return 1; else return(a*exponent(a,x-1)); } main(){ printf("%d\n", exponent(1, 1)); }
// returns the minimum value in ns - iterative public static final int findMinIterative(final int[] ns) { if (ns.length == 0) { return 0; // return 0 if ns is an empty array } // search each element of ns int min = ns[0]; for (int i = 0; i < ns.length; ++i) { // if an element smaller than min is found, store that as the new min if (ns[i] < min) { min = ns[i]; } } return min; } // returns the minimum value in ns - recursive // Note that this problem does not lend itself to recursion; the solution is very similar to the iterative approach public static final int findMinRecursive(final int[] ns) { if (ns.length == 0) { return 0; // return 0 if ns is an empty array } // start recursion return findMinRecursive(0, ns[0], ns); } // recursive part of algorithm private static final int findMinRecursive(final int i, final int min, final int[] ns) { // bounds check if (i >= ns.length) { return min; } // recurse on next value of ns return findMinRecursive(i + 1, Math.min(min, ns[i]), ns); }
Just change the number assigned to P if you want a different number of palindromes: public class PalindromeNumbers { public static final int P = 50; public static void main(String[] args) { getNums(P); } public static void getNums(int n) { int count = 0, current = 1; while(count < n) { if(palindrome("" + current) { System.out.println(current); count++; } current++; } public static boolean palindrome(String s) { if(s.length() s.charAt(s.length - 1) return palindrome(s.substring(1, s.length - 2) else return false; } }
public static final int getSum(final int n) { int sum = 0; for(int i = 1; i <= n; ++i) { sum += i; } return sum; }
class MyClass { // Declare a static method to return the square of a number. public static int getSquare(final int n) { return n*n; } public static void main(String[] args) { // Call the static method to find 1522 System.out.println( MyClass.getSquare(152) ); } }
// Returns the index of n in ns or -1 if not found. public static final int linearSearch(final int n, final int[] ns) { for(int i = 0; i < ns.length; ++i) { // If found, return the index if(ns[i] == n) { return i; } } return -1; }
public static final double getAverage(final int[] ns) { if (ns.length == 0) { return 0.0; } int sum = 0; for (int n : ns) { sum += n; } return ((double) sum) / ns.length; }
//with a function: int exponent(int a, int b){ int returnValue; if (x==0) return 1; else return(a*exponent(a,x-1)); } main(){ printf("%d\n", exponent(1, 1)); }
// returns the minimum value in ns - iterative public static final int findMinIterative(final int[] ns) { if (ns.length == 0) { return 0; // return 0 if ns is an empty array } // search each element of ns int min = ns[0]; for (int i = 0; i < ns.length; ++i) { // if an element smaller than min is found, store that as the new min if (ns[i] < min) { min = ns[i]; } } return min; } // returns the minimum value in ns - recursive // Note that this problem does not lend itself to recursion; the solution is very similar to the iterative approach public static final int findMinRecursive(final int[] ns) { if (ns.length == 0) { return 0; // return 0 if ns is an empty array } // start recursion return findMinRecursive(0, ns[0], ns); } // recursive part of algorithm private static final int findMinRecursive(final int i, final int min, final int[] ns) { // bounds check if (i >= ns.length) { return min; } // recurse on next value of ns return findMinRecursive(i + 1, Math.min(min, ns[i]), ns); }
Just change the number assigned to P if you want a different number of palindromes: public class PalindromeNumbers { public static final int P = 50; public static void main(String[] args) { getNums(P); } public static void getNums(int n) { int count = 0, current = 1; while(count < n) { if(palindrome("" + current) { System.out.println(current); count++; } current++; } public static boolean palindrome(String s) { if(s.length() s.charAt(s.length - 1) return palindrome(s.substring(1, s.length - 2) else return false; } }
public class CreateDatabase{ public static void main(){ int a=10; int b=20; int result =a+b; System.out.println(result); } }
Implement these methods: public static int smallest(int[] arr) { int small = arr[0]; for(int i = 1; i < arr.size(); i++) if(arr[i] < small) small = arr[i]; return small; } public static int largest(int[] arr) { int large = arr[0]; for(int i = 1; i < arr.size(); i++) if(arr[i] > large) large = arr[i]; return large; }
public class Main{ public static void main(String[] args){ System.out.println("the factorial of 5 is: " + getFactorial(5)); } public static int getFactorial(int num){ return num + getFactorial(num-1); } }
public class FindLeastAndGreatest { public static void main(String[] args) { // number can't be equal with each other int a = 7; int b = 7; int c = 6; System.out.println(least(a,b,c)); System.out.println(greatest(a,b,c)); } public static int least(int a, int b, int c) { int least = 0; if(a < b && a < c) { least = a; } else if(b < a && b < c) { least = b; } else { least = c;} return least; } public static int greatest(int a, int b, int c) { int greatest = 0; if(a > b && a > c) { greatest = a; } else if(b > a && b > c) { greatest = b; } else { greatest = c;} return greatest; } }
You create a static integer as an instance variable by declaring it in a class using a statement that consists of the privacy level, then the keywords "static" and "int", and finally, the name of the variable. For example:public class TheAnswerIsHere {public static int example = 0;}will define an int example with initial value 0. The variable is accessed through the statement TheAnswerIsHere.example.