Give the steps to write and excute a java program?
Basic Java Program:
1.Prime Number program in java
public class PrimeExample{
public static void main(String args[]){
int i,m=0,flag=0;
int n=3;//it is the number to be checked
m=n/2;
if(n==0||n==1){
System.out.println(n+" is not prime number");
}else{
for(i=2;i<=m;i++){
if(n%i==0){
System.out.println(n+" is not prime number");
flag=1;
break;
if(flag==0) { System.out.println(n+" is prime number"); }
}//end of else
Output:
3 is prime number.
*********************************************
2.Amstrong number program
Example:
import java.util.Scanner;
import java.lang.Math;
public class ArmstsrongNumberExample
//function to check if the number is Armstrong or not
static boolean isArmstrong(int n)
int temp, digits=0, last=0, sum=0;
//assigning n into a temp variable
temp=n;
//loop execute until the condition becomes false
while(temp>0)
temp = temp/10;
digits++;
temp = n;
while(temp>0)
//determines the last digit from the number
last = temp % 10;
//calculates the power of a number up to digit times and add the resultant to the sum
variable
sum += (Math.pow(last, digits));
//removes the last digit
temp = temp/10;
//compares the sum with n
if(n==sum)
//returns if sum and n are equal
return true;
//returns false if sum and n are not equal
else return false;
//driver code
public static void main(String args[])
int num;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the limit: ");
//reads the limit from the user
num=sc.nextInt();
System.out.println("Armstrong Number up to "+ num + " are: ");
for(int i=0; i<=num; i++)
//function calling
if(isArmstrong(i))
//prints the armstrong numbers
System.out.print(i+ ", ");
Output:
Enter the limit: 999
Armstrong Number up to 999 are:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407
************************************************
3.RandomNumberExample1.java
import java.lang.Math;
public class RandomNumberExample1
public static void main(String args[])
// Generating random numbers
System.out.println("1st Random Number: " + Math.random());
System.out.println("2nd Random Number: " + Math.random());
System.out.println("3rd Random Number: " + Math.random());
System.out.println("4th Random Number: " + Math.random());
Output:
1st Random Number: 0.17434160924512265
2nd Random Number: 0.4297410090709448
********************************************
4.CreateObjectExample1.java
public class CreateObjectExample1
void show()
System.out.println("Welcome to javaTpoint");
}
public static void main(String[] args)
//creating an object using new keyword
CreateObjectExample1 obj = new CreateObjectExample1();
//invoking method using the object
obj.show();
Output:
Welcome to javaTpoint
*********************************************
Describe how to create classes and objects?
program:
class Lamp {
// stores the value for light
// true if light is on
// false if light is off
boolean isOn;
// method to turn on the light
void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);
// method to turnoff the light
void turnOff() {
isOn = false;
System.out.println("Light on? " + isOn);
class Main {
public static void main(String[] args) {
// create objects led and halogen
Lamp led = new Lamp();
Lamp halogen = new Lamp();
// turn on the light by
// calling method turnOn()
led.turnOn();
// turn off the light by
// calling method turnOff()
halogen.turnOff();
Output:
Light on? true
Light on? false
====================================================================
Example: Create objects inside the same class
class Lamp {
// stores the value for light
// true if light is on
// false if light is off
boolean isOn;
// method to turn on the light
void turnOn() {
isOn = true;
System.out.println("Light on? " + isOn);
public static void main(String[] args) {
// create an object of Lamp
Lamp led = new Lamp();
// access method using object
led.turnOn();
output:
Light on? true
============================================================
explain one dimentional arrays example using standard methods?
program:
class OnedimensionalStandard
{
public static void main(String args[])
int[] a=new int[3];//declaration
a[0]=10;//initialization
a[1]=20;
a[2]=30;
//printing array
System.out.println("One dimensional array elements are");
System.out.println(a[0]);
System.out.println(a[1]);
System.out.println(a[2]);
Output:
10
20
30
Two Dimentional Arrays Examples
// Two Dimensional Array in Java Example
package ArrayDefinitions;
public class TwoDimentionalArray {
public static void main(String[] args) {
int[][] a = { {15, 25, 35}, {45, 55, 65} };
int[][] b = {{12, 22, 32}, {55, 25, 85} };
int[][] Sum = new int[2][3];
int rows, columns;
for(rows = 0; rows < a.length; rows++) {
for(columns = 0; columns < a[0].length; columns++) {
Sum[rows][columns] = a[rows][columns] + b[rows]
[columns];
System.out.println("Sum Of those Two Arrays are: ");
for(rows = 0; rows < a.length; rows++) {
for(columns = 0; columns < a[0].length; columns++) {
System.out.format("%d \t", Sum[rows][columns]);
System.out.println("");
Output:
Sum of those two Arrays are:
27 47 67
100 80 150
==========================================================================
Example of default constructor:
//Java Program to create and call a default constructor
class Bike1{
//creating a default constructor
Bike1(){System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b=new Bike1();
Output:
Bike is created
=================================================
2nd example: Default Constructor
//Let us see another example of default constructor
//which displays the default values
class Student3{
int id;
String name;
//method to display the value of id and name
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
Output:
0 null
0 null
==================================================================
Example of parameterized constructor
In this example, we have created the constructor of Student class that have two parameters.
We can have any number of parameters in the constructor.
//Java Program to demonstrate the use of the parameterized constructor.
class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
//method to display the values
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
Output:
111 Karan
222 Aryan
==============================================
Constructor Overloading in Java
In Java, a constructor is just like a method but without return type. It can also be overloaded
like Java methods.
Constructor overloading in Java is a technique of having more than one constructor with
different parameter lists. They are arranged in a way that each constructor performs a
different task. They are differentiated by the compiler by the number of parameters in the
list and their types.
Example of Constructor Overloading
//Java program to overload constructors
class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
void display(){System.out.println(id+" "+name+" "+age);}
public static void main(String args[]){
Student5 s1 = new Student5(111,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
Test it Now
Output:
111 Karan 0
222 Aryan 25
======================================================================
Example of static variable
//Java Program to demonstrate the use of static variable
class Student{
int rollno;//instance variable
String name;
static String college ="ITS";//static variable
//constructor
Student(int r, String n){
rollno = r;
name = n;
//method to display the values
void display (){System.out.println(rollno+" "+name+" "+college);}
//Test class to show the values of objects
class TestStaticVariable1{
public static void main(String args[]){
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
Output:
111 Karan ITS
222 Aryan ITS
===============================================
Java static method
If you apply static keyword with any method, it is known as static method.
A static method belongs to the class rather than the object of a class.
A static method can be invoked without the need for creating an instance of a class.
A static method can access static data member and can change the value of it.
Example of static method
//Java Program to demonstrate the use of a static method.
class Student{
int rollno;
String name;
static String college = "ITS";
//static method to change the value of static variable
static void change(){
college = "BBDIT";
//constructor to initialize the variable
Student(int r, String n){
rollno = r;
name = n;
}
//method to display values
void display(){System.out.println(rollno+" "+name+" "+college);}
//Test class to create and display the values of object
class TestStaticMethod{
public static void main(String args[]){
Student.change();//calling change method
//creating objects
Student s1 = new Student(111,"Karan");
Student s2 = new Student(222,"Aryan");
Student s3 = new Student(333,"Sonoo");
//calling display method
s1.display();
s2.display();
s3.display();
Test it Now
Output:111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
Another example of a static method that performs a normal calculation
//Java Program to get the cube of a given number using the static method
class Calculate{
static int cube(int x){
return x*x*x;
public static void main(String args[]){
int result=Calculate.cube(5);
System.out.println(result);
Test it Now
Output:125
======================================================
Final keyword example program
class Bike9{
final int speedlimit=90;//final variable
void run(){
speedlimit=400;
public static void main(String args[]){
Bike9 obj=new Bike9();
obj.run();
}//end of class
Test it Now
Output:Compile Time Error
==============================================
Java final method
If you make any method as final, you cannot override it.
Example of final method
class Bike{
final void run(){System.out.println("running");}
}
class Honda extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda honda= new Honda();
honda.run();
Test it Now
Output:Compile Time Error
========================================================
Java final class
If you make any class as final, you cannot extend it.
Example of final class
final class Bike{}
class Honda1 extends Bike{
void run(){System.out.println("running safely with 100kmph");}
public static void main(String args[]){
Honda1 honda= new Honda1();
honda.run();
Test it Now
Output:Compile Time Error
===========================================
Q) Is final method inherited?
Ans) Yes, final method is inherited but you cannot override it. For Example:
class Bike{
final void run(){System.out.println("running...");}
class Honda2 extends Bike{
public static void main(String args[]){
new Honda2().run();
Test it Now
Output:running...
==================================================