HelpMeOutAlways: Java
Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Saturday, March 2, 2019

Logic Gates Program In Java(Project)
March 02, 20190 Comments

Source Code



import java.util.Scanner;
//=====================================================================================================================
class star{
      public void star(){
        for(int i=1;i<=50;i++){
            System.out.print("*");
        }
    }
}
class OrGate extends star{
    int or1,or2;

    public void getSet(){
        Scanner Sc=new Scanner(System.in);
     
        System.out.println("\nEnter your Two numbers(either(1/0))\n");
        or1=Sc.nextInt();
        or2=Sc.nextInt();
    }
    public void or(){
        if((or1==0)&&(or2==0)){
            System.out.println("Your output is:");
            System.out.println("0");
        }
        else{
            System.out.println("Your Output is:");
            System.out.println("1");
        }
    }
}
//======================================================================
class AndGate extends star{
    int and1,and2;
 
    public void getSet(){
        Scanner Sc=new Scanner(System.in);
     
        System.out.println("\nEnter your Two numbers(either(1/0))\n");
        and1=Sc.nextInt();
        and2=Sc.nextInt();
    }
    public void and(){
        if((and1==1)&&(and2==1)){
            System.out.println("Your output is:");
            System.out.println("1");
        }
        else{
            System.out.println("Your Output is:");
            System.out.println("0");
        }
    }
}




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

class NotGate extends star{
    int not1,not2;
 
    public void getSet(){
        Scanner Sc=new Scanner(System.in);
     
        System.out.println("\nEnter your one numbers(either(1/0))\n");
        not1=Sc.nextInt();
        //not2=Sc.nextInt();
    }
    public void or(){
        if(not1==0){
            System.out.println("Your output is:");
            System.out.println("1");
        }
        else if(not1==1){
            System.out.println("Your Output is:");
            System.out.println("0");
        }
        else{
            System.out.println("Invalid Input");
        }
    }
}

//======================================================================
class NorGate extends star{
    int nor1,nor2;
 
    public void getSet(){
        Scanner Sc=new Scanner(System.in);
     
        System.out.println("\nEnter your Two numbers(either(1/0))\n");
        nor1=Sc.nextInt();
        nor2=Sc.nextInt();
    }
    public void nor(){
        if((nor1==0)&&(nor2==0)){
            System.out.println("Your output is:");
            System.out.println("1");
        }
        else {
            System.out.println("Your Output is:");
            System.out.println("0");
        }
     
    }
}

//======================================================================
class NandGate extends star{
    int nand1,nand2;
 
    public void getSet(){
        Scanner Sc=new Scanner(System.in);
     
        System.out.println("\nEnter your Two numbers(either(1/0))\n");
        nand1=Sc.nextInt();
        nand2=Sc.nextInt();
    }
    public void nand(){
        if((nand1==1)&&(nand2==1)){
            System.out.println("Your output is:");
            System.out.println("0");
        }
        else {
            System.out.println("Your Output is:");
            System.out.println("1");
        }
     
    }
}

//======================================================================
class XorGate extends star{
    int xor1,xor2;
 
    public void getSet(){
        Scanner Sc=new Scanner(System.in);
     
        System.out.println("\nEnter your Two numbers(either(1/0))\n");
        xor1=Sc.nextInt();
        xor2=Sc.nextInt();
    }
    public void xor(){
        if(xor1==xor2){
            System.out.println("Your Output is");
            System.out.println("0");
         
        }
        else if(xor1==xor2){
             System.out.println("Your Output is");
         System.out.println("0");
        }
        else{
            System.out.println("1");

        }
    }
}
//======================================================================
class XnorGate extends star{
    int xnor1,xnor2;
 
    public void getSet(){
        Scanner Sc=new Scanner(System.in);
     
        System.out.println("\nEnter your Two numbers(either(1/0))\n");
        xnor1=Sc.nextInt();
        xnor2=Sc.nextInt();
    }
    public void xnor(){
        if((xnor1==1)&&(xnor2==1)){
            System.out.println("Your output is:");
            System.out.println("1");
        }
        else if((xnor1==0)&&(xnor2==0)) {
            System.out.println("Your Output is:");
            System.out.println("1");
        }
        else{
            System.out.println("0");
        }
     
    }
}
//======================================================================
public class Main{
    public static void main(String args[]){
 System.out.println("Note: You have to give input either 1 or 0");     
System.out.println("1. OR Gate");
System.out.println("2.AND Gate");
System.out.println("3. NOT Gate");
System.out.println("4. NOR Gate");
System.out.println("5. NAND Gate");
System.out.println("6. XOR Gate");
System.out.println("7. XNOR Gate");
System.out.println("Which operation would you like to perform:");     
int menu;
 Scanner In=new Scanner(System.in);
 menu=In.nextInt();
 
    switch(menu){
    case 1:
       
         OrGate obj1=new OrGate();
         obj1.star();
         System.out.println("\nOR Gate\n");
         obj1.star();
        obj1.getSet();
        obj1.or();
     
       break;
     case 2:
         AndGate obj2=new AndGate();
         obj2.star();
         System.out.println("\nAND Gate\n");
         obj2.star();
        obj2.getSet();
        obj2.and();
     
        break;
    case 3:
        NotGate obj3=new NotGate();
        obj3.star();
         System.out.println("\n NOT Gate\n");
         obj3.star();
        obj3.getSet();
        obj3.or();
        break;
     
     
    case 4:
        NorGate obj4=new NorGate();
        obj4.star();
         System.out.println("\nNOR Gate\n");
         obj4.star();
        obj4.getSet();
        obj4.nor();
        break;
    case 5:
        NandGate obj5=new NandGate();
        obj5.getSet();
        obj5.nand();
     
        break;
    case 6:
         XorGate obj6=new XorGate();
         obj6.star();
         System.out.println("\n XOR Gate\n");
         obj6.star();
        obj6.getSet();
        obj6.xor();
     
        break;
    case 7:
         XnorGate obj7=new XnorGate();
         obj7.star();
         System.out.println("\n XNOR Gate\n");
         obj7.star();
        obj7.getSet();
        obj7.xnor();
     
        break;
    default:
    System.out.println("Invalid Input");
     
     
    }
    }

 
    }

Output

logic gate program

logic gates

 

Reading Time:

Saturday, February 23, 2019

Program Swapping Two Numbers In Java Without 3rd Variable
February 23, 20190 Comments

Source Code

import java.util.Scanner;
class InterChangeNo{
    
    public void swap(){
        int a, b;
      System.out.println("Enter the value of a and b:");
      Scanner in = new Scanner(System.in);
 
      a = in.nextInt();
      b = in.nextInt();
 
      System.out.println("Before Swapping\nx = "+a+"\ny = "+b);
 
      a = a + b;
      b = a - b;
      a = a - b;
 
      System.out.println("After Swapping\nx = "+a+"\ny = "+b);
    }
    
}
 
class Main
{
   public static void main(String args[])
   {
      InterChangeNo obj=new InterChangeNo();
      obj.swap();
   }
}

Output

Java program

Reading Time:
Program Swapping Two Numbers In Java With 3rd Variable
February 23, 20190 Comments

Source Code

import java.util.Scanner;
 class InterchangeNo{
 
  public void swap(){
   int x, y, temp;
      System.out.println("Enter any two numbers for swaping them:");
      Scanner in = new Scanner(System.in);
   
      x = in.nextInt();
      y = in.nextInt();
   
      System.out.println("Before Swapping it was\nx = "+x+"\ny = "+y);
   
      temp = x;
      x = y;
      y = temp;
   
      System.out.println("After Swapping it is\nx = "+x+"\ny = "+y); 
  }
 }
class Main{
   public static void main(String args[]){
   InterchangeNo obj=new InterchangeNo();
   obj.swap();
   
   }
}

Output

java program

Reading Time:

Tuesday, February 19, 2019

A Program That Take String As Input And Gives it's Reverse Without Using Any Built-In-Function(JAVA)
February 19, 20190 Comments

Source Code

import java.util.Scanner;

class ReverseString{
    public void stringReverse(){
    String original, reverse = "";
      Scanner in = new Scanner(System.in);

      System.out.println("Enter a string to reverse");
      original = in.nextLine();

      int length = original.length();

      for ( int i = length - 1 ; i >= 0 ; i-- )
         reverse = reverse + original.charAt(i);

      System.out.println("Reverse of entered string is: "+reverse);
    }
}
 class Main{
   public static void main(String args[]){
     ReverseString obj= new ReverseString();
     obj.stringReverse();
   }
}

Output

java string program

Reading Time:
A Program That Takes String As an Input And Prints It's Reverse(Java)
February 19, 20190 Comments

Source Code

import java.util.Scanner;
  class ReverseString{
   
      public void stringReverse(){
    System.out.println("Enter string to reverse:");
     
        Scanner read = new Scanner(System.in);
        String str = read.nextLine();
     
        StringBuilder sb = new StringBuilder();
     
        for(int i = str.length() - 1; i >= 0; i--)
        {
            sb.append(str.charAt(i));
        }
     
        System.out.println("Reversed string is:");
        System.out.println(sb.toString());
      }
  }
   
    class Main{
    public static void main(String[] args)
    {
    ReverseString obj=new ReverseString();
    obj.stringReverse();
    }
}


Output

java program string revese

Reading Time:

@way2themes