//First way Method
public static void main(String[] args) {
printMessage();
add(5,9);
multiply(10, 10);
boolean sum = sum(3,1,5);
System.out.println(sum);
}
public static void printMessage() {
System.out.println("hapy birthday to you: ");
}
public static void add(int a, int b) {
System.out.println(a + b);
}
public static void multiply(int a, int b) {
System.out.println(a * b);
}
} //output:
hapy birthday to you:
14
100
// second way Return Method:
public static void main(String[] args) {
int sum = add(5,9);
System.out.println(sum);
int product = multiply(10,10);
System.out.println(product);
}
public static int add(int a, int b) {
return a+b;
}
public static int multiply(int a, int b) {
return a * b;
}
}
//output:
14
100
// String type Method.
public static void main(String[] args) {
String name = self("my name is mohamed, i am 20 years old!");
System.out.println(name);
String home = house("My house is very beautifull! ");
System.out.println(home);
}
public static String self(String n) {
return n.toLowerCase();
}
public static String house(String h) {
return h.toUpperCase();
}
}
//Array Method:
public static void main(String[] args) {
String array = myArray("This is my aweosme array:");
System.out.println(array);
int[] awesomeArray = arrayFromInt(4, 6, 7, 8);
System.out.println(awesomeArray[0]);
System.out.println(awesomeArray[1]);
System.out.println(awesomeArray[2]);
System.out.println(awesomeArray[3]);
}
public static String myArray(String a) {
return a.toUpperCase();
}
public static int[] arrayFromInt(int a, int b, int c, int d) {
int[] array = new int[4];
array [0] = a;
array [1] = b;
array [2] = c;
array [3] = d;
return array;
}
//output:
THIS IS MY AWEOSME ARRAY:
4
6
7
8
public static boolean sum(int x, int y, int z) {
if(x<=y && y<= z ) {
return true;
}
else {
return false;
}
}
}
//output:
false
import javax.swing.*;
public class FirstSwingExample {
public static void main(String[] args) {
JFrame f=new JFrame();//creating instance of JFrame
JButton b=new JButton("click");//creating instance of JButton
b.setBounds(130,100,100, 40);//x axis, y axis, width, height
f.add(b);//adding button in JFrame
f.setSize(400,500);//400 width and 500 height
f.setLayout(null);//using no layout managers
f.setVisible(true);//making the frame visible
}
}
List<String> songList = new ArrayList<>();
songList.add("Some song"); //Repeat until satisfied
System.out.println("\n\tWelcome! Please choose a song!");
String songChoice = scan.nextLine();
while (!songList.contains(songChoice)) {
//Do stuff when input is not a recognised song
}
import java.util.Stack;
/**
* Java Program to implement a binary search tree. A binary search tree is a
* sorted binary tree, where value of a node is greater than or equal to its
* left the child and less than or equal to its right child.
*
* @author WINDOWS 8
*
*/
public class BST {
private static class Node {
private int data;
private Node left, right;
public Node(int value) {
data = value;
left = right = null;
}
}
private Node root;
public BST() {
root = null;
}
public Node getRoot() {
return root;
}
/**
* Java function to check if binary tree is empty or not
* Time Complexity of this solution is constant O(1) for
* best, average and worst case.
*
* @return true if binary search tree is empty
*/
public boolean isEmpty() {
return null == root;
}
/**
* Java function to return number of nodes in this binary search tree.
* Time complexity of this method is O(n)
* @return size of this binary search tree
*/
public int size() {
Node current = root;
int size = 0;
Stack<Node> stack = new Stack<Node>();
while (!stack.isEmpty() || current != null) {
if (current != null) {
stack.push(current);
current = current.left;
} else {
size++;
current = stack.pop();
current = current.right;
}
}
return size;
}
/**
* Java function to clear the binary search tree.
* Time complexity of this method is O(1)
*/
public void clear() {
root = null;
}
}
import java.io.*;
import java.util.*;
class GFG
{
static int maxCuts(int n, int a, int b, int c)
{
if(n == 0) return 0;
if(n < 0) return -1;
int res = Math.max(maxCuts(n-a, a, b, c),
Math.max(maxCuts(n-b, a, b, c),
maxCuts(n-c, a, b, c)));
if(res == -1)
return -1;
return res + 1;
}
public static void main(String [] args)
{
int n = 5, a = 2, b = 1, c = 5;
System.out.println(maxCuts(n, a, b, c));
}
}
class Solution{
//Function to count the frequency of all elements from 1 to N in the array.
public static void frequencyCount(int arr[], int N, int P)
{
//Decreasing all elements by 1 so that the elements
//become in range from 0 to n-1.
int maxi = Math.max(P,N);
int count[] = new int[maxi+1];
Arrays.fill(count, 0);
for(int i=0;i<N;i++){
count[arr[i]]++;
}
for(int i=0;i<N;i++){
arr[i] = count[i+1];
}
}
}
// IntendedActivity - the file you wish to open
// CurrentActivity - the file where this code is placed
Intent intent = new Intent (this, IntendedActivity.class);
CurrentActivity.this.startActivity(intent);
public class LocalDatabase extends SQLiteOpenHelper {
private static final String mDatabaseName = "LocalDatabase";
private static final int mDatabaseVersion = 1;
public LocalDatabase(Context context) {
super(context, mDatabaseName, null, mDatabaseVersion);
SQLiteDatabase db = this.getWritableDatabase();
}
}
Comments