Tuesday, 29 August 2017

Core Java Program To Convert Rupees Into Dollar. 60 Rupees = 1 Dollar

Definition: Write a program to convert rupees to dollar. 60 rupees=1 dollar



Program

public class Convert {
public static void main(String args[])
{
if(args != null && args.length == 2)
{
int x = Integer.parseInt(args[0]);
int y = Integer.parseInt(args[1]);
System.out.println("Doller=" + y*x);
 }
else
System.out.println("Wrong number of command line arguments. This program require 2 arguments");
}

}

Output

convert rupees to dollar


Java program to enter two numbers and perform operations using switch case

Java program to enter two numbers and perform mathematical operations using switch case

Program

import java.util.Scanner;

class CalC{
          public static void main(String []args){
                   Scanner s = new Scanner(System.in);
                   int a,b;
                   System.out.println("Enter Tow Number...");
                   a = s.nextInt();
                   b = s.nextInt();
                  
                   System.out.println("1.Addition");
                   System.out.println("2.Substraction");
                   System.out.println("3.Multiplication");
                   System.out.println("4.Division");
                   System.out.println("5.Modulo");
                   System.out.print("Chose Any one");
                   int c = s.nextInt();
                  
                   switch(c){
                             case 1:
                             a = a + b;
                             System.out.println("Addition is : "+a);
                             break;
                            
                             case 2:
                             a = a - b;
                             System.out.println("Substraction is : "+a);
                             break;
                            
                             case 3:
                             a = a * b;
                             System.out.println("Multiplication is : "+a);
                             break;
                            
                             case 4:
                             a = a / b;
                             System.out.println("Division is : "+a);
                             break;
                            
                             case 5:
                             a = a % b;
                             System.out.println("Modulo is : "+a);
                             break;
                            
                             default :
                             System.out.println("Enter Right Choise");
                             break;
                   }
          }

}

Output

mathematical operations java core

Java programming tutorial to calculate percentage marks of the student using else-if

Java program that calculate percentage marks of the student using else-if

Program

class Student {
public static void main(String args[])
{
if(args != null && args.length == 6)
{       
int s1 = Integer.parseInt(args[0]);
int s2 = Integer.parseInt(args[1]);
int s3 = Integer.parseInt(args[2]);
int s4 = Integer.parseInt(args[3]);
int s5 = Integer.parseInt(args[4]);
int s6 = Integer.parseInt(args[5]);
int ans=s1+s2+s3+s4+s5+s6/args.length;

System.out.println("Student 6 sub avg = " + ans);
System.out.print("Gread is : ");
if(ans>=90)
System.out.println("A");
else if(ans>=80)
System.out.println("B");
else if(ans>=70)
System.out.println("C");
else if(ans>=60)
System.out.println("D");
else
System.out.println("Fail");

}
else{
System.out.println("Wrong number of command line arguments. This program require 6 arguments");
}
}

}

Output

calculate percentage Java

How to find second half of given string in core java program

Write a program to find length of string and print second half of the string

Program

import java.util.Scanner;
class String_second_half
{       
          public static void main(String args[])
          {
                   Scanner in = new Scanner(System.in);
                   System.out.println("ENTER TO ANY STRING");
                   String Str;
                   Str = in.nextLine();
                   int n =Str.length();
                   int ans = (n/2)+1;
                   System.out.println("String half length is:" + ans);
}

 }

Output

length of string core java

Java Core | Java program to accept a line and check how many consonants and vowels are there in line

Java program to accept a line and check how many consonants and vowels are there in line

Program

import java.util.Scanner;
class consonants_vowels
{
          public static void main(String args[])
          {       
                   Scanner in = new Scanner(System.in);
                   String Str;
                   int i,vowels=0,consonants=0,Digit=0,whitespace=0;
                   char ch;
                   System.out.println("Enter a any String");
                   Str = in.nextLine();
                  
                   for(i=0;i<Str.length();i++)
                   {
                             ch = Str.charAt(i);
                            
                             if((ch == 'A') || (ch == 'E') || (ch == 'I') || (ch == 'O') || (ch == 'U') ||
                                (ch == 'a') || (ch == 'e') || (ch == 'i') || (ch == 'o') || (ch == 'u'))
                                {
                                         vowels++;
                                }
                             else if(Character.isDigit(ch))
                                {
                                         Digit++;
                                }
                             else if(Character.isWhitespace(ch))
                                {
                                         whitespace++;
                                }
                            
                             else
                                      {
                                                consonants++;
                                      }
                   }
                   System.out.println("in a line "+ vowels +" vowels");
                   System.out.println("in a line "+ consonants +" consonants");
                   System.out.println("in a line "+ Digit +" Digit");
                   System.out.println("in a line "+ whitespace +" whitespace");

          }

Output

how many consonants and vowels are there in line java core

Java Program to count the number of words that start with capital letters

Java Program to count the number of words that start with capital letters

Program

import java.util.*;
class start_capital_letter{
          public static void main(String m[]){
                   Scanner in=new Scanner(System.in);
                   String s=new String();
                   System.out.println("Enter a line:");
                   s=in.nextLine();
                   char c;
                   int ct=0;
                   for(int i=0;i<s.length();i++){
                             c=s.charAt(i);
                             if(c>=65 && c<=90){
                                      ct++;
                             }
                   }
                   System.out.println("total  number of words start with capital letters are :"+ct);

} }

Output

count the number of words

How find given number or string is palindrome or not in java program

Java program to find that given number or string is palindrome or not

Program

import java.util.Scanner;
class palindrome
{
          public static void main(String args[])
          {
                   int r,sum=0,temp,n;
                   Scanner in = new Scanner(System.in);
                   System.out.println("Enter to any number");
                   n=in.nextInt();
                   temp = n;
                             while(n>0){
                                      r=n%10;
                                      sum=(sum*10)+r;
                                      n=n/10;
                             }
                   if(temp==sum)
                             System.out.println("number is palindrome");
                   else
                             System.out.println("number is not palindrome");

                   }        }

Output

palindrome  Core Java


Java programming tutorial to demonstrate Constructor Overloading

Create a class Box and display the area of box using a method area(). Initialize all the variables using Constructor and demostrate Constructor Overloading.

Program

class Box{
          int w,l;
          Box(){
                   w=10;
                   l=10;
          }
          Box(int a,int b){
                   w=a;
                   l=b;
          }
          void area(){
                   System.out.println("Area of Box is  : "+(w*l));
          }
}
class Main{
          public static void main(String []args){
                   Box b1 = new Box();
                   Box b2 = new Box(50,60);
                  
                   b1.area();
                   b2.area();
          }

}

Output

Constructor Overloading java core

java programming tutorial to use of Method Overloading in java

Create a class Demo and show the use of Method Overloading

Program

class Addition{
          void add(int i1,int i2){
                   System.out.println("Addition of Integers is"+(i1+i2));
          }
          void add(double d1,double d2){
                   System.out.println("Addition of Doubles is"+(d1+d2));
          }
}

class Demo{
          public static void main(String []args){
                   Addition ad = new Addition();

                   ad.add(20,40);        ad.add(20.40,80.90);       }      }

Output

Method Overloading Core Java

how to Handle all exceptions that can be generated in this java program

Java program that divides two numbers. Handle all exceptions that can be generated in this program

Program

import java.util.Scanner;
class Division {
  public static void main(String[] args) {
  int a, b, Ans;
  Scanner input = new Scanner(System.in);
  System.out.println("Input First integers");
  a = input.nextInt();
  System.out.println("Input Second integers");
  b = input.nextInt();
            try {
                   Ans  = a / b;
                   System.out.println("Result = " + Ans);
            }
            catch (ArithmeticException e) {
                   System.out.println("Exception caught: Division by zero.");
            }

  }      }

Output

 Handle all exceptions java Core

Java Programming tutorial to demonstrates the use of throw keyword in exception handling

WAP that creates a custom exception InvalidAgeException which will be thrown when user enters age below 18

Program

import java.util.Scanner;
public class Custom_exception{
          static void validate(int age){ 
           if(age<18) 
            throw new ArithmeticException("not valid"); 
           else 
            System.out.println("welcome to vote"); 
   } 
   public static void main(String args[]){
            int i;
                   Scanner input = new Scanner(System.in);
                   System.out.println("Enter Your age");
                   i = input.nextInt();

            validate(i);           }  }

Output

Custom exception  handling Core Java

How to create class abstract figure in java program

WAP that create class abstract figure.

On that class area method is there.

Inherits class rectangle, circle.

All are extend with figure & write the method in all class that prints the area.

Program

import java.util.Scanner;
abstract class figure{
          abstract void area();
}

class circle extends figure{
 Scanner in = new Scanner(System.in);
 int r;

circle(){
          System.out.println("Enter to circle r");
          r = in.nextInt();
}

void area(){
          double ans;
          ans = (3.14 * r * r);
          System.out.println("circle area is" + ans);
          }
}
class rect extends figure{
          Scanner in = new Scanner(System.in);
          float l,w;
          rect()
          {
                             System.out.println("Enter to rect l");
                             l = in.nextInt();
                             System.out.println("Enter to rect w");
                             w = in.nextInt();
          }
          void area(){
          double ans;
          ans = (l*w);
          System.out.println("rect area is" + ans);
          }

}
class call{
          public static void main(String args[])
          {
                   figure c = new circle();
                   c.area();
                   figure r = new rect();
                   r.area();
                  
          }

}

Output

abstract class

how to create interface in java program

W.A.P that create interface on that you declare one method interest(p,r,n) then create two class simple and compound that implements interest and find simple and compound interest.

Program

interface Bank
{
          void interest(int p,int r,int n);
}
class SimpleInt implements Bank
{
          public void interest(int p,int r,int n)
          {
                   System.out.println("simple interest is:\t"+((p*r*n)/100));
          }
}
class CompoundInt implements Bank
{
          public void interest(int p,int r,int n)
          {
                   System.out.println("Compound interest is:\t"+p*(Math.pow((1+(r/100)),n)));
          }
}
class interest
{
          public static void main(String args[])
          {
                   SimpleInt s=new SimpleInt();
                   s.interest(10,100,2);
                   CompoundInt c=new CompoundInt();
                   c.interest(10,100,2);
          }

}

Output

 create interface core Java

core Java Program to how a method can invoke a super class method

W.A.P that illustrate how method can invoke a super class method.

There is one supercalss A.

class B extends class A and class C extends class B.

Each of these classes define a getdescription() method that returns a string.

That string includes description of the class plus description of each super class.

Inherits each of these class and invoke the gerdescription() method.

Program

class a{
          String disdata()
          {
                   return "class A:";
          }
}
class b extends a{
          String disdata()
          {
                   return ("class B:"+super.disdata());
          }
}
class c extends b{
          String disdata()
          {
                   return ("class c:"+super.disdata());
          }
}
class call{
          public static void main(String args[])
          {
                   c objC = new c();
                   System.out.println(objC.disdata());
          }

}

Output

super class method java core