Java Tutorials

Java sample programs, Java examples, slideshows on Java, presentations on Java, videos tutorials on Java, Java source codes, Free Java Source Code, Free Java Programs, Free java Tutorials, Free Java Interview Questions, Free Mock Test on Java, Free Java Projects, Open Source Java Projects, etc.

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

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/

Using For Each in Java

Objective:

  • How to use For Each in Java?
  • How to use advance for in Java?
  • How to use advance for loop in Java Programming Language?
  • Types of For loop in Java

 

Source Code:

[sourcecode lang=”Java”]

class ForEachExample
{
public static void main(String args[])
{
//declaring an array
int a[]={1,2,3,4,5,6};

// Old way
System.out.println("Old way");
for(int i=0; i<a.length;i++)
{
System.out.println(a[i]);
}

//New way: for-each loop
System.out.println("For each");
for(int i:a)
{
System.out.println(i);
}
}
}

[/sourcecode]

Pattern Programs in Java – Pattern 3

Pattern 3 :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

 

Java Code for above pattern :

[sourcecode lang=”java”]

import java.util.Scanner;

&nbsp;

public class Pattern3

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern….!!!");

//Printing upper half of the pattern

for (int i = 1; i <= rows; i++)

{

for (int j = 1; j <= i; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//Printing lower half of the pattern

for (int i = rows-1; i >= 1; i–)

{

for (int j = 1; j <= i; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//Closing the resources

sc.close();

}

}

[/sourcecode]

Output :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1

 

Pattern Programs in Java – Pattern 2

Pattern 2 :

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Java Program for the above pattern:

[sourcecode language=”java”]

import java.util.Scanner;

public class MainClass

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

//Taking rows value from the user
System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern….!!!");

for (int i = 1; i <= rows; i++)

{

for (int j = 1; j <= i; j++)

{

System.out.print(i+" ");

}

System.out.println();

}

//Close the resources
sc.close();

}

}

[/sourcecode]

Pattern Programs In Java – Pattern 1

Q. Write a Java program to print given pattern.

Pattern 1 :

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
1 2 3 4 5 6 7

 

Java Program:

[sourcecode language=”java”]

import java.util.Scanner;

public class MainClass

{

public static void main(String[] args)

{

Scanner sc = new Scanner(System.in);

//Taking rows value from the user

System.out.println("How many rows you want in this pattern?");

int rows = sc.nextInt();

System.out.println("Here is your pattern….!!!");

for (int i = 1; i <= rows; i++)

{

for (int j = 1; j <= i; j++)

{

System.out.print(j+" ");

}

System.out.println();

}

//Close the resources

sc.close();

}

}

[/sourcecode]

Learning with uCertify

My Experience with uCertify :

I had an access to uCertify 1Z0-808 – Oracle Certified Associate Java SE 8 Programmer course, and I think it’s a pretty good supplement study guide for the course. The questions and flash cards make good revision and I am especially fond of the exam mode and exam readiness assessment that really helps to determine the how well one grasps the information and concepts. For a technical course of this nature, some labs would help to drive home the real-world application of the skills learn

ucertify

About Exam:

Oracle Java SE 8 Programmer I exam is designed to certify the competency of Java professionals in developing Java applications, mastering  Java data types, operators and decision control structures, and understanding OOPs concepts of Java. Java SE 8 significantly changes the way Java Programmers write code. Earning a Java SE 8 Certification gives you the tools to make the most of the new features within Java SE 8.

The Java SE 8 Oracle Certified Associate (OCA) certification helps you build a basic understanding of Java, while expanding your knowledge of general programming.

The ideal candidate who would earn this certification typically has a technical background and wants to improve programming skills, or may be new to object-oriented programming and Java.

 

About uCertify:

uCertify is a leading provider of online Test preps, Courses & Labs for IT and Project Management certification exams.Our courses are authored by the industry experts (with 15+ years of experience)  and we have a fan base of  600,000 happy customers in 200 countries worldwide.

 

Parcelable Vs Serializable

Android Tutorials

Android Tutorials

Objectives :

  • What is Parcelable?
  • What is Serializable?
  • What are the differences between Parcelable and Serializable?
  • Explain : Parcelable Vs. Serializable
  • What is Parcelable and Serializable in Android?

 

Parcelable Vs. Serializable

Parameter

Parcelable  Serializable
What is it? Google developped Parcelable for better performance on android Serializable is a standard Java interface
Purpose For marshaling and unmarshaling Java objects For marshaling and unmarshaling Java objects
Package android.os java.io
Speed 10 times Faster than Serializable because it is optimized for usage on android development Slower than Parcelable
Implementation Time Requires more time than Serializable Requires very less time as compared to Parcelable
Degree of Implementation Difficult Easier than Parcelable
Memory Less temporary objects Serializable interface create a lot of temporary objects and cause quite a bit of garbage collection
Use with Intent Parcelable array can be pass via Intent in android

Cannot be used with Intent

 

How to change editor full screen in NetBeans IDE?

Objectives :

  • How to change editor full screen in NetBeans IDE?

NetBeans IDE is having a new and great feature where we can full screen the code editor window.

This small video tutorial shows how to change editor full screen in NetBeans IDE?

[youtube=”https://www.youtube.com/watch?v=w49znamwczw&feature=youtu.be”]

Being a trainer, I need to share screen with code and sometime only code nothing else. This is one of the most useful feature for me while teaching Java technologies and UI technologies such as HTML5, CSS, JS, etc. This is why I love NetBeans IDE a lot.

Hope this tutorial is helpful for you as well.