Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Compile and Execute Java Online

//Name: Erik sadoff

import static java.lang.System.*;

public class cardRunner
{
    public static void main(String args[])
    {
        
        Card one = new Card("Spades", 9);
        out.println(one.getSuit());
        out.println(one.getFace());
        
        Card two = new Card("Diamonds", 1);
        out.println(two);
        two.setFace(3);
        out.println(two);
        
        Card three = new Card("Clubs", 4);
        out.println(three);
        
        Card four = new Card ("Spades", 1);
        out.print (four);
        out.println (" 11");
        
        Card five = new Card ("Hearts", 13);
        out.print(five);
        out.println(" 10");
         
        Card six = new Card ("Hearts", 12);
        out.print(six);
        out.println(" 10");
    
        Card seven = new Card ("Hearts", 11);
        out.print (seven);
        out.println(" 10");
        
        Card eight = new Card ("Clubs", 12);
        out.print (eight);
        out.println (" 10");
        
    }
}
//
class Card
{
    public static final String FACES[]={"Zero", "Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"};

    private String suit;
    private int face;
    //
    public Card (String s, int f)
    {
        suit = s;
        face = f;
    }
    public void setFace (int f)
    {
        face = f;
    }
    public void setSuit (String s)
    {
        suit = s;
    }
    //
    public String getSuit()
    {
        return suit;
    }
    public int getFace()
    {
        return face;
    }
    //
    public String toString()
    {
        return FACES[face] + " of " + suit;
    }
}
class BlackJackCard extends Card
{
    public BlackJackCard (String s, int f)
    {
        super (s, f);
    }
    public int getValue()
    {
        if(getFace() == 1)
        
            return 11;
        
        if(getFace() > 10)
        
            return 10;
        else
            return getFace();
    }
    public String toString()
    {
        return super.toString() + " " + getValue();
    
    }
    
}

javascript:void(0);

public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
        String x="hello from hatem";
        System.out.println(x.substring(2,4));
        for (int a =0 ;a<10;a++)
        {System.out.println("hellooooo");}
        
        int [] arr =new int[5];
       int  arr.add(1,new Integer(5));
        
        
        
     }
}

Compile and Execute Java Online

import java.util.*;
public class HelloWorld{

     public static void main(String []args){
        
        String a = "straight";
        String b = "s6t";
        //Boolean c = helper(a,b);
        System.out.println(helper(a,b));
     }
     public static boolean helper(String a, String b){
         if(a.length()==0 && b.length()==0)return true;
         else if(a.length()==0 || b.length()==0)return false;
         //case1
         if(Character.isDigit(b.charAt(0))){
             int i=0,num=0;
             while(i<b.length() && Character.isDigit(b.charAt(i))){
                 num = num*10 + b.charAt(i) - '0';
                 i++;
             }
         if(num>a.length())return false;
         else return helper(a.substring(num), b.substring(i));
            
         }else
         //case 2
             {
             if(a.charAt(0) != b.charAt(0))return false;
             else{
                 return helper(a.substring(1),b.substring(1));
             }
         }
     }
}

WILL_SORT_TEST

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class WillSortTest {

    public static void main(String args[]) {

        List<DataDupeEndpoint> endpointList = getTestEndpointList();

        System.out.println("ORIGINAL ENDPOINT LIST;");

        for (DataDupeEndpoint item : endpointList) {
            System.out.println(item.toString());
        }

        endpointList.sort(getEndpointComparator());

        System.out.println("\n\n");
        System.out.println("SORTED ENDPOINT LIST:");

        for (DataDupeEndpoint item : endpointList) {
            System.out.println(item.toString());
        }
    }

    private static List<DataDupeEndpoint> getTestEndpointList() {
        List<DataDupeEndpoint> endpointList = new ArrayList<>();

        DataDupeEndpoint dataDupeEndpoint1 = new DataDupeEndpoint()
        {{
            source = "TRVDEV";
            target = "TRVQA";
            schemaMap = Arrays.asList(new SchemaMap[] {
                    new SchemaMap() {{
                        sourceSchema = "DBO";
                        targetSchema = "BIRPT";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "BIRPT";
                        targetSchema = "BIRPT";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "A";
                        targetSchema = "B";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "B";
                        targetSchema = "A";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "A";
                        targetSchema = "A";
                    }},
            });
        }};

        DataDupeEndpoint dataDupeEndpoint2 = new DataDupeEndpoint()
        {{
            source = "TRVUAT";
            target = "TRVPRD_2";
        }};

        DataDupeEndpoint dataDupeEndpoint3 = new DataDupeEndpoint()
        {{
            source = "TRVDEV";
            target = "TRVQA";
            schemaMap = Arrays.asList(new SchemaMap[] {
                    new SchemaMap() {{
                        sourceSchema = "DBO";
                        targetSchema = "BIRPT2";
                    }},
                    new SchemaMap() {{
                        sourceSchema = "BIRPT";
                        targetSchema = "BIRPT";
                    }}
            });
        }};

        DataDupeEndpoint dataDupeEndpoint4 = new DataDupeEndpoint()
        {{
            source = "TRVDEV";
            target = "TRVQA";
            schemaMap = Arrays.asList(new SchemaMap[] {
                    /*new SchemaMap() {{
                        sourceSchema = "DBO";
                        targetSchema = "BIRPT1";
                    }},*/
                    new SchemaMap() {{
                        sourceSchema = "BIRPT";
                        targetSchema = "BIRPT";
                    }}
            });
        }};

        DataDupeEndpoint dataDupeEndpoint5 = new DataDupeEndpoint()
        {{
            source = "TRVUAT";
            target = "TRVPRD_1";
        }};

        DataDupeEndpoint dataDupeEndpoint6 = new DataDupeEndpoint()
        {{
            source = "TRVUAT";
            target = "TRVPRD_2";
            schemaMap = Arrays.asList(new SchemaMap[] {
                    new SchemaMap() {{
                        sourceSchema = "BIRPT";
                        targetSchema = "BIRPT";
                    }}
            });
        }};

        endpointList.add(dataDupeEndpoint6);
        endpointList.add(dataDupeEndpoint1);
        endpointList.add(dataDupeEndpoint2);
        endpointList.add(dataDupeEndpoint3);
        endpointList.add(dataDupeEndpoint4);
        endpointList.add(dataDupeEndpoint5);
        //endpointList.add(dataDupeEndpoint6);

        return endpointList;
    }

    private static Comparator getEndpointComparator() {
        Comparator endpointComparator = new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                DataDupeEndpoint e1 = o1 instanceof DataDupeEndpoint ? (DataDupeEndpoint) o1 : null;
                DataDupeEndpoint e2 = o2 instanceof DataDupeEndpoint ? (DataDupeEndpoint) o2 : null;

                int sourceEndpointCompare = e1.source.compareToIgnoreCase(e2.source);
                int targetEndpointCompare = e1.target.compareToIgnoreCase(e2.target);

                if (sourceEndpointCompare != 0) {
                    return sourceEndpointCompare;
                } else {
                    if (targetEndpointCompare != 0) {
                        return targetEndpointCompare;
                    } else {
                        if (e1.schemaMap == null && e2.schemaMap == null) {
                            return 0;
                        } else if (e1.schemaMap == null && e2.schemaMap != null) {
                            return -1;
                        } else if (e1.schemaMap != null && e2.schemaMap == null) {
                            return 1;
                        } else {
                            e1.schemaMap.sort(schemaMapComparator);
                            e2.schemaMap.sort(schemaMapComparator);

                            int schemaMap1 = e1.schemaMap.size();
                            int schemaMap2 = e2.schemaMap.size();

                            if (schemaMap1 < schemaMap2) {
                                return -1;
                            } else if (schemaMap1 > schemaMap2) {
                                return 1;
                            } else {
                                for (int i = 0; i < schemaMap1; i++) {
                                    int sourceSchemaCompare = e1.schemaMap.get(i).sourceSchema.compareToIgnoreCase(e2.schemaMap.get(i).sourceSchema);
                                    int targetSchemaCompare = e1.schemaMap.get(i).targetSchema.compareToIgnoreCase(e2.schemaMap.get(i).targetSchema);

                                    if (sourceSchemaCompare != 0) {
                                        return sourceSchemaCompare;
                                    } else {
                                        if (targetSchemaCompare != 0) {
                                            return targetSchemaCompare;
                                        }
                                    }
                                }
                                return 0;
                            }
                        }
                    }
                }
            }

            Comparator schemaMapComparator = new Comparator() {
                @Override
                public int compare(Object o1, Object o2) {
                    SchemaMap s1 = o1 instanceof SchemaMap ? (SchemaMap) o1 : null;
                    SchemaMap s2 = o2 instanceof SchemaMap ? (SchemaMap) o2 : null;

                    int sourceSchemaCompare = s1.sourceSchema.compareToIgnoreCase(s2.sourceSchema);
                    int targetSchemaCompare = s1.targetSchema.compareToIgnoreCase(s2.targetSchema);

                    if (sourceSchemaCompare != 0) {
                        return sourceSchemaCompare;
                    } else {
                        return targetSchemaCompare;
                    }
                }
            };
        };
        return endpointComparator;
    }

    private static class DataDupeEndpoint {
        public String source;
        public String target;
        public List<SchemaMap> schemaMap;

        @Override
        public String toString() {
            return "DataDupeEndpoint{" +
                    "source='" + source + '\'' +
                    ", target='" + target + '\'' +
                    ", schemaMap=" + schemaMap +
                    '}';
        }
    }

    private static class SchemaMap {
        public String sourceSchema;
        public String targetSchema;

        @Override
        public String toString() {
            return "SchemaMap{" +
                    "sourceSchema='" + sourceSchema + '\'' +
                    ", targetSchema='" + targetSchema + '\'' +
                    '}';
        }
    }
}

quiz

import java.util.scanner ;
public class HelloWorld{

     public static void main(String []args){
        System.out.println("Hello World");
    Scanner scan = new Scanner (System.in);
    System.out.println("enter array length :"  );
    int x = scan.nextInt(); 
    double array[]= new double[x];
    for (i=0; i<x ; i++)
    {
        System.out.println ("enter" + (i+1)+ "elements to store in array :");
        array[i]= scan.nextDouble();
    }
     System.out.println ("elements of array are:");
    for  (i=0; i<x ; i++)
    {
         System.out.println(array[i]+" " );
    }
    System.out.println();
    
    double sum= 0, avg= 0;
        for  (i=0; i<x ; i++)
        {
            sum+=array[i];
        }
    avg= sum/x ;
    
     System.out.println("the sum is :" + sum);
     System.out.println("the avg is :" + avg);
    
    double min=0 ;
    for (i=0; i<x ; i++)
    {
        if (min> array[i])
        {
            min = array[i];
        }
        }
        
        double max=0 ;
    for (i=0; i<x ; i++)
    {
        if (max< array[i])
        {
            max = array[i];
        }
        }
        
     }
}
    
    
    
    
    
    
     }
}

Compile and Execute Java Online

import java.util.*;

public class HelloWorld{


    public static boolean isSubstring() {
        String str1 = "f";
        String str2 = "abcdef";
        int j;
        if(str1 == null || str1.isEmpty()) return false;
//        System.out.println(str2.charAt(str2.length()-1));
        for(int i = 0; i < str2.length(); ++i) {
            if(str1.charAt(0) == str2.charAt(i)) { //First matching character
//            System.out.println("First match "+i);
                for(j = 0; j < str1.length(); ++j) {
                    System.out.println("j "+j);
                    if(str2.charAt(i+j) != str1.charAt(j))
                        break;
                    //if we reached the end
                    if(j == (str1.length()-1))
                        return true;
                }
            }
        }
        return false;
    }
public static void main(String args[]) 
    { 
        System.out.println("Is SubString "+isSubstring());
         /* creating a binary tree and entering 
        the nodes */
        BinaryTree tree = new BinaryTree(); 
        tree.root = new Node(8);
        tree.root.left = new Node(5);
        tree.root.right = new Node(11);
        tree.root.left.left = new Node(3); 
        tree.root.left.right = new Node(6); 
        tree.root.right.left = new Node(10);
        tree.root.right.right = new Node(12);
        tree.root.right.left.left = new Node(8);
//        tree.root.right.left.right = new Node(9);

        tree.inOrderWithRec(tree.root);
        System.out.println("InOrder");
        tree.preOrderWithRec(tree.root);
        System.out.println("Preorder");
        tree.postOrderWithRec(tree.root);
        System.out.println("PostOrder");
        int h = tree.height(tree.root);
        System.out.println("Ht "+h);

        tree.sumOfAllNodes(tree.root);
        
//        tree.countLeaves(tree.root);
//          tree.heightOfAll(tree.root);
          System.out.println("Count is "+BinaryTree.count);
        
//        tree.numberOfWaysToTraverse(tree.root);
        
        // int arr[] = {12, 11, 13, 5, 6, 7}; 
        // int n = arr.length;
        
        // for(int i = n/2 - 1; i > 0 ; --i) {
        //     tree.heapifyMinHeap(arr, n, i);
        // }
        // for(int i = n-1; i >= 0; --i) {
        //     //Move current root to the end
        //     int temp = arr[0];
        //     arr[0] = arr[i];
        //     arr[i] = temp;
        //     tree.heapifyMinHeap(arr, i, 0);
        // }
        // for(int i = 0; i < n; ++i) {
        //     System.out.print(" "+arr[i]);
        // }
//        tree.nthPostOrder(tree.root, 3);
//        tree.inOrderWithStack();
    }
}
     
class BinaryTree
{ 
    static int count = 0;
    Node root; 
    public void inOrderWithRec(Node node) {
        if(node == null) return;
        inOrderWithRec(node.left);
        System.out.print(node.data + " ");
        inOrderWithRec(node.right);
    }
    
    public int heightOfAll(Node node) {
        if(node == null) return 0;
        if(node.left == null && node.right == null) {
            ++count;
        }
        if(node.left != null)
            heightOfAll(node.left);
        if(node.right != null)
            heightOfAll(node.right);
            
        return count;
    }
    
    public void nthInOrder(Node node, int nThNode) {
        if(node == null) return;
        
        if(count <= nThNode) {
            nthInOrder(node.left, nThNode);
            if(++count == nThNode)
                System.out.print(node.data + " ");
            nthInOrder(node.right, nThNode);
        }
    }
    
    public void nthPreOrder(Node node, int nThNode) {
        if(node == null) return;
        
        if(count <= nThNode) {
            if(++count == nThNode)
                System.out.print(node.data + " ");
            nthPreOrder(node.left, nThNode);
            nthPreOrder(node.right, nThNode);
        }
    }
    
    public void nthPostOrder(Node node, int nThNode) {
        if(node == null) return;
        
        if(count <= nThNode) {
            nthPostOrder(node.left, nThNode);
            nthPostOrder(node.right, nThNode);
            if(++count == nThNode)
                System.out.print(node.data + " ");
        }
    }
    
    public void inOrderWithStack() {
        int count = 0;
        Node currentNode = root;
        Stack<Node> stack = new Stack<>();
        while(currentNode != null || stack.size() >0) {
            while(currentNode != null) {
                stack.push(currentNode);
                currentNode = currentNode.left;
                ++count;
            }
            currentNode = stack.pop();
            System.out.print(currentNode.data + " ");
            currentNode = currentNode.right;
        }
        System.out.println("Ht "+count);
    }
    
    public void preOrderWithRec(Node node) {
        if(node == null) return;
        
        System.out.print(node.data + " ");
        preOrderWithRec(node.left);
        preOrderWithRec(node.right);
    }
    
    public int height(Node node) {
        if(node == null) return 0;
        if(node.left == null && node.right == null) {
            ++count;
        }
        int lHt = 0, rHt = 0;
        lHt = height(node.left);
        rHt = height(node.right);
        return (lHt > rHt ? 1 + lHt : 1+ rHt);
    }
    
    public void postOrderWithRec(Node node) {
        if(node == null) return;
        
        postOrderWithRec(node.left);
        postOrderWithRec(node.right);
        System.out.print(node.data + " ");
    }
    
    public void bfsWithRecMain(Node root, int height) {
        for(int i = 1; i < height; ++i) {
            bfsWithRec(root, i);
        }
    }
    
    //level order traversal
    public void bfsWithRec(Node root, int level) {
        if(root == null) return;
        if(level == 1) {
            System.out.println(""+root.data);
        } else if(level > 1) {
            bfsWithRec(root.left, level-1);
            bfsWithRec(root.right, level-1);
        }
    }
    
    public void bfsWithQueue(Node root) {
        Queue<Node> queue = new LinkedList();
        queue.add(root);
        Node currentNode = null;
        while(!queue.isEmpty()) {
           currentNode = queue.poll();
           if(currentNode != null) {
               System.out.println(" "+currentNode.data);
               queue.add(currentNode.left);
               queue.add(currentNode.right);
           }
        }
    }
    
    public int findNodesGreaterThan(Node root, int x) {
        int count = 0;
        Queue<Node> queue = new LinkedList();
        queue.add(root);
        Node currentNode = null;
        while(!queue.isEmpty()) {
           currentNode = queue.poll();
           if(currentNode != null) {
               if(currentNode.data > x) {
                    System.out.println(" "+currentNode.data);
                    ++count;
               }
               queue.add(currentNode.left);
               queue.add(currentNode.right);
           }
        }
        return count;
    }
    
    public int sumOfAllNodes(Node root) {
        int sum = 0;
        Queue<Node> queue = new LinkedList();
        queue.add(root);
        Node currentNode = null;
        while(!queue.isEmpty()) {
            currentNode = queue.poll();
            if(currentNode != null) {
                sum += currentNode.data;
               queue.add(currentNode.left);
               queue.add(currentNode.right);
            }
        }
        System.out.println("Sum is "+sum);
        return sum;
    }

    
    public long factorial(int n) {
        return n*factorial(n-1);
    }
    
    public void heapifyMinHeap(int arr[], int n, int i) {
        int largest = i;
        int l = 2*i + 1;
        int r = 2*i + 2;
        
        if(l < n && arr[l] > arr[largest]) {
            largest = l;
        }
        if(r < n && arr[r] > arr[largest]) {
            largest = r;
        }
        if(largest != i) {
            //Move the current root to end
            int swap = arr[i];
            arr[i] = arr[largest];
            arr[largest] = swap;
            heapifyMinHeap(arr, n, i);
        }
    }
    
    public void numberOfWaysToTraverse(Node root) {
        // long int ways = 1;
        // if(root == null) {
        //     System.out.println("No way of traversing the tree");
        // } else {
        //     Queue<Node> queue = new LinkedList();
        //     queue.add(root);
        //     while(!queue.isEmpty()) {
        //         Node currentNode = queue.poll();
                
                
        //     }
        // }
    }
    
    //bfs variation
    public int countLeaves(Node root) {
        int count = 0;
        Queue<Node> queue = new LinkedList();
        queue.add(root);
        Node currentNode = null;
        while(!queue.isEmpty()) {
           currentNode = queue.poll();
           if(currentNode != null) {
               if(currentNode.left == null
                    && currentNode.right == null) {
                   ++count;
               }
               queue.add(currentNode.left);
               queue.add(currentNode.right);
           }
        }
        System.out.println("Number of leaves "+count);
        return count;
    }
    
    public void mergeTwoArrays() {
        int arr1[] = {1, 2, 3, 5, 6, 7};
        int arr2[] = {4, 6, 7, 8, 9, 10};
        int len1 = arr1.length;
        int len2 = arr2.length;
        int arr3[] = new int[len1+len2];
        Arrays.fill(arr3, 0);
        int i = 0, j = 0, k = 0;
        while(i < len1 && j < len2) {
            int a1 = arr1[i];
            int a2 = arr2[j];
            
            if(a1 < a2) {
                if(k > 0 && (arr3[k-1] == a1)) {
                    ++i;
                    continue;
                } else {
                    arr3[k] = a1;
                    ++i;
                }
            } else {
                if(k > 0 && (arr3[k-1] == a2)) {
                    ++j;
                    continue;
                } else {
                    arr3[k] = a2;
                    ++j;
                }
            }
            ++k;
        }
        while(i < len1) {
            arr3[k++] = arr1[i++];
        }
        while(j < len2) {
            arr3[k++] = arr2[j++];
        }
//        for(int l = 0; i < arr3.length; ++i) {
            System.out.print(Arrays.toString(arr3));
//        }
    }
}
class Node 
{ 
    int data; 
    Node left, right; 
  
    public Node(int item) 
    { 
        data = item; 
        left = right = null; 
    } 
}

ProdutosGenericos

package produtos;

import java.text.DecimalFormat;

public class Produto<T>{
    private T codigo;
    private String descricao;
    private double preco;
    
    public Produto(T codigo, String descricao, double preco){
        this.codigo =  codigo;
        this.descricao = descricao;
        this.preco = preco;
    }
    public T getCodigo(){
        return codigo;
    }
    
    public String getdescricao(){
        return descricao;
    }
    
    public double getPreco(){
        return preco;
    }
       
    @Override
    public String toString(){
        DecimalFormat formata_dinho = new DecimalFormat(".00");
        return "Produto:\n\t" + "resumo: " + codigo + "\n\tdesc:" + descricao + "\n\tpreco: " + formata_dinho.format(preco) + "}";
    }
    public static void main(String[] args){
        Produto<Integer> novo_produto = new Produto<>(0001,"Primeiro produto da Cia",3000);
        System.out.println(novo_produto);
    }

}

example

public class Test {

   public static void main(String args[]) {
      String str1 = "22";
      String str2 = new String("111");
      String str3 = new String("Integers are not immutable");
      
      int result = str1.compareTo( str2 );
      System.out.println(result);
      
      result = str2.compareTo( str3 );
      System.out.println(result);
   }
}

Compile and Execute Java Online

// Edu H.

class Banco{
    static int cdBanco = 1;
    final int maxContas = 10;
    String nmBanco;
    Conta[] contas;    

    Banco(String mmBanco){
        cdBanco++;
    }
    
    void criaConta(){
//        Conta conta = new Conta(cdConta, Cliente cliente, double saldo);
//        Conta continha = new Conta(1,
    }
        
    void leConta(){
        
    }
}


class Cliente {
    String nmCliente;
    String cdCPF;
    
    Cliente(String nome, String cpf){
        this.nmCliente = nome;
        this.cdCPF = cpf;
    }
}


class Conta{
    int cdConta;
    double saldo;
    Cliente cliente;
    
    Conta(int cdConta, Cliente cliente, double saldo){
        this.cdConta = cdConta;
        this.saldo = saldo;
        this.cliente = cliente;
    }
    
    double getSaldo(){
        saldo = this.saldo;
        return saldo;
    }
    
    void aplica(){
        
    }
    
    void debita(){
        
        
    }
}

public class Teste1{
    public static void main (String[] args){
        
        Banco itau = new Banco("Itau");
        Cliente eduh = new Cliente("Edu H.", "0945839485");
        Conta continha = new Conta(1, eduh, 1200.50);
        System.out.println(itau.nmBanco, " = ", continha.saldo);
        
    }
}


test

public class Puppy {
   public Puppy(String name) 
   {
       while(i!=10)
       {
    
      System.out.println("Passed Name is :" + name );

       }
       i--;
   }

   public static void main(String []args) 
   {
      Puppy myPuppy = new Puppy( "tommy"+" "+"tommy" );
   }
}

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.