Copyrighted By @Curious_Coder
JAVA PROGRAMS
WITH OUTPUT
Copyrighted By @Curious_Coder
Copyrighted By @Curious_Coder
Program no 1
1) WAP using nested switch case which
covers the following operators with
expressions :
(a) Mathematical
(b) Logical
(c) Relational
PROGRAMME:- A
public class Switch{
public static void main(String []args){
[Link]("[Link]
OPERATORS\[Link] OPERATORS\[Link]
OPERATORS\n\t\t(Choice=1)\n"); //Main menu
int choice=1;
int a=10,b=5,c;
switch(choice) //Main switch case
{
Copyrighted By @Curious_Coder
case 1 : //arithmatic operators
[Link]("[Link]\[Link]\[Link]
on\[Link]\nVALUES OF a AND b ARE 10 AND 5
RESPECTIVELY\n\t\t(Choice=3)\n");
int aChoice=3;
switch(aChoice) //Nested switch case
{
case 1: //addition
c=a+b;
[Link]("Addtion of a and b is "+ c+"\n");
break;
case 2: //subtraction
c=a-b;
[Link]("Subtraction of a and b is "+
c+"\n");
break;
Copyrighted By @Curious_Coder
case 3: //Multiplication
c=a*b;
[Link]("Multiplication of a and b is "+
c+"\n");
break;
case 4: //division
c=a/b;
[Link]("Divisiom of a and b is "+ c+"\n");
break;
default:
[Link]("Please enter right choice");
}
break;
case 2 : //logical operator
[Link]("[Link] AND\[Link] OR\[Link]
Operator\n");
Copyrighted By @Curious_Coder
int lchoice=1;
switch(lchoice)
{
case 1: // AND operator
int age=25;
if(age>18 && age<50)
[Link]("(AGE=25)\nYou are eligible for
this job.\n");
break;
case 2: //0r operator
int salary=3000;
if(salary<5000 || salary>2000)
[Link]("(SALARY=3000)\n The job is
offordable.\n");
break;
case 3: //not operator
int Age=20;
Copyrighted By @Curious_Coder
if(Age!=18 || Age>18)
[Link]("(Age=20)\nYou are not eligible
for voting\n");
break;
default:
[Link]("Please enter right choice");
}
break;
case 3: //Relational operator
[Link]("[Link] than operator\[Link]
than operator\[Link] than or equal operator\[Link]
than or equal operator\n");
int lChoice=4;
switch(lChoice)
{
case 1: //greater than operator
Copyrighted By @Curious_Coder
int aage=20;
if(aage > 18)
[Link]("(Age=20) You are eligible for
having a driving licens");
break;
case 2: //less than operator
int A=10;
if(A < 18)
[Link]("(Age=10) You are not eligible for
having a driving licens");
break;
case 3: //greater than equal to
int p=18;
if(p >= 18)
[Link]("(Age=18) You are eligible for
having a driving licens");
break;
Copyrighted By @Curious_Coder
case 4: //less than or equal to
int q=13;
if(q <= 18)
[Link]("(Age=13) You are not eligible for
having a driving licens");
break;
default:
[Link]("Please enter right choice");
}
break;
default:
[Link]("Please enter right choice");
}
}
}
Copyrighted By @Curious_Coder
OUTPUT:
Copyrighted By @Curious_Coder
PROGRAM : 2
WAP to print following patterns :
(a *****
****
***
**
*
(b) *
**
***
****
*****
****
***
**
*
Programme:-
public class Pattern{
public static void main(String []args){
[Link]("PATTERN NO: 1");
for (int i= 5; i>= 1; i--)
Copyrighted By @Curious_Coder
{
for (int j=5; j>i;j--)
{
[Link](" ");
}
for (int k=1;k<=i;k++)
{
[Link]("*");
}
[Link]("");
}
[Link]("\n\nPATTERN NO: 2");
for (int i = 1; i <= 5; i++)
{
for (int j = 5; j > i; j--)
{
Copyrighted By @Curious_Coder
[Link](" ");
}
for (int k = 1; k <= i; k++)
{
[Link]("*");
}
[Link]();
}
for (int i = 1; i <= 5-1; i++)
{
for (int j = 1; j <= i; j++)
{
[Link](" ");
}
for (int k = 5-1; k >= i; k--)
{
[Link]("*");
}
Copyrighted By @Curious_Coder
[Link]();
}
}
}
OUTPUT:
Copyrighted By @Curious_Coder
Program : 3
3) WAP to find
(a) palendrome
(b) fibonacci series
(c) leap year
(d) even/odd number
Programmer:
(a) palindrome :
public class Palindrome{
public static void main(String []args){
[Link]("---------------Palindrome ---------- \n");
int r,sum=0,temp;
int n=345;
temp=n;
while(n>0){
r=n%10;
Copyrighted By @Curious_Coder
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
[Link]("palindrome number ");
else
[Link]("not palindrome");
}
}
OUTPUT :
Copyrighted By @Curious_Coder
(b) fibonacci series
public class Fibonacci{
public static void main(String []args){
[Link]("---------------
Fibonacci series ------------------------ \n");
int n1=0,n2=1,n3, ,count=10;
[Link](n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
[Link](" "+n3);
n1=n2;
n2=n3;
}
}
}
OUTPUT:
Copyrighted By @Curious_Coder
Copyrighted By @Curious_Coder
c) leap year
public class LeapYear{
public static void main(String []args){
[Link]("\n---------------Leap year-----------------
\n(year=2020)");
int year=2020;
if ((year % 4 == 0) && (year % 100!= 0))
[Link]("Entered year is a leap year");
else if(year%400 == 0)
[Link]("Entered year is leap year");
else
[Link]("Entered year is year is not a leap
year");
}
}
OUTPUT:
Copyrighted By @Curious_Coder
(d) even/odd number
public class EvenOdd{
public static void main(String []args){
[Link]("\n---------------Even/Odd number------------
-----\n(num=10)");
int num=10;
if(num % 2 == 0)
[Link](num + " is even");
else
[Link](num + " is odd");
Copyrighted By @Curious_Coder
}
}
OUTPUT:
Copyrighted By @Curious_Coder
PROGRAM : 4
4) WAP to perform following matrix operations
:
(a) addtion
(b) transpose
(c) multiplication
//PROGRAM NO: 4
public class Matrix{
public static void main(String []args){ //main
function
int mat1[][]={ {1,2},{3,8} } , mat2[][]={ {3,8},{6,9}};
//function call
add(mat1,mat2);
multiply(mat1,mat2);
trans(mat1);
}
Copyrighted By @Curious_Coder
public static void add(int arr1[][],int arr2[][]){
//funtion for matrix addition
[Link]("----------------MTRIX ADDITION--------------
\n");
int result[][] = new int[2][2];
int i,j;
for(i=0;i<2;i++){
for(j=0;j<2;j++){
result[i][j]=arr1[i][j]+arr2[i][j];
[Link](result[i][j] );
}
[Link]();
}
}
public static void multiply(int ary1[][],int ary2[][]){
//function for matrix multiplication
Copyrighted By @Curious_Coder
[Link]("----------------MTRIX Multiplication---------
-----\n");
int c[][]=new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
c[i][j]=0;
for(int k=0;k<2;k++){
c[i][j]+=ary1[i][k]*ary2[k][j];
}
[Link](c[i][j]+" ");
}
[Link]();
}
}
public static void trans(int mat1[][]){ //function for
transpose of matrix
Copyrighted By @Curious_Coder
[Link]("-------------------Matric Transpose------------
--\n");
int result[][]=new int[2][2];
for(int i=0;i<2;i++){
for(int j=0;j<2;j++){
result[i][j]=mat1[j][i];
[Link](result[i][j]);
}
[Link]();
}
}
}
OUTPUT :
Copyrighted By @Curious_Coder
@Curious_Coder
Practical No-1
• Program 1: Java Program to Design Login
Window Using AWT Controls
CODE:
import [Link]; import
[Link].*;
import [Link];
import [Link];
public class login extends Applet
{
Label title = new Label("Login Page");
Label username = new Label("Username");
Label password = new Label("Password");
TextField tusername = new TextField(20);
TextField tpassword = new TextField(10);
Button loginn = new Button("Login");
Button reset = new Button("Reset");
@Curious_Coder
TextField er = new TextField();
public void init() {
setSze(500, 500); setLayout(null);
//Setting Bounds
[Link](150, 50, 200, 100);
[Link](20, 150, 150, 100);
[Link](20, 240, 150, 100);
[Link](180, 180, 250, 40);
[Link](180, 270, 250, 40);
[Link](100, 350, 100, 40);
[Link](250, 350, 100, 40); [Link](180,
400, 250, 40);
//Setting Fonts
[Link](new Font("Lucida",[Link],34));
[Link](new Font("Lucida",[Link],24));
[Link](new Font("Lucida",[Link],24));
[Link](new Font("Lucida",[Link],24));
[Link](new Font("Lucida",[Link],24));
[Link]('*'); add(username);
add(title);
@Curious_Coder
add(password);
add(tusername);
add(tpassword);
add(loginn);
add(reset);
add(er);
setVisible(true);
}
}
/*
<APPLET CODE= [Link] WIDTH=500 HEIGHT=500>
</APPLET>
*/
OUTPUT:
@Curious_Coder
Java Program to Design Registration
• Program 2:
Form Using AWT Controls with ActionListener
CODE:
import [Link]; import
[Link].*;
import [Link];
import [Link];
public class Registration extends Applet implements
ActionListener
{
Label title = new Label(" Registration Page");
Label username = new Label("Username");
Label password = new Label("Password");
TextField tusername = new TextField(20);
TextField tpassword = new TextField(10);
Button Registration = new Button(" Registration");
Button reset = new Button("Reset"); TextField
er = new TextField();
public void init()
{
setSize(500, 500);
setLayout(null);
//Setting Bounds
@Curious_Coder
[Link](150, 50, 200, 100);
[Link](20, 150, 150, 100);
[Link](20, 240, 150, 100);
[Link](180, 180, 250, 40);
[Link](180, 270, 250, 40);
[Link](100, 350, 100, 40);
[Link](250, 350, 100, 40); [Link](180,
400, 250, 40);
//Setting Fonts
[Link](new Font("Lucida",[Link],34));
[Link](new Font("Lucida",[Link],24));
[Link](new Font("Lucida",[Link],24));
[Link](new Font("Lucida",[Link],24));
[Link](new Font("Lucida",[Link],24));
[Link](new Font("Lucida",[Link],24));
[Link]('*');
add(username); add(title);
add(password);
add(tusername);
add(tpassword); add(
Registration); add(reset);
add(er); setVisible(true);
[Link](this);
[Link](this);
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
@Curious_Coder
String user = [Link]();
String pass = [Link]();
if([Link]()== Registration)
{
if([Link]("1806087") &&
[Link]("1806087"))
{
Frame f1=new Frame("REGISRATION FORM ");
[Link](true);
[Link](700,700);
//Labels
Label name = new Label("Full Name :");
Label email = new Label("Email :");
Label addr = new Label("Address :");
Label gender = new Label("Gender :");
Label Dept = new Label("Department :");
Label Hobbies = new Label("Hobbies :");
TextField tname = new TextField(30);
TextField temail = new TextField(20);
Checkbox crick = new Checkbox("Cricket");
Checkbox footb = new Checkbox("Football");
Checkbox tenn = new Checkbox("Tennies");
Checkbox read = new Checkbox("Reading");
TextArea taddr = new TextArea();
CheckboxGroup gen = new CheckboxGroup();
Checkbox male = new Checkbox("Male",gen,false);
@Curious_Coder
Checkbox female = new
Checkbox("Female",gen,false);
Choice department= new Choice();
[Link]("Computer Engineering");
[Link]("Electrical Engineering");
[Link]("Mechanical Engineering");
[Link]("EntC Engineering");
[Link]("Civil Engineering");
[Link]("Information Technology");
[Link]("Computer Engineering");
[Link](null);
//Bounds
[Link](30,50,140,25);
[Link](170,50,360,30);
[Link](30,100,100,25);
[Link](170,100,290,30);
[Link](30,160,120,25);
[Link](170,160,260,70);
[Link](30,240,120,25);
[Link](170,240,80,25);
[Link](290,240,100,25);
[Link](30,300,140,25);
[Link](190,300,280,250);
[Link](30,360,140,25);
[Link](190,360,100,25);
[Link](190,390,150,25);
[Link](190,420,150,25);
@Curious_Coder
[Link](190,450,150,25);
//Changing Fonts [Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
@Curious_Coder
[Link](new Font("Lucida",[Link],24));
[Link](new
Font("Lucida",[Link],24));
//adding elements
[Link](name);
[Link](tname); [Link](email);
[Link](temail);
[Link](addr); [Link](taddr);
[Link](gender);
[Link](male);
[Link](female); [Link](Dept);
[Link](department);
[Link](Hobbies); [Link](crick);
[Link](footb);
[Link](tenn); [Link](read);
}
else
{
[Link]("Wrong Username Or
Password");
}
}
else if([Link]()==reset)
{
[Link]("");
[Link]("");
}
@Curious_Coder
}
}
/*
<APPLET CODE= [Link] WIDTH=500 HEIGHT=500>
</APPLET>
*/
@Curious_Coder
OUTPUT:
@Curious_Coder
@Curious_Coder
•
Program 3: Java Program to Design Calculator
Using AWT Controls with ActionListener
CODE:
import [Link]; import
[Link].*; import
[Link]; import
[Link]; import
[Link].*;
public class Calculator extends Applet implements ActionListener
{
TextField disp1,disp2,disp3;
Button plus,minus,divide,multiply,equalto,clear;
String num1,num2,results,Operator;
Double n,n2,result; public void
init()
{
setSize(320,350);
disp1 = new TextField("0"); disp2
@Curious_Coder
= new TextField("0"); disp3 = new
TextField("Result here"); plus =
new Button("+");
minus = new Button("-"); divide = new
Button("/"); multiply = new Button("*");
equalto = new Button("="); clear = new
Button("clear"); setLayout(null);
[Link](new Font("Lucida",[Link],20));
[Link](new Font("Lucida",[Link],20));
[Link](new Font("Lucida",[Link],20));
[Link](5,5,300,50);
[Link](5,55,300,50);
[Link](5,110,300,50);
[Link](10,190,70,70);
[Link](90,190,70,70);
[Link](10,270,70,70);
[Link](90,270,70,70);
[Link](190,180,70,80);
[Link](190,270,70,80); add(disp1); add(disp2);
add(disp3); add(plus); add(minus);
@Curious_Coder
add(divide); add(multiply); add(equalto);
add(clear); [Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
}
@Override public void
actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if([Link]()== plus)
{
num1 = [Link]().toString();
Operator="+"; n=
[Link](num1);
num2 = [Link]().toString();
n2 = [Link](num2);
result = n + n2 ;
}
@Curious_Coder
else if([Link]()== minus)
{
num1 = [Link]().toString();
Operator="-"; n=
[Link](num1);
num2 = [Link]().toString();
n2 = [Link](num2);
result = n - n2 ;
}
else if([Link]()== divide)
{
num1 = [Link]().toString();
Operator="/"; n=
[Link](num1);
num2 = [Link]().toString();
n2 =[Link](num2);;
result = n / n2 ;
}
else if([Link]()== multiply)
{
@Curious_Coder
num1 = [Link]().toString();
Operator="*"; n=
[Link](num1);
num2 = [Link]().toString();
n2 =[Link](num2);
result = n * n2 ;
}
else if([Link]()== equalto)
{
results = [Link](result);
[Link](num1+Operator+num2+"="+resul
ts);
}
else if([Link]()==clear)
{
[Link]("0");
[Link]("0");
[Link](" Result here");
}
}
@Curious_Coder
}
/*
<applet code = "[Link]" width="320"
height="350">
</applet>
*/
OUTPUT:
@Curious_Coder
@Curious_Coder
@Curious_Coder
• Program 4: Write a code on following output
(Modifying the program to move a ball in
response to up/down/left/right buttons, as well as
the 4 arrow keys)
CODE:
import [Link]; import
[Link].*; import
[Link]; import
[Link]; import
[Link]; import
[Link];
public class Movetheball extends Applet implements
ActionListener,KeyListener { int x,y;
Button up,down,left,right;
public void init() {
[Link](this);
setSize(400,400);
@Curious_Coder
setBackground([Link]); x=150;
y=100;
up = new Button("UP");
down= new Button("DOWN");
left = new Button("LEFT");
right = new Button("RIGHT");
setLayout(null);
[Link](this);
[Link](150,250,70,25);
[Link](150,350,70,25);
[Link](100,300,70,25);
[Link](200,300,70,25);
[Link](this);
[Link](this);
[Link](this);
[Link](this); add(up);
add(down); add(left);
add(right);
@Curious_Coder
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if([Link]()==up)
{
y=y-20;
repaint();
}
else if([Link]()== down)
{
y=y+20;
repaint();
}
else if([Link]()==left)
@Curious_Coder
{
x=x-20;
repaint();
}
else if([Link]()==right)
{
x=x+20;
repaint();
}
@Override public void
keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Curious_Coder
@Override
public void keyPressed(KeyEvent e) {
int KeyCode = [Link]();
switch(KeyCode)
{
case KeyEvent.VK_UP:
y=y-20;
repaint(); break;
case KeyEvent.VK_DOWN:
y=y+20;
repaint();
break;
case KeyEvent.VK_LEFT:
x=x-20;
repaint();
break; case
KeyEvent.VK_RIGHT:
@Curious_Coder
x=x+20;
repaint();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void paint(Graphics g)
{
[Link]([Link]);
[Link](x, y, 80, 80);
}
}
/*
<applet code = "[Link]" width="400" height="400">
@Curious_Coder
</applet>
*/
OUTPUT:
@Curious_Coder
• Program 5: Write a code on following output(To
click on the button change the colour of the
ball )
CODE:
import [Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link]; import
[Link];
public class ChangeColor extends Applet implements
ActionListener {
Button change_color;
int i=1; public void
init()
{
setSize(400,400); setBackground([Link]);
@Curious_Coder
change_color = new Button("CHANGE COLOR");
setLayout(null);
change_color.setBounds(150,330,120,50);
change_color.addActionListener(this);
add(change_color);
}
public void paint(Graphics g)
{
if(i==1)
{
[Link]([Link]);
}
else if(i==2)
{
[Link]([Link]);
}
else if(i==3)
{
[Link]([Link]);
}
else if(i==4)
@Curious_Coder
{
[Link]([Link]);
}
else if(i==5) {
[Link]([Link]);
i=1;
}
[Link](100, 100, 200,200);
}
@Override
public void actionPerformed(ActionEvent e) {
if([Link]()==change_color)
{
i=i+1;
repaint();
}
}
}
/*
<applet code = "[Link]" width="400" height="400">
</applet>
@Curious_Coder
*/
OUTPUT:
@Curious_Coder