0% found this document useful (0 votes)
14 views6 pages

Java Programming Practical Examples

The document contains several Java programs demonstrating various concepts such as calculating interest, arithmetic operations, block scope, prime number generation, room attributes, and 2D arrays. Each program includes a main method that executes specific functionalities and outputs results to the console. The examples illustrate fundamental programming techniques and object-oriented principles in Java.

Uploaded by

pdiya4023
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)
14 views6 pages

Java Programming Practical Examples

The document contains several Java programs demonstrating various concepts such as calculating interest, arithmetic operations, block scope, prime number generation, room attributes, and 2D arrays. Each program includes a main method that executes specific functionalities and outputs results to the console. The examples illustrate fundamental programming techniques and object-oriented principles in Java.

Uploaded by

pdiya4023
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

Computer Java Practical

[Link]

public class Interest


{
public static void main(String[]args)
{
double principal;
double rate;
double duration;
double maturity;
double interest;

principal=17000;
rate=9.50;
duration=3;
interest=principal*duration*rate/100;
maturity=principal+interest;

[Link]("principal amount"+principal+"rupees");
[Link]("deposit for duration of"+duration+"years");
[Link]("interest rate"+rate+"%");
[Link]("interest amount"+interest+"rupees");
[Link]("maturity amount"+maturity+"rupees");
}
}
[Link]

class ArithOperators
{
public static void main(String[]args)
{
short x=6;
int y=4;
float a=12.5f;
float b=7.2f;

[Link]("x is"+x+", y is"+y);


[Link]("x+y="+(x+y));
[Link]("x-y="+(x-y));
[Link]("x/y="+(x/y));
[Link]("x%y="+(x%y));
x=-6;
[Link]("x%y="+(x%y));
y=-4;
[Link]("x%y="+(x%y));

[Link]("a is"+a+",b is"+b);


[Link]("a/b="+(a/b));
[Link]("a/x="+(a/x));
[Link]("a%x="+(a%x));
[Link]("a%b="+(a%b));
}
}
[Link]

class Block
{
public static void main(String[]args)
{
int x=10;

blk1:
{
int y=50;
[Link]("inside the block1:");
[Link]("x:"+x);
[Link]("y:"+y);
}
blk2:
{
int y=20;
[Link]("inside the block2:");
[Link]("x:"+x);
[Link]("y:"+y);
}
[Link]("outside the block: x is"+x);
}
}
[Link]

class Prime
{
public static void main(String[]s)
{
boolean prime;
int i, last, n;

[Link]("prime numbers between 3 and 100:");


for (n=3;n<100;n=n+2)
{
if(n<4)
{
prime=true;
[Link](n);
continue;
}
prime=true;
i=3;last=(int)[Link](n);
do
{
if(n%i==0)
{
prime=false;
break;
}
i=i+2;
}while(prime&&(i<last));
if(prime)[Link](n);
}
}
}
[Link]

class Room
{
float length,width,height;
byte nWindows;
void setAttr(float l, float w, float h, byte n)
{
length=l; width=w; height=h;
nWindows=n;
}
double area()
{
return (length*width);
}
void display()
{
[Link]("/nLength:"+length);
[Link]("Width"+width);
[Link]("Height"+height);
[Link]("Numbers of Window:"+nWindows);
}
}
class RoomDemo
{
public static void main(String args[])
{
Room r1;
r1=new Room();
Room r2=new Room();
[Link]();
[Link]();
[Link](18, 12.5f, 10, (byte)2);
[Link](14, 11, 10, (byte)1);
[Link]();
[Link]();
[Link]("\n Area of room with
length"+[Link]+"width"+[Link]+"is"+[Link]());
[Link]("\n Area of room with
length"+[Link]+"width"+[Link]+"is"+[Link]());
}
}
[Link]

class Array2D
{
public static void main (String[]s)
{
int marks1[] [];
marks1= new int[5][3];
int marks2[][]=new int[5][3];
int [][]marks3=new int[5][3];
int marks4[][]={{50,60,70},{35,30,50},
{70,75,80},{80,85,90},{50,50,55}};
int[][]marks5={{50,60,70},{35,30,50},
{70,75,80},{80,85,90},{50,50,55}};
[Link]("2-D Array marks1\n:");
display(marks1,5,3);
[Link]("2-D Array marks2:\n");
display(marks2,5,3);
[Link]("2-D Array marks3:\n");
display(marks3,5,3);
[Link]("2-D Array marks4:\n");
display(marks4,5,3);
[Link]("2-D Array marks5:\n");
display(marks5,5,3);
}
static void display (int arr[][],int rows, int cols)
{
for (int i=0;i<rows;i++)
{
for(int j=0;j<cols; j++)
{
[Link](arr[i][j]+"\t");
}
[Link]();
}
}
}

You might also like