CS Project For Practice JAVA: FREE SOURCE CODE

0
22

String Functions :

package com.company.yash;

public class stringFuntions {
    public static void main(String[] args) {
        //String Funtions
        // Here we are creating the strings
        String first = "Java ";
        String second = "Python ";
        String third = "JavaScript ";

        // Printing the strings we have created above
        System.out.println(first);   // print Java
        System.out.println(second);  // print Python
        System.out.println(third);   // print JavaScript

        // Lenght oF Strings
        System.out.println("length :" + third.length() ); //finding the lenth of javacript

        // Join Two Strings
        //first way
        System.out.println(first + " and " + third + " Both are different languages");
        //second way
        System.out.println(first.concat(second));

        //Compare two Strings
        String first1 = "java programming";
        String second1 = "ython programming";

        // compare first and second strings
        boolean result1 = first1.equals(second1);
        System.out.println("Strings first and second are equal: " + result1);

    }
}


String Array :

package com.company.yash;

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

public class StringArray {
    public static void main(String[] args)
    {
        // Defining a String Array
        String sa[] = { "A", "B", "C", "D", "E", "F" };
        //
        System.out.println("Initial Array:\n" + Arrays.toString(sa));

        String ne = "G"; // Define new element to add
        List<String> l = new ArrayList<String>(Arrays.asList(sa)); // Convert Array to ArrayList
        l.add(ne); // Add new element in ArrayList l
        sa = l.toArray(sa); // Revert Conversion from ArrayList to Array
        // printing the new Array
        System.out.println("Array with added Value: \n" + Arrays.toString(sa)) ;
    }
}

Int Array :

package com.company.yash;

public class intarray {
    public static void main(String args[]){
        int a[]=new int[5];//declaration and instantiation
        a[0]=10;//initialization
        a[1]=20;
        a[2]=70;
        a[3]=40;
        a[4]=50;

        //traversing array

        for(int i=0;i<a.length;i++)//length is the property of array
            System.out.println(a[i]);
    }
}

Merging Array :

package com.company.yash;

public class Mergingarray {
    public static void main(String[] args)
    {
        int firstArray[] = { 11,22,33,44,55,98,76,54,60};
        int secondArray[] = {66,77,88,99,22,67,21,90,80,70};
        int source_arr[], sourcePos, dest_arr[], destPos, len;
        source_arr = firstArray;
        dest_arr = secondArray;
        sourcePos = 2;
        destPos = 4;
        len = 3;

        // Print elements of source

        System.out.print("source_array : ");
        for (int i = 0; i < firstArray.length; i++)
            System.out.print(firstArray[i] + " ");
        System.out.println("");
        System.out.println("sourcePos : " + sourcePos);

        // Print elements of destination

        System.out.print("dest_array : ");
        for (int i = 0; i < secondArray.length; i++)
            System.out.print(secondArray[i] + " ");
        System.out.println("");
        System.out.println("destPos : " + destPos);
        System.out.println("len : " + len);

        //invoking arraycopy() method

        System.arraycopy(source_arr, sourcePos, dest_arr,destPos, len);

        // Print elements of destination after

        System.out.print("Resultant array : ");
        for (int i = 0; i < secondArray.length; i++)
            System.out.print(secondArray[i] + " ");
    }
}

Pure Impure Fun :

package com.company.yash;

class Myclass{
    int num = 5;
}

public class pureimpure {
    public static void main(String[] args) {
        Myclass obj = new Myclass();    //num = 5
        update(obj);
        display(obj);
    }
    public static void update(Myclass ob) {  //impure Function
        ob.num++;
    }

    public static void display(Myclass obj){  //Pure Funtion
        System.out.println("Result  : " + obj.num);
    }
}

Math Functions :

package com.company.yash;

public class mathFunctions {
    public static void main(String[] args) {
        double x = 28;
        double y = 4;

        double angle = 30;

        // return the maximum of two numbers
        System.out.println("Maximum number of x and y is: " +Math.max(x, y));

        // return the square root of y
        System.out.println("Square root of y is: " + Math.sqrt(y));

        //returns 28 power of 4 i.e. 28*28*28*28
        System.out.println("Power of x and y is: " + Math.pow(x, y));

        // return the logarithm of given value
        System.out.println("Logarithm of x is: " + Math.log(x));
        System.out.println("Logarithm of y is: " + Math.log(y));

        // return the logarithm of given value when base is 10
        System.out.println("log10 of x is: " + Math.log10(x));
        System.out.println("log10 of y is: " + Math.log10(y));

        // return the log of x + 1
        System.out.println("log1p of x is: " +Math.log1p(x));

        // return a power of 2
        System.out.println("exp of a is: " +Math.exp(x));

        // return (a power of 2)-1
        System.out.println("expm1 of a is: " +Math.expm1(x));

        // converting values to radian
        double b = Math.toRadians(angle);

        // return the trigonometric sine of a
        System.out.println("Sine value of a is: " +Math.sin(angle));

        // return the trigonometric cosine value of a
        System.out.println("Cosine value of a is: " +Math.cos(angle));

        // return the trigonometric tangent value of a
        System.out.println("Tangent value of a is: " +Math.tan(angle));

        // return the trigonometric arc sine of a
        System.out.println("Sine value of a is: " +Math.asin(angle));

        // return the trigonometric arc cosine value of a
        System.out.println("Cosine value of a is: " +Math.acos(angle));

        // return the trigonometric arc tangent value of a
        System.out.println("Tangent value of a is: " +Math.atan(angle));

        // return the hyperbolic sine of a
        System.out.println("Sine value of a is: " +Math.sinh(angle));

        // return the hyperbolic cosine value of a
        System.out.println("Cosine value of a is: " +Math.cosh(angle));

        // return the hyperbolic tangent value of a
        System.out.println("Tangent value of a is: " +Math.tanh(angle));
    }

}

Constructor :

package com.company.yash;

public class constructor {

    //creating a default constructor
//    constructor(){
//        System.out.println("FAANG IS MY DREAM JOB");
//    }
    //===============================================//
//    int id;
//    String name;
//    //creating a parameterized constructor
//    constructor(int i ,String n){
//        id = i;
//        name = n;
//    }
//    //method to display the values
//    void display(){
//        System.out.println(id+" "+name);
//    }

    //===============================================//

    //Contructor Overloading

    int id;
    String name;
    int classN;
    //creating three arg constructor
    constructor(int i , String n, int c) {
        id=i ; name= n; classN = c;
    }

    void display(){
         System.out.println(id+" "+name+" "+classN);
    }


    public static void main(String[] args) {
        //calling a default constructor
        //constructor constructor = new constructor();

        //==================================//
//        //creating objects and passing values
//        constructor c1 = new constructor(101,"Yash");
//        constructor c2 = new constructor(102,"Second Yash");
//
//        //calling method to display the values of object
//        c1.display();
//        c2.display();

        //==================================//

        //Constructor Overloading

        constructor c1 = new constructor(222,"Yash",3);
        constructor c2 = new constructor(786,"Second Yash",4);
        c1.display();
        c2.display();


    }
}

Function Overloading :

package com.company.yash;

public class functionOverloading {
    // Overloaded sum(). This sum takes two int parameters
    public int sum(int x, int y)
    {
        return (x + y);
    }

    // Overloaded sum(). This sum takes three int parameters
    public int sum(int x, int y, int z)
    {
        return (x + y + z);
    }

    // Overloaded sum(). This sum takes two double parameters
    public double sum(double x, double y)
    {
        return (x + y);
    }

    // Driver code
    public static void main(String args[])
    {
        functionOverloading s = new functionOverloading();
        System.out.println(s.sum(10, 20));
        System.out.println(s.sum(10, 20, 30));
        System.out.println(s.sum(10.5, 20.5));
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here