Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Kripal Java File

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Kripal Java File

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 66

SCHOOL OF COMPUTER APPLICATIONS

Manav Rachna International Institute of Research and Studies

School of Computer Applications

Department of Computer Applications


Submitted By
Student Name Kripal Singh
Roll No 23/SCA/MCA/080
Programme Master of computer Applications
Semester 1st Semester
Section/Group B/B2
Department School of Computer Applications
Subject Java Programming
Batch 2023-2025
Submitted To
Faculty Name Dr. Anjali
INDEX

S.
No. Date Aim of the Experiment Signature/Date Grade
1 Write a Program to find Area of the Circle

2 Write a Program to find sum & reverse of a 3


digit number
3 Write a Program to swap two variable without
using third variable
4 Write a Program to find simple interest

5 Write a Program to find list of prime number

6 Write a Program to find a sequence of n


number which are divisible by 3 and 5 both
7 Write a Program to find Roots of quadratic
equation
8 Write a program to find factorial of a Number

9 Write a Program to find a sequence of


Fibonacci series upto n terms
10 Write a program to check whether the given
number is palindrome or not
S.
No. Date Aim of the Experiment Signature/Date Grade
11 Write a program to find HCF of two
numbers.

12 Write a Java Program that will display the


sum of 1+1/2+1/3…..+1/n.

13 Write a Java Program that will print the


following outputs

14 Write a Java Program to find product of two


matrices

15 Write a Java Program to find sum and


subtraction of two matrices.

16 Write a Java Program to sort the list in


ascending Order.

17 Write a Java Program to convert decimalinto


binary number.

18 Write a Java Program to find largest and


smallest of n numbers.

19 Write a java program which shows the


application of constructors.

20 Write a java program to find the electricitybill


using inheritance. The details are as follow:
Units Bill Rate
1-100 Rs 2 per unit
101-300 Rs 5 per unit
301-500 Rs 6 per unit
Above 500 Rs 8 per unit
S.
No. Date Aim of the Experiment Signature/Date Grade
21 Write a java program to find the result sheet of a student
using Interfaces. The details are as follow:
Marks Grade
>90 Excellent
>60 and <=90 Good
Below 60 Average
The format of output is as follow:
Marks of subject 1
Marks of Subject 2
Marks of subject 3
Marks obtained
Total Marks
%Age

22 Write a java program which shows importing of


classes from other packages.

23 Write a java program which creates threadsusing


the thread class.

24 Write a java program which use try and


catch for exception handling.

25 Write a java program which use multiple


catch blocks.

26 Write a java program which shows throwingour


own exception.

27 Write a program to handle Labels andButtons


using AWT Controls.

28 Write a program to handle Check Boxes using


AWT Controls

29 Write a program to handle Lists and Scroll


Bars using AWT Controls
Experiment No : 1
Title : : Write a Java Program to find the Area of circle.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO1
Source code:
import java.util.*;
class area{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Radius of a Circle ");
float r = sc.nextFloat();

double r1;
r1= r*(3.14);
System.out.println("Area of Circle is " +r );
}
}

Output
Experiment No : 2
Title: Write a program to find sum and reverse of three digit number.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
public class p2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("\n Enter the Number you want to reverse --- ");
int r = sc.nextInt();
int s=0,n;
int rev=0;
while(r>0)

{
n=r%10;
s=s+n;
rev=rev*10+n;
r=r/10;

}
System.out.println("sum of digits " + s);
System.out.println("reverse of digits " + rev);

}
}
Output
Experiment No : 3
Title : Write a program to swap two numbers without using third
variable.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO1
Source code:
import java.util.*;
public class p3 {
public static void main(String[] args) {
int a=10,b=5;

System.out.println("\n The two Integers are a = " +a+ " and b = " +b);
a=a+b;
b=a-b;
a=a-b;
System.out.println("After Swapping a = " +a+ " and b = " +b);
}
}
Output
Experiment No: 4
Title : Write a program to find simple interest.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO1
Source code:
import java.util.*;
public class p4 {
public static void main(String[] args) {
int pa,r,t;
Scanner sc = new Scanner(System.in);

System.out.println("\t ---> Enter :- Prinicpal Amount , Rate and Time ");


pa = sc.nextInt();
r = sc.nextInt();
t = sc.nextInt();
int si = pa*r*t;
System.out.println("\t --- The Simple Interest is "+si);
}

}
Output
Experiment No: 5
Title : Write a program to find List of Prime Numbers.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.util.*;

public class p11 {

public static void main(String[] args) {

int lim, count;

Scanner sc = new Scanner(System.in);

System.out.println("Enter the limit");

lim = sc.nextInt();

for (int num = 1; num <= lim; num++) {

count = 0;

for (int divisor = 2; divisor <= num / 2; divisor++) {

if (num % divisor == 0) {

count++;

break;

}
}

if (count == 0 && num > 1) {

System.out.println(num);

}
}

Output
Experiment No: 6
Title : Write a program to find sequence of n numbers which are
divisible by 3 and 5 both.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.util.*;
public class p5 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the value of n: ");
int n = sc.nextInt();

int count = 0, num = 1;


System.out.println("Sequence of " + n + " numbers divisible by both 3 and 5:");
while (count < n) {
if (num % 3 == 0 && num % 5 == 0) {
System.out.print(num + " ");
count++;

}
num++;
}
}
}
Output
Experiment No: 7
Title : Write a program to find the Roots of a Quadratic Equation.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO1
Source code:
import java.util.*;

class p12{

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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

double a = scanner.nextDouble();

System.out.print("Enter coefficient b: ");

double b = scanner.nextDouble();

System.out.print("Enter coefficient c: ");

double c = scanner.nextDouble();

double discriminant = b * b - 4 * a * c;

double[] roots;

if (discriminant > 0) {

double root1 = (-b + Math.sqrt(discriminant)) / (2 * a);


double root2 = (-b - Math.sqrt(discriminant)) / (2 * a);
roots = new double[]{root1, root2};

} else if (discriminant == 0) {
double root = -b / (2 * a);

roots = new double[]{root};

} else {
System.out.println("No real roots");

return;

if (roots.length == 2) {

System.out.println("Root 1: " + roots[0] + ", Root 2: " +


roots[1]);

} else {

System.out.println("Root: " + roots[0]);

}
}

Output
Experiment No: 8
Title : Write a program to find the factorial of n Number.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.util.*;
public class p6 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int n = sc.nextInt();

int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}

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


}

}
Output
Experiment No: 9
Title : Write a program to find the sequence of Fibonacci series up to
n terms.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.util.Scanner;
public class p7 {
public static void main(String[] args) {
int sum,a=0,b=1,num;

Scanner sc = new Scanner(System.in);


System.out.println("Enter the Number pto which fibonnaci series will printS");
num = sc.nextInt();
System.out.println("\n\n0 \n1");
for(int i=0;i<num-2;i++)

{
sum = a + b;
System.out.println("\n"+sum);
a=b;

b=sum;
}
}

}
Output
Experiment No: 10
Title : Write a program to check whether given number is palindrome
or not.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.util.*;
public class p8 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

System.out.println("\n Enter the Number to check if it's palindrome --- ");


int r = sc.nextInt();
int n, rev=0,org;
org=r;
while(r>0)
{
n=r%10;
rev=rev*10+n;
r=r/10;
}

if(org==rev)System.out.println("It is palindrome");
else

System.out.println("Not Palindrome");
}
}

Output
Experiment No: 11
Title : Write a program to find HCF of two numbers.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.util.*;

class p9{

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the Both Values to Find their HCF ");

int n1,n2,hcf;

n1= sc.nextInt();

n2= sc.nextInt();

for (int i =2; i<=n1 || i<=n2; i++)


{

if ( n1%i == 0 && n2%i == 0 )

hcf=i;

System.out.println(" \n --> Hcf is " + hcf);

break;

}
}

}
Output
Experiment No: 12
Title : Write a Java Program that will display the sum of
1+1/2+1/3…..+1/n.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
import java.util.*;

public class p10 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int n;

double sum = 0;

System.out.println("Enter the Value of N upto which you want the sum


of series 1 + 1/2 + 1/3 + ..... + 1/n");

n = sc.nextInt();
for (int i = 1; i <= n; i++) {

sum = (sum + (1.0 / i));

System.out.println("Sum of Series: " + sum);

}
Output
Experiment No: 13
Title : Write a Java Program that will print the following outputs:

1
22
333
4444
55555
4444
333
22
1

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
public class y1 {

public static void main(String[] args) {

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

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

System.out.print(i);

}
System.out.println();

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

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

System.out.print(i);
}

System.out.println();

Output
Title : Write a Java Program that will print the following outputs:

1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
class y1 {
public static void main(String[] args) {

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

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

System.out.print(" ");

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

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

}
System.out.println();

}
Output

Title : Write a Java Program that will print the following outputs:

$
$$
$$$
$$$$
$$$$$

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
class y1 {

public static void main(String[] args) {


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

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

System.out.print("$");
}

System.out.println();

Output
Experiment No: 14

Title : Write a Java Program to find product of two matrices


Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
class y1 {

public static void main(String[] args) {


int r1 = 3, c1 = 3;

int r2 = 3, c2 = 3;

int[][] matrix1 = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

int[][] matrix2 = {{9, 8, 7}, {6, 5, 4}, {3, 2, 1}};

int[][] product = new int[r1][c2];

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

for (int j = 0; j < c2; j++) {

for (int k = 0; k < c1; k++) {

product[i][j] += matrix1[i][k] * matrix2[k][j];

}
}

System.out.println("Product of matrices:");

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


for (int j = 0; j < c2; j++) {

System.out.print(product[i][j] + " ");

}
System.out.println();

Output
Experiment No: 15
Title : Write a Java Program to find sum and subtraction of two
matrices.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
import java.util.Scanner;

class y1 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int r1, c1, r2, c2;

System.out.print("Enter the number of rows for matrix 1: ");


r1 = sc.nextInt();

System.out.print("Enter the number of columns for matrix 1: ");

c1 = sc.nextInt();

System.out.print("Enter the number of rows for matrix 2: ");

r2 = sc.nextInt();

System.out.print("Enter the number of columns for matrix 2: ");

c2 = sc.nextInt();

if (r1 != r2 || c1 != c2) {
System.out.println("Matrices must have the same dimensions for
addition and subtraction.");

return;

int[][] matrix1 = new int[r1][c1];

int[][] matrix2 = new int[r2][c2];

System.out.println("Enter elements of matrix 1:");

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

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

matrix1[i][j] = sc.nextInt();

System.out.println("Enter elements of matrix 2:");

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

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

matrix2[i][j] = sc.nextInt();

int[][] sumMatrix = new int[r1][c1];

int[][] subMatrix = new int[r1][c1];

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

for (int j = 0; j < c1; j++) {


sumMatrix[i][j] = matrix1[i][j] + matrix2[i][j];

subMatrix[i][j] = matrix1[i][j] - matrix2[i][j];

System.out.println("Sum of the matrices:");

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

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

System.out.print(sumMatrix[i][j] + " ");

System.out.println();
}
System.out.println("Subtraction of the matrices:");

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

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

System.out.print(subMatrix[i][j] + " ");

System.out.println();
}

sc.close();

Output
Experiment No: 16
Title : Write a Java Program to sort the list in ascending Order.

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
import java.util.*;

class y1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter the size of the list: ");

int size = scanner.nextInt();

int[] arr = new int[size];

System.out.println("Enter the elements of the list:");

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

arr[i] = scanner.nextInt();
}

Arrays.sort(arr);

System.out.println("Sorted list in ascending order:");


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

System.out.print(arr[i] + " ");

Output
Experiment No: 17
Title : Write a Java Program to convert decimal into binary number.

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.util.Scanner;

class y1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

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


int decimal = scanner.nextInt();

String binary = "";

while (decimal > 0) {

int remainder = decimal % 2;

binary = remainder + binary;

decimal = decimal / 2;

System.out.println("Binary equivalent: " + binary);

Output
Experiment No: 18
Title : Write a Java Program to find largest and smallest of n numbers.

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
import java.util.Scanner;

class y1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

int n;

System.out.print("Enter the number of elements: ");

n = scanner.nextInt();

int[] numbers = new int[n];

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

System.out.print("Enter number " + (i + 1) + ": ");

numbers[i] = scanner.nextInt();

int largest = numbers[0];

int smallest = numbers[0];

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


if (numbers[i] > largest) {

largest = numbers[i];

if (numbers[i] < smallest) {

smallest = numbers[i];

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

System.out.println("Smallest number: " + smallest);

Output
Experiment No: 19
Title : Write a java program which shows the application of
constructors.

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
class y1 {
int num;

y1() {

num = 10;

System.out.println("Default constructor called. Value of num: " +


num);

y1(int value) {

num = value;

System.out.println("Parameterized constructor called. Value of num:


" + num);

public static void main(String[] args) {

y1 obj1 = new y1();

y1 obj2 = new y1(20);


}

}
Output
Experiment No: 20
Title : Write a java program to find the electricity bill using
inheritance. The details are as follow.

Units Bill Rate


1-100 Rs 2 per unit
101-300 Rs 5 per unit
301-500 Rs 6 per unit
Above 500 Rs 8 per unit

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
import java.util.Scanner;

class ElectricityBill {

int units;

ElectricityBill(int units) {

this.units = units;

double calculateBill() {

if (units <= 100) {

return units * 2;
} else if (units <= 300) {

return 100 * 2 + (units - 100) * 5;


} else if (units <= 500) {

return 100 * 2 + 200 * 5 + (units - 300) * 6;

} else {

return 100 * 2 + 200 * 5 + 200 * 6 + (units - 500) * 8;

public class y1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.println("Enter the number of units consumed: ");

int units = scanner.nextInt();

ElectricityBill bill = new ElectricityBill(units);

double totalBill = bill.calculateBill();

System.out.println("Electricity Bill: Rs " + totalBill);

Output
Experiment No: 21
Title : Write a java program to find the result sheet of a student
using Interfaces. The details are as follow:
Marks Grade
>90 Excellent
>60 and <=90 Good
Below 60 Average

The format of output is as follow:


Marks of subject 1
Marks of Subject 2
Marks of subject 3
Marks obtained
Total Marks
%Age

Objective : Revision of Programming Techniques using Java.


Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
import java.util.Scanner;

interface Result {

void calculateResult();

class Student implements Result {

int subject1, subject2, subject3;


public void calculateResult() {
int totalMarks = subject1 + subject2 + subject3;

double percentage = (totalMarks / 300.0) * 100;

System.out.println("Marks of subject 1: " + subject1);

System.out.println("Marks of subject 2: " + subject2);

System.out.println("Marks of subject 3: " + subject3);

System.out.println("Marks obtained: " + totalMarks);

System.out.println("Total Marks: 300");

System.out.println("%Age: " + percentage);

if (percentage > 90) {

System.out.println("Grade: Excellent");

} else if (percentage > 60) {

System.out.println("Grade: Good");

} else {

System.out.println("Grade: Average");
}

public class y1 {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

Student student = new Student();

System.out.print("Enter marks for subject 1: ");

student.subject1 = scanner.nextInt();

System.out.print("Enter marks for subject 2: ");

student.subject2 = scanner.nextInt();

System.out.print("Enter marks for subject 3: ");


student.subject3 = scanner.nextInt();

student.calculateResult();

Output
Experiment No: 22
Title : Write a java program which shows importing of classes from
other packages.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
// Dell.java

package laptop;

public class Dell {

public static String model = "Inspiron";

public static int ramSize = 8;

// Hp.java

package laptop;

public class Hp {
public static String model = "Pavilion";

public static int ramSize = 16;

// Main class import HP and Dell

import laptop.Dell;

import laptop.Hp;

public class y1 {
public static void main(String[] args) {

System.out.println("Dell Model: " + Dell.model);

System.out.println("Dell RAM Size: " + Dell.ramSize + "GB");

System.out.println("Hp Model: " + Hp.model);

System.out.println("Hp RAM Size: " + Hp.ramSize + "GB");

}
}

Output
Experiment No: 23
Title : Write a java program which creates threads using the thread
class.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
class y1 extends Thread {

public void run() {

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

System.out.println("Thread: " + Thread.currentThread().getId()


+ " Count: " + i);

try {

Thread.sleep(2000);

} catch (InterruptedException e) {

e.printStackTrace();

}
}

public static void main(String args[]) {

y1 thread1 = new y1();

y1 thread2 = new y1();

thread1.start();

thread2.start();

}
Output
Experiment No: 24
Title : Write a java program which use try and catch for exception
handling.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2

Source code:
public class y1 {

public static void main(String[] args) {


try {

int numerator = 10;

int denominator = 0;

int result = numerator / denominator;

System.out.println("Result: " + result);

} catch (ArithmeticException e) {
System.out.println("Error: Division by zero is not allowed.");

Output
Experiment No: 25
Title : Write a java program which use multiple catch blocks.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
public class y1 {
public static void main(String[] args) {

try {

int[] numbers = {1, 2, 3};

int result = numbers[4] / 0;

} catch (ArithmeticException e) {

System.out.println("ArithmeticException: " + e.getMessage());

} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("ArrayIndexOutOfBoundsException: " +
e.getMessage());

} catch (Exception e) {

System.out.println("Exception: " + e.getMessage());

Output
Experiment No: 26
Title : Write a java program which shows throwing our own
exception.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.util.Scanner;

class y1 {

static void validateAge(int age) throws CustomException {

if (age < 18) {

throw new CustomException("Age must be 18 or above");

} else {

System.out.println("Valid Age: " + age);

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

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

int userAge = scanner.nextInt();

try {

validateAge(userAge);

} catch (CustomException e) {

System.out.println("Exception: " + e.getMessage());

}
}

class CustomException extends Exception {

public CustomException(String message) {

super(message);
}

Output
Experiment No: 27
Title : Write a program to handle Labels and Buttons using AWT
Controls.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.awt.*;
import java.awt.event.*;

public class y1 {
public static void main(String[] args) {
Frame frame = new Frame("Sample program by yKD");
Label label = new Label("Hello, YKD!");
Button button = new Button("Click here Boss");

frame.setLayout(new FlowLayout());
frame.add(label);
frame.add(button);

frame.setSize(300, 150);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}
}
Output
Experiment No: 28
Title : Write a program to handle Check Boxes using AWT Controls
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.awt.*;
import java.awt.event.*;

public class y1 {
public static void main(String[] args) {
Frame frame = new Frame("Checkbox Example");

CheckboxGroup genderGroup = new CheckboxGroup();


Checkbox maleCheckbox = new Checkbox("Male", genderGroup, false);
Checkbox femaleCheckbox = new Checkbox("Female", genderGroup, false);

CheckboxGroup courseGroup = new CheckboxGroup();


Checkbox bcaCheckbox = new Checkbox("BCA", courseGroup, false);
Checkbox mcaCheckbox = new Checkbox("MCA", courseGroup, false);

frame.setLayout(new FlowLayout());
frame.add(new Label("Select Gender:"));
frame.add(maleCheckbox);
frame.add(femaleCheckbox);

frame.add(new Label("Select Course:"));


frame.add(bcaCheckbox);
frame.add(mcaCheckbox);

frame.setSize(300, 150);
frame.setVisible(true);

frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent windowEvent){
System.exit(0);
}
});
}
}

Output
Experiment No: 29
Title : Write a program to handle Lists and Scroll Bars using AWT
Controls.
Objective : Revision of Programming Techniques using Java.
Pre-requisted:
 Knowledge of OOPs Concepts
 Eclipse Code
Targeted CO:CO2
Source code:
import java.awt.*;
import java.awt.event.*;

class y1 {
y1() {
Frame f = new Frame("List and ScrollBar Example");

List l = new List();


l.add("Aarav");
l.add("Aryan");
l.add("Rohan");
l.add("Kiran");
l.add("Arjun");
l.add("Vishal");
l.add("Siddharth");
l.add("Harsh");
l.add("Rahul");
l.add("Aditya");
l.add("Aniket");

Scrollbar s = new Scrollbar();

f.add(l);

f.setSize(400, 600); // Increased frame height for easy scrolling


f.setLayout(new FlowLayout());
f.setVisible(true);

f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public static void main(String args[]) {


new y1();
}
}

Output

You might also like