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

Java example to print root, height of a tree and check whether it is balanced or not

import java.util.*;

public class Question4_3 {

    static class Node {
        Integer data;
        Node left;
        Node right;

        public Node(Integer data) {
            this.data = data;
        }

        public int height() {
            int leftHeight = left != null ? left.height() : 0;
            int rightHeight = right != null ? right.height() : 0;
            return 1 + Math.max(leftHeight, rightHeight);
        }

        public boolean isBalanced() {
            int leftHeight = left != null ? left.height() : 0;
            int rightHeight = right != null ? right.height() : 0;
            return (Math.abs(leftHeight - rightHeight) <= 1);
        }
    }

    public static Node minimalBST(int[] sorted) {
        return minimalBST(sorted, 0, sorted.length - 1);
    }

    private static Node minimalBST(int[] sorted, int start, int end) {

        if (start > end) {
            return null;
        }

        int mid = (start + end) / 2;
        
        Node node = new Node(sorted[mid]);

        node.left = minimalBST(sorted, start, mid - 1);
        node.right = minimalBST(sorted, mid + 1, end);

        return node;
    }

    public static Node insert(Node node, Integer data) {
        if (data == null) {
            return null;
        }

        if (node == null) {
            node = new Node(data);
            return node;
        }

        if (data.compareTo(node.data) <= 0) {
            if (node.left == null) {
                node.left = new Node(data);
            } else {
                insert(node.left, data);
            }
        } else {
            if (node.right == null) {
                node.right = new Node(data);
            } else {
                insert(node.right, data);
            }
        }

        return node;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

        Node root = minimalBST(array);

        System.out.println("Root? " + root.data);
        // System.out.println("Created BST? " + root.isBST());
        System.out.println("Height: " + root.height());
        System.out.println("Is balanced? " + root.isBalanced());

        Random random = new Random();

        root = null;
        array = new int[20];

        for (int i = 0; i < array.length; i++)
        {
            array[i] = random.nextInt(array.length);
            root = insert(root, array[i]);
            System.out.print(array[i] + " ");
        }
        System.out.println();

        // root = minimalBST(array);

        System.out.println("Root? " + root.data);
        System.out.println("Height: " + root.height());
        System.out.println("Is balanced? " + root.isBalanced());
    }
}

Java example to create class to input age of a puppy, increase age by 1 and print it

public class ExamplePupAge {
    int age;
    
    public void pup(int age) {
        age = age + 1;
        System.out.println("My pup's age is: " + age);
    }

    public static void main(String[] args) {
        ExamplePupAge doggy = new ExamplePupAge();
        doggy.pup(1);
    }
}

teste

//Caixa.java:
public class Caixa {

//Variaveis de classe
//O volume total e compartilhado:
public static double volumeTotal = 0;

//Atributos especificos:
private double altura;
private double comprimento;
private double largura;

//Construtor:
public Caixa(double a, double b, double c) {
altura = a;
comprimento = b;
largura = b;
volumeTotal = volumeTotal+(altura*comprimento*largura);
}

//Metodo calcular volume:
public double volume() {
    return altura*comprimento*largura;
}
}

Bash shell example to print Hello World

# Hello World program in Bash Shell

echo "Hello World!" 

Java example to print Hello World

public class HelloWorld{
    
     public static void main(String []args){
        System.out.println("Hello World");
     }
     
}

Objective-C example to print Hello World

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
   NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

   NSLog (@"hello world");
   [pool drain];
   return 0;
}

C++ example to print your name on the console

cpp

#include <iostream>
using namespace std;

int main()
{
    //printing any name
    cout << "Ranjit kumar" << endl;

    return 0;
}

C program to demonstrate example of for loop with false condition

c

#include <stdio.h>

int main()
{

    for (; 0;) {
        printf("Hello it will execute");
    }

    return 0;
}

C++ example to Insert, Deleted, Search students record using class

cpp

//============================================================================
// Name        : DFS_AssignmentNo1.cpp
// Author      : Rushikesh
// Version     :
// Copyright   : @rushikesh
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
#define MAX 100

class LinearArray {
public:
    long int ACN;
    string FNAME, LNAME;
    int FEES, YEAR;
    void insert();
    void display();
    //void searchbyyear(int year);
    //void searchbyfees(int fee);
};

void LinearArray::insert()
{
    cout << "\n************************************************************";
    cout << "\nEnter the Adhar Card No.:";
    cin >> ACN;
    cout << "\nEnter the first name:";
    cin >> FNAME;
    cout << "\nEnter the Last name:";
    cin >> LNAME;
    cout << "\nEnter the fees:";
    cin >> FEES;
    cout << "\nEnter the Year:";
    cin >> YEAR;
    cout << "\n************************************************************";
}

void LinearArray::display()
{
    cout << "\n************************************************************";
    cout << "\nAdhar card NO.=" << ACN;
    cout << "\nFirst Name=" << FNAME;
    cout << "\nLast name=" << LNAME;
    cout << "\nFees=" << FEES;
    cout << "\nYear=" << YEAR;
    cout << "\n************************************************************";
}

int main()
{
    static int STRENGTH = 0;
    LinearArray a[30];
    int ch1, n, fee, year, temp = 0;
    char ch2;
    do {
        cout << "************************************************************";
        cout << "\n1.Insert record\n2.Display records\n3.Search records by using Fees\n4.Search by using Year";
        cout << "\n************************************************************";
        cout << "\nEnter your choice:";
        cin >> ch1;
        switch (ch1) {
        case 1:
            cout << "\nHow many records you want to insert...?";
            cin >> n;
            temp += n;
            while (STRENGTH < temp) {
                a[STRENGTH].insert();
                STRENGTH++;
            }
            break;
        case 2:
            for (int i = 0; i < STRENGTH; i++) {
                a[i].display();
            }
            break;
        case 3:
            cout << "\nEnter the fees:";
            cin >> fee;
            cout << "\nDetails of the Students whose Fees are " << fee << " and more than " << fee << ":";
            for (int i = 0; i < STRENGTH; i++) {
                if (a[i].FEES >= fee) {
                    cout << "\nAdhar card NO.=" << a[i].ACN;
                    cout << "\nFirst Name=" << a[i].FNAME;
                    cout << "\nLast name=" << a[i].LNAME;
                    cout << "\nYear=" << a[i].YEAR;
                }
            }
            break;
        case 4:
            cout << "\nEnter the year:";
            cin >> year;
            cout << "\nDetails of the Students whose year is " << year;
            for (int i = 0; i < STRENGTH; i++) {
                if (a[i].YEAR >= year) {
                    cout << "\n************************************************************";
                    cout << "Adhar card NO.=" << a[i].ACN;
                    cout << "First Name=" << a[i].FNAME;
                    cout << "Last name=" << a[i].LNAME;
                    cout << "Fees=" << a[i].FEES;
                    cout << "\n************************************************************";
                }
            }
            break;
        default:
            cout << "\nInvalid Choice!!!";
            break;
        }
        cout << "\nDo you want to continue...?(Y/N):";
        cin >> ch2;
    } while (ch2 == 'Y' || ch2 == 'y');
    return 0;
}

C language example to print Hello World

c

#include <stdio.h>

int main()
{
    printf("Hello, World!\n");

    return 0;
}

Advertisements
Loading...

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