Java Programs
Java Programs
JAVA PROGRAMS
WITH OUTPUT
Copyrighted By @Curious_Coder
Copyrighted By @Curious_Coder
Program no 1
PROGRAMME:- A
public class Switch{
System.out.println("1.ARITHMATIC
OPERATORS\n2.LOGICAL OPERATORS\n3.RELATIONAL
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
System.out.println("1.Addition\n2.Subtraction\n3.Multiplicati
on\n4.Division\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;
System.out.println("Addtion of a and b is "+ c+"\n");
break;
case 2: //subtraction
c=a-b;
System.out.println("Subtraction of a and b is "+
c+"\n");
break;
Copyrighted By @Curious_Coder
case 3: //Multiplication
c=a*b;
System.out.println("Multiplication of a and b is "+
c+"\n");
break;
case 4: //division
c=a/b;
System.out.println("Divisiom of a and b is "+ c+"\n");
break;
default:
System.out.println("Please enter right choice");
}
break;
int lchoice=1;
switch(lchoice)
{
case 1: // AND operator
int age=25;
if(age>18 && age<50)
System.out.println("(AGE=25)\nYou are eligible for
this job.\n");
break;
if(Age!=18 || Age>18)
System.out.println("(Age=20)\nYou are not eligible
for voting\n");
break;
default:
System.out.println("Please enter right choice");
}
break;
int aage=20;
if(aage > 18)
System.out.println("(Age=20) You are eligible for
having a driving licens");
break;
default:
System.out.println("Please enter right choice");
}
break;
default:
System.out.println("Please enter right choice");
}
}
}
Copyrighted By @Curious_Coder
OUTPUT:
Copyrighted By @Curious_Coder
PROGRAM : 2
(a *****
****
***
**
*
(b) *
**
***
****
*****
****
***
**
*
Programme:-
public class Pattern{
{
for (int j=5; j>i;j--)
{
System.out.print(" ");
}
for (int k=1;k<=i;k++)
{
System.out.print("*");
}
System.out.println("");
}
System.out.print(" ");
}
for (int k = 1; k <= i; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= 5-1; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(" ");
}
for (int k = 5-1; k >= i; k--)
{
System.out.print("*");
}
Copyrighted By @Curious_Coder
System.out.println();
}
}
}
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{
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
System.out.println("palindrome number ");
else
System.out.println("not palindrome");
}
}
OUTPUT :
Copyrighted By @Curious_Coder
System.out.println("---------------
Fibonacci series ------------------------ \n");
int n1=0,n2=1,n3, ,count=10;
System.out.print(n1+" "+n2);
for(i=2;i<count;++i)
{
n3=n1+n2;
System.out.print(" "+n3);
n1=n2;
n2=n3;
}
}
}
OUTPUT:
Copyrighted By @Curious_Coder
Copyrighted By @Curious_Coder
c) leap year
public class LeapYear{
System.out.println("\n---------------Leap year-----------------
\n(year=2020)");
int year=2020;
if ((year % 4 == 0) && (year % 100!= 0))
System.out.println("Entered year is a leap year");
else if(year%400 == 0)
System.out.println("Entered year is leap year");
else
System.out.println("Entered year is year is not a leap
year");
}
}
OUTPUT:
Copyrighted By @Curious_Coder
System.out.println("\n---------------Even/Odd number------------
-----\n(num=10)");
int num=10;
if(num % 2 == 0)
System.out.println(num + " is even");
else
System.out.println(num + " is odd");
Copyrighted By @Curious_Coder
}
}
OUTPUT:
Copyrighted By @Curious_Coder
PROGRAM : 4
//PROGRAM NO: 4
System.out.println("----------------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];
System.out.println(result[i][j] );
}
System.out.println();
}
}
System.out.println("----------------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];
}
System.out.print(c[i][j]+" ");
}
System.out.println();
}
}
System.out.println("-------------------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];
System.out.println(result[i][j]);
}
System.out.println();
}
}
}
OUTPUT :
Copyrighted By @Curious_Coder
@Curious_Coder
Practical No-1
CODE:
import java.applet.Applet; import
java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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
//Setting Bounds
title.setBounds(150, 50, 200, 100);
username.setBounds(20, 150, 150, 100);
password.setBounds(20, 240, 150, 100);
tusername.setBounds(180, 180, 250, 40);
tpassword.setBounds(180, 270, 250, 40);
loginn.setBounds(100, 350, 100, 40);
reset.setBounds(250, 350, 100, 40); er.setBounds(180,
400, 250, 40);
//Setting Fonts
title.setFont(new Font("Lucida",Font.PLAIN,34));
username.setFont(new Font("Lucida",Font.PLAIN,24));
tusername.setFont(new Font("Lucida",Font.PLAIN,24));
username.setFont(new Font("Lucida",Font.PLAIN,24));
tpassword.setFont(new Font("Lucida",Font.PLAIN,24));
tpassword.setEchoChar('*'); add(username);
add(title);
@Curious_Coder
add(password);
add(tusername);
add(tpassword);
add(loginn);
add(reset);
add(er);
setVisible(true);
}
}
/*
<APPLET CODE= login.class WIDTH=500 HEIGHT=500>
</APPLET>
*/
OUTPUT:
@Curious_Coder
CODE:
import java.applet.Applet; import
java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//Setting Bounds
@Curious_Coder
}
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
@Curious_Coder
f1.setLayout(null);
//Bounds
name.setBounds(30,50,140,25);
tname.setBounds(170,50,360,30);
email.setBounds(30,100,100,25);
temail.setBounds(170,100,290,30);
addr.setBounds(30,160,120,25);
taddr.setBounds(170,160,260,70);
gender.setBounds(30,240,120,25);
male.setBounds(170,240,80,25);
female.setBounds(290,240,100,25);
Dept.setBounds(30,300,140,25);
department.setBounds(190,300,280,250);
Hobbies.setBounds(30,360,140,25);
crick.setBounds(190,360,100,25);
footb.setBounds(190,390,150,25);
tenn.setBounds(190,420,150,25);
@Curious_Coder
read.setBounds(190,450,150,25);
//Changing Fonts name.setFont(new
Font("Lucida",Font.PLAIN,24));
tname.setFont(new
Font("Lucida",Font.PLAIN,24));
email.setFont(new
Font("Lucida",Font.PLAIN,24));
temail.setFont(new
Font("Lucida",Font.PLAIN,24));
addr.setFont(new
Font("Lucida",Font.PLAIN,24));
taddr.setFont(new
Font("Lucida",Font.PLAIN,24));
gender.setFont(new
Font("Lucida",Font.PLAIN,24));
male.setFont(new Font("Lucida",Font.PLAIN,24));
female.setFont(new
Font("Lucida",Font.PLAIN,24));
Dept.setFont(new
Font("Lucida",Font.PLAIN,24));
department.setFont(new
Font("Lucida",Font.PLAIN,24));
Hobbies.setFont(new
Font("Lucida",Font.PLAIN,24));
crick.setFont(new
Font("Lucida",Font.PLAIN,24));
footb.setFont(new
Font("Lucida",Font.PLAIN,24));
@Curious_Coder
tenn.setFont(new Font("Lucida",Font.PLAIN,24));
read.setFont(new
Font("Lucida",Font.PLAIN,24));
//adding elements
f1.add(name);
f1.add(tname); f1.add(email);
f1.add(temail);
f1.add(addr); f1.add(taddr);
f1.add(gender);
f1.add(male);
f1.add(female); f1.add(Dept);
f1.add(department);
f1.add(Hobbies); f1.add(crick);
f1.add(footb);
f1.add(tenn); f1.add(read);
}
else
{
er.setText("Wrong Username Or
Password");
}
}
else if(e.getSource()==reset)
{
tusername.setText("");
tpassword.setText("");
}
@Curious_Coder
}
}
/*
<APPLET CODE= Registration.class 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:
num1 = disp1.getText().toString();
Operator="*"; n=
Double.parseDouble(num1);
num2 = disp2.getText().toString();
n2 =Double.parseDouble(num2);
result = n * n2 ;
}
else if(e.getSource()== equalto)
{
results = String.valueOf(result);
disp3.setText(num1+Operator+num2+"="+resul
ts);
}
else if(e.getSource()==clear)
{
disp1.setText("0");
disp2.setText("0");
disp3.setText(" Result here");
}
}
@Curious_Coder
}
/*
<applet code = "Calculator.class" width="320"
height="350">
</applet>
*/
OUTPUT:
@Curious_Coder
@Curious_Coder
@Curious_Coder
CODE:
import java.applet.Applet; import
java.awt.*; import
java.awt.event.ActionEvent; import
java.awt.event.ActionListener; import
java.awt.event.KeyEvent; import
java.awt.event.KeyListener;
setBackground(Color.green); x=150;
y=100;
up = new Button("UP");
down= new Button("DOWN");
left = new Button("LEFT");
right = new Button("RIGHT");
setLayout(null);
up.addKeyListner(this);
up.setBounds(150,250,70,25);
down.setBounds(150,350,70,25);
left.setBounds(100,300,70,25);
right.setBounds(200,300,70,25);
up.addActionListener(this);
down.addActionListener(this);
left.addActionListener(this);
right.addActionListener(this); add(up);
add(down); add(left);
add(right);
@Curious_Coder
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==up)
{
y=y-20;
repaint();
}
else if(e.getSource()== down)
{
y=y+20;
repaint();
}
else if(e.getSource()==left)
@Curious_Coder
{
x=x-20;
repaint();
}
else if(e.getSource()==right)
{
x=x+20;
repaint();
}
}
@Curious_Coder
@Override
public void keyPressed(KeyEvent e) {
x=x+20;
repaint();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.fillOval(x, y, 80, 80);
}
}
/*
<applet code = "Movetheball.class" width="400" height="400">
@Curious_Coder
</applet>
*/
OUTPUT:
@Curious_Coder
CODE:
import java.applet.Applet; import
java.awt.Button; import
java.awt.Color; import
java.awt.Graphics; import
java.awt.event.ActionEvent; import
java.awt.event.ActionListener;
public class ChangeColor extends Applet implements
ActionListener {
Button change_color;
int i=1; public void
init()
{
setSize(400,400); setBackground(Color.BLACK);
@Curious_Coder
{
g.setColor(Color.ORANGE);
}
else if(i==5) {
g.setColor(Color.PINK);
i=1;
}
g.fillOval(100, 100, 200,200);
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==change_color)
{
i=i+1;
repaint();
}
}
}
/*
<applet code = "ChangeColor.class" width="400" height="400">
</applet>
@Curious_Coder
*/
OUTPUT:
@Curious_Coder