Employee details program in java

Employee detail program in java as as below.

Here two classes are created

  1. Employee class
  2. EmployeeMain class

Details of these two classes are as below

Employee.java

This class contains various field of employees like id, fname, lname, address etc.

Its corresponding getter and setter method is created in program.

Employee.java is a java bean and it good example of java encapsulation.

fields of Employee object can not be directly accessible outside the class to access this we need getter and setter methods.

Default and parameterized constructor are created to initialize employee object.

toString() of Object class is overridden to convert object to String.

Some fields like hra, providentFund and grossSalary are calculated from basicSalary.

So user provides only basicSalary value and other derived values are calculated based on it.

Two different methods are used to initialize values(Parameterised constructor and setter methods).

So calculation of derived values are done inside constructor and in setter method.

Providing addDate value is optional for user it user does not provide this value constructor or setter method does this.

EmployeeMain.java

This class contain public static void main() (java main method) inside main tho objects of Employee class is created.

First object is created using parameterized constructor and object’s value is printer by overriding the toString() of Object class.

Second object is created using default constructor of employee and values are assigned using setter methods of class.

To print value of object getter method is used. Insted of using getter method toString() can also be used.

Its up to used which method he wants to use to set values in object.

Result of above program is

If any one want to change value of any field then appropriate setter method needs to called

like want to update lname of employee object then use

employee.setLname("Verma");

this will assign new value to lname.

Learn database operations with employee details

Write a java program to reverse a string

Using StringBuffer or StringBuilder class

Output

Using Loops(Iteration)

Copy and reverse the string

Here getting input from user

converted into character array using toCharArray() of String

Initialize the other character array with same length as c character array.

Loop char array c in reverse order and r in forward order.

this will copy character array c in reverse order to r.

converted char array r to String

print both character array

Output

Even or odd program in java

The number which is divisible by 2 is known as even number and the number which is not divisible by 2 is odd number.

in programming we use % operator to check divisibility.

The % operator returns the remainder of division. If a number is completely divisible its remainder is 0.

So any number is completely divisible by 2 then its remainder is zero.

4%2,20%2,60%2 all return zero.

5%2,45%2,51%2 its remainder is non zero.

Even or odd number program in java using if else

Scanner class is used to take input from standard input stream that is keyboard.

Then perform modulo operation with 2 if remainder is 0 than a even number else number is odd.

Output

Even or odd number program in java using switch case

switch(number%2) returns 0 or non zero value. So there should be two case statement case 0 for even numbers and default for non zero numbers.

Output

Even or odd number program in java using ternary operator

Output

String reverse program in java

String Reverse using StringBuilder

Leap Year Program in Java

A Leap Year is a year which has 366 days. This year February is of 29 days. Leap years occurs in every four years.

A leap year has following constraints.

We can check in programming as below

  • A year is divisible by 400 then it is a leap year
  • A year is divisible by 4 and not divisible by 100 then the year is leap year

See another program

How to Parse JSON Array With Gson

Parsing JSON array is very easy with Gson.

Follow these steps to read JSON Array

  1. Create GSON Object
  2. Read array with fromJson()

To read jsonString as array we use following syntax

Gson gson=new Gson();
ElementType [] refVar=gson.fromJson(jsonString,ElementType[].class);

To read jsonString as List TypeToken is used to find the type of class

List eList=(List)gson.fromJson(reader, new TypeToken>(){}.getType());
List employeeList=(List)gson.fromJson(reader, new TypeToken>(){}.getType());

Here ElementType is Empolyee so we read data as below

Employee [] employees = gson.fromJson(reader, Employee[].class);

JSON Array is below. It is stored in server to read this URL is used

Employee.java

This is simple Java bean containing get and set methods.

The employee class should have same filed name as in JSON. Field name in JSON should match with field name with Employee class.

ReadEmployee.java

This file contains main method.

To read data from URL URL class and HTTPURLConnection is used.

To read data Gson Object is created and fromJson() is used.

fromJson() parse json string and store in array of Employee.

Result

Concatenate & Buffer File in Java

Concatenate: It is possible to concatenate two or more files and save in a different file.

In java, by using SequenceInputStream class we can concatenate two or more files.

Buffer Files: In java, we can create a buffer to store temporary data that is read from & written to a stream and this process known as i/o buffer operation.

Buffer is  sit between programmer and source/destination file.

Buffer can be created by using following classes:

  • BufferedInputClass
  • BufferedOutputClass

Example: write a program to concatenate two file A & B and concatenated data print on output screen.

Suppose we have a File “A.txt”

Hello students I am aditya.  

 and                         File “B.txt”

Hello  

Output: Hello students I am aditya.                                                                                           Hello

Example: Write a program to copy the content of one file to another file using buffer/BufferReader.                                                                

Suppose we have a File “A .txt”                               

Hello students I am aditya.  

Output:                                              File “B”

Hello students I am aditya.  

Note: If there is no file “B” it will be create automatically.

Example: Write a program to concatenate two file A & B and save the concatenated data on a file C.

Suppose we have a File “A.txt” and File “B.txt”

Hello students I am aditya.  
Hello  

Output:               File “C”

Hello students I am aditya. Hello  

Note: Ifthere is no file “C” it will be create automatically.

File I/O with Primitive Data Type In Java

In java, basic input-output stream provides a method for reading & writing bytes or character in a file. If we want to read-write primitive data typa(such as int , float etc.) than we can use filter classes to filter data in the original stream.

DataInputStream & DataOutputStream are two filter classes used for creating “data streams” for handling primitive data type.

Example: Write a program to write & read a primitive data on a same file.

Character Stream I/O in Java

In a java programming, like a Byte stream Character stream is also used for input-output for 16 bit. But Byte streams are used to perform input and output of 8-bit.

Most common Character streams classes are, FileReader and FileWriter.

 FileReader- FileReader uses for reads two bytes at a time.

 FileWriter– FileWriter uses for writes two bytes at a time.

Example: Write a program, to copy the content of file A into file B using Byte stream class.

Note: if there is only file A in which data is written and no file B then, file B will be create automatically.

Suppose we have a File “A”

Hello students I am aditya.  

Output: When we run above program one new file create with Name “B” and content of file “A” will be copy into file “B”.

            File “B”

Hello students I am aditya.  

Byte Streams in Java

In java programming, to perform input and output of 8-bit, Byte stream is used.

Most common byte streams classes are, FileInputStream and FileOutputStream.

FileInputStream : FileInputStream is used for reads one byte at a time .

FileOutputStream : FileOutputStream is used for writes one byte at a time

Writing Bytes

Example: Write a program, to write a bytes to a  file using Byte stream class.

Result

checking file A.txt it will show following content.

Note: If there is no file A then, file A will be create automatically

Reading bytes from a file

Suppose we have a file “A.txt”

Hello Students I am Aditya

Example: Write a program, to read a bytes from file A using Byte stream class and print on output screen.

Example: Write a program, to copy the content of file A into file B using Byte stream class.

Note: if there is only file A in which data is written and no file B then, file B will be create automatically.

Suppose we have a File “A”

Hello students I am aditya.  

Output: When we run above program one new file create with Name “B” and content of file “A” will be copy into file “B”.

File “B”

Hello students I am aditya.