Programs to demonstrate java basics

Posted: June 10, 2012 by bhumika289 in JAVA

Basic java program:

package javaapplication1;
public class JavaApplication1
{

/* This is my first java program.
* example of multi-line comments.
*/

public static void main(String[] args)
{
System.out.println(“This is a basic java program. Welcome to the programming world!!!!”);

//example of single line comment
}
}

Output:

This is a basic java program. Welcome to the programming world!!!!

Program to demonstrate objects and classes:

package Employee;

class Worker
{
String name;
int age;

void setData(String name1, int age1)
{
name=name1;
age=age1;
}
}
public class Employee
{
public static void main(String[] args)
{
int age=21;
Worker w=new Worker();
w.setData(“Makarand”, age);
System.out.print(“Name:”+w.name+”\nAge:”+w.age);
}
}

output:

Name:Makarand

Age:21

Program to demonstrate use of ‘if’ statement:

class Largest
{
public static void main(String args[])
{
int x1=10,x2=21,x3=3;
int large;
large = x1;
if (x2 > large)
large = x2;
if (x3 > large)
large = x3;
System.out.println(“Largest number=” + large);
}
}

Output:

Largest number=21

Program to demonstrate use of ‘if else’ statement:

class result
{
public static void main(String args[])
{
int marks = 40;
if (marks<40)
System.out.println(“The student has failed”);
else
System.out.println(“The student has Passed”);
}
}

Output:

The student has Passed

Program to demonstrate use of ‘if- else- if’ statement:

class result
{
public static void main(String args[])
{
int marks = 62;
if (marks<40)
System.out.println(“Fails”);
else if(marks>=40 && marks<60)
System.out.println(“Pass class”);
else if(marks>=60 && marks<75)
System.out.println(“First class”);
else
System.out.println(“Distinction”);
}
}

Output:

First class

Program to demonstrate use of nested if statement:

class Maximum
{
public static void main(String args[])
{
int a=10,b=21,c=3;
int max=0;
if (a>b) //1
{
if(a>c) //2
max=a;
else //2
max=c;
}
else //1
{
if(b>c) //3
max=b;
else //3
max=c;
}
System.out.println(“maximum=” +max);
}
}

Output:

maximum=21

Program to demonstrate use of ‘switch’ statement:

class Calculate
{
public static void main(String[] args)
{
int a=10,b=2,ch;
double ans;
ch=’3′;
switch(ch)
{
case 1:
ans=a+b;
System.out.println(“a+b=” + ans);
break;

case 2:
ans=a-b;
System.out.println(“a-b=” + ans);
break;

case 3:
ans=a*b;
System.out.println(“a*b=” + ans);
break;

case 4:
ans=a/b;
System.out.println(“a/b=” + ans);
break;

default:
System.out.println(“Incorrect choice”);
}
}
}

output:

a*b=20

Program to demonstrate use of ‘for’ loop:

class tableof5

{
public static void main(String args[])
{
int i;
for (i=1;i<=10;i++)
{
System.out.println(“5*”+i+”=”+(5*i));
}
}
}

output:

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

Program to demonstrate use of ‘while’ loop:

class tableof5

{
public static void main(String args[])
{
int i=1;
while(i<=10)
{
System.out.println(“5*”+i+”=”+(5*i));
i++;
}
}
}

output:

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

Program to demonstrate use of ‘do-while’ loop:

class tableof5

{
public static void main(String args[])
{
int i=1;
do
{
System.out.println(“5*”+i+”=”+(5*i));
i++;
}
while(i<=10);
}
}

output:

5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50

Program to demonstrate use of ‘break’ statement:

class break1
{
public static void main(String args[])
{
int i = 1;
while (i<=10)
{
System.out.println(i);
i++;
if (i==5)
{
break;
}
}
}
}

output:

12345

Program to demonstrate use of ‘labelled break’ statement:

class break2
{
public static void main (String args[])
{
boolean t=true;
first:
{
second:
{
third:
{
System.out.println(“third block, stmt1”);
if(t)
break third;
System.out.println(“third black, stmt2”);
}
System.out.println(“second block, stmt1”);
if(t)
break second;
System.out.println(“second black, stmt2”);
}
System.out.println(“first block, stmt1”);
if(t)
break first;
System.out.println(“first black, stmt2”);
}
}
}

output:

third block, stmt1
second block, stmt1
first block, stmt1

Program to demonstrate use of ‘continue’ statement:

class continue1
{
public static void main(String args[])
{
for (int i=1; i<=10; i++)
{
if (i%2 == 0)
continue;
System.out.println(i);
}
}

}

output:

13579

Program to demonstrate use of ‘return’ statement:

class Worker
{
String name;
int age;

void setData(String name1, int age1)
{
name=name1;
age=age1;
}
String getName()
{
return name;
}
int getAge()
{
return age;
}
}
public class Employee
{
public static void main(String[] args)
{
int age=21;
Worker w=new Worker();
w.setData(“Makarand”, age);
System.out.print(“Name:”+w.getName()+”\nAge:”+w.getAge());
}
}

output:

Name:Makarand
Age:21

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s