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

String Concatenation in Java

public class HelloWorld{

     public static void main(String []args){
      char caracter='a'; 
        String cadena="juan jesus";
        char caracterDos='e';
        String cadenaDos= "jimenez";
        System.out.println(cadena+cadenaDos);
     }
}

peso

import java.util.Scanner;
 
public class ProgramacondicionesAnidadas2 {
    public static void main(String[] args) {
        Scanner teclado=new Scanner(System.in);
        //----------------VARIABLES---------------
        double p,e,imc;
        //----------------ENTRADAS----------------
        System.out.println("Cual es su peso ");
        p= teclado.nextDouble();
        System.out.println("Cual es tu estatura");
        e= teclado.nextDouble();
        //----------------CALCULO----------------
        imc= p/(e*e);
        System.out.println("SU IMC ES: "+imc);
        //--------SITUACION IMC POR PERSONA-------    
        if (imc<18.5) {
            System.out.println("BAJO PESO");
        } else if (imc>=18.5 && imc<=24.9) {
            System.out.println("NORMAL");
        } else if (imc>=25 && imc<=29.9) {
            System.out.println("SOBREPESO");
        } else {
            System.out.println("OBESIDAD");
        }
    }
 
}

Alfan

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class TestThread {
	
   public static void main(final String[] arguments) throws InterruptedException {
      ThreadPoolExecutor executor = (ThreadPoolExecutor)Executors.newCachedThreadPool();

      //Stats before tasks execution
      System.out.println("Largest executions: "
         + executor.getLargestPoolSize());
      System.out.println("Maximum allowed threads: "
         + executor.getMaximumPoolSize());
      System.out.println("Current threads in pool: "
         + executor.getPoolSize());
      System.out.println("Currently executing threads: "
         + executor.getActiveCount());
      System.out.println("Total number of threads(ever scheduled): "
         + executor.getTaskCount());

      executor.submit(new Task());
      executor.submit(new Task());

      //Stats after tasks execution
      System.out.println("Core threads: " + executor.getCorePoolSize());
      System.out.println("Largest executions: "
         + executor.getLargestPoolSize());
      System.out.println("Maximum allowed threads: "
         + executor.getMaximumPoolSize());
      System.out.println("Current threads in pool: "
         + executor.getPoolSize());
      System.out.println("Currently executing threads: "
         + executor.getActiveCount());
      System.out.println("Total number of threads(ever scheduled): "
         + executor.getTaskCount());

      executor.shutdown();
   }  

   static class Task implements Runnable {

      public void run() {

         try {
            Long duration = (long) (Math.random() * 5);
            System.out.println("Running Task! Thread Name: " +
               Thread.currentThread().getName());
            TimeUnit.SECONDS.sleep(duration);
            System.out.println("Task Completed! Thread Name: " +
               Thread.currentThread().getName());
         } catch (InterruptedException e) {
            e.printStackTrace();
         }
      }
   }
}

Compile and Execute Java Online

//How do you find the missing number in a given integer array of 1 to 100
import java.util.*;
import java.util.Arrays;
import java.util.List;

public class HelloWorld{

     public static void main(String []args){
        // System.out.println("Hello World");
        List<Integer> numbers = new ArrayList<Integer>();
        numbers.add(1);
        numbers.add(3);
        numbers.add(90);
        numbers.add(95);
        List<Integer> missingNum = new ArrayList<Integer>();
        
        
        int status = 1;
        for(int i=0;i<numbers.size();i++){
            if(status == numbers.get(i)){
                status++;
            }else{
                int num=numbers.get(i);
                for(int j=status;j<num;j++){
                    missingNum.add(j);
                }
                status=numbers.get(i)+1;
            }
        }
        if(status!=100){
          for(int i=status;i<=100;i++){
              missingNum.add(i);
          } 
        }
        for(int i=0;i<missingNum.size();i++){
            System.out.println(missingNum.get(i));
        }
        
     }
}

Simple Inheritance using Java

abstract class GameObject {
    public abstract void touch(GameObject other);
    // NOTE: This is ugly because these two definitions need to be in the main
    //       class, it could be avoided by some more functionality i think?
    public abstract void touch(Mazub other);
    public abstract void touch(Plant other);
}

class Mazub extends GameObject {
    @Override
    public void touch(GameObject other) {
        other.touch(this);
    }
    
    @Override
    public void touch(Plant plant) {
        System.out.println("Mazub touches Plant");
    }
    
    @Override
    public void touch(Mazub mazub) {
        System.out.println("Mazub touches Mazub");
    }
}

class Plant extends GameObject {
    @Override
    public void touch(GameObject other) {
        other.touch(this);
    }
    
    @Override
    public void touch(Mazub mazub) {
        System.out.println("Plant touches Mazub");
    }
    
    @Override
    public void touch(Plant plant) {
        System.out.println("Plant touches Plant");
    }
}


public class HelloWorld {
     public static void main(String []args) {
        GameObject a = new Mazub();
        GameObject b = new Plant();
        a.touch(b);
        b.touch(a);
     }
}

Constructor Overloading using Java

class Area
{
    int s1,s2,s3;
    Area(int s1)
    {
        int a;
        this.s1=s1;
        a=this.s1*this.s1*3.141;
        System.out.println("The area of circle is"+a);
    }
    Area(int l,int b)
    {
        int a;
        s1=l;
        s2=b;
        a=s1*s2;
        System.out.println("The area of rectangle is"+a);
    }
    Area(int a,int b,int c)
    {
        int s,ar;
        s1=a;
        s2=b;
        s3=c;
        s=(s1+s2+s3)/2;
        ar=(s*(s-s1)*(s-s2)*(s-s3))^(1/2);
        System.out.println("The area of triangle"+ar);
    }
}
public class AreaDemo
{
    public static void main(String args[])
    {
        Area a1=new Area(5);
        Area a2=new Area(5,10);
        Area a3=new Area(5,6,7);
    }
}

Method Overloading using Java

class Area
{
    int s1,s2,s3;
    void Area(int s1)
    {
        int a;
        this.s1=s1;
        a=this.s1*this.s1*3.141;
        System.out.println("The area of circle is"+a);
    }
    void Area(int l,int b)
    {
        int a;
        s1=l;
        s2=b;
        a=s1*s2;
        System.out.println("The area of rectangle is"+a);
    }
    void Area(int a,int b,int c)
    {
        int s,ar;
        s1=a;
        s2=b;
        s3=c;
        s=(s1+s2+s3)/2;
        ar=(s*(s-s1)*(s-s2)*(s-s3))^(1/2);
        System.out.println("The area of triangle"+ar);
    }
}
public class AreaDemo
{
    public static void main(String args[])
    {
        Area a1=new Area();
        a1.Area(5);
        a1.Area(5,10);
        a1.Area(5,6,7);
    }
}

Compile and Execute Java Online

public class Add
{
public static void main(String... ags)
{
    int x=5,y=6,z;
    z=x+y;
    System.out.println("sum="+z);
    
}
}

Copying part of Array in Java

package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {
   public static void main(String[] args) {

      // intializing an array arr1
      long[] arr1 = new long[] {15, 10, 45};

      // printing the array
      System.out.println("Printing 1st array:");
      for (int i = 0; i < arr1.length; i++){
         System.out.print(arr1[i]+" ");  
      }
      
      System.out.println("");  

      // copying array arr1 to arr2 with newlength as 5
      long[] arr2 = Arrays.copyOf(arr1, 16);

      // printing the array arr2
      System.out.println("Printing new array:");
      for (int i = 0; i < arr2.length; i++) {
         System.out.print(arr2[i]+" ");
      }
   }
}

fhgfgh

public class program

Advertisements
Loading...

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