Write a program to HTTP Grab with FileChannel.

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

public class MainClass {

  public static void main(String[] args) throws IOException {

    URL u = new URL("http://www.java2s.com");
    String host = u.getHost();
    int port = 80;
    String file = "/";

    SocketAddress remote = new InetSocketAddress(host, port);
    SocketChannel channel = SocketChannel.open(remote);
    FileOutputStream out = new FileOutputStream("yourfile.htm");
    FileChannel localFile = out.getChannel();

    String request = "GET " + file + " HTTP/1.1\r\n" + "User-Agent: HTTPGrab\r\n"
        + "Accept: text/*\r\n" + "Connection: close\r\n" + "Host: " + host + "\r\n" + "\r\n";

    ByteBuffer header = ByteBuffer.wrap(request.getBytes("US-ASCII"));
    channel.write(header);

    ByteBuffer buffer = ByteBuffer.allocate(8192);
    while (channel.read(buffer) != -1) {
      buffer.flip();
      localFile.write(buffer);
      buffer.clear();
    }

    localFile.close();
    channel.close();
  }
}

Write a program to Save Web page to a file.

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.swing.text.html.parser.DTD;

public class MainClass
    {
     public static void main(String[] args)
     { 
     try
      {
      URL u = new URL("http://www.java2s.com");
      OutputStream out = new FileOutputStream("test.htm");
      InputStream in = u.openStream();
      DTD html = DTD.getDTD("html");
      System.out.println(html.getName());
      in.close();
      out.flush();
      out.close();
      } 
      catch (Exception e) {
      System.err.println("Usage: java PageSaver url local_file");
    }

  }

}

Write a program to check your own ip address...

import java.net.*;

class CheckIp
{
       public static void main(String args[])
      {
       try
       {
         InetAddress address=InetAddress.getLocalHost();
         System.out.println(address);
        }
     catch(UnknownHostException e)
   {
  System.out.println("could not find yahoo .com");
}
}
}

Write a program to comarision of two string.

class stringmethod
  {
    public static void main(String[] args)
      {
        String string1 = "Hi";
        String string2 = new String("Hello");
        if (string1 == string2)
           {
            System.out.println("The strings are equal.");
            }
        else
               {
                System.out.println("The strings are unequal.");
                }
        }
}               


Output of the program:-


The strings are unequal

Write a program to print a Stact Trace Message.

public class PrintStack
{
           public static void main (String args[])
              {
               String str = "Exception" ;
               int len=0;
           try
              {
             StringBuffer sbuf = new StringBuffer(str);
            len = str.length() ;
           for(int ct=len;ct>=0;ct--)
            {
          System.out.print(sbuf.charAt(ct));
           }
       }
       catch(Exception e)
      {
      e.printStackTrace();
       }
     }
   }



OUTPUT OF THE PROGRAM:-


java.lang.StringIndexOutOfBoundsException: String index out of range: 9
at java.lang.StringBuffer.charAt(Unknown Source)
at PrintStack.main(PrintStack.java:13)

Write a program to take input from user and give the output.

/* Reading from the console. */
import java.util.Scanner;

import static java.lang.System.out;

public class ConsoleInput {

  public static void main(String[] args) {

    // Create a Scanner which is chained to System.in, i.e. to the console.
    Scanner lexer = new Scanner(System.in);

    // Read a list of integers.
    int[] intArray = new int[3];
    out.println("Input a list of integers (max. " + intArray.length + "):");
    for (int i = 0; i < intArray.length;i++)
      intArray[i] = lexer.nextInt();
    for (int i : intArray)
       out.println(i);


    // Read names
    String firstName;
    String lastName;
    String name;
    String repeat;
    do {
      lexer.nextLine(); // Empty any input still in the current line
      System.out.print("Enter first name: ");
      firstName = lexer.next();
      lexer.nextLine();
      System.out.print("Enter last name: ");
      lastName = lexer.next();
      lexer.nextLine();
      name = lastName + " " + firstName;
      System.out.println("The name is " + name);
      System.out.print("Continue? (y/n): ");
      repeat = lexer.next();
    } while (repeat.equals("y"));
    lexer.close();
  }
}

Write a program to calculate the date and the date formet and time formate.

/*
   Formatted Time and Date
*/
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

import static java.lang.System.out;
import static java.util.Calendar.*;
import static java.util.Locale.*;

class FormattedTimeDate {

  public static void main(String[] args) {

Calendar myCalender = Calendar.getInstance();

{
// Formatting the hour
out.printf("Hour(00-24):%tH\n", myCalender);
out.printf("Hour(01-12):%tI\n", myCalender);
out.printf("Hour(0-23):%tk\n", myCalender);
out.printf("Hour(1-12):%tl\n", myCalender);
}

{
// Formatting the minutes
out.printf("Minutes(00-59):%tM\n", myCalender);
}

{
// Formatting the seconds
out.printf("Seconds(00-60):%tS\n", myCalender);
// out.printf("Millieseconds(000-999):%tL\n", myCalender);
out.printf("Nanoseconds(000000000-999999999):%tN\n", myCalender);
}

{
// PM/pm and AM/am
out.printf("%tp\n", myCalender);
out.printf("%Tp\n", myCalender);
}

{
// Formatting the year
out.printf("%tC%n", myCalender);
out.printf("%tY%n", myCalender);
out.printf("%ty%n", myCalender);
}

{
// Formatting the month
out.printf("%tB%n", myCalender);
out.printf("%tb%n", myCalender);
out.printf("%th%n", myCalender);
out.printf("%tm%n", myCalender);
out.printf(US,"%tB%n", myCalender);

}

{
// Formatting the day
out.printf("%tA%n", myCalender);
out.printf("%ta%n", myCalender);
out.printf("%tj%n", myCalender);
out.printf("%td%n", myCalender);
out.printf("%te%n", myCalender);
out.printf(US,"%tA%n", myCalender);
}

{
// Composite Date/Time Format Conversions
out.printf("%tR%n", myCalender);
out.printf("%tT%n", myCalender);
out.printf("%tr%n", myCalender);
out.printf("%tD%n", myCalender);
out.printf("%tF%n", myCalender);
out.printf("%tc%n", myCalender);
}

{
// Misc. usage
// Note that the specifiers refer to the same argument in the format string.
out.printf("%1$tm %1$te %1$tY%n", myCalender);
out.printf(US, "The world will end on %1$tA, %1$te. %1$tB %1$tY" +
               " at %1$tH:%1$tM:%1$tS.%n", myCalender);
out.printf("The world will end on %1$tA, %1$te. %1$tB %1$tY at %1$tH:%1$tM:%1$tS.%n",
           myCalender);
//out.printf("%1$^-15tA, %1$6te. %1$^10tB %1$tY, %1$tH:%1$tM:%1$tS.%n", <==== !!!!!!!!!
//           myCalender);

Calendar birthdate = new GregorianCalendar(1949, MARCH, 1);
out.printf("Author's Birthday: %1$tD%n", birthdate);
}

  }
}

Write a program of logic gates.

import logicgate.And;
import logicgate.Or;
import logicgate.Not;
import java.util.Scanner;

class Gate
{
public static void main(String[] args)
{
  int a,b;
System.out.println("Enter Values");
 Scanner sc=new Scanner(System.in);
  And and=new And();
  Or or=new Or();
  Not not=new Not();

a=sc.nextInt();
b=sc.nextInt();

and.doOperation(a,b);
or.doOperation(a,b);
not.doOperation(a);
}
}

Write a program to perform 3X3 matrix addition.

class MatrixAdd
{
 public static void main(String arg[])
 {
  int a[][]= new int[3][3];
  int b[][] = new int[3][3];
  int c[][]= new int[3][3];
  int i,j,k=0;
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    a[i][j]=k;
     k++;
    b[i][j]=k;
   }
  }
  System.out.println("1st mat. is");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +a[i][j]);
    }
     System.out.println("\n");
   }
   System.out.println("2nd mat. is");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +b[i][j]);
    }
     System.out.println("\n");
   }
  System.out.println("addtn is");
 for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    c[i][j]=a[i][j]+b[i][j];
   }
  }
   for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +c[i][j]);
    }
     System.out.println("\n");
   }
 }
}

Write a program to print the diagonal elements of 3X3 matrix .

class Dmatrix
{
 public static void main(String arg[])
 {
  int a[][]= new int[3][3];
  int i,j,k=0;
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
   {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
    a[i][j]=k;
     k++;
   }
  }
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     System.out.print("  " +a[i][j]);
         
     }
     System.out.println("\n");
    }
System.out.println("digo. elment are ");
  for(i=0;i<3;i++)
  {
   for(j=0;j<3;j++)
    {
     if(i!=j)
     System.out.print(" ");
     else
     System.out.println(" "+a[i][j]);
     }
    }
 }
}
 

Write a program to take input from user in a integer and print a table of that value , like Enter Value : 3 3 X 1 = 3 3 X 2 = 6 . 3 X 10 = 30

class Table
{
public static void main(String args[])
{
int a=Integer.parseInt(System.console().readLine("enter the value of a"));
int i,count;
for(i=1;i<=10;i++)
{
count=a*i;
System.out.println(" "+count);
}
}
}

Write a program that ask user to enter values in two integer a and b, now write a code to swap the values of a and b without taking any temporary variable.

import java.util.Scanner;
public class Swap
{
public static void main(String[]hjk)
{
int a,b;
Scanner s1=new Scanner(System.in);
System.out.println("enter first num as a");
a=s1.nextInt();
Scanner s2=new Scanner(System.in);
System.out.println("enter second num as b");
b=s2.nextInt();
a=a+b;
b=a-b;
a=a-b;
System.out.println("the swapped num are");
System.out.println("a="+a);
System.out.println("b="+b);

}
}

WAP to print the pelendrome 5 digits.

class Palendrome
  {
    public static void main(String args[])
      {
        int n=Integer.parseInt(System.console().readLine("enter the 5 no. digitno. n"));
        int a=n;
        int b=a%10;
        b=b+10000;
        a=a/10;

        int c=a%10;
        c=c+1000;
        a=a/10;

        int d=a%10;
        d=d+100;
        a=a/10;

        int e=a%10;
        e=e+10;
        a=a/10;

        int sum=a+b+c+d+e;
        if(sum==n)
        {
          System.out.println("Entered no. is pelendrome");
        }
        else
        {
          System.out.println("Entered no. is pelendrome");
        }
     }
  }

W.A.P. to convert Celsius to Fahrenheit

class ConvertCtoF
  {
     public static void main(String[] args)
       {
         int c;
        
         System.out.println("Fahrenheit is:->");
         for(c=0;c<=100;c++)
           {
             float f=c*(9/5f)+32;
          
             System.out.println("Celsius="+c+"Fahrenheit="+f);
           }
        }
   }
            

Write a program to display grade message according to the marks obtained by the student

import java.util.*;
class StudentGrade
  {
   public static void main(String a[])
     {
       int studentmarks;
       Scanner in=new Scanner(System.in);
       System.out.println("enter the marks of student");
       studentmarks=in.nextInt();
       if((studentmarks>=300)&&(studentmarks<=500))
         {
           System.out.println("Grade A");
         }
       if((studentmarks>=225)&&(studentmarks<=299))
         {
           System.out.println("Grade B");
         }
       if((studentmarks>=165)&&(studentmarks<=224))
         {
           System.out.println("Grade C");
         }
       if(studentmarks<165)
         {
           System.out.println("Fail");
         }
     }
   }

Write a program to check that the entered no is prime or not.

import java.util.*;
class PrimeNo
 {
  public static void main(String[] args)
  {
   int num;
   int i;
   Scanner in=new Scanner(System.in);
   System.out.println("enter the value of num");
   num=in.nextInt();
   for (i=2; i < num ;i++ )
    {
     int n = num%i;
     if (n==0)
       {
         System.out.println("not Prime!");
         break;
       }
    }
     if(i == num)
      {
       System.out.println("Prime number!");
      }
  }
}

Write a program to print the Fibonacci series.

class Fib {
    public static void main(String args[ ])
         {
        int n=Integer.parseInt(System.console().readLine("Enter the value of n="));
        int i,a=0,b=1,c=0;
        System.out.println("fibnoci series is:");
       
        System.out.print(a);
        System.out.print(" "+b);
        for(i=0;i<n-2;i++)
                {
            c=a+b;
            a=b;
            b=c;
            System.out.print(" "+c);
        }
        System.out.println();
        System.out.print("th value of the series is: "+c);
    }
}

Write a program to find out the factorial of the given no.

class Factorial
 {
   public static void main(String args[])
     {
       int i,num;
       num=Integer.parseInt(System.console().readLine("Enter the number="));
       int fact=1;
       for(i=1;i<=num;i++)
         {
           fact=fact*i;
          
         }
      
       System.out.println("factorial of Number="+fact);
     }
 }

Prompt the user to provide two no. Using conditional operator to find the smallest no and print that no.

import java.util.*;
class CheckCondn
 {
  public static void main(String[] args)
  {
   int num1,num2;
   int i;
   Scanner in=new Scanner(System.in);
   System.out.println("enter the value of num1");
   num1=in.nextInt();
   System.out.println("enter the value of num");
   num2=in.nextInt();
   if(num1<num2)
     {
        System.out.println("num1 is smallest");
     }
   else
     {
        System.out.println("num2 is smallest");
     }
   }
}

Write a program to display 5 Armstrong no.

class Armstrong
{
     public static void main (String [] args)
       {
          //Program to find armstrong number from 1 to 1000


          for (int k=1;k<=1000;k++)
            {

               int n=k;

               int d=0;

               int s=0;

               while(n>0)

                 {

                    d = n % 10;

                    s = s+(d * d * d);

                    n = n/10;

                 }

               if (s==k)

                  {

                    System.out.println (k+" is Armstrong number");
                  }
            }

Create a java class and implement a proper encapsulation.

abstract class EncaTest
{
 public void disp()
   {
     System.out.println("i m pop3");
   }
 }
class Tester extends EncaTest
 {
  public void display()
   {
     System.out.println("hello m display");
   }
  public static void main(String args[])
   {
     Tester ab=new Tester();
     ab.disp();
     ab.display();

   }
 }

Calculate the area of circle and cylinder by creating methods name area of circle and area of cylinder in a class named Area using a constant variable PI=3.14.

class Area
 {
     double pi=3.14;
     int r=Integer.parseInt(System.console().readLine("enter the value r"));
     int h=Integer.parseInt(System.console().readLine("enter the value h"));
     void areaOfCircle()
       {
         double area1=pi*r*r;
         System.out.println("area of circle="+area1);
       }
     void areaOfCylinder()
       {
         double area2=2*pi*r*(r+h);
         System.out.println("area of cylinder="+area2);
       }
   public static void main(String args[])
    {
         Area a=new Area();
         a.areaOfCircle();
         a.areaOfCylinder();
    }
}
        

Define a class named “Test” with an instance variable num. Define a Constructor and a method named getReverse(). Create an object of the class pass an integer to the constructor to initialize num. Call getReverse() to get the reverse and print the reverse no.

class Test
{
  int num;
  Test(int n)
  {
   num=n;
   System.out.println("value of num="+num);
  }
  void getReverse()
  {
   int a=num%10;
   a=a*100;
   num=num/10;
   int b=num%10;
   b=b*10;
   num=num/10;
   int reNo=a+b+num;
   System.out.println("ReverseNO.="+reNo);
  }
 public static void main(String args[])
  {
   Test aa=new Test(234);
   aa.getReverse();
  }
}
   

Define a class named “Employee” with the following private fields: EmpID , EmpName , DeptID , Bloodgroup , Salary. Declare 2 public methods to get and set the private fields. Create an array of employee objects. Prompt the user for providing values. Show the details of each employee.

class Employee
{
 int empID,DeptID,Salary;
 String empName,BloodGroup;

   void setEmpDetails()
    {
     empID=Integer.parseInt(System.console().readLine("Enter the empID="));
     empName=System.console().readLine("Enter the empname=");
     DeptID=Integer.parseInt(System.console().readLine("Enter the DeptID="));
     BloodGroup=System.console().readLine("Enter the BloodGroup=");
     Salary=Integer.parseInt(System.console().readLine("Enter the Salary="));
    }

  void getEmpDetails()
   {
    System.out.println(" ");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println("************Employee Details**************");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println(" ");
    System.out.println("Employee Id            :"+empID);
    System.out.println("Employee Name          :"+empName);
    System.out.println("Department Id          :"+DeptID);
    System.out.println("Employee Blood group   :"+BloodGroup);
    System.out.println("Employee Salary        :"+Salary);
   }
}

class EmployeeTestDrive
  {
      public static void main(String[] args)
       {
         Employee e=new Employee();
         e.setEmpDetails();
         e.getEmpDetails();
       }
  }

Write a program to demonstrate the difference between static & non-static variables.

public class StaticTest
   {

             private int count=0;
             public static void main(String args[])
                {
        StaticTest a=new StaticTest();
 
                  
       a.count++; //compiler error: non-static variable count cannot be referenced from a static context
  System.out.print("ab="+count);              
 }
 

   }

W.A.P. to print the marksheet of the student in the following manner Marksheet ------------------------------------ Name : Rahul Jain Roll No : 0205ca071101 College : IIIT Allahabad College ------------------------------------ Subject : Marks Sub1 : Sub2 : Sub3 : Sub4 : Sub5 : ------------------------------------- Total : Percent :

class Ms
 {
   int RollNo,Sub1,Sub2,Sub3,Sub4,Sub5,Total;
   String Name,College;
   double Percent;
   void markSheet()
    {
    Name=System.console().readLine("Enter the name of Student:");
    RollNo=Integer.parseInt(System.console().readLine("Enter the Rollno:"));
    College=System.console().readLine("Enter the name of Student's College:");
    Sub1=Integer.parseInt(System.console().readLine("Enter the no Sub1:"));
    Sub2=Integer.parseInt(System.console().readLine("Enter the no Sub2:"));
    Sub3=Integer.parseInt(System.console().readLine("Enter the no Sub3:"));
    Sub4=Integer.parseInt(System.console().readLine("Enter the no Sub4:"));
    Sub5=Integer.parseInt(System.console().readLine("Enter the no Sub5:"));
    }
   void printMarkSheet()
    {
    System.out.println("             Marksheet  ");
    System.out.println("-----------------------------------");
    System.out.println("Name    :"+Name);
    System.out.println("Roll No :"+RollNo);
    System.out.println("College :"+College);
    System.out.println("  ");
    System.out.println("  ");
    System.out.println("-----------------------------------");
    System.out.println("Sub1    :"+Sub1);
    System.out.println("Sub2    :"+Sub2);
    System.out.println("Sub3    :"+Sub3);
    System.out.println("Sub4    :"+Sub4);
    System.out.println("Sub5    :"+Sub5);
    System.out.println("-----------------------------------");
    Total=Sub1+Sub2+Sub3+Sub4+Sub5;
    System.out.println("Total   :"+Total);
    System.out.println("  ");
    System.out.println("  ");
    Percent=Total/5;
    System.out.println("Percent :"+Percent);
    }
  public static void main(String[] args)
    {
     Ms ab=new Ms();
     ab.markSheet();
     ab.printMarkSheet();
    }
}

Write a program to print Fibonacci series up to n term.

//Write a program to print the Fibonacci series.
class Fib {
    public static void main(String args[ ])
         {
        int n=Integer.parseInt(System.console().readLine("enter the no. n="));
        int i,a=1,b=1,c=0;
        System.out.println("fibnoci series is:");
       
        System.out.print(a);
        System.out.print(" "+b);
        for(i=0;i<=n;i++)
                {
            c=a+b;
            a=b;
            b=c;
            System.out.print(" "+c);
        }
        System.out.println();
        System.out.print("th value of the series is: "+c);
    }
}

Print the star pattern in the oposite pyramid.

class Pyramid
  {
    public static void main(String args[])
     {
       int i,j,k;
       for(i=1;i<=5;i++)
         {
         for(j=1;j<=i;j++)
           {
             System.out.print("  ");
           }
          for(k=5;k>=i;k--)
           {
             System.out.print(" *");
           }
             for(k=4;k>=i;k--)
           {
             System.out.print(" *");
           }
          
          System.out.println("  ");
          System.out.println("  ");          
       }
     }
  }

Print the alternate traingle of 0 and 1.

class AlternetPrint
  {
    public static void main(String args[])
     {
       int i,j,a=1;
        for(i=0;i<=9;i++)
         {
           i++;
           for(j=a;j<=i;j++)
             {
               int k=j%2;
               System.out.print(k);
              
             }a++;
            System.out.println(" ");
         }
      }
  }

Print a dimond safe from 5 to 5.

class Diamond
   {
     public static void main(String args[])
      {
        int i,j,a=5,b=1,c=2,d=3;

        for(i=5;i>=1;i--)
         {

               for(j=5;j>=b;j--)
                  {

                    System.out.print(" ");

                  }b++;

               for(j=5;j>=i+1;j--)

                  {

                    System.out.print(j);
                  }
               for(j=a;j<=5;j++)
                  {
                    System.out.print(j);
                  }a--;
               System.out.println(" ");
          }


          for(i=5;i>=1;i--)
            {
               for(j=6;j>=i;j--)
                 {
                    System.out.print(" ");
                 }
               for(j=5;j>=d;j--)
                 {
                    System.out.print(j);
                 }d++;
               for(j=c;j<=5;j++)
                 {
                   System.out.print(j);
                 }c++;
             
           System.out.println(" ");
         }
        
          
    
       }
   }

Program that return the sum of square of all odd numbers of inputted number.

class SumOfSqure

 {
   public static void main(String args[])
   {
    int sum=0;
    int arr[]={10,12,11,13,15};
    for(int i=1;i<=4;i++)
   {
       if(arr[i]%2==1)
    {
      int a=arr[i]*arr[i];
      sum=sum+a;
     }
    }
   System.out.println("sum of squre of odd no="+sum);
  }
}

Write a program in which :A class Player have there methods play(),pause(),stop(). And create two another classes Generic Player that extends class Player. The play,pause and stop method of the generic Player can play,pause and stop respectively. The play,pause and stop method of the Dvd Player can play,pause and stop respectively. Make a class Player and call all methods of Generic Player and Dvd Player through the object reference of Player.

class Player
 {
   void play()
   {
    System.out.println("Start the game");
   }
   void pause()
   {
    System.out.println("stop the game for rest 5 min.");
   }
   void stop()
   {
    System.out.println("finish the game");
   }
 }
class GenericPlayer extends Player
  {
    void play()
   {
    System.out.println("Start the game for GenericPlayer");
   }
    void pause()
   {
    System.out.println("resume the game for GenericPlayer");
   }
    void stop()
   {
    System.out.println("end the game for GenericPlayer");
   }
}
class DvdPlayer extends GenericPlayer
  {
    void play()
   {
    System.out.println("Start the game for dvdPlayer");
   }
    void pause()
   {
    System.out.println("resume the game for dvdPlayer");
   }
    void stop()
   {
    System.out.println("end the game for dvdPlayer");
   }
   public static void main(String args[])
    {
     DvdPlayer ab=new DvdPlayer();
     ab.play();
     ab.pause();
     ab.stop();
    }
    
}

Write a program to show the constructor overloading at the version of constructor in available in a class.

class OverLoad
  {
    int age, num1,num2,num3,sum,e,b,c;
    String name;
    OverLoad(String n,int a) //constructor
    {
      name=n;
      age=a;
    }
    void disp()
     {
      System.out.println("Name of the person  :"+name);
      System.out.println("age  of the person  :"+age);
     }
   
    OverLoad(int e,int b,int c)
    {
     num1=e;
     num2=b;
     num3=c;
    
    }
   
     void show()
     {
       int sum=num1+num2+num3;
       System.out.println("sum  =:"+sum);
     }
  public static void main(String args[])
    {
       OverLoad ob=new OverLoad("kapil" ,19);

      
       ob.disp();
       OverLoad ab=new OverLoad(4,5,6);
       ab.show();
    }
 }
     

     

A electricity board charges the following rates to domestic users to charge large consumption of energy. For first 100 units - 40paise/ unit , for next 200 units – 50paise/ unit , for next or beyond 300 units – 60paise /unit, if the total bill is more than Rs.250 then an additional charge of 15% is added to the bill. Write a program that read the names of the users and number of units consumed and print the charges with names.

class Charge
   {
     public static void main(String[] args)
      {
       float p;
       int units=Integer.parseInt(System.console().readLine("enter the units="));
       String userName=System.console().readLine("entr the user name=");
        if(units<=100)
          {
            p=units*.4f;
            System.out.println("rs="+p);
           }
          else
            {
             if((units>100)&&(units<=300))
             {
                p=(100*.40f)+((units-100)*.50f);
                System.out.println("rs="+p);
             }
            else
               {
                 p=(100*(40/100f))+(200*(50/100f))+((units-300)*(60/100f));
                 System.out.println("rs="+p);
               }
             }
          if(p>250)
             {
               p=p+p*(15/100f);
               System.out.println("Bill="+p);
             }
        System.out.println("userName="+userName+" & Bill="+p);
      }
  }

              
          

Create a class Calculator that has the following features: addition, subtraction, multiplication, division. Create another class named ScientificCalculator that will have the features of Calculator and some other features like: to find square, cube, power, factorial.... Note:Inheritance to complete this task.

class Calculator
  {
   public void calc()
    {
    int add, sub, mul, a, b;
    double div;
    a=Integer.parseInt(System.console().readLine("enter the first no.  :"));
    b=Integer.parseInt(System.console().readLine("enter the second no. :"));
    add=a+b;
    System.out.println("addition of two no.      ="+add);
    sub=a-b;
    System.out.println("substraction of two no.  ="+sub);
    mul=a*b;
    System.out.println("multiplecation of two no.="+mul);
    div=(double)a/b;
    System.out.println("division of two no.      ="+div);
   }
  }
class ScientificCalculator extends Calculator
{
   void scincalc()
   {
    System.out.println("****************values of ScientificCalculator******************");
   int n=Integer.parseInt(System.console().readLine("enter the  no.  :"));
   int sqr=n*n;
   System.out.println("Square  of the no.      ="+sqr);
   int cube=n*n*n;
   System.out.println("cube of the no.="+cube);
   int fact=1;
   for(int j=1;j<=n;j++)
      {
        fact=fact*j;
      }
    System.out.println("factorial of the no.="+fact);
   int d=Integer.parseInt(System.console().readLine("enter the power no.  :"));
   int pow=n;
   for(int i=1;i<d;i++)
     {
     
       pow=pow*n;
     }
  
    System.out.println("power of n to d is ="+pow);
   
  
  }
   public static void main(String args[])
    {
      ScientificCalculator ab=new ScientificCalculator();
      ab.calc();
      ab.scincalc();
      
    }
 }