Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

Java 7 new feature - Binary Literals

Program to demonstrate Java 7 new feature - Binary Literals.



 

public class BinaryLiterals {

 public static void main(String[] args) {
  
  // Before Java 7
  // In order to use Binary Numbers as Integral types
  // Integer class static method parseInt was used. 
  // This method Parses the string argument as a signed 
  // integer in the radix specified by the second argument.
  int number = Integer.parseInt("1011", 2);
  
  System.out.println("The value of number is : " + number);
  
  // After Java 7
  // Binary Literals were introduced which were represented by
  // prefix such as 0b and 0B. The prefix denoted that number
  // coming after that is binary number
  int number2 = 0b1011;  // 0b or 0B
  
  System.out.println("The value of number2 is : " + number2);
  
 }

}



Output of the program : 

 

How to reverse a number in Java ?.



Program to demonstrate how to reverse a number in Java



 
import java.util.Scanner;

public class ReverseNumber {

 public static void main(String[] args) {

  Scanner scanner = new Scanner(System.in); 

  System.out.print("Enter a number to reverse : "); 

  int original = scanner.nextInt();
  int reverse = 0;
  int remainder;

  while(original != 0) { 

   remainder = original % 10; 
   reverse = reverse * 10 + remainder; 
   original = original / 10; 

  }

  System.out.println("Reverse of number is : " + reverse);


 }

}


Output of the program : 

 

JOptionPane : Message Dialog in Java Swing

Program to demonstrate working of JOptionPane's Message Dialog in Java Swing

import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

public class MessageDialogDemo {

 public static void main(String[] args) {

  JOptionPane.showMessageDialog(null, "Hello World !!!"); 
  
  JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome",
    JOptionPane.PLAIN_MESSAGE);
  
  JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome",
    JOptionPane.INFORMATION_MESSAGE);
  
  JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome",
    JOptionPane.WARNING_MESSAGE);
  
  JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome",
    JOptionPane.QUESTION_MESSAGE);
  
  JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome",
    JOptionPane.ERROR_MESSAGE);
  ImageIcon icon = new ImageIcon("CD.png");
  
  JOptionPane.showMessageDialog(null, "Hello World !!!", "Welcome", 
    JOptionPane.INFORMATION_MESSAGE, icon);  

 }

}



Output of the program :









Watch the Youtube video below for better understanding ... 


 

StringTokenizer class in Java

Program to demonstrate StringTokenizer class in Java

import java.util.StringTokenizer;

public class StringTokenizerDemo {

 public static void main(String[] args) {

  System.out.println("Using , and = as delimiter ..... ");
  String input = "First Name=Dinesh," +
           "Last Name=Varyani," +
           "Blog Name=Learn Java by Examples," +
           "Blog Url=www.hubberspot.com";

  StringTokenizer stringTokenizer = new StringTokenizer(input,"=,");

  while(stringTokenizer.hasMoreElements()) {
   String info = stringTokenizer.nextToken();
   String description = stringTokenizer.nextToken();
   System.out.println(info + " : " + description);
  }

  System.out.println();
  System.out.println("Using default delimiter which is whitespace ... ");

  input = "FirstName Dinesh " +
    "LastName Varyani " +
    "BlogName LearnJavaByExamples " +
    "BlogUrl www.hubberspot.com";

  stringTokenizer = new StringTokenizer(input);

  while(stringTokenizer.hasMoreElements()) {
   String info = stringTokenizer.nextToken();
   String description = stringTokenizer.nextToken();
   System.out.println(info + " : " + description);
  }
 }
}



Output of the program : 

 

How to implement the hashCode and equals method using Apache Commons in Java.?

Program to demonstrate how to implement the hashCode and equals method using Apache Commons in Java.
In order to use below code, you have to get Apache's commons-lang jar.

package com.hubberspot.apache.commons;

import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;

public class Author {

 private int id;
 private String name;
 private List < String > books;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

 public List < String > getBooks() {
  return books;
 }

 public void setBooks(List < String > books) {
  this.books = books;
 }

 @Override
 public int hashCode() {
  return new HashCodeBuilder()
  .append(id)
  .append(name)
  .append(books)
  .toHashCode();
 }

 @Override
 public boolean equals(final Object object) {

  if(object instanceof Author){
   final Author other = (Author) object;
   return new EqualsBuilder()
   .append(id, other.id)
   .append(name, other.name)
   .append(books, other.books)
   .isEquals();
  } else {
   return false;
  }
 }

 public static void main(String[] args) {
  // Creating Author1
  Author author1 = new Author();
  author1.setId(1234);
  author1.setName("Martin Fowler");

  List < string > books = new ArrayList < String >();
  books.add("Code Refactoring in Java");
  books.add("Java Design Patterns");

  author1.setBooks(books);

  // Creating Author2
  Author author2 = new Author();
  author2.setId(1234);
  author2.setName("Martin Fowler");

  books = new ArrayList < String >();
  books.add("Code Refactoring in Java");
  books.add("Java Design Patterns");

  author2.setBooks(books);

  // Creating Author3
  Author author3 = new Author();
  author3.setId(1235);
  author3.setName("Steve Holzner");

  books = new ArrayList < String >();
  books.add("Design Patterns for Dummies");
  books.add("Ajax for Dummies");

  author3.setBooks(books);

  System.out.println("author1.hashCode() = " + 
    author1.hashCode());
  System.out.println("author2.hashCode() = " +
    author2.hashCode());
  System.out.println("author1.equals(author2) = "
    + author1.equals(author2));

  System.out.println("author1.hashCode() = " +
    author1.hashCode());
  System.out.println("author3.hashCode() = " +
    author3.hashCode());
  System.out.println("author1.equals(author3) = " +
    author1.equals(author3));

  System.out.println("author2.hashCode() = " +
    author2.hashCode());
  System.out.println("author3.hashCode() = " +
    author3.hashCode());
  System.out.println("author2.equals(author3) = " +
    author2.equals(author3));

 }

}


Output of the program : 

 

How to generate random number in Java using Random class ?.

Program to demonstrate how to generate random number in Java.

 

package com.hubberspot.util;

import java.util.Random;

public class RandomNumberGenerator {

    public static void main(String[] args) {
    
        // 1. create a Random object
        Random random = new Random();
        int number;
        
        for(int i=1; i < = 5; i++) { 
           
            for(int j=1; j < = 5; j++) {
                // 2. nextInt(6) will return number 
                // from 0 to 5 and adding 1 to it 
                // will return number in the range
                // 1 to 6
                number = random.nextInt(6) + 1;
                System.out.print(number + " ");
            }
            System.out.println();
        }

    }

}


Output of the program : 

 

JColorChooser in Java Swing


Program to demonstrate how to use JColorChooser in Java Swing

package com.hubberspot.swing;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

class JColorChooserFrame extends JFrame {

    private JPanel panel;
    private JButton button;
    private Color color = Color.BLUE;
    
    public JColorChooserFrame() { 
      
        setTitle("JColorChooser Test Frame");
        panel = new JPanel();
        panel.setBackground(color);
        
        button = new JButton();
        button.setText("Select Color");
        
        button.addActionListener(new ActionListener() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                
                color = JColorChooser.showDialog(null, "Pick a color", color);
                
                panel.setBackground(color);
                
            }
        });
        
        
        add(panel,BorderLayout.CENTER);
        add(button, BorderLayout.SOUTH);
        
    }     
}


public class JColorChooserDemo {

    public static void main(String[] args) {

        JColorChooserFrame frame = new JColorChooserFrame();
        frame.setSize(450, 200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    }

}



Output of the program : 



 

Java 7 new feature : Creating files and directories

Program to demonstrate Java 7 new feature : Creating files and directories.

package com.hubberspot.nio;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;

public class FileCreation {

    public static void main(String[] args) {

        Path pathToDirectory = FileSystems.getDefault().getPath("C:/Ablage/dev/workspace");
        
        try {
            
            Files.createDirectories(pathToDirectory);
            System.out.println("Directory created at path : " + pathToDirectory);
        
        } catch (IOException e) {
            
            e.printStackTrace();
        }

        Path pathToFile = FileSystems.getDefault().getPath("C:/Ablage/dev/workspace/hello.pdf");
        
        try {
            
            Files.createFile(pathToFile);
            System.out.println("File created at path : " + pathToFile);
            
        } catch (IOException e) {
        
            e.printStackTrace();
        }
        
    }

}



Output of the program : 

 
 
© 2021 Learn Java by Examples Template by Hubberspot