Thursday 30 August 2018

                                   Internal working of HashMap through Program in java

package govind;

public class ProductClass {

String ppname;
float pprice;


public ProductClass()
{

}
public ProductClass(String ppname, float pprice) {
super();
this.ppname = ppname;
this.pprice = pprice;
}
@Override
public int hashCode() {

System.out.println("Ïnside hashcode method");
final int prime = 31;
int result = 1;
result = prime * result + ((ppname == null) ? 0 : ppname.hashCode());
result = prime * result + Float.floatToIntBits(pprice);
return result;
}
@Override
public boolean equals(Object obj) {
System.out.println("Inside equals method");
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ProductClass other = (ProductClass) obj;
if (ppname == null) {
if (other.ppname != null)
return false;
} else if (!ppname.equals(other.ppname))
return false;
if (Float.floatToIntBits(pprice) != Float.floatToIntBits(other.pprice))
return false;
return true;
}




}
=======================================================================================================================================

package govind;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class ProductClassMain {

public static void main(String[] args) {
Map< ProductClass, String> map=new HashMap<>();
map.put(new ProductClass("Nokia", 45000.00f),"Mobile");
map.put(new ProductClass("Dell", 50000.00f),"Laptop");
Set<Entry<ProductClass, String>> set = map.entrySet();
Iterator<Entry<ProductClass, String>> it = set.iterator();
while (it.hasNext()) {
Entry<ProductClass, String> kk = it.next();
ProductClass p = kk.getKey();
System.out.println(p.ppname+" "+p.pprice);
System.out.println(kk.getValue());

}

System.out.println("Try to Insert Duplicate key");
map.put(new ProductClass("Nokia", 45000.00f),"Mobile");

}

}


==========================================================================================================================================

o/p:-----------------------------------


Ïnside hashcode method
Ïnside hashcode method
Dell 50000.0
Laptop
Nokia 45000.0
Mobile
Try to Insert Duplicate key
Ïnside hashcode method
Inside equals method



                                                            equals method example  in java
package govind;
public class Product {
private String productName;
private float productPrice;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public float getProductPrice() {
return productPrice;
}
public void setProductPrice(float productPrice) {
this.productPrice = productPrice;
}

}
=============================================================================================================================
package govind;
public class ProductMain {
public static void main(String[] args) {
Product p1=new Product();
p1.setProductName("Laptop");
p1.setProductPrice(45000.00f);
Product p2=new Product();
p2.setProductName("Laptop");
p2.setProductPrice(45000.00f);
System.out.println(p1.equals(p2));
}
}
===================================================================================================================================
o/p:---------------------------
false
package govind;
public class Product {
private String productName;
private float productPrice;
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public float getProductPrice() {
return productPrice;
}
public void setProductPrice(float productPrice) {
this.productPrice = productPrice;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Product other = (Product) obj;
if (productName == null) {
if (other.productName != null)
return false;
} else if (!productName.equals(other.productName))
return false;
if (Float.floatToIntBits(productPrice) != Float.floatToIntBits(other.productPrice))
return false;
return true;
}
}
========================================================================================================================================================
package govind;
public class ProductMain {
public static void main(String[] args) {
Product p1=new Product();
p1.setProductName("Laptop");
p1.setProductPrice(45000.00f);
Product p2=new Product();
p2.setProductName("Laptop");
p2.setProductPrice(45000.00f);
System.out.println(p1.equals(p2));
}
}
================================================================================================================================================================
o/p:-----------------------
true








































Wednesday 22 August 2018

3 Ways to check if String is null or empty in Java

if(stirng != null && !string.isEmpty())
{
 System.out.println("String is not null and not empty");
}

====================================================================

if(stirng != null && string.length() > 0)
{
   System.out.println("String is not null and not empty");
}


======================================================================
if(stirng != null && string.trim().length() > 0)
{
System.out.println("String is not null and not empty");
}


Saturday 18 August 2018

package govind;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.Month;
import java.time.format.DateTimeFormatter;

public class Java8DateTimeExample {

public static void LocalDateTimeApiJava8Concept()
{
 
    // the current date
    LocalDate date = LocalDate.now();
    System.out.println("The current date is "+
                        date);
 
 
    // the current time
    LocalTime time = LocalTime.now();
    System.out.println("The current time is "+
                        time);
     
 
    // will give us the current time and date
    LocalDateTime current = LocalDateTime.now();
    System.out.println("The current date and time : "+
                        current);
 
    // to print in a particular format
    DateTimeFormatter format =
      DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss"); 
      String formatedDateTime = current.format(format); 
      System.out.println("In foramatted manner "+
                        formatedDateTime);
 
 
    // printing months days and seconds
    Month month = current.getMonth();
    int day = current.getDayOfMonth();
    int seconds = current.getSecond();
    System.out.println("Month : "+month+" day : "+
                        day+" seconds : "+seconds);
 
    // printing some specified date
    LocalDate date2 = LocalDate.of(1950,1,26);
    System.out.println("The repulic day :"+date2);
 
    // printing date with current time.
    LocalDateTime specificDate =
        current.withDayOfMonth(24).withYear(2016);

    System.out.println("Specfic date with "+
                       "current time : "+specificDate);
}

   
    public static void main(String[] args)
    {
        LocalDateTimeApiJava8Concept();
    }
}






package govind;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.Month;


public class Java8TimeDateConcept {

public static void main(String[] args) {

        LocalDate today = LocalDate.now();
System.out.println("Today Date="+today);
int year = today.getYear();
int month = today.getMonthValue();
int day = today.getDayOfMonth();
DayOfWeek dayOfweek = today.getDayOfWeek();
Month month1 = today.getMonth();
int lm = today.lengthOfMonth();
int ly = today.lengthOfYear();
System.out.println("Year="+year);
System.out.println("Month="+month);
System.out.println("Day="+day);
System.out.println( "DayOfWeek="+dayOfweek);
System.out.println("Month="+month1);
System.out.println("Length of the Month="+lm);
System.out.println("Length of the Year="+ly);

}
}

o/p:------------------------------------------------------------------

Wednesday 15 August 2018

//write a program  to create a Flag in java using applet?

// Flag.java
import java.applet.*;
import java.awt.*;
public class Flag extends Applet
{
public void paint(Graphics g)
{

g.drawLine(500,640,500,680);
g.drawRect(300,150,500,100);
g.setColor(Color.WHITE);
g.fillRect(300,150,500,100);
g.setColor(Color.BLUE);
g.drawString("Jai Hind Jai Maa Bharat",800,215);
g.drawRect(280,50,20,550);
g.setColor(Color.blue);
g.fillRect(280,50,20,550);
g.drawRect(200,600,200,40);
g.setColor(Color.blue);
g.fillRect(200,600,200,40);
g.drawRect(100,640,400,40);
g.setColor(Color.blue);
g.fillRect(100,640,400,40);

g.drawRect(300,50,500,100);
g.setColor(Color.ORANGE);
g.fillRect(300,50,500,100);
g.drawRect(300,250,500,100);
g.setColor(Color.GREEN);
g.fillRect(300,250,500,100);

g.drawLine(490,200,590,200);
g.setColor(Color.BLUE);

g.drawOval(490,150,100,100);
g.setColor(Color.BLUE);
g.drawLine(540,150,540,250);

g.drawLine(555,150,525,250);
g.drawLine(570,158,510,241);
g.drawLine(578,165,500,230);
g.drawLine(585,175,495,225);
g.drawLine(588,187,492,213) ;

g.drawLine(493,185,587,215);
g.drawLine(500,170,580,230);
g.drawLine(512,160,572,238);
g.drawLine(520,155,565,242);
g.drawLine(530,150,555,250);

}
}

//Flag.html

<html>
<head><title>First applet</title>
</head>
<body>
<applet code="Flag.class" width="100" height="500">
</applet>
</body>
</html>




O/p:----------------------------------------






Tuesday 14 August 2018


How to write String in a file using BufferedWriter?

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFileExample {



public static void main(String[] args) {

BufferedWriter bw = null;
FileWriter fw = null;

try {

String content = "victory has hundred fathers but defeat is orphan";

fw = new FileWriter("e:/govindbufferedwriterexample.txt");
bw = new BufferedWriter(fw);
bw.write(content);

System.out.println("File created successfully inside e folder and file name is govindbufferedwriterexample.txt and content is victory has hundred fathers but defeat is orphan ");

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bw != null)
bw.close();

if (fw != null)
fw.close();

} catch (IOException ex) {

ex.printStackTrace();

}

}

}

}







import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class WriteToFileExampleTryWithResources {



public static void main(String[] args) {

try (BufferedWriter bw = new BufferedWriter(new FileWriter("e:/govind.txt"))) {

String content = "My name is khan";

bw.write(content);

// no need to close it.
//bw.close();

System.out.println("File created successfully in side e folder with govind.txt file and content is my name is khan");

} catch (IOException e) {

e.printStackTrace();

}

}

}




 BufferedWriter – Append


 append to end of file
FileWriter fw = new FileWriter(FILENAME, true);
BufferedWriter bw = new BufferedWriter(fw);

Monday 13 August 2018

//How to read file content line by line in java?


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class ReadLinesFromFile {

    public static void main(String a[ ]){
        BufferedReader br = null;
        String strLine = "";
        try {
            br = new BufferedReader( new FileReader("d:/govindjava.txt"));
                                                  //or
            //br = new BufferedReader( new FileReader("d:\\govindjava.txt"));
            while( (strLine = br.readLine()) != null){
                System.out.println(strLine);
            }
        } catch (FileNotFoundException e) {
            System.err.println("Unable to find the file: fileName");
        } catch (IOException e) {
            System.err.println("Unable to read the file: fileName");
        }
    }
}





//How to read file content line by line in java?(try with resources concept-java 7 concept)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadLinesFromFileJava7 {


public static void main(String[] args) {

try (BufferedReader br = new BufferedReader(new FileReader("d:/try-with-resources.txt"))) {

String  str=" ";

while ((str = br.readLine()) != null) {
System.out.println(str);
}

} catch (IOException e) {
e.printStackTrace();
}

}

}












Saturday 11 August 2018

                                                                 try with resource concept:-


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Example1 {

public static void main(String[] args) {

BufferedReader br = null;

try {

String line;

br = new BufferedReader(new FileReader("d:\\govind.txt"));

while ((line = br.readLine()) != null) {
System.out.println(line);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}

}
}








import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class Example2 {

public static void main(String[] args) {

try (BufferedReader br = new BufferedReader(new FileReader("d:\\govind.txt")))
{

String line;

while ((line = br.readLine()) != null) {
System.out.println(line);
}

} catch (IOException e) {
e.printStackTrace();
}

}
}





Wednesday 1 August 2018


                                                                  jquery selector



1.select  element by id

<!DOCTYPE html>
    <html lang="en-US">
    <head>
        <title>HTML Tutorial</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <meta charset="windows-1252">
    <script>

    $(document).ready(function(){
        $("#submit").on("click", function(){
        var a = parseInt($('#istno').val());
        var b = parseInt($('#2ndno').val());
           var sum = a + b;
            alert("Sum="+ sum);
        })
    })
    </script>
    </head>
    <body>
       <input type="text" id="istno" name="option">
       <input type="text" id="2ndno" name="task">
    <input id="submit" type="button" value="press me">

    </body>

    </html>






===========================================================================================================================================
2.  select  element by class name


<!DOCTYPE html>
    <html lang="en-US">
    <head>
        <title>HTML Tutorial</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
        <meta charset="windows-1252">
    <script>

    $(document).ready(function(){
        $("#submit").on("click", function(){
        var a = parseInt($('.istno').val());
        var b = parseInt($('.2ndno').val());
           var sub= a - b;
            alert("Substraction="+ sub);
        })
    })
    </script>
    </head>
    <body>
       <input type="text" class="istno" name="option">
       <input type="text" class="2ndno" name="task">
    <input id="submit" type="button" value="press me">

    </body>

    </html>




                                                               jQuery Selectors

=======================================================================================================================================
Selecting an element by ID in jQuery
Selecting elements by class name in jQuery
Selecting elements by element name in jQuery
Selecting elements by attribute in jQuery
Selecting elements by compound CSS selector in jQuery
Selecting elements by jQuery custom selector
===========================================================================================================================================
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Select Element by ID</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // Highlight element with id mark
    $("#mark").css("background", "blue");
});
</script>
</head>
<body>
    <p id="mark">Govind Ballabh Khan.</p>
    <p>My name is khan.</p>
    <p>Khan is my title but i am maithil brahmin</p>
    <p><strong>Note:</strong> The value of the id attribute must be unique in an HTML document.</p>
</body>
</html>                           


=========================================================================================================================================

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Select Element by Class</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // Highlight elements with class mark
    $(".mark").css("background", "yellow");
});
</script>
</head>
<body>
     <p class="mark">Govind Ballabh Khan.</p>
     <p class="mark">My name is khan.</p>
    <p>Khan is my title but i am maithil brahmin</p>
</body>
</html> 

==========================================================================================================================================



<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Select Element by Name</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // Highlight paragraph elements
    $("p").css("background", "red");
});
</script>
</head>
<body>
    <h1>Govind Ballabh Khan.</h1>
    <p>My name is khan.</p>
    <p>Khan is my title but i am maithil brahmin</p>

</body>
</html>


===========================================================================================================================================

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Select Element by Attribute</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // Highlight paragraph elements
    $('input[type="text"]').css("background", "yellow");
    $('input[type="password"]').css("background", "blue");
});
</script>
</head>
<body>
    <form>
        <label>Name: <input type="text"></label>
        <label>Password: <input type="password"></label>
        <input type="submit" value="Sign In">
    </form>
</body>
</html>


===========================================================================================================================================


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Select Element by Compound Selector</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

    // Highlight li elements inside the ul elements
    $("ul li").css("background", "yellow");
 
    // Highlight li elements only inside the ul element with id mark
    $("ul#mark li").css("background", "red");
 
    // Highlight li elements inside all the ul element with class mark
    $("ul.mark li").css("background", "green");
 
   
});
</script>
</head>
<body>
    <p>This is a paragraph.</p>
    <p>This is another paragraph.</p>
    <p>This is one more paragraph.</p>
    <ul>
        <li>mango</li>
        <li>mango</li>
        <li>mango</li>
    </ul>
    <ul id="mark">
        <li>apple</li>
        <li>apple</li>
        <li>apple</li>
    </ul>
    <ul class="mark">
        <li>orange</li>
        <li>orange</li>
        <li>orange</li>
    </ul>
   
</body>
</html>                           



==========================================================================================================================================

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery Custom Selector</title>
<style type="text/css">
    /* Some custom style */
    *{
        padding: 5px;
    }
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    // Highlight table rows appearing at odd places
    $("tr:odd").css("background", "yellow");
 
    // Highlight table rows appearing at even places
    $("tr:even").css("background", "orange");
 
    // Highlight first paragraph element
    $("p:first").css("background", "red");
 
    // Highlight last paragraph element
    $("p:last").css("background", "green");
 
    // Highlight all input elements with type text inside a form
    $("form :text").css("background", "purple");
 
    // Highlight all input elements with type password inside a form
    $("form :password").css("background", "blue");
 
    // Highlight all input elements with type submit inside a form
    $("form :submit").css("background", "violet");
});
</script>
</head>
<body>
    <table border="1">
        <thead>
            <tr>
                <th>No.</th>
                <th>Name</th>
                <th>Email</th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <td>1</td>
                <td>Govind Ballabh Khan</td>
                <td>govindkhan@mail.com</td>
            </tr>
            <tr>
                <td>2</td>
                <td>sanjay</td>
                <td>sanjay@mail.com</td>
            </tr>
            <tr>
                <td>3</td>
                <td>Dinesh</td>
                <td>dinesdh@gmail.com</td>
            </tr>
        </tbody>
    </table>
    <p>Govind Ballabh Khan.</p>
    <p>My Name is khan</p>
    <p>I am not muslim i am maithil brahmin</p>
    <form>
        <label>Name: <input type="text"></label>
        <label>Password: <input type="password"></label>
        <input type="submit" value="Sign In">
    </form>
</body>
</html>                           


=========================================================================================================================================