This website includes Education Information like a programming language, job interview question, general knowledge.mathematics

Education log

PageNavi Results No.

Ads

Wednesday, October 16, 2019

java basic programs list

                               java basic programs list


This article learn today java basic programs list. this article provides a java program that included hello java addition two number programs. included  basic java programs list





1. Hello Word Program java program:

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



2. Area Calculate Program :

import java.util.Scanner;
class AreaOfRectangle {
   public static void main (String[] args)
   {
                       Scanner scanner = new Scanner(System.in);
                       System.out.println("Enter the length of Rectangle:");
                       double length = scanner.nextDouble();
                       System.out.println("Enter the width of Rectangle:");
                       double width = scanner.nextDouble();
                       //Area = length*width;
                       double area = length*width;
                       System.out.println("Area of Rectangle is:"+area);
   }
}



3. Add Two Number java program :

import java.util.Scanner;

class AddNumbers
{
   public static void main(String args[])
   {
      int x, y, z;

      System.out.println("Enter two integers to calculate their sum");
      Scanner in = new Scanner(System.in);
    
      x = in.nextInt();
      y = in.nextInt();
      z = x + y;
    
      System.out.println("Sum of the integers = " + z);
   }
}



4. Java Array java program:



import java.util.Scanner;

class ArmstrongNumber
{
   public static void main(String args[])
   {
      int n, sum = 0, temp, remainder, digits = 0;

      Scanner in = new Scanner(System.in);
      System.out.println("Input a number to check if it is an Armstrong number");     
      n = in.nextInt();

      temp = n;
    
      // Count number of digits
    
      while (temp != 0) {
         digits++;
         temp = temp/10;
      }

      temp = n;

      while (temp != 0) {
         remainder = temp%10;
         sum = sum + power(remainder, digits);
         temp = temp/10;
      }

      if (n == sum)
         System.out.println(n + " is an Armstrong number.");
      else
         System.out.println(n + " isn't an Armstrong number.");       
   }
  
   static int power(int n, int r) {
      int c, p = 1;

      for (c = 1; c <= r; c++)
         p = p*n;

      return p; 
   }
}


5. Java Array java program:


class Testarray{ 
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<="" pre="">



6.Date Time java program: 

import java.util.*;
import java.text.*;

public class DateDemo {

   public static void main(String args[]) {
      Date dNow = new Date( );
      SimpleDateFormat ft =
      new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");

      System.out.println("Current Date: " + ft.format(dNow));
   }
}



7. Demonstration access control in java :

class Test
{
int a; // default access
public int b; // public access
private int c; // private access
// methods to access c
void setc(int i)  // set c's value
{
c = i;
}
int getc()  // get c's value
{
return c;
}
}
class assgn6
{
public static void main(String args[])
{
Test ob = new Test();
// These are OK, a and b may be accessed directly
ob.a = 10;
ob.b = 20;
// This is not OK and will cause an error
// ob.c = 100; // Error!
// You must access c through its methods
ob.setc(100); // OK
System.out.println("a, b, and c: " + ob.a + " " +
ob.b + " " + ob.getc());
}




8. Demonstration of classes and objects and method in java :

class Dog
{
// Instance Variables
String name;
String breed;
int age;
String color;
// Constructor Declaration of Class
public Dog(String name, String breed,
int age, String color)
{
this.name = name;
this.breed = breed;
this.age = age;
this.color = color;
}
// method 1
public String getName()
{
return name;
}
// method 2
public String getBreed()
{
return breed;
}
// method 3
public int getAge()
{
return age;
}
// method 4
public String getColor()
{
return color;
}
}
public class assgn3
{
public static void main(String[] args)
{
Dog tuffy = new Dog("tommy","labrador", 5, "white");

System.out.println("Hi my name is "+ tuffy.getName()+
".\nMy breed,age and color are " +
tuffy.getBreed()+"," + tuffy.getAge()+
","+ tuffy.getColor());
}
}




9. Demonstration of inheritance using java (member access) java program:

class Vehicle
{
    String vehicleType;
}
public class assgn10 extends Vehicle {
   
    String modelType;
    public void showDetail()
    {
        vehicleType = "Car";        //accessing Vehicle class member
        modelType = "sports";
        System.out.println(modelType+" "+vehicleType);
    }
    public static void main(String[] args)
    {
        assgn10 car =new assgn10();
        car.showDetail();
    }
}



10.Even Or Odd Number java program:

import java.util.Scanner;

class OddOrEven
{
   public static void main(String args[])
   {
      int x;
      System.out.println("Enter an integer to check if it is odd or even");
      Scanner in = new Scanner(System.in);
      x = in.nextInt();
    
      if (x % 2 == 0)
         System.out.println("The number is even.");
      else
         System.out.println("The number is odd.");
   }
}



11. Factorial Number java program:

import java.util.Scanner;
class Factorial
{
                    public static void main(String[] args)
                    {
    int no, fect=1;
                    Scanner s=new Scanner(System.in);
    System.out.println("Enter any number :");
                    no=s.nextInt();
    for(int i=1; i<=no; i++)
                    {
                    fect=fect*i;
                    }
                    System.out.println("Factorial is :" +fect);
                    }
}


12. Prime Number java program:

import java.util.*;

class PrimeNumbers
{
   public static void main(String args[])
   {
      int n, status = 1, num = 3, count, j;

      Scanner in = new Scanner(System.in);
      System.out.println("Enter the number of prime numbers you want");
      n = in.nextInt();
    
      if (n >= 1)
      {
         System.out.println("First "+n+" prime numbers are:");
         System.out.println(2);
      }
    
      for (count = 2; count <=n;)
      {
         for (j = 2; j <= Math.sqrt(num); j++)
         {
            if (num%j == 0)
            {
               status = 0;
               break;
            }
         }
         if (status != 0)
         {
            System.out.println(num);
            count++;
         }
         status = 1;
         num++;
      }       
   }
}


13. Palindrome java program:


import java.util.*;

class Palindrome
{
   public static void main(String args[])
   {
      String original, reverse = ""; // Objects of String class
      Scanner in = new Scanner(System.in);
    
      System.out.println("Enter a string to check if it is a palindrome");
      original = in.nextLine();
    
      int length = original.length();
    
      for (int i = length - 1; i >= 0; i--)
         reverse = reverse + original.charAt(i);
        
      if (original.equals(reverse))
         System.out.println("The string is a palindrome.");
      else
         System.out.println("The string isn't a palindrome.");
        
   }
}     


14. Reverse String java program:


import java.util.*;

class ReverseString
{
   public static void main(String args[])
   {
      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 the string: " + reverse);
   }
}



15. Try Catch java program:


import java.util.Scanner;
class Excp
{
 public static void main(String args[])
 {
  int a,b,c;
  try
  {
   a=0;
   b=10;
   c=b/a;
   System.out.println("This line will not be executed");
  }
  catch(ArithmeticException e)
  {
   System.out.println("Divided by zero");
  }
  System.out.println("After exception is handled");
 }
}



16. If else java program:

import java.util.Scanner;

class IfElse {
  public static void main(String[] args) {
    int marksObtained, passingMarks;
  
    passingMarks = 40;
  
    Scanner input = new Scanner(System.in);
  
    System.out.println("Input marks scored by you");
  
    marksObtained = input.nextInt();
  
    if (marksObtained >= passingMarks) {
      System.out.println("You passed the exam.");
    }
    else {
      System.out.println("Unfortunately, you failed to pass the exam.");
    }
  }
}



17. If else java program:

import java.util.Scanner;

class IfElse {
  public static void main(String[] args) {
    int marksObtained, passingMarks;
  
    passingMarks = 40;
  
    Scanner input = new Scanner(System.in);
  
    System.out.println("Input marks scored by you");
  
    marksObtained = input.nextInt();
  
    if (marksObtained >= passingMarks) {
      System.out.println("You passed the exam.");
    }
    else {
      System.out.println("Unfortunately, you failed to pass the exam.");
    }
  }
}



18. Do Loop java program:


package com.journaldev.javadowhileloop;

public class JavaDoWhileLoop {

                    public static void main(String[] args) {

                                         int i = 5;
                                         do {
                                                             System.out.println(i);
                                                             i++;
                                         } while (i <= 10);
                    }
}


19. Do while Loop java program:


package com.journaldev.javadowhileloop;

public class DoWhileLoopExample {
    public static void main(String args[]){
         int i=10;
         do{
              System.out.println(i);
              i--;
         }while(i>1);
    }
}




20. While Loop java program:


package com.journaldev.javadowhileloop;

public class WhileLoopExample {
    public static void main(String args[]){
         int i=10;
         while(i>1){
              System.out.println(i);
              i--;
         }
    }
}


No comments:

Post a Comment