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

Tuesday, November 15, 2022

Interfaces in Java

package inhert;
public class Inhert  {
 public interface person{
         void name(String n);
         void age(int a );
         void clas(int c);
    }
   
    public static class usman implements person{
    public  void name(String n){
        System.out.println("Usman name= "+n);
    }
    public void age(int a){
        System.out.println("Usman age= "+a);
    }
    public void clas(int c){
        System.out.println("Usman class= "+c);
    }
    }
   
    public static class usama implements person{
    public void name(String n){
        System.out.println("Usman name= "+n);
    }
    public void age(int a){
        System.out.println("Usman class= "+a);
    }
    public void clas(int c){
        System.out.println("Usman class= "+c);
    }  
    }
   
  
       public static void main(String[] args){
           usama obj=new usama();
           obj.name("Usama hussain");
           obj.age(20);
           obj.clas(10);
          
           usman obj1=new usman();
           obj1.name("Usman Siddique");
           obj1.age(21);
           obj1.clas(9);
}   }
================================
An interface is a reference type in Java, it is similar to class, it is a collection of abstract methods. A class implements an interface, thereby inheriting the abstract methods of the interface.
Along with abstract methods an interface may also contain constants, default methods, static methods, and nested types. Method bodies exist only for default methods and static methods.
Writing an interface is similar to writing a class. But a class describes the attributes and behaviours of an object. And an interface contains behaviours that a class implements.
Unless the class that implements the interface is abstract, all the methods of the interface need to be defined in the class.
An interface is similar to a class in the following ways:
  • An interface can contain any number of methods.
  • An interface is written in a file with a .java extension, with the name of the interface matching the name of the file.
  • The byte code of an interface appears in a .class file.
  • Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
However, an interface is different from a class in several ways, including:
  • You cannot instantiate an interface.
  • An interface does not contain any constructors.
  • All of the methods in an interface are abstract.
  • An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final.
  • An interface is not extended by a class; it is implemented by a class.
  • An interface can extend multiple interfaces.

Declaring Interfaces:

The interface keyword is used to declare an interface.

Friday, April 8, 2016

Type Casting in Java: Type Conversion in Java


Convert Sting to Int:

String str = "1234";
int foo = Integer.praseInt(str);

Convert Char to Int:

char a = '3';
int foo = (int) a-48;

String to Char Array:

String str = "1234";
char [] charary = str.toCharArray();

Covert Int to String:

int w=1234;
String se=Integer.toString(w);

Convert Double to int:

double x=22.4;
int a= (int)x;

Convert long to int:

long a =10;
int  s = (int) a;

Convert String to BigInteger:


String b="1234422";

BigInteger a=new BigInteger(b); 

Thursday, April 7, 2016

File Handling in Java::Reading and Writing

Reading and writing files in Java using Formatter, Scanner and bufferedreader:


Simple Open a File in Java:

package myfirstproject;
import java.io.*;
public class MyFirstProject {  
    public static void main(String args[]) throws IOException
   {
      File name=new File("C:\\usman\\input.txt");
      if(name.exists()){
          System.out.println(name.getName()+"File is exists");
      }
      else{
          System.out.println("File does not exists");
      }
}
}


Craete New File in Java:

package myfirstproject;
import java.io.*;
import java.util.*;

public class MyFirstProject {  
    public static void main(String args[]) throws IOException
   {
       /*final Formatter x;
       x =new  Formatter("C:\\usman\\fatima.txt");
     
     
*/      File name=new File("C:\\usman\\input1.txt");
        name.createNewFile();
      if(name.exists()){
          System.out.println(name.getName()+"File is exists");
      }
      else{
          System.out.println("File does not exists");
      }
}


}Write in File Using Formatter in Java:
package myfirstproject;
import java.io.*;
import java.util.*;
import java.lang.*;

public class MyFirstProject {  
    public static void main(String args[]) throws IOException
   {
       final Formatter x;
       x = new Formatter("C:\\usman\\usman.txt");
       x.format("%s%s%s%d", "usman " ,"Siddique ", "fatima " ,3);
     
       File name=new File("C:\\usman\\usman.txt");
       if(name.exists()){
           System.out.println("true");
      }
     
       x.close();
}

}



Reading File Using Scanner in Java:
package myfirstproject;
import java.io.*;
import java.util.*;
import java.lang.*;

public class MyFirstProject {  
    private Scanner a;
    public static void main(String args[]) throws IOException
   {
       final Formatter x;
       x = new Formatter("C:\\usman\\input.txt");
     
        MyFirstProject obj=new MyFirstProject();
        obj.a=new Scanner(new File("C:\\usman\\usman.txt"));
        while(obj.a.hasNext()){
            String value=obj.a.next();
       x.format("\n%s\t%d", value  ,3);
        }
       File name=new File("C:\\usman\\usman.txt");
       if(name.exists()){
           System.out.println("true");
      }
     
       x.close();
}

}

Thursday, September 24, 2015

Multilevel Inheritance and Function Overriding in Java

package oop;
public class Oop {

    public static class A{
        void func(){
            System.out.println("Function of A");
        }
    void func1(){
            System.out.println("Function of A");
        }
    }
    public static class B extends A{
        void func(){
            System.out.println("Function of B");
        } 
    void func1(){
        super.func1();
            System.out.println("Function of B");
        }    
    }
    public static class C extends B{
        void func(){
            System.out.println("Function of C");
        }        
    void  func1(){
        super.func1();
            System.out.println("Function of C");
        }
    }
    public static void main(String[] args) {
     
     
        C obj4=new C();
        obj4.func();
        obj4.func1();
    }
    }


Saturday, September 12, 2015

Constructor and Object in Java

Different Types Of Constructor in Java ... Default Constructor,Pramiterized Constructor ,,Over-loaded Constructor:-

package oop;
public class Oop {

    public int ID;
    public String name;
    public char bloodgroup;
   
    Oop(){
        System.out.println("This is the Default Constructor");
    }
    Oop(int a){
        System.out.println("This is a prametrized Constructor No= "+a);
    }
    Oop(String a){
        System.out.println("This is an other Constructor name= "+a);
    }
   
    public static void main(String[] args) {
        Oop obj1=new Oop();
        Oop obj2=new Oop(2015);
        Oop obj3=new Oop("3rd Constructor");
       
        obj1.ID=34;
        obj1.name="usman";
        obj1.bloodgroup='a';
               
    }
}

=================================
package usman1;
public class Usman1 {
    int age;
    String name;
    public Usman1(int a,String b){
age=a;
name=b;
System.out.println("Name= "+name+" age= "+age);
    }
    public static void main(String[] args) {
     Usman1 obj=new Usman1(20,"Usman Siddique");
    }  
    }

Monday, February 2, 2015

Augmented Matrix Solution By reducing it to Row Echelon Form in Java

Read Also:  


Augmented Matrix Solution By reducing it to Row Echelon Form And then Calculate the result by backward substitution (Code in Java) 



package echolon;
import java.util.Scanner;
/**
 *
 * @author usman
 */
public class Echolon {
     Scanner usman=new Scanner(System.in);
    int i,j,k,n,a;
    float [][] A=new float[20][20];
    float  sum;
    float c;
    float [] x=new float[10];
    
    public  Echolon(){
    sum=0;
    System.out.println("\nEnter the order of matrix: ");
    n=usman.nextInt();
    a=n;
    System.out.println("\nEnter the elements of augmented matrix row-wise:\n\n");
    for(i=1; i<=n; i++)
    {
for(j=1; j<=(n+1); j++)

            if(a<j){System.out.println("\nEnter the Value of B: (1st row):- \n ");}
   System.out.println("A["+i+"]["+j+"] : ");
            
   A[i][j]=usman.nextFloat();
}
    }
    }

    void operation() {
    for(j=1; j<=n; j++)
    {
for(i=1; i<=n; i++)
{
   if(i>j)
   {
c=A[i][j]/A[j][j];
for(k=1; k<=n+1; k++)
{
   A[i][k]=A[i][k]-c*A[j][k];
}
   }
}
    }

    x[n]=A[n][n+1]/A[n][n];
     for(i=n-1; i>=1; i--)
    {
sum=0;
for(j=i+1; j<=n; j++)
{
   sum=sum+A[i][j]*x[j];
}
x[i]=(A[i][n+1]-sum)/A[i][i];
    }
    }

    void result(){
    System.out.println("\nThe solution is: \n");
    for(i=1; i<=n; i++)
    {
System.out.println("\nx"+i+" =  "+x[i]+"\t");
    }
    }

void display()
{
System.out.println("\n The Matrix is :-\n ");
for(int i=1;i<=n;i++){
System.out.println("\n");
for(int j=1;j<=n+1;j++){
System.out.println(A[i][j]+"     ");
}}
}


public static void main(String args[]){
Echolon obj=new Echolon();
obj.operation();
obj.display();
obj.result();
}
}



Sunday, February 1, 2015

GPA Calculator in JAVA


package usman1;
import java.util.Scanner;
public class Usman1 {
public static void main (String args[]){

  String grade = "";
  double credit1;
  double credit2;
  double credit3;
  double credit4;
  double gradeValue=0;
  double totPtsClass1=0;
  double totPtsClass2=0;
  double totPtsClass3=0;
  double totPtsClass4=0;
  double totPts=0;
  double totalCredits= 0;
  double gpa;

  Scanner console = new Scanner (System.in);
  System.out.println("Please enter the number of credits of the class 1 (A number)");
  credit1 = console.nextDouble();
  System.out.println("Please enter your grades for the class 1(Capital letters such as A,B+, C-)");
  grade = console.next();

  if (grade.equals ("A"))
    gradeValue= 4.00;
  else if (grade.equals("A-"))
    gradeValue= 3.67;
  else if (grade.equals("B+"))
    gradeValue = 3.33;
  else if (grade.equals("B"))
    gradeValue = 3.00;
  else if (grade.equals ("B-"))
    gradeValue = 2.67;
  else if (grade.equals("C+"))
    gradeValue = 2.33;
  else if (grade.equals("C"))
    gradeValue = 2.00;
  else if (grade.equals ("D+"))
  gradeValue = 1.33;
  else if (grade.equals ("D"))
    gradeValue = 1.00;
  else if (grade.equals ("F"))
    gradeValue = 0;
  else if (grade.equals ("FX"))
    gradeValue = 0;
  else
    System.out.println ("Invalid Grade");

  totPtsClass1 = gradeValue * credit1;

  System.out.println("Please enter the number of credits of the class 2 (A number)");
  credit2 = console.nextDouble();
  System.out.println("Please enter your grades for the class 2 (Capital letters such as A,B+, C-)");
  grade = console.next();

  if (grade.equals ("A"))
    gradeValue= 4.00;
  else if (grade.equals("A-"))
    gradeValue= 3.67;
  else if (grade.equals("B+"))
    gradeValue = 3.33;
  else if (grade.equals("B"))
    gradeValue = 3.00;
  else if (grade.equals ("B-"))
    gradeValue = 2.67;
  else if (grade.equals("C+"))
    gradeValue = 2.33;
  else if (grade.equals("C"))
    gradeValue = 2.00;
  else if (grade.equals ("D+"))
  gradeValue = 1.33;
  else if (grade.equals ("D"))
    gradeValue = 1.00;
  else if (grade.equals ("F"))
    gradeValue = 0;
  else if (grade.equals ("FX"))
    gradeValue = 0;
  else
  System.out.println ("Invalid Grade");

  totPtsClass2 = gradeValue * credit2;

  System.out.println("Please enter the number of credits of the class 3 (A number)");
  credit3 = console.nextDouble();
  System.out.println("Please enter your grades for the class 3 (Capital letters such as A,B+, C-)");
  grade = console.next();

  if (grade.equals ("A"))
    gradeValue= 4.00;
  else if (grade.equals("A-"))
    gradeValue= 3.67;
  else if (grade.equals("B+"))
    gradeValue = 3.33;
  else if (grade.equals("B"))
    gradeValue = 3.00;
  else if (grade.equals ("B-"))
    gradeValue = 2.67;
  else if (grade.equals("C+"))
    gradeValue = 2.33;
  else if (grade.equals("C"))
    gradeValue = 2.00;
  else if (grade.equals ("D+"))
  gradeValue = 1.33;
  else if (grade.equals ("D"))
    gradeValue = 1.00;
  else if (grade.equals ("F"))
    gradeValue = 0;
  else if (grade.equals ("FX"))
    gradeValue = 0;
  else
    System.out.println ("Invalid Grade");

    totPtsClass3 = gradeValue * credit3;

  System.out.println("Please enter the number of credits of the class 4 (A number)");
  credit4 = console.nextDouble();
  System.out.println("Please enter your grades for the class 4 (Capital letters such as A,B+, C-)");
  grade = console.next();

  if (grade.equals ("A"))
    gradeValue= 4.00;
  else if (grade.equals("A-"))
    gradeValue= 3.67;
  else if (grade.equals("B+"))
    gradeValue = 3.33;
  else if (grade.equals("B"))
    gradeValue = 3.00;
  else if (grade.equals ("B-"))
    gradeValue = 2.67;
  else if (grade.equals("C+"))
    gradeValue = 2.33;
  else if (grade.equals("C"))
    gradeValue = 2.00;
  else if (grade.equals ("D+"))
  gradeValue = 1.33;
  else if (grade.equals ("D"))
    gradeValue = 1.00;
  else if (grade.equals ("F"))
    gradeValue = 0;
  else if (grade.equals ("FX"))
    gradeValue = 0;
  else
    System.out.println ("Invalid Grade");
  totPtsClass4 = gradeValue * credit4;

  totPts= totPtsClass1+totPtsClass2+totPtsClass3+totPtsClass4;
  totalCredits = credit1+credit2+credit3+credit4;
  gpa= totPts / totalCredits;

  System.out.printf("Your GPA is: %.2f\n", + gpa);
}
}


Sunday, January 4, 2015

First Hello World Android Program

Please follow below give steps for create First Hello World Android Program in eclipse :-
Step 1:Install  new version of android supported Eclipse
Step 2:After install the eclipse go in File Section
First Hello World Android Program-1
Step 3:File->new->Android Application Project

 First Hello World Android Program-1

Step 4:After click on Android Application project
Step 5:Type Application Name,First letter Must be in uppercase
First Hello World Android Program-3
Step 6:Click on Next->Next->Next->Next
step 7:choose the Activity Name
First Hello World Android Program-7
Step 8:Click on Finish button

Your Android project is ready to work.Your project have following sections and these section have following file and what is use of these files and folders
1.src folder:-this folder contain package of your project
this package have classes where you want to coding and implement functional activity in your project.
2.gen folder:-it have supported package that used to run your project
3.Android 4.4.2(Android version 19):-contain android .jar file
4.Android Dependencies:-have the other outer supported .jar files
5.Referenced Libraries:-it have reference libraries like Android-support-v4 .jar file
6.bin folder:in bin folder .class and .apk file are create after “run” the project
7.lib folder:-it have current running libraries,you also add external .jar file in bin folder
8.res folder:-in drawable folder you add the images that is used in your project.
9.layout folder:-create .xml file in layout folder.it is a very important part in project,by this you design your project.
values folder:-you add your style,color .xml file
10.Android Manifest.xml file:-it is very important file in android project.it having following information
a)android version information
b)package name
c)SDK version information
d)when .xml file open first when project is run.
Source Code :-
Helloworld.java
package com.example.helloworld;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}