Java Training

Java program to generate random numbers

Objectives:

  • Write a Java program to to generate random numbers and print the same
  • Write a program to generate random numbers
  • Write a program to generate random numbers in Java

Java Program:

[sourcecode lang="java"]

import java.util.Scanner;
import java.util.Random;

class GenerateRandomNumber
{
public static void main(String[] args)
{
int maxRange;

//create objects
Scanner SC = new Scanner(System.in);
Random rand = new Random();

System.out.print("Please enter maximum range: ");
maxRange=SC.nextInt();

for(int i=1; i<=10; i++)
{
System.out.println(rand.nextInt(maxRange));
}
}
}

[/sourcecode]

Output:

Please enter maximum range: 500
467
61
100
449
68
316
445
224
54
498

Find duplicate characters in string

Objectives:

  • Write a Java program to find duplicate characters in the string and print the duplicate characters
  • Write a program to find duplicate characters in String

Java Program:

[sourcecode lang="java"]
public class DuplicateCharacters {
    public static void main(String args[]) {

        String str = "NITIN";
        int count = 0;

        char input[] = str.toCharArray();
        System.out.println("Duplicate Characters are:");

        for (int i = 0; i &amp;lt; str.length(); i++) {
            for (int j = i + 1; j &amp;lt; str.length(); j++) {
                if (input[i] == input [j]) {
                    System.out.println(input[j]);
                    count++;
                    break;
                }
           }
        }
    }
}
[/sourcecode]

Output:

Duplicate Characters are: N I

Moved by Java

PC: @mimaraslan

Happy Birthday Java | PC: @mimaraslan

As #Java is turning 25 years tomorrow, here is my story with Java. I Love Java, I am #MovedByJava

2004 – Learned Java
2005 – Taught Java to some of my college friends and juniors
2006 – Started Institute to teach Java and related technologies
2007 – Joined a company as Java Developer, developed various projects
2008 – Joined Java User Group Nagpur (India) – known as #JUGNAGPUR2004 – Learned Java
2005 – Taught Java to some of my college friends and juniors
2006 – Started Institute to teach Java and related technologies
2007 – Joined a company as Java Developer, developed various projects
2008 – Joined Java User Group Nagpur (India) – known as #JUGNAGPUR
2009 – Developed numerous project on Java
2010 – Started side hustle by taking small Java and Web projects with 5 clients
2010 – Community Contributor at #NetBeans (#NetCAT)
2010 – Started writing blogs on Java and NetBeans
2010 – NetBeans Platform Training and Certification
2011 – Received the title of “Outstanding Programmer”
2011 – Published first Java Video Tutorial on YouTube
2011 – Cracked Oracle Certifications in a month – OCJP & OCWCD
2011 – Conducted Corporate Training for India’s biggest Training Company – #CDAC
2011 – Joined an MNC as Senior Corporate Trainer for Java, conducted several induction programs and corporate batches. The biggest batch size was 1250
2011 – Community Contributor at NetBeans (NetCAT)
2011 – Delivered Seminar on Java at 5 Colleges & Universities
2012 – Completed a big project on Java for a first overseas client
2012 – attended my first #JavaOne Conference in India
2012 – Conducted Workshops and delivered Seminar on Java and #Android at 20 Colleges and Universities
2013 – Published my first book on Java and NetBeans (Step by Step guide to developing different Java applications using NetBeans IDE)
2013 – Speaker at JavaOne India, conducted a Hands-On-Lab on #JavaEE and #HTML5 using NetBeans IDE
2013 – Conducted Workshops and delivered Seminar on Java and Android at 20 Colleges and Universities
2013 – Conducted Induction Training on Java for 5 corporate companies
2013 – Started my company with few clients and developed 10+ Java Projects
2013 – Community Contributor at NetBeans (NetCAT)
2014 – Conducted Workshops and delivered Seminar on Java and Android at 30 Colleges and Universities
2014 – Conducted Induction Training on Java for 10 corporate companies
2014 – Community Contributor at NetBeans (NetCAT)
2014 – Developed 15+ Java and Android Projects
2015 – Got Selected as “NetBeans Dream Team” Member
2015 – First International Training in Gulf on Java and Android (30 days)
2015 – Awarded as “Best Entrepreneur for Corporate Training in India”
2015 – Awarded as “Individual Professional”
2015 – Developed a self-paced video course on Java and Android for a multinational company headquartered in the UK
2015 – Conducted Workshops and delivered Seminar on Java and Android at 30 Colleges and Universities
2015 – Conducted Induction Training on Java, JavaEE, #Hibernate, and #Struts for 10+ corporate companies
2015 – Developed 10+ Java and Android Projects
2016 – Conducted Workshops and delivered Seminar on Java and Android at 40 Colleges and Universities
2016 – Conducted Induction Training on Java, JavaEE, #Hibernate, #Struts, and #Spring for 15+ corporate companies
2016 – Community Contributor at NetBeans (NetCAT)
2016 – Developed 10+ Java, Android, and Web Projects
2017 – Conducted Workshops and delivered Seminar on Java and Android at 30+ Colleges and Universities
2017 – Conducted Induction Training on Java, JavaEE, #Hibernate, #Struts, and #Spring for 5+ corporate companies
2017 – Developed 15+ Java, Android, and Web Projects
2018 – Conducted Workshops and delivered Seminar on Java and Android at 20 Colleges and Universities
2018 – Conducted Induction Training on Java, JavaEE, #Hibernate, #Struts, and #Spring for 10+ corporate companies
2018 – Developed 5+ Java, Android, and Web Projects
2018 – Lead for Java User Group, Pune
2019 – Conducted Workshops and delivered Seminar on Java and Android at 30 Colleges and Universities
2019 – Conducted Induction Training on Java, JavaEE, #Hibernate, #Struts, and #Spring for 10+ corporate companies
2019 – Conducted Java Induction Training in Gulf
2019 – Developed 10+ Java, Android, and Web Projects
2019 – Lead for Java User Group Nagpur
2020 – Conducted Workshops and delivered Seminar on Java and Android at 5+ Colleges and Universities
2020 – Developed 2 Java and Android Projects
2020 – Conducting Online Training on Java
2020 – Writing another Book on Java
2020 – Recording video courses on Java

PC: @mimaraslan

Love you Java and Wish you a very happy birthday

Thank you, Java

Aatul Palandurkar

Passing String in Switch Case in Java

Objectives:

  • How to use switch case in Java?
  • How to use switch case with string in Java?
  • How to pass string literals in switch case in Java?

 

Source Code:

[sourcecode lang=”java”]

public class SwitchWithStringExample

{
public static void main(String[] args)
{
String str = "two";
switch(str)
{
case "one":
System.out.println("one");
break;
case "two":
System.out.println("two");
break;
case "three":
System.out.println("three");
break;
default:
System.out.println("no match");
} // switch close
} // main close
} // class close

[/sourcecode]

 

Steps to run above Java code:

Consider that we have saved code at C drive (c:\>) and the file is saved as SwitchWithStringExample.java

  1. Compilation : c:\> javac SwitchWithStringExample.java
  2. Interpretation : c:\> java SwitchWithStringExample

 

Output:

two

 

We conduct Java Training and Workshop for Corporates and College Students, if interested, please write to us on kloudsancyber@gmail.com

 

Aatul Palandurkar
International Trainer, and Author, NetBeans Dream Team Member

https://www.facebook.com/aatulpalandurkar
https://www.instagram.com/aatulpalandurkar/

Oracle Java Certifications For You

Oracle Java Certifications For You

The Java programming language is among the most widely used software development platforms in the IT industry nowadays. It is extensively being used and appreciated by individuals as well as organizations to transform their innovative ideas into working software solutions.

 

The Java programming language is among the most widely used software development platforms in the IT industry nowadays. It is extensively being used and appreciated by individuals as well as organizations to transform their innovative ideas into working software solutions.

 

Oracle Corporation is constantly working in the area of Java platform development. They provide Java certifications as well. Each of these java certifications verifies a certain level of expertise and knowledge of the Java platform belonging to specific domains.

 

Adding the best Java certification to your resume will help you grab the attention of the employer. A Java certification can validate your knowledge and expertise in working with Java. Preparing for a Java Certification can help you enhance your Java programming skills.

 

Oracle Java Certifications

 

Before diving in detail into the various Java certification courses offered by the Oracle Corporation, let’s first skim through a summarized introduction. Oracle Java certifications are categorized as follows:

 

Entry Level

  • Oracle Certified Associate Java Programmer (OCAJP)

 

Professional Level

  • Oracle Certified Professional Java Programmer (OCPJP)
  • Oracle Certified Professional Java Application Developer (OCPJAD)

 

Master/Architect Level

  • Oracle Certified Master Java Enterprise Architect (OCMJEA)

 

 

The Oracle OCP Java SE 8 Programmer I certification will validate your strong Java programming skills. To earn this certification you will have to pass the 1Z0-808 exam. The 1Z0-808 exam objectives include topics such as Java basics, Java data types, array, loop contracts, methods and encapsulation and so on.

 

 

 

The Oracle OCP Java SE 8 Programmer II certification is a professional-level certification designed to validate the foundation skills of database administration. The OCP 1Z0-809 exam objectives include exceptions and assertions; generics and collections; Java Class Design, Java File I/O (NIO.2), Java I/O Fundamentals, Java Stream API, and localization.

 

 

 

Oracle OCP Java SE 11 Developer certification covers a wide range of available Java 11 programming concepts. To earn this certification you will have to clear the 1Z0-815 exam. The Oracle 1Z0-815 exam covers topics such as Encapsulation, creating and using methods; creating simple Java programs, describing and using objects and classes; handling exceptions, Java technology, reusing implementations through Inheritance, 

understanding modules, and more.

As we all know that Java is employed for developing a wide array of applications. This programming language is used in creating server-side applications to mobile applications. So, if you are thinking about starting your career in this field, then you must consider these certifications. We offer courses that will help you prepare for the certification exams.

 

Java Program to Convert Binary to Decimal

Objectives :

  • Binary to Decimal Conversion
  • Converting Binary to Decimal
  • Write a program to convert binary to decimal
  • Write a program to convert binary number to decimal format
  • Write a program to convert binary to decimal using Scanner class
  • Write a program to convert binary to decimal, take input using Scanner class

Java Program :

[sourcecode lang=”java”]

import java.util.Scanner;

public class BinaryToDecimal {

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print(&quot;Enter binary number: &quot;);
String binary = scanner.nextLine();
int decimal = binaryToDecimal(binary);
System.out.println(&quot;Decimal equivalent of &quot;+ binary +&quot; is &quot;+ decimal);
}

private static int binaryToDecimal(String binary) {
final int base = 2;
int decimal = 0;
for (int i = 0; i &lt; binary.length(); i++) {
if (binary.charAt(i) == ‘0’) {
decimal += 0 * Math.pow(base, binary.length() – i – 1);
} else if (binary.charAt(i) == ‘1’) {
decimal += 1 * Math.pow(base, binary.length() – i – 1);
} else {
System.out.println(&quot;Invalid Binary Number&quot;);
System.out.println(&quot;Binary Number Contains only 0’s or 1’s&quot;);
System.exit(0);
}
}
return decimal;
}
}

[/sourcecode]

Output :
Enter binary number: 1110
Decimal equivalent of 1110 is 14

Java Program to convert Decimal to Binary

Objectives :

  • Decimal to Binary Conversion
  • Converting Decimal to Binary
  • Converting Decimal Number to Binary format
  • Write a program to convert decimal to binary
  • Write a program to convert decimal number to binary format
  • Write a program to convert positive decimal number to binary format
  • Write a program to convert decimal to binary using Scanner class
  • Write a program to convert decimal to binary, take input using Scanner class

 

Java Program to convert Decimal to Binary : 

[sourcecode lang=”java”]
public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int[25];
int n = 0;
// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}
// Print Binary number
for(int i = n-1;i >= 0;i–){
System.out.print(binary[i]);
}
}

public static void main(String[] args){
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(14);
}
}
[/sourcecode]

Output :

1110

 

Java Program to convert Decimal to Binary using Scanner class :

[sourcecode lang=”java”]

import java.util.Scanner;

public class DecimalToBinary {

public void toBinary(int number){
int binary[] = new int[14];
int n = 0;

// Convert Decimal to Binary
while(number > 0){
binary[n++] = number%2;
number = number/2;
}

// Print Binary Number
for(int i = n-1;i >= 0;i–){
System.out.print(binary[i]);
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num= scanner.nextInt();
DecimalToBinary obj = new DecimalToBinary();
obj.toBinary(num);

}
}

[/sourcecode]

Output :

Enter a number: 14

1110

Java Program to find prime number upto N number

Objectives :

  • Write a Java program to find prime number upto N number
  • Write a Java program to print prime number upto N number
  • Write a Java program to find prime number upto N number using Command Line Arguments
  • Write a Java Program to find prime number upto N number via Command Line Arguments
  • Write a Java program to find prime number upto N number using Scanner class

 

Java Program / Code :

Method 1 : Java Program to find Prime Number upto N number using Scanner class

[sourcecode lang=”Java”]

import java.util.Scanner;
class PrimeNumber
{
public static void main(String[] args)
{
int n,p;
Scanner s=new Scanner(System.in);
System.out.println(“Enter number : ”);
n=s.nextInt();
for(int i=2;i<n;i++)
{
p=0;
for(int j=2;j<i;j++)
{
if(i%j==0)
p=1;
}
if(p==0){
System.out.println(i);
}
}
}
}
[/sourcecode]

Method 2 : Java Program to find Prime Number upto N number using Scanner class and writing function to find prime number

Method isPrime() for checking if a number is prime or not

[sourcecode lang=”java”]
public class PrimeNumber{
public boolean isPrime(int num) {
if ( num < 2 ){
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if ( num % i == 0 ) {
return false;
}
}
return true;
}
}

// Mock Test Class to test above code
import java.util.Scanner;

public class Demo
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);

System.out.println("Please enter a number: ");
int num = scanner.nextInt();

if ( num < 2 ) {
System.out.println("\n There are no Prime Numbers available");
System.exit(0);
}
System.out.println("\n Prime Numbers from 1 to "+ num);
PrimeNumber primeNum = new PrimeNumber();

for (int i = 3; i <= num; i++) {
if ( primeNum.isPrime(i) ) {
System.out.print(", " + i);
}
}
}
}
[/sourcecode]

Method 3 : Java Program to find Prime Number upto N number using Command line input

[sourcecode lang=”Java”]

class PrimeNumber
{
public static void main(String[] args)
{
int n,p;
n=Integer.parseint(args[0]);
for(int i=2;i&lt;n;i++)
{
p=0;
for(int j=2;j&lt;i;j++)
{
if(i%j==0)
p=1;
}
if(p==0){
System.out.println(i);
}
}
}
}
[/sourcecode]

Steps to run above program via command line :

  1. Compilation : C:\JavaPrograms>javac PrimeNumber.java
  2. Interpretation : C:\JavaPrograms>java PrimeNumber 20

Output :

2

3

5

7

11

13

17

19

Program to find Sum of Digits in Java

Objectives :

  • Write a program to compute sum of digits of a given number.
  • Write a program to find sum of digits of a given number.
  • Write a program to calculate sum of digits of a given number.
  • Write a program to compute sum of digits of a given integer number.
  • Write a program to compute sum of digits of a number entered via Command Line.
  • Write a program to compute sum of digits of a given number. Take input from Command Line.
  • Write a program to compute sum of digits of a given number. Take input using Scanner class.

Following is the Java Program to compute Sum of Digits of a given integer number;

Method 1 : Java Program to find Sum of Digits when number is entered from command line

[sourcecode lang=”java”]

class SumOfDigits
{
public static void main(String args[])
{
int n;
int a=0;
int sum=0;

//taking integer number from command line and parsing the same
n=Integer.parseInt(args[0]);

while(n!=0)
{
a=n%10;
n=n/10;
sum=sum+a;
}
System.out.println("Sum of digits: " + sum);
}
}

[/sourcecode]

Steps to run above program via command line :

  1. Compilation : C:\JavaPrograms>javac SumOfDigits.java
  2. Interpretation : C:\JavaPrograms>java SumOfDigits 12345

Output : Sum of digits: 15

Method 2 : Java Program to find Sum of Digits if input is taken using Scanner class

[sourcecode lang=”java”]

import java.util.Scanner;

public class SumOfDigits {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n;
int a=0;
System.out.print("Enter a positive number: ");
n = in.nextInt();

if (n <= 0)
System.out.println("You have entered a negative number.");
else {
int sum = 0;

while (n != 0) {

a=n%10;
n=n/10;
sum=sum+a;
}
System.out.println("Sum of digits: " + sum);
}
}
}

[/sourcecode]

Steps to run above program via command line :

  1. Compilation : C:\JavaPrograms>javac SumOfDigits.java
  2. Interpretation : C:\JavaPrograms>java SumOfDigits 12345

Output :

Trial 1 : With positive number

Enter a positive number: 12345

Sum of digits: 15

Trial 2 : With negative number

Enter a positive number: -12345

You have entered a negative number.

Simple Interest Example in Java

Starting a series of some Java programs as many students were asking to share the programs. Here is the first program of the series.

Objectives:

  • Write a Program that calculates and prints the simple interest using the formula : Simple Interest = PTR/100
  • How to calculate Simple Interest in Java?
  • Write a program to calculate Simple Interest in Java.
  • Write a Java program that calculates and prints the simple interest using the formula : “SimpleInterest = PTR/100” and input values P, T, R should be accepted as command line.
  • How to use Command Line Arguments in Java?
  • How to pass values from command line in Java?
  • How to take input in Java from console?

 

This particular program can be written in various ways, we will try to write the solution program in different ways here and one can try to understand the difference between them.

Method 1 :

[sourcecode lang=”java”]
import java.util.Scanner;

public class SimpleInterestExample {

public static void main(String[] args) {

int p,t,r, result;

Scanner sc = new Scanner(System.in);

System.out.println(&quot;Enter the Value of P : &quot;);
p = sc.nextInt();

System.out.println(&quot;Enter the Value of T : &quot;);
t = sc.nextInt();

System.out.println(&quot;Enter the Value of R : &quot;);
r = sc.nextInt();

result = (p*t*r)/100;
System.out.println(&quot;Interest is : &quot; + result);
}
}
[/sourcecode]

This one is the simplest way to write the program to find simple interest in Java. Here we have simply created the object of Scanner class for taking input. Scanner class was introduced in Java 6. To use Scanner class we will have to import the class from java.util package.

Method 2:

Now we will write a Java program that calculates and prints the simple interest using the Scanner class again but with object oriented programming approach.

[sourcecode lang=”java”]
import java.util.Scanner;

public class SimpleInterestExample
{
double principalAmount = 0;
double interestRate = 0;
double term = 0;
double simpleInterest = 0;

public void calculateSimpleInterest()
{
Scanner input = new Scanner(System.in);

System.out.print(&quot;Enter the Principal amount : &quot;);
principalAmount = input.nextDouble();

System.out.print(&quot;Enter the Rate As a decimal : &quot;);
interestRate = input.nextDouble();

System.out.print(&quot;Enter the amount of time in years : &quot;);
term = input.nextDouble();

simpleInterest = (principalAmount * interestRate * term) / 100;
}

public void displaySimpleInterest()
{
System.out.println(&quot;The Simple Interest is : &quot; + simpleInterest);
}
}
[/sourcecode]

Mock Test Program for above code so that we can test the code. keep both the classes in same package and run the Mock Test Program or MockTestProgram.java

[sourcecode lang=”java”]
public class MockTestProgram{

public static void main(String[]args);
{
SimpleInterestExample simpleInt = new SimpleInterestExample();
simpleInt.calculateSimpleInterest();
simpleInt.displaySimpleInterest();
}
}
[/sourcecode]

Method 3:

Now we will write a Java program that calculates and prints the simple interest using the formula : “SimpleInterest = PTR/100” and input values P, T, R should be accepted as command line.

[sourcecode lang=”java”]
import java.util.Scanner;

public class SimpleInterestExample {

public static void main(String[] args) {
double p,t,r, result;

p = Double.parseDouble(args[0]);
t = Double.parseDouble(args[1]);
r = Double.parseDouble(args[2]);

Scanner sc = new Scanner(System.in);

System.out.println(&quot;Enter the Value of P : &quot;);
p = sc.nextDouble();

System.out.println(&quot;Enter the Value of T : &quot;);
t = sc.nextDouble();

System.out.println(&quot;Enter the Value of R : &quot;);
r = sc.nextDouble();

result = (p*t*r)/100;
System.out.println(&quot;Interest is : &quot; + result);
}
}
[/sourcecode]

 

I will be sharing more programs soon as many students were asking for programs which were carried out during Java training and were asked in assignments after Java training. By the time enjoy coding in Java and use NetBeans, it will ease your life.