0% found this document useful (0 votes)
5 views9 pages

Java Programming Examples for Beginners

The document contains a series of Java programs designed for educational purposes, covering various fundamental concepts such as printing output, calculating totals and percentages, checking odd/even numbers, calculating factorials, and working with arrays. It also includes examples of object-oriented programming techniques like method overloading, file handling, and user input using the Scanner class. Each program is presented with its code and a brief description of its functionality.

Uploaded by

manavkhatri911
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views9 pages

Java Programming Examples for Beginners

The document contains a series of Java programs designed for educational purposes, covering various fundamental concepts such as printing output, calculating totals and percentages, checking odd/even numbers, calculating factorials, and working with arrays. It also includes examples of object-oriented programming techniques like method overloading, file handling, and user input using the Scanner class. Each program is presented with its code and a brief description of its functionality.

Uploaded by

manavkhatri911
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Std.

:- XII-Science Java Programs

Program-1 Simple java program to print hello world.

public class hello


{
public static void main(String[] args)
{
[Link]("Hello World");
}
}

Program-2 Sample Java Program to calculate Total Mark and Pecentage

public class student


{
public static void main(String[] args)
{
int rno,phy,chem,bio;
double total,per;-
rno=1;
phy=80;
chem=70;
bio=80;
total=phy+chem+bio;
per=total/3;
[Link]("roll no="+rno);
[Link]("physics="+phy);
[Link]("chem="+chem);
[Link]("bio="+bio);
[Link]("total="+total);
[Link]("persentage="+per);
}
}

rogram -3 Java Program to check odd/even number using if else

public class ifdemo


{
public static void main(String[] args)
{
int no=53;
if(no%2==0)
{
[Link]("The number is even");
}
else
{
[Link]("The Number is odd");
}

}
}

Program -4 Java Program to calculate factorial number using for loop


public class fact
{
public static void main(String[] args)
{
int i, fact=1;
int no=5;
for(i=1;i<=no;i++)
{
fact=fact*i;
}
[Link]("Factorial of 5 is ="+fact);
}
}

Program-5 Java Program to print all Even numbers from 1 to 20 using while loop.

public class whiledemo


{
public static void main(String[] args)
{
int no=20;
int i=0;
while(i<=20)
{
if(i%2==0)
{
[Link]("Even Num="+i);
}
i++;
}
}
}

Program –6 Java Program using instance variable and instance method to calculate area of room

class room
{
float len,wid, hg;
void setattr( float l , float w, float h)
{
len=l;
wid=w;
hg=h;
}
double area()
{
return(len*len);
}
void disp()
{
[Link]("Length="+len);
[Link]("Width ="+wid);
[Link]("Hight="+hg);
}
}
public class roomdemo
{
public static void main(String[] args)
{
room r1= new room();
room r2= new room();

[Link]();
[Link]();

[Link](18.0f,12.5f,10.0f);
[Link](14.0f,11.0f,10.0f);

[Link]();
[Link]();

[Link]("area="+[Link]());
[Link]();
}
}

Program-7 Java program to calculate result of student using class variable and class method

class student
{
static int chem,bio, math;

static void set_marks(int c, int b, int m)


{
chem=c;
bio=b;
math=m;
}
static double result()
{
double total;
total=chem+math+bio;
return(total/3);
}
static void disp()
{
[Link](" Marks of Chem="+chem);
[Link](" Marks of Math="+math);
[Link](" Marks of biology="+bio);
}
}
public class studentdemo
{
public static void main(String args[])
{

[Link]("Result of student1");
student.set_marks(85,95,80);
[Link]();
[Link](" Percentge of Student1= "+[Link]());
[Link]("Result of student2");
student.set_marks(75,85,78);
[Link]();
[Link](" Percentge of Student1= "+[Link]());
}
}
Program- 8 Java Program using polymorphism ( Method Overloading)

class poly
{
static int i ;
static void printline()
{
for(i=0;i<=25;i++)
{
[Link]("& ");
}
[Link]();
}

static void printline( int n)


{
for(i=0;i<=n;i++)
{
[Link]("#");
}
[Link]();
}

static void printline(int n , char ch)


{
for(i=0;i<=n;i++)
{
[Link](ch);
}
[Link]();
}
}
public class polydemo
{
public static void main(String[] args)
{
[Link]();
[Link](50);
[Link](35,'+');
}
}

Program -9 Java program to print various pattern using constructor

class const1
{
int i ;
const1()
{
for(i=0;i<=25;i++)
{
[Link]("& ");
}
[Link]();
}

const1( int n)
{
for(i=0;i<=n;i++)
{
[Link]("#");
}
[Link]();
}

const1(int n , char ch)


{
for(i=0;i<=n;i++)
{
[Link](ch);
}
[Link]();
}
}
public class constdemo
{
public static void main(String[] args)
{
const1 c1= new const1 ();
const1c2= new const1(20);
const1 c3= new const1(20,'+');
}
}

Program -10 Java program to initialize and display single Dimension Array

class array
{
public static void main(String[] args)
{
int marks[] ={30,75,85,90,95};
for(int i =0;i<5;i++)
{
[Link](" Marks["+i+"]="+marks[i]);
}
}
}

Program-11 Java Program to find out sum and average of array element

class arrayavg
{
public static void main(String[] args)
{
int marks[] ={30,75,85,90,95};
double sum=0, avg;
for(int i =0;i<5;i++)
{
[Link](" Marks["+i+"]="+marks[i]);
sum=sum+marks[i];
}
avg=sum/5;
[Link]("Sum of Number="+sum);
[Link]("Average of Number="+avg);

}
}
Program-12 Java Program to search element from the single dimension array.
class arraysearch
{
public static void main(String[] args)
{
int marks[] ={30,70,65,90,55};
int index;
[Link]("Initial Array");
display(marks);
index=search(marks,65);
[Link]("The value 65 found at postion "+ index);
}
static void display(int m[])
{
for(int i =0;i<5;i++)
{
[Link](" Marks["+i+"]="+m[i]);
}
}
static int search(int m[] , int value)
{
for(int i=0;i<=[Link];i++)
{
if(m[i]==value)
return i;
}
return -1;
}
}

Program -13 Java Program to sort single dimension array


class arraysearch
{
public static void main(String[] args)
{
int marks[] ={30,70,65,90,55};
int index;
[Link]("Initial Array");
display(marks);
index=search(marks,65);
[Link]("The value 65 found at postion "+ index);
}
static void display(int m[])
{
for(int i =0;i<5;i++)
{
[Link](" Marks["+i+"]="+m[i]);
}
}
static int search(int m[] , int value)
{
for(int i=0;i<=[Link];i++)
{
if(m[i]==value)
return i;
}
return -1;
}
}
Program 14 : Write java program to make comparison between two string using string class method.
class stringdemo
{
public static void main(String[] args)
{
String str1= new String(" i like java");
String str2= new String(" LIKE JAVA");
String str3= new String(str1);
[Link]("String1="+str1);
[Link]("String1="+str2);
[Link]("String1="+str3);
[Link]("Equality b/W String1 & String3="+[Link](str3));
[Link]("Equality b/W String1 & String2="+[Link](str2));

[Link]("Comparsion of String");
[Link]("Comparision b/W String1 & String3="+[Link](str3));
[Link]("Comparision b/W String1 & String2="+[Link](str1));

[Link]("Equality b/W String1 & String2(Ignorecase)="+[Link](str3));


[Link]("Converstion of string1 to uppercase="+[Link]());
[Link]("Equality b/W String1 & String3="+[Link](str3));
}

Program-15 Java Program to write content into the file

import [Link].*;
class filewrite
{
public static void main(String[] args)
{
int i;
try
{
FileWriter f1= new FileWriter("[Link]");
[Link](" File writing Starts");
for(i=0;i<=10;i++)
{
[Link]("\n" +i+" --> Hello");

}
[Link]("End of file");
[Link]();
}
catch( Exception eobj)
{
[Link](eobj);
}

}
Program-16 Java Program to read content from file

import [Link].*;
class fileread
{
public static void main(String[] args)
{
int i;
char ch;
try
{
FileReader f1= new FileReader("[Link]");
while((i=[Link]()) != -1)
{
ch=(char)i;
[Link](ch);
}
}
catch( Exception eobj)
{
[Link](eobj);
}

Program 17 : Write java program which read content from file stream using FileReader class
import [Link].*;
class fileread
{
public static void main(String[] args)
{
int i;
char ch;
try
{
FileReader f1= new FileReader("[Link]");
while((i=[Link]()) != -1)
{
ch=(char)i;
[Link](ch);
}
}
catch( Exception eobj)
{
[Link](eobj);
}
}
}

Program 18 : Write Java program to take input from user using scanner class method.
import [Link].*;
import [Link].*;
class scannerdemo
{
public static void main(String[] args)
{
int number1;
int number2;
int sum=0;
Scanner kbin=null;
try
{
kbin = new Scanner([Link]);
[Link]("Enter First No:");
number1=[Link]();
[Link]("Enter Second No:");
number2=[Link]();
sum = number1 + number2;
[Link]("Answer is : " +sum);
}
catch(Exception eobj)
{
[Link](eobj);
}
}

You might also like