0% found this document useful (0 votes)
139 views152 pages

JAVA Practical Journal

The document contains 17 programming assignments completed by Nikita Jadhav, a BCA student. Each assignment includes the code for a Java program to solve a given problem and the corresponding output. The problems involve basic Java concepts like data types, operators, decision making, loops, strings and user input.

Uploaded by

Nikita Jadhav
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)
139 views152 pages

JAVA Practical Journal

The document contains 17 programming assignments completed by Nikita Jadhav, a BCA student. Each assignment includes the code for a Java program to solve a given problem and the corresponding output. The problems involve basic Java concepts like data types, operators, decision making, loops, strings and user input.

Uploaded by

Nikita Jadhav
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
You are on page 1/ 152

JAVA

JOURNAL
NAME:
NIKITA JADHAV
ROLL NO: 17
BCA SEM 4
(TOTAL ASSIGNMENT: 60)

1
Assignment 1: Program to print "Hello World"

Code:

public class Hello

public static void main (String args[])

System.out.println("Hello World!");

2
Output:

3
Assignment 2: Program for Incremental Operator

Code:

public class Incremental

public static void main (String args[])

int a=10,b=12;

System.out.println(++a);

System.out.println(a++);

System.out.println((a++)+(b--));

4
Output:

5
Assignment 3: Program for Decremental Operator

Code:

public class Decremental

public static void main (String args[])

int a=10,b=12;

System.out.println(--a);

System.out.println(a--);

System.out.println((a--)+(b++));

6
Output:

7
Assignment 4: Program to Compare two numbers

Code:

public class Compare2

public static void main (String args[])

int a=10,b=12;

if (a>b)

System.out.println(a+" is Greater than "+b);

else

System.out.println(a+" is Less than "+b);

8
Output:

9
Assignment 5: Program to Compare three numbers

Code:

public class Compare3

public static void main(String[] args)

int a=10, b=20, c=40;

if(a > b && a > c)

System.out.println("Largest number is: "+a);

else if(b > c)

System.out.println("Largest number is: "+b);

else

System.out.println("Largest number is: "+c);

10
Output:

11
Assignment 6: Program to Print numbers from 1 to 10
using for loop

Code:

public class Numbers1to10

public static void main(String[] args)

for(int i=1;i<=10;i++){

System.out.println(i);

12
Output:

13
Assignment 7: Program to Print even numbers between 2
to 50

Code:

public class Even2to50

public static void main(String[] args)

for(int i=2;i<=50;i++){

if(i%2==0){

System.out.println(i);

14
Output:

15
Assignment 8: Program to Print 10 to 1 using while loop

Code:

public class Numbers10to1

{public static void main(String[] args)

int j=10;

while(j>=1){

System.out.println(j);

j--;

16
Output:

17
Assignment 9: Program to Print odd numbers between 50
to 100 using do while loop

Code:

public class Odd50to100

public static void main(String[] args)

int i=50;

do{

if(i%2!=0)

System.out.println(i);

i++;

}while(i<=100);

18
Output:

19
Assignment 10: Program to Print a Star Pyramid

Code:

import java.io.*;

public class StarPyramidPattern

public static void printTriagle(int n)

for (int i=0; i<n; i++)

for (int j=n-i; j>1; j--)

System.out.print(" ");

for (int j=0; j<=i; j++ )

System.out.print("* ");

System.out.println();

public static void main(String args[])

int n = 5;

printTriagle(n);

20
}

Output:

21
Assignment 11: Program to Print given pattern

Code:

import java.util.Scanner;

public class GivenPattern

public static void main(String[] args)

int rows = 3;

for (int i = 1; i <= rows; i++)

for (int j = 1; j <= i; j++)

if(j%2 == 0)

System.out.print("-");

else

System.out.print("+");

System.out.println();

}}

22
Output:

23
Assignment 12: Program to accept numeric value as age
from user and check validation for voting

Code:

import java.util.Scanner;

public class AgeVerification

public static void main(String[] args)

int age;

Scanner sc = new Scanner(System.in);

System.out.print("Enter your age=");

age = sc.nextInt();

if (age >= 18)

System.out.println("You are eligible for vote.");

else

System.out.println("You are not eligible for vote.");

24
Output:

25
Assignment 13: Program to accept total obtained marks
and find out the grade epending upon the conditions

Code:

import java.util.Scanner;

public class Grading

public static void main(String[] args)

int T;

Scanner sc = new Scanner(System.in);

System.out.print("Enter your total marks=");

T = sc.nextInt();

if (T >=90 && T <=100)

System.out.println("You Grade is A+");

else if (T >=80 && T <=89)

System.out.println("You Grade is A");

else if (T >=60 && T <=79)

System.out.println("You Grade is B");

26
else if (T >=50 && T <=69)

System.out.println("You Grade is C");

else if (T >=36 && T <=59)

System.out.println("You Grade is D");

else if (T <=35)

System.out.println("You Grade is F");

else

System.out.println("PLEASE ENTER VALID MARKS!!!");

27
Output:

28
Assignment 14: Program to accept 3 subject marks from the
user and calculate the Totalmarks, Percentage and Average

Code:

import java.util.Scanner;

public class TAPofUser

public static void main(String args[])

float eng, math, comp;

double total, average, percentage; Scanner op=new

Scanner(System.in); /* Input marks of all five subjects */

System.out.println("<<<Enter marks of Three subjects>>>");

System.out.print("Enter marks of English subjects:");

eng=op.nextFloat();

System.out.print("Enter marks of maths

subjects:"); math=op.nextFloat();

System.out.print("Enter marks of computers

subjects:"); comp=op.nextFloat();

/* Calculate total, average and percentage

*/ total = eng + math + comp; average =

(total / 3.0);

percentage = (total / 300.0) * 100;

29
/* Print all results */

System.out.println("Total marks ="+total);

System.out.println("Average marks = "+average);

System.out.println("Percentage = "+percentage);

Output:

30
Assignment 15: Program to show String function in JAVA

Code:

public class StringExample

public static void main(String args[])

String s1="java";//creating string by java string

literal char ch[]={'s','t','r','i','n','g','s'};

String s2=new String(ch);//converting char array to string

String s3=new String("example");//creating java string by new keyword

System.out.println(s1);

System.out.println(s2);

System.out.println(s3);

31
Output:

32
Assignment 16: Program to accept 2 Strings from the user
and compare which string is greater with respect to the
length

Code:

import java.util.Scanner;

public class GreaterString

public static void main(String args[])

String s1, s2;

Scanner in = new Scanner(System.in);

System.out.println("Enter the first string");

s1 = in.nextLine();

System.out.println("Enter the second

string"); s2 = in.nextLine();

if (s1.compareTo(s2) > 0){

System.out.println("The first string is greater than the

second."); System.out.println("string length is: "+s1.length()); }

else if (s1.compareTo(s2) < 0){

33
System.out.println("The first string is smaller than the second.");

System.out.println("string length is: "+s2.length()); }

else {

System.out.println("Both the strings are equal.");

System.out.println("Both string's length is: "+s1.length());

34
Output:

35
Assignment 17: Program to accept string from user to check
if it is Palindrome or not using inbult finction

Code:

import java.util.Scanner;

public class StringPalindrome

public static void main (String[] args) throws

java.lang.Exception {

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print("Enter a string: ");

String str= sc.nextLine();

String reverse = new StringBuffer(str).reverse().toString();

if (str.equals(reverse))

System.out.println("String is palindrome");

else

System.out.println("String is not palindrome");

36
Output:

37
Assignment 18: program to check Password and Confirm
Password are same or not

Code:

import java.util.Scanner;

public class PasswordCheck

public static void main(String[] args)

Scanner sc= new Scanner(System.in);

System.out.print("Enter your password: ");

String myStr1= sc.nextLine();

System.out.print("Confirm Password: ");

String myStr2= sc.nextLine();

if(myStr1.equals(myStr2))

System.out.println("Entered Password is Correct you are Logged in");

else

System.out.println("Entered password does not match");

38
Output:

39
Assignment 19: Program to accept username from the user
and check whether its length is between 8 to 15 characters

Code:

import java.util.Scanner;

public class UsernameCheck

public static void main (String[] args) throws java.lang.Exception

Scanner sc= new Scanner(System.in); //System.in is a standard input

stream System.out.print("Enter a Username:"); String str= sc.nextLine();

if(str.length() < 8 || str.length() > 15)

System.out.println("Username should have characters between 8 to 15");

else{

System.out.println("Your Username is Valid");

40
Output:

41
Assignment 20: Program to accept word from user and
convert it to uppercase

Code:

import java.util.Scanner;

public class ConvertTOupr

public static void main(String args[])

Scanner sc= new Scanner(System.in); //System.in is a standard input stream

System.out.print("Enter a string: ");

String s1= sc.nextLine();

String s1upper=s1.toUpperCase();

System.out.println("Converted to uppercase "+s1upper);

42
Output:

43
Assignment 21: Program to accept First name, Middle name
and Last name from the user and display Full name

Code:

import java.util.Scanner;

public class Fullname

public static void main(String args[])

String s1, s2, s3;

Scanner in = new Scanner(System.in);

System.out.println("Enter the first name");

s1 = in.nextLine();

System.out.println("Enter the middle

name"); s2 = in.nextLine();

System.out.println("Enter the last name");

s3 = in.nextLine();

System.out.println("Your full name is "+s1+" "+s2+" "+s3+".");

44
Output:

45
Assignment 22: Program to accept "India is my country, I
love India" and replace "India" with "America"

Code:

import java.util.Scanner;

public class WordReplace

public static void main(String args[])

Scanner s = new Scanner(System.in);

System.out.print("Enter a Sentence: ");

String s1= s.nextLine();

String replaceString=s1.replace("India","America");

System.out.println(replaceString);

46
Output:

47
Assignment 23: Program to perform Addition of 2 numbers
using parameterised function

Code:

import java.util.Scanner;

public class SumUsingParafun

public static void main(String[] args) {

int a, b;

Scanner sc = new Scanner(System.in);

System.out.print("Enter 1st Number ");

a = sc.nextInt();

System.out.print("Enter 2nd Number ");

b = sc.nextInt();

System.out.println("The sum of a and b is = " + Integer.sum(a, b));

48
Output:

49
Assignment 24: Program to create class example

Code:

public class Computer {

Computer() {

System.out.println("Constructor of Computer class.");

void computer_method() {

System.out.println("Power gone! Shut down your PC soon...");

public static void main(String[] args) {

Computer c = new Computer();

Laptop l = new Laptop();

c.computer_method();

l.laptop_method();

class Laptop {

Laptop() {

System.out.println("Constructor of Laptop class.");

void laptop_method() {

System.out.println("99% Battery available.");

50
Output:

51
Assignment 25: Program to accept number from user and
find its Factorial

Code:

import java.util.Scanner;

public class Factorial

public static void main(String args[])

int i,fact=1;

int number;

Scanner s = new Scanner(System.in);

System.out.print("Enter the number to get its factorial: ");

number = s.nextInt();

for(i=1;i<=number;i++)

fact=fact*i;

System.out.println("Factorial of "+number+" is: "+fact);

52
Output:

53
Assignment 26: Program to accept full name from user

Code:

import java.util.Scanner;

public class Acceptfullname

public static void main(String args[])

String s1;

Scanner in = new Scanner(System.in);

System.out.println("Enter your full name: ");

s1 = in.nextLine();

System.out.println("Your full name is "+s1+".");

54
Output:

55
Assignment 27: Program for college to enroll 10 students
data for enqiry process

Code:

import java.util.Scanner;

class Student {

String name;

String stu_id;

int score;

public Student() {

this(" ", " ", 0);

public Student(String initName, String initId, int initScore) {

name = initName;

stu_id = initId;

score = initScore;

public class Main {

public static void main(String[] args) { Scanner in =

new Scanner(System.in);

System.out.println("Input number of students:");

int n = Integer.parseInt(in.nextLine().trim()); // you can input 10 here

System.out.println("Input Student Name, ID, Score:");

56
Student stu = new Student();

Student max = new Student();

Student min = new Student(" ", " ", 100);

for (int i = 0; i < n; i ++) {

stu.name = in.next();

stu.stu_id = in.next();

stu.score = in.nextInt();

if (max.score < stu.score) {

max.name = stu.name;

max.stu_id = stu.stu_id;

max.score = stu.score;

if (min.score > stu.score) {

min.name = stu.name;

min.stu_id = stu.stu_id;

min.score = stu.score;

System.out.println("name, ID of the highest score and the lowest


score:");

System.out.println(max.name + " " +

max.stu_id); System.out.println(min.name + " " +

min.stu_id); in.close();

57
Output:

58
Assignment 28: Program to do function overloading which
accepts radius and calculate the area of the circle

Code:

import java.util.Scanner;

import java.lang.Math.*;

public class Circle

void circleCalculate(Float radius) {

Double area = Math.PI * radius * radius;

System.out.println("area of the circle = " + area);

void circleCalculate(Float radius, int x) { Double

circumference = Math.PI * x * radius;

System.out.println("circumference = " + circumference);

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.print("chose between: 1. area or 2. circumference \n (Note: to exit


press 0): ");

int choice = input.nextInt();

59
if (choice == 1) {

Circle obj = new Circle();

System.out.print("enter radius of the circle: ");

Float radius = input.nextFloat();

obj.circleCalculate(radius);

} else if (choice == 2) {

Circle obj = new Circle();

int x = 2;

System.out.print("enter radius of the circle: ");

Float radius = input.nextFloat();

obj.circleCalculate(radius, x);

else if (choice == 0){

System.out.print("Thanks for trying out my CLI app \n");

System.out.print(":)");

else{

System.out.print("Incorrect selection of choice! \n");

System.out.print("Chose again\n\n");

main(null);

input.close();

60
Output:

61
Assignment 29: Program to perform function overloading,
Take students subjects

Code:

import java.util.*;

class Overloading {

void admission(String s, int a) {

System.out.println("The enquiry is for BBA");

void admission(String s, int a, int x) {

System.out.println("The enquiry is for BCA");

public static void main(String[] args) { Overloading obj

= new Overloading(); Scanner input = new

Scanner(System.in); System.out.println("Please

choose enquiry process: "); System.out.println("1.BBA,

2.BCA");

int choice = input.nextInt();

62
if (choice == 1) {

System.out.println("Enter name and 12th total");

String name = input.next();

int total = input.nextInt();

obj.admission(name, total);

} else {

System.out.println("Enter name, 12th total and math marks");

String name = input.next();

int total = input.nextInt();

int math = input.nextInt();

obj.admission(name, total, math);

input.close();

63
Output:

64
Assignment 30: Program to accept number from user and
check whether its prime or not

Code:

import java.util.Scanner;

public class PrimeExample {

public static void main(String[] args) {

Scanner s = new Scanner(System.in);

System.out.print("Enter a number : ");

int n = s.nextInt();

if (isPrime(n)) {

System.out.println(n + " is a prime

number"); } else {

System.out.println(n + " is not a prime

number"); } }

public static boolean isPrime(int n) {

if (n <= 1) {

return false;

for (int i = 2; i < Math.sqrt(n); i++) {

if (n % i == 0) {

return false;

}}

return true; }

65
Output:

66
Assignment 31: Program to accept number from user and
find out its factorial

Code:

import java.util.Scanner;

class FactorialExample2

static int factorial(int n){

if (n == 0)

return 1;

else

return(n * factorial(n-1));

public static void main(String args[]){

int number;

Scanner s = new Scanner(System.in);

System.out.print("Enter the number to get its factorial: ");

number = s.nextInt();

int i,fact=1;

fact = factorial(number);

System.out.println("Factorial of "+number+" is: "+fact);

67
Output:

68
Assignment 32: Program for interface named as rate
calculation use an abstract fun

Code:

abstract class Bank{

abstract double getRateOfInterest(); }

class SBI extends Bank{

double getRateOfInterest(){return 7.5;} }

class HDFC extends Bank{

double getRateOfInterest(){return 8;} }

class AXIS extends Bank{

double getRateOfInterest(){return 7.25;} }

class Bank1{

public static void main(String args[]){

Bank b;

b=new SBI();

System.out.println("Rate of Interest is: "+b.getRateOfInterest()+"

%"); b=new HDFC();

System.out.println("Rate of Interest is: "+b.getRateOfInterest()+"

%"); b=new AXIS();

System.out.println("Rate of Interest is: "+b.getRateOfInterest()+" %");

69
Output:

70
Assignment 33: Program to accept Length and Breadth of
rectangle using class

Code:

import java.util.Scanner;

public class Rectangle

void RectangleLength(Float length, Float bredth)

System.out.println("Length of the rectangle = " + length);

System.out.println("Bredth of the rectangle = " + bredth);

public static void main(String[] args)

Scanner input = new Scanner(System.in);

Rectangle obj = new Rectangle();

System.out.print("Enter Length of the rectangle: ");

Float length = input.nextFloat();

System.out.print("Enter Bredth of the rectangle: ");

Float bredth = input.nextFloat();

obj.RectangleLength(length, bredth);

71
Output:

72
Assignment 34: Program to perform Multithreading
Example

Code:

class RunnableDemo implements Runnable

{ private Thread t;

private String threadName;

RunnableDemo( String name) {

threadName = name;

System.out.println("Creating " + threadName );

public void run() {

System.out.println("Running " + threadName );

try {

for(int i = 4; i > 0; i--) {

System.out.println("Thread: " + threadName + ", " + i);

// Let the thread sleep for a while.

Thread.sleep(50);

} catch (InterruptedException e) {

System.out.println("Thread " + threadName + " interrupted.");

System.out.println("Thread " + threadName + " exiting.");

73
}

public void start () {

System.out.println("Starting " + threadName );

if (t == null) {

t = new Thread (this, threadName);

t.start ();

public class TestThread {

public static void main(String args[]) {

RunnableDemo R1 = new RunnableDemo( "Thread-1");

R1.start();

RunnableDemo R2 = new RunnableDemo( "Thread-

2"); R2.start();

74
Output:

75
Assignment 35: Program to accept number from the user to
get all even, odd and check whether entered number is
even or odd integers

Code:

import java.util.Scanner;

public class Multithreading

public static void main(String[] args) {

Even even = new Even();

even.start();

Odd odd = new Odd();

odd.start();

UserInput userInput = new UserInput();

userInput.start();

class Even extends Thread {

@Override

public void run() {

System.out.println("Even numbers: ");

for (int i = 2; i <= 50; i = i + 2) {

System.out.println(i);

76
}

class Odd extends Thread {

@Override

public void run() {

System.out.println("Odd numbers: ");

for (int i = 3; i <= 50; i = i + 2) {

System.out.println(i);

class UserInput extends Thread {

@Override

public void run() {

Scanner input = new Scanner(System.in);

System.out.print("Enter an integer: ");

int n = input.nextInt();

if (n % 2 == 0)

System.out.println(n + " is even");

else

System.out.println(n + " is odd");

77
Output:

78
Assignment 36: Program to create and run the following
threads Print "Hi" 10 times and "Bye" 20 times

Code:

public class OddEvenRunnable3 implements Runnable{

public int PRINT_NUMBERS_UPTO=20;

static int number=1;

int remainder;

static Object lock=new Object();

OddEvenRunnable3(int remainder)

this.remainder=remainder;

@Override

public void run() {

while (number < PRINT_NUMBERS_UPTO)

{ synchronized (lock) {

while (number % 2 != remainder) {

try {

lock.wait();

} catch (InterruptedException e) {

79
e.printStackTrace();

System.out.println(Thread.currentThread().getName() + " ");

number++;

lock.notifyAll();

public static void main(String[] args)

OddEvenRunnable3 oddRunnable=new OddEvenRunnable3(1);

OddEvenRunnable3 evenRunnable=new OddEvenRunnable3(0);

Thread t1=new Thread(oddRunnable,"Hi"); Thread

t2=new Thread(evenRunnable,"Bye\nBye");

t1.start();

t2.start();

80
Output:

81
Assignment 37: Program to accept Starting and Ending
number from user and print Even and Odd numbers

Code:

import java.util.Scanner;

public class StartEnd

public static void main(String args[]) {

Scanner s = new Scanner(System.in);

System.out.print("Enter starting number : ");

int start= s.nextInt();

System.out.print("Enter ending number : ");

int end= s.nextInt();

int i;

for(i=start;i<=end;i++){

if(i%2==0){

System.out.println("Even "+i);

else{

System.out.println("Odd "+i);

82
Output:

83
Assignment 38: Program to plot a button in AWT

Code:

import java.awt.*;

public class ButtonDemo1

public static void main(String[] args)

Frame f1=new Frame("studytonight ==> Button Demo");

Button b1=new Button("Press Here");

b1.setBounds(80,200,80,50);

f1.add(b1);

f1.setSize(500,500);

f1.setLayout(null);

f1.setVisible(true);

84
Output:

85
Assignment 39: Program to Plot a label, button and
textfield through AWT

Code:

import java.awt.*;

import java.awt.event.*;

public class TextFieldExample extends Frame implements ActionListener{

TextField tf1,tf2;

Label tf3;

Button b1,b2;

TextFieldExample(){

tf1=new TextField();

tf1.setBounds(50,50,150,20);

tf2=new TextField();

tf2.setBounds(50,100,150,20);

tf3=new Label();

tf3.setBounds(50,150,150,20);

b1=new Button("+");

b1.setBounds(50,200,50,50);

b2=new Button("-");

b2.setBounds(120,200,50,50);

b1.addActionListener(this);

b2.addActionListener(this);

add(tf1);add(tf2);add(tf3);add(b1);add(b2);

86
setSize(300,300);

setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e) {

String s1=tf1.getText();

String s2=tf2.getText();

int a=Integer.parseInt(s1);

int b=Integer.parseInt(s2);

int c=0;

if(e.getSource()==b1){

c=a+b;

}else if(e.getSource()==b2){

c=a-b;

String result=String.valueOf(c);

tf3.setText(result);

public static void main(String[] args) {

new TextFieldExample();

87
Output:

88
Assignment 40: Program to draw a smiley face using applet

Code:

import java.applet.Applet;

import java.awt.Color;

import java.awt.Graphics;

public class appletpro extends Applet {

@Override

public void paint(Graphics g){

g.setColor(Color.YELLOW);

g.fillOval(50,50,200,200);

g.drawOval(50,50,200,200);

g.setColor(Color.red);

g.fillOval(70,90,50,50);

g.drawOval(70,90,50,50);

g.setColor(Color.red);

g.fillOval(180,90,50,50);

g.drawOval(180,90,50,50);

g.setColor(Color.black);

g.drawLine(150, 115, 150, 180);

g.drawArc(95, 150, 110, 60, 180, 180);

89
Output:

90
Assignment 41: Program to plot button through AWT with
a frame

Code:

import java.awt.*;

public class SimpleExample extends Frame{

SimpleExample(){

Button b=new Button("Button!!");

b.setBounds(50,50,50,50);

add(b);

setSize(500,300);

setTitle("This is my First AWT example");

setLayout(new FlowLayout());

setVisible(true);

public static void main(String args[]){

SimpleExample fr=new SimpleExample();

91
Output:

92
Assignment 42: Program to accept name from the user and
show welcome person name

Code:

import java.awt.*;

import java.awt.event.*;

public class TextFieldExample extends Frame implements ActionListener{

TextField tf1;

Label tf3;

Button b1;

TextFieldExample(){

tf1=new TextField();

tf1.setBounds(50,50,160,20);

tf3=new Label();

tf3.setBounds(50,150,160,20);

b1=new Button("Show");

b1.setBounds(50,200,80,80);

b1.addActionListener(this);

add(tf1);add(tf3);add(b1);

setSize(400,400);

setLayout(null);

setVisible(true);

public void actionPerformed(ActionEvent e) {

String s1=tf1.getText();

93
if(e.getSource()==b1){

tf3.setText("Welcome "+s1);

public static void main(String[] args) {

new TextFieldExample();

94
Output:

95
Assignment 43:Program to create a login form

Code:

import java.awt.*;

import java.awt.event.*;

class MyLoginWindow extends Frame

TextField name,pass;

Button b1,b2;

MyLoginWindow()

setLayout(new FlowLayout());

this.setLayout(null);

Label n=new Label("Name:",Label.CENTER);

Label p=new Label("password:",Label.CENTER);

name=new TextField(20); pass=new

TextField(20);

pass.setEchoChar('#');

b1=new Button("submit");

b2=new Button("cancel");

this.add(n);

this.add(name);

this.add(p);

this.add(pass);

this.add(b1);

96
this.add(b2);

n.setBounds(70,90,90,60);

p.setBounds(70,130,90,60);

name.setBounds(200,100,90,20);

pass.setBounds(200,140,90,20);

b1.setBounds(100,260,70,40);

b2.setBounds(180,260,70,40);

public static void main(String args[])

MyLoginWindow ml=new MyLoginWindow();

ml.setVisible(true);

ml.setSize(400,400);

ml.setTitle("my login window");

97
Output:

98
Assignment 44: Program to create window with 3 checkbox
labeled Red, Green and Blue when checked on one the
background color changes respectively

Code:

import java.awt.*;

import java.awt.event.*;

public class CheckboxExample

CheckboxExample(){

Frame f= new Frame("CheckBox Example"); final

Label label = new Label("Select one color");

label.setAlignment(Label.CENTER);

label.setSize(400,100);

Checkbox checkbox1 = new Checkbox("Red");

checkbox1.setBounds(100,100, 50,50);

Checkbox checkbox2 = new Checkbox("Green");

checkbox2.setBounds(100,150, 50,50);

Checkbox checkbox3 = new Checkbox("Blue");

checkbox3.setBounds(100,200, 50,50);

f.add(checkbox1); f.add(checkbox2); f.add(checkbox3); f.add(label);

checkbox1.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

if(e.getStateChange()==1){

f.setBackground(Color.RED);

99
}

});

checkbox2.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

if(e.getStateChange()==1){

f.setBackground(Color.GREEN);

});

checkbox3.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

if(e.getStateChange()==1){

f.setBackground(Color.BLUE);

});

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(String args[]) {

new CheckboxExample();

100
Output:

101
Assignment 45: Program to create window with 3
radiobuttons labeled Red, Green and Blue when checked on
one the background color changes respectively

Code:

import java.awt.*;

import java.awt.event.*;

public class backcolorradio

Frame f;

backcolorradio()

Frame f= new Frame("radioBtnGroup Example");

Label l1=new Label("Please select only one option:");

l1.setBounds(10, 40,200,40); f.add(l1);

CheckboxGroup cbg = new CheckboxGroup();

Checkbox radioBtn1 = new Checkbox("Red", cbg,

false); radioBtn1.setBounds(20,100, 70,50);

Checkbox radioBtn2 = new Checkbox("Green", cbg, true);

radioBtn2.setBounds(20,140, 70,50);

Checkbox radioBtn3 = new Checkbox("Blue", cbg,

false); radioBtn3.setBounds(20,180, 70,50);

f.add(radioBtn1);

102
f.add(radioBtn2);

f.add(radioBtn3);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

radioBtn1.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

f.setBackground(Color.RED);

l1.setBackground(Color.RED);

radioBtn1.setBackground(Color.RED);

radioBtn2.setBackground(Color.RED);

radioBtn3.setBackground(Color.RED);

});

radioBtn2.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

f.setBackground(Color.GREEN);

l1.setBackground(Color.GREEN);

radioBtn1.setBackground(Color.GREEN);

radioBtn2.setBackground(Color.GREEN);

radioBtn3.setBackground(Color.GREEN);

});

103
radioBtn3.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

f.setBackground(Color.BLUE);

l1.setBackground(Color.BLUE);

radioBtn1.setBackground(Color.BLUE);

radioBtn2.setBackground(Color.BLUE);

radioBtn3.setBackground(Color.BLUE);

});

public static void main(String args[])

new backcolorradio();

104
Output:

105
Assignment 46: Program to change button label from OK to
KO when clicked and vise versa

Code:

import java.awt.*;

import java.awt.event.*;

public class OKtoKO {

public static void main(String[] args) { Frame

f=new Frame("Button Example"); Button

b=new Button("OK");

b.setBounds(50,100,60,40);

b.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

if(b.getLabel().equals("OK")){

b.setLabel("KO"); }

else{

b.setLabel("OK"); }

});

f.add(b);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

106
Output:

107
Assignment 47 Program to create window with 3 Scrollbars
labeled Red, Green and Blue when checked on one the
background color changes respectively

Code:

import java.awt.*;

import java.awt.event.*;

class ScrollbarExample{

ScrollbarExample(){

Frame f= new Frame("Scrollbar Example");

Label l1=new Label("Please Scroll Any Bar:");

l1.setBounds(10, 40,200,40);

final Scrollbar r=new Scrollbar();

r.setBounds(50,100, 50,100);

Scrollbar g=new Scrollbar();

g.setBounds(110,100, 50,100);

Scrollbar b=new Scrollbar();

b.setBounds(170,100, 50,100);

f.add(l1);

f.add(r);

f.add(g);

f.add(b);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

108
r.addAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e)

{ f.setBackground(Color.RED);

l1.setBackground(Color.RED);

l1.setText("Red");

});

g.addAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

f.setBackground(Color.GREEN);

l1.setBackground(Color.GREEN);

l1.setText("Green");

});

b.addAdjustmentListener(new AdjustmentListener() {

public void adjustmentValueChanged(AdjustmentEvent e) {

f.setBackground(Color.BLUE);

l1.setBackground(Color.BLUE);

l1.setText("Blue"); }

});

public static void main(String args[]){

new ScrollbarExample();

109
Output:

110
Assignment 48: Program to create 2 buttons and one
textfield on clicking button label will be displaied in the
textfield

Code:

import java.awt.*;

import java.awt.event.*;

public class BtntoText

public static void main(String[] args) {

Frame f=new Frame("Btn label to Text");

TextField l1=new TextField("Please Click on any Button:");

l1.setBounds(10, 40,200,40);

Button b1=new Button("Button 1");

b1.setBounds(50,100,60,40);

b1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

l1.setText(b1.getLabel());

});

Button b2=new Button("Button 2");

b2.setBounds(150,100,60,40);

b2.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

l1.setText(b2.getLabel());

111
}

});

f.add(l1);

f.add(b1);

f.add(b2);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

112
Output:

113
Assignment 49: Program to accept text formatting using
combo buttons

Code:

import java.awt.*;

import java.awt.event.*;

public class ChoiceExample

ChoiceExample(){

Frame f= new Frame();

final Label label = new Label();

label.setAlignment(Label.CENTER);

label.setSize(400,100);

Button b=new Button("Confirm");

b.setBounds(200,100,50,20);

final Choice c=new Choice();

c.setBounds(100,100, 75,75);

c.add("2021");

c.add("2020");

c.add("2019");

c.add("2018");

c.add("2017");

f.add(c);f.add(label); f.add(b);

f.setSize(400,400);

114
f.setLayout(null);

f.setVisible(true);

b.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String data = "HAPPY NEW YEAR "+ c.getItem(c.getSelectedIndex());

label.setText(data);

});

public static void main(String args[])

new ChoiceExample();

115
Output:

116
Assignment 50: Program to draw foll layout with help of
AWT

Code:

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class Fontfamily implements

ActionListener {

Checkbox ch1,ch2;

Button b;

Label l1;

Frame f;

Fontfamily()

f= new Frame();

l1=new Label("Hello World");

l1.setBounds(100,100, 80, 50);

f.add(l1);

ch1=new Checkbox("Font Colour");

117
ch1.setBounds(100,150, 80, 50);

f.add(ch1);

ch2=new Checkbox("Font Face");

ch2.setBounds(100,200, 80, 50);

f.add(ch2);

b=new Button("Submit");

b.setBounds(100,250, 80, 50);

f.add(b);

b.addActionListener(this);

f.setSize(500, 500);

f.setLayout(null);

f.setVisible(true);

public static void main(String args[])

Fontfamily obj=new Fontfamily();

@Override

118
public void actionPerformed(ActionEvent e )

Font serif = new Font("Monospaced",Font.PLAIN,12);

if(e.getSource().equals(b))

if(ch1.getState())

l1.setForeground(Color.red);

if(ch2.getState())

l1.setFont(serif);

119
Output:

120
Assignment 51: Program to find lenght of String entered in
textfield

Code:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class StringlenAWT implements

ActionListener {

JFrame f;

JButton b3;

JLabel l2;

JTextField t1;

StringlenAWT()

f= new JFrame("Enter a Word");

JLabel l1=new JLabel("Enter a word");

l1.setBounds(100,20,200,40);

f.add(l1);

t1=new JTextField();

t1.setBounds(200,20,180,40);

f.add(t1);

121
b3=new JButton("Length");

b3.setBounds(340,80,100,40);

f.add(b3);

b3.addActionListener((ActionListener) this);

l2=new JLabel("Length is ");

l2.setBounds(100,120,200,40);

f.add(l2);

f.setSize(800,800);

f.setLayout(null);

f.setVisible(true); }

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource().equals(b3)){

String s=t1.getText();

l2.setText("Length is "+s.length()); }

public static void main(String args[]){

new StringlenAWT(); }

122
Output:

123
Assignment 52: Program to create a Calculator

Code:

import java.awt.*;

import java.awt.event.*;

public class Calculator implements

ActionListener {

Frame f;

Label l1,l2,l3,l4;

Button b1, b2,b3,b4;

TextField t1,t2;

Calculator()

f=new Frame();

l1=new Label("Enter First Number: ");

l1.setBounds(20,50,150,40);

f.add(l1);

l2=new Label("Enter Second Number: ");

l2.setBounds(20, 100, 150, 30);

f.add(l2);

124
t1=new TextField();

t1.setBounds(190, 50, 150, 30);

f.add(t1);

t2=new TextField();

t2.setBounds(190, 100, 150, 30);

f.add(t2);

b1=new Button("+");

b1.setBounds(20, 150, 80, 40);

f.add(b1);

b1.addActionListener(this);

b2=new Button("-");

b2.setBounds(120, 150, 80, 40);

f.add(b2);

b2.addActionListener(this);

b3=new Button("*");

b3.setBounds(220, 150, 80, 40);

f.add(b3);

b3.addActionListener(this);

b4=new Button("/");

b4.setBounds(320, 150, 80, 40);

125
f.add(b4);

b4.addActionListener(this);

l3=new Label("Result: ");

l3.setBounds(20, 200, 150, 30);

f.add(l3);

l4=new Label();

l4.setBounds(190, 200, 150, 30);

f.add(l4);

f.setSize(500,500);

f.setLayout(null);

f.setVisible(true);

126
public static void main(String args[])

Calculator obj=new Calculator();

@Override

public void actionPerformed(ActionEvent e)

if(e.getSource().equals(b1))

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());

int c=a+b;

l4.setText(String.valueOf(c));

if(e.getSource().equals(b2))

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());

int c=a-b;

l4.setText(String.valueOf(c));

127
}

if(e.getSource().equals(b3))

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());

int c=a*b;

l4.setText(String.valueOf(c));

if(e.getSource().equals(b4))

int a=Integer.parseInt(t1.getText());

int b=Integer.parseInt(t2.getText());

int c=a/b;

l4.setText(String.valueOf(c));

128
Output:

129
Assignment 53: Program to create a button in Jswing

Code:

import javax.swing.*;

public class ButtonExample

public static void main(String[] args)

JFrame f=new JFrame("Button Example");

JButton b=new JButton("Click Here");

b.setBounds(50,100,95,30);

b.setLocation(100, 100);

f.add(b);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

130
output:

131
Assignment 54: Program to create button in jswing using
Constructor

Code:

import javax.swing.*;

public class ButtonExample

JFrame f;

JButton b;

ButtonExample(){

f=new JFrame("Button Example");

b=new JButton("Click Here");

b.setBounds(50,100,95,30);

b.setLocation(100, 100);

f.add(b);

f.setSize(400,400);

f.setLayout(null);

f.setVisible(true);

public static void main(String[] args)

ButtonExample obj=new ButtonExample();

132
Output:

133
Assignment 55: Program to do string functions

Code:

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.*;

public class Text implements

ActionListener{ JFrame f;

JButton b1,b2,b3,b4,b5;

JLabel l2;

JTextField t1;

Text()

f= new JFrame("Enter a Word");

JLabel l1=new JLabel("Enter a word");

l1.setBounds(100,20,200,40);

f.add(l1);

t1=new JTextField();

t1.setBounds(200,20,180,40);

f.add(t1);

b1=new JButton("Show");

b1.setBounds(100,80,100,40);

134
f.add(b1);

b1.addActionListener((ActionListener) this);

b2=new JButton("Clear");

b2.setBounds(220,80,100,40);

f.add(b2);

b2.addActionListener((ActionListener) this);

b3=new JButton("Length");

b3.setBounds(340,80,100,40);

f.add(b3);

b3.addActionListener((ActionListener) this);

b4=new JButton("Palindrome");

b4.setBounds(460,80,100,40);

f.add(b4);

b4.addActionListener((ActionListener) this);

b5=new JButton("Vowel");

b5.setBounds(580,80,100,40);

f.add(b5);

135
b5.addActionListener((ActionListener) this);

l2=new JLabel("You entered ");

l2.setBounds(100,120,200,40);

f.add(l2);

f.setSize(800,800);

f.setLayout(null);

f.setVisible(true);

public static void main(String args[]){

new Text();

@Override

public void actionPerformed(ActionEvent e) {

if(e.getSource().equals(b1)){

l2.setText("You entered "+t1.getText());

else if(e.getSource().equals(b2)){

t1.setText("");

l2.setText("You entered ");

136
else if(e.getSource().equals(b3)){

String s=t1.getText();

l2.setText("Length is "+s.length()); }

else if(e.getSource().equals(b4)){

String str=t1.getText();

String reverse = new StringBuffer(str).reverse().toString(); if

(str.equals(reverse)) {

l2.setText("Word is Palindrome"); }

else {

l2.setText("Word is not Palindrome"); }

else if(e.getSource().equals(b5)){

String v=t1.getText();

int count = 0;

for (int i=0 ; i<v.length(); i++){

char ch = v.charAt(i);

if(ch == 'a'|| ch == 'e'|| ch == 'i' ||ch == 'o' ||ch == 'u'||

ch == 'A'|| ch == 'E'|| ch == 'I' ||ch == 'O' ||ch == 'U'){

count ++;

l2.setText("Number of Vowels are: "+count);

137
Output:

138
Assignment 56: Program to display radio buttons
to select your gender

Code:

import javax.swing.*;

import java.awt.event.ActionListener;

import java.awt.event.ActionEvent;

import java.lang.NullPointerException;

import java.awt.event.ItemEvent;

import java.awt.event.ItemListener;

public class Gender

JFrame f;

JLabel l2;

JRadioButton r1,r2;

Gender()

f=new JFrame("Select your gender");

l2=new JLabel("No option selected");

l2.setBounds(90,120,200,40);

f.add(l2);

JRadioButton r1=new JRadioButton("Male");

JRadioButton r2=new JRadioButton("Female");

139
r1.setBounds(75,50,100,30);

r2.setBounds(75,100,100,30);

ButtonGroup bg=new ButtonGroup();

bg.add(r1);bg.add(r2);

f.add(r1);f.add(r2);

f.setSize(300,300);

f.setLayout(null);

f.setVisible(true);

r1.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

l2.setText("You are Male");

});

r2.addItemListener(new ItemListener() {

public void itemStateChanged(ItemEvent e) {

l2.setText("You are Female");

});

public static void main(String[] args) {

new Gender();

140
Output:

141
Assignment 57: Program to print given pattern

Code:

public class Pattern01

public static void main(String[] args)

int flag = 1;

for (int i = 1; i <=4; i++) {

for (int j = 4; j >= i; j--) {

System.out.print(flag);

System.out.println();

if(flag == 0) {

flag=1;

else {

flag=0;

142
Output:

143
Assignment 58: Program to print given pattern

Code:

public class Pattern2

public static void main(String[] args)

for (int i = 1; i <=4; i++)

for (int j = 4; j >= i; j--)

System.out.print(" "+i);

System.out.println();

144
Output:

145
Assignment 59: Program to print Hello World! an name of
user in AWT with white background

Code:

import java.awt.*;

import java.awt.Color;

import javax.swing.JFrame;

import javax.swing.*;

public class HelloName

public static void main(String args[]){

JFrame f= new JFrame();

f.getContentPane().setBackground(Color.BLACK);

//here i have changed it to black

//because white is unable to differentiate with the original background

JLabel l1=new JLabel("Hello World! ");

l1.setForeground(Color.red);

l1.setBounds(100,80,200,40);

JLabel l2=new JLabel("I am Ajit");

l2.setForeground(Color.green);

l2.setBounds(200,80,200,40);

f.add(l1); f.add(l2); f.setSize(400,400); f.setLayout(null); f.setVisible(true);

}}

146
Output:

147
Assignment 60: Program to make a Login window in Jswing

Code:

import javax.swing.*;

import java.awt.*;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

public class LoginFrame extends JFrame implements ActionListener {

Container container = getContentPane();

JLabel userLabel = new JLabel("USERNAME");

JLabel passwordLabel = new JLabel("PASSWORD");

JTextField userTextField = new JTextField();

JPasswordField passwordField = new JPasswordField();

JButton loginButton = new JButton("LOGIN");

JButton resetButton = new JButton("RESET");

JCheckBox showPassword = new JCheckBox("Show Password");

LoginFrame() {

setLayoutManager();

setLocationAndSize();

addComponentsToContainer();

148
addActionEvent();

public void setLayoutManager() {

container.setLayout(null);

public void setLocationAndSize() {

userLabel.setBounds(50, 150, 100, 30);

passwordLabel.setBounds(50, 220, 100, 30);

userTextField.setBounds(150, 150, 150, 30);

passwordField.setBounds(150, 220, 150, 30);

showPassword.setBounds(150, 250, 150, 30);

loginButton.setBounds(50, 300, 100, 30);

loginButton.setForeground(Color.WHITE);

loginButton.setBackground(Color.GREEN);

resetButton.setBounds(200, 300, 100, 30);

resetButton.setForeground(Color.WHITE);

resetButton.setBackground(Color.GREEN);

public void addComponentsToContainer() {

container.add(userLabel);

container.add(passwordLabel);

149
container.add(userTextField);

container.add(passwordField);

container.add(showPassword);

container.add(loginButton);

container.add(resetButton);

container.setBackground(Color.WHITE);

public void addActionEvent() {

loginButton.addActionListener(this);

resetButton.addActionListener(this);

showPassword.addActionListener(this);

@Override

public void actionPerformed(ActionEvent e) {

if (e.getSource() == loginButton) {

String userText;

String pwdText;

userText = userTextField.getText();

pwdText = passwordField.getText();

if (userText.equals("scott") && pwdText.equals("tiger")) {

JOptionPane.showMessageDialog(this, "Login Successful");

} else {

150
JOptionPane.showMessageDialog(this, "Invalid Username or Password");

if (e.getSource() == resetButton) {

userTextField.setText("");

passwordField.setText("");

if (e.getSource() == showPassword) {

if (showPassword.isSelected()) {

passwordField.setEchoChar((char) 0);

} else {

passwordField.setEchoChar('*');

public static void main(String[] a) {

LoginFrame frame = new LoginFrame();

frame.setTitle("Login Form");

frame.setVisible(true);

frame.setBounds(10, 10, 370, 600);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setResizable(false); } }

151
Output:

152

You might also like