Wednesday, December 28, 2016

Data Types in Java

DATA TYPES IN JAVA :

Following are the 8 Basic Data Types in Java:

1. int - Integer
2. short - Short Integer
3. long - Long Integer
4. char - Character
5. float - Single Precision Floating point
6. double - Double Precision floating point
7. boolean - true/false
8. byte -  8-bit integer


Following Program Illustrates various data types -:

public class datatypes {
public static void main(String[] args) {

int num=1000000; //integer variable
short s=5000; //short variable
long l=900000000; //long variable
char c='c'; //char variable
float f =159.55f; //float variable ('f' keyword should be used for float)
double d=95735.561; //double variable
boolean b=true; //boolean variable

System.out.println("Value of num is " + num);
System.out.println("Value of s is " + s);
System.out.println("Value of l is " + l);
System.out.println("Value of c is " + c);
System.out.println("Value of f is " + f);
System.out.println("Value of d is " + d);
System.out.println("Value of b is " + b);
}
}





Note:-
1. The name of Source File should be exactly the same as the name of outer class.
2. In order to use float variable, keyword 'f' must be added at the end of its value.
3. '+' operator is used to combine output line and value.

Monday, December 26, 2016

Sum of Two Variables

In Java to accept input from user there are many libraries like Scanner,Textfields etc...

To use Scanner we have to import the Scanner into our program so we can do this by using
following statement

import java.util.Scanner;

now we can create object of Scanner type by using following statements

Scanner input=new Scanner(System.in);

thus we can accept data from user

Program :

import java.util.Scanner;
public class JavaApplication1 {

   public static void main(String []args)
   {
     float a;
     int b;
     Scanner input=new Scanner(System.in);
     System.out.println("Enter Two Values");
     a=input.nextFloat();        //for accepting float data
     b=input.nextInt();           //for accepting int data   
     float c=a+b;
     System.out.println("Sum is :"+c );
}

}

Output :



Saturday, December 24, 2016

Printing a Variable

In Java We Can Print The Value Of Variable by using a '+' symbol like as shown in following program 

Program :


public class JavaApplication1 {

    public static void main(String[] args) {
        int a=15;
        System.out.println("Value Is :"+a);
    }
}


Output:


Printing sum of two variables :


public class JavaApplication1 {

    public static void main(String[] args) {
        int a=15,b=10;
        System.out.println("Sum Is :"+(a+b));
    }
}

Output:



JAVA BASICS Hello World

Java is a high-level programming language originally developed by Sun Microsystems
which was initiated by James Gosling  and released in 1995.

Java Basic Program Hello World :


public class JavaApplication1
 {

    public static void main(String[] args)   //main function
    {
        System.out.println("Hello World");  //for output
    }
    

 }


Output:




Points To Remember:
1. Java is case sensitive so be careful about upper case and lower case alphabets.
2. While using Netbeans the name of class must be same as project name and using Notepad the name of file must be same as the class name with .java extension.
3.Rules for identifiers must be followed.

Thursday, December 22, 2016

Project: Banking System

Description: The C++ programs on BANKING SYSTEM has account class with data members like account number, name, deposit, withdraw amount and type of account. Customer data is stored in a binary file. A customer can deposit and withdraw amount in his account. User can create, modify and delete account.

Code:

#include<iostream>
#include<fstream>
#include<stdlib.h>
using namespace std;
class Account
{
char name[30];
int deposit;
char type[10];
public:
long long account_no;
void getdata()
{
cout<<"Enter Name =";
cin.ignore();
cin.getline(name,30);
cout<<"Enter Type of Account =";
cin>>type;
cout<<"Enter Initial Deposit =";
cin>>deposit;
cout<<"Enter Account Number =";
cin>>account_no;
}
void putdata()
{   cout<<"\n*********************";
cout<<"\nName="<<name;
cout<<"\nAccount Number="<<account_no;
cout<<"\nBalance= "<<deposit;
cout<<"\nType= "<<type<<"\n";
}
void depposit()
{
int x;
cout<<"Enter Amount To Deposit =";
cin>>x;
deposit=deposit+x;
cout<<"\nAmount Deposited\nNew Balance is "<<deposit;
}
void withdraw()
{
int x;
cout<<"Enter Amount To Withdraw ";
cin>>x;
if(x<deposit)
{
deposit=deposit-x;
cout<<"\nNew Balance "<<deposit;
   }
   else
   {
    cout<<"\nNot Enough Balance";
}
   }
void modify()
{
cout<<"\nEnter Name =";
cin.ignore();
cin.getline(name,30);
cout<<"Enter Type of Account=";
cin>>type;
}
};
main()
{
    Account a;
    int password;
cout<<"\n****Welcome To Abc Bank****\n";
int x;
cout<<"\nEnter Password: ";
cin>>password;
if(password==1507623)
{   system("cls");
cout<<"\nChoose From Menu Below\n";
while(1)
{
cout<<"\n\nPress\n1. To Create An Account\n2. To Modify An Account\n3. To Display An Account\n4. To Delete An Account\n5. To Display All Accounts\n6. To Deposit Amount\n7. To Withdraw Amount\n8. To Exit\n";
cin>>x;
system("cls");
switch(x)
{
case 1:
{
ofstream fout("Banking.bin",ios::app | ios::binary);
cout<<"Enter Details\n";
a.getdata();
fout.write((char *)&a,sizeof(a));
fout.close();
break;
}
case 2:
   {
cout<<"Enter Account Number ";
   long long y;char ch;
   int flag=0;
   cin>>y;
   fstream file("Banking.bin",ios::in | ios::binary | ios::out);
while(file&&flag==0)
{  
file.read((char *)&a,sizeof(Account));
if(a.account_no==y)
{
cout<<"Enter New Details\n";
a.modify();
                     file.seekp(-(sizeof(Account)), ios::cur);
file.write((char *) &a,sizeof(Account));
flag=1;
}
}
if(flag==0)
{
cout<<"Invalid Account Number";
}
file.close();
   break;
}
                    case 3:

{
cout<<"\nEnter Account Nunber ";
long long y;
cin>>y;
ifstream fin("Banking.bin",ios::binary);
int flag=0;

while(fin&&flag==0)
{   fin.read((char *)&a,sizeof(Account));
if(a.account_no==y)
{
cout<<"\nDetails Are\n";
a.putdata();
flag=1;
}
}
if(flag==0)
{
cout<<"Invalid Account Number";
}
fin.close();
break;
}
   case 4:
               {
            cout<<"\nEnter Account Nunber ";
long long y;
cin>>y;
fstream fin("Banking.bin",ios::binary |ios::in);
fstream file("Bank2.bin",ios::binary | ios::out);
fin.seekg(0,ios::beg);
while(fin.read((char *)&a,sizeof(Account)))
{
if(a.account_no!=y)
{ file.write((char *)&a,sizeof(Account));}
}
fin.close();
file.close();
remove("Banking.bin");
rename("Bank2.bin","Banking.bin");
cout<<"\nRecord Deleted\n";
break;
               }

case 5:
{
ifstream fin("Banking.bin",ios::binary);
while(fin.read((char *)&a,sizeof(Account)))
{
a.putdata();
   }
   fin.close();
     break;
   }
case 6:
{
cout<<"\nEnter Account Nunber ";
long long y;
cin>>y;
fstream fin("Banking.bin",ios::binary | ios::in | ios::out);
int flag=0;
while(fin&&flag==0)
{   fin.read((char *)&a,sizeof(Account));
if(a.account_no==y)
{
  a.depposit();
                    fin.seekp(-(sizeof(Account)),ios::cur);
fin.write((char *)&a,sizeof(Account));
  flag=1;
}
   }
if(flag==0)
{
cout<<"Invalid Account Number";
}
          fin.close();
  break;
           }
case 7:
{
cout<<"\nEnter Account Nunber ";
long long y;
cin>>y;
fstream fin("Banking.bin",ios::binary | ios::in | ios::out);
int flag=0;
while(fin&&flag==0)
{   fin.read((char *)&a,sizeof(Account));
if(a.account_no==y)
{
 a.withdraw();
    fin.seekp(-(sizeof(Account)),ios::cur);
fin.write((char *)&a,sizeof(Account));
  flag=1;
}
   }
if(flag==0)
{
cout<<"Invalid Account Number";
}
     fin.close();
 break;
           }
                case 8:
    exit(1);
   default: cout<<"\a\nInvalid Choice \t Try Again";
    }
    }
}
else
{
cout<<"\a\nInvalid Password";
}
}

Output:

Wednesday, November 9, 2016

Pointer Arithmetic

Pointer Arithmetic involves the following operations
1.Increment / Decrement pointer
2.Addition  / Subtraction of integer
3.Subtraction of Two Pointers

Following Program Illustrates the Concept:

#include<iostream>
using namespace std;
main()
{
int a[3]={1,2,3};
int *p,*q;
p=&a[0];
q=&a[2];
//incrementing decrementing
cout<<"Value"<<"\t"<<"Address"<<"\n";
cout<<*p<<"\t"<<p<<"\n";
p++;
cout<<*p<<"\t"<<p<<"\n";
p--;
cout<<*p<<"\t"<<p<<"\n";
// addition and subtraction of integer
p+=2;
cout<<*p<<"\t"<<p<<"\n";
p-=2;
cout<<*p<<"\t"<<p<<"\n";
//subtaction of two pointers
cout<<"q-p= ";
cout<<q-p;
}

Output:




File Opening Modes App(append) vs Ate(at the end)

Both Ate and App makes the file pointer points to the end of file,but there is a difference in these two.
That during App mode we cannot change the position of pointer using seekg i.e "No Modify"
While in Ate mode we can change the position of pointer using seekg i.e "Modify Allowed".

Following Program Illustrates the concept:

#include<iostream>
#include<fstream>
using namespace std;
main()
{ int a;
fstream file("file.txt",ios::in|ios::out|ios::app);
    fstream file2("file2.txt",ios::in|ios::out|ios::ate);
file<<1507;
file2<<1507;
file.seekg(0,ios::beg);
file2.seekg(0,ios::beg);
file<<623;
file2<<623;
file.close();
file2.close();
}

Special Thanks To Miss Naina Thaman For This Program.

File Output:
file.txt:                                                                                file2.txt: