Q1]to accept two numbers from user using reader class and display the two numbers as it is on the command prompt and also display the swapping of those two numbers on the command prompt using third variable.
import java.io.*;
class Q1
{
public static void main(String args[])
{
try
{
BufferedReader r1=new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter 1st no:");
int n1=Integer.parseInt(r1.readLine());
System.out.println("enter 2no:");
int n2=Integer.parseInt(r1.readLine());
System.out.println("value of 1st variable: "+n1+", value of 2nd no:"+n2);
int n3=n1;
n1=n2;
n2=n3;
System.out.println("after swapping \n value of 1st variable: "+n1+"value of 2nd variable: "+n2);
}
catch(Exception e)
{
}
}
}
Q2]to accept two strings from user and check whether the two strings are same or not. Display the result on the command prompt.
import java.util.Scanner;
class Q2
{
public static void main(String args[])
{
Scanner s1= new Scanner(System.in);
System.out.println("enter 1st string: ");
String str1=s1.next();
System.out.println("enter 2nd string: ");
String str2=s1.next();
if(str1 == str2)
{
System.out.println("string are same");
}
else
{
System.out.println("STring are not same");
}
}
}
Q3]Write a Java program to accept two numbers from user using reader class and display the two options to the end user:
Display the max of two numbers
Display the min of two numbers
Accept the choice from user and print the output according to the user’s choice.
(Use if-else statement)
import java.io.*;
class Q3
{
public static void main(String args[])
{
int n1, n2;
try
{
BufferedReader r1 = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter 1st number: ");
n1 = Integer.parseInt(r1.readLine());
System.out.print("Enter 2nd number: ");
n2 = Integer.parseInt(r1.readLine());
System.out.println("1. Maximum of 2 numbers");
System.out.println("2. Minimum of 2 numbers");
System.out.print("Enter your choice: ");
int ch = Integer.parseInt(r1.readLine());
switch(ch)
{
case 1:
if(n1 > n2)
System.out.println("Maximum: " + n1);
else
System.out.println("Maximum: " + n2);
break;
case 2:
if(n1 < n2)
System.out.println("Minimum: " + n1);
else
System.out.println("Minimum: " + n2);
break;
default:
System.out.println("Wrong choice");
}
}
catch(Exception e)
{
System.out.println("Error: " + e.getMessage());
}
}
}
Q4]Write a Java program to accept a single number (any digit) from user and print the following options to the end user:
Check whether that number is palindrome or not.
Check whether that number is Armstrong or not.
Accept the choice from user and print the output according to the user’s choice.
import java.util.*;
class Q4
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.print("enter no:");
int n1=s1.nextInt();
int onum=n1;
int rev,sum=0;
System.out.print("1.palindrome:");
System.out.print("2.Armstrong:");
System.out.println("enter your choice:");
int ch=s1.nextInt();
switch(ch)
{
case 1:
while(n1>0)
{
rev=n1%10;
sum=sum*10+rev;
n1=n1/10;
}
if(onum==sum)
System.out.println("no. is plaindrome");
else
System.out.println("no. is not palindrome");
break;
case 2:
while(n1>0)
{
rev=n1%10;
sum=sum+(rev*rev*rev);
n1=n1/10;
}
if(onum==sum)
System.out.println("no. is armstrong ");
else
System.out.println("no. is not armstrong");
break;
}
}
}
Q5]to accept the side of the square from user and print the following options to the end user:
i. Display the area of the square
ii. Display the perimeter of the square
Accept the choice from user and print the output according to the user’s choice.
import java.util.Scanner;
class Q5
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.print("enter no: ");
int n=s1.nextInt();
System.out.println("1. area of square:");
System.out.println("2. perimeter of square:");
System.out.print("enter your choice:");
int ch=s1.nextInt();
switch(ch)
{
case 1:
int a=n*n;
System.out.println("area of sqaure:"+a);
break;
case 2:
int p=4*n;
System.out.println("perimeter of square:"+p);
break;
default:
System.out.println("invalid choice");
}
}
}
Q6]Write a Java program to accept the start and end number from user and print the following options to the end user:
i. Display all the numbers from start to end
ii. Display all the numbers from end to start
Accept the choice from user and print the output according to the user’s choice.
import java.util.Scanner;
class Q6
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.println("enter start no: ");
int start=s1.nextInt();
System.out.println("enter end no:");
int end=s1.nextInt();
System.out.println("1.all no. from start to end");
System.out.println("2.all no. from end to start");
System.out.println("enter your choice:");
int ch=s1.nextInt();
switch(ch)
{
case 1:
for(int i=start;i<=end;i++)
System.out.println(i);
break;
case 2:
for(int i=end;i>=start;i--)
System.out.println(i);
break;
}
}
}
Q7]Write a Java program to accept the start and end number from user and print the following options to the end user:
i. Display all even numbers
ii. Display all odd numbers
Accept the choice from user and print the output according to the user’s choice
import java.util.Scanner;
class Q7
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.println("enter start no:");
int start=s1.nextInt();
System.out.println("enter end no:");
int end=s1.nextInt();
System.out.println("all even no:");
System.out.println("all odd no:");
System.out.println("enter your choice:");
int ch=s1.nextInt();
switch(ch)
{
case 1:
for(int i=start;i<=end;i++)
{
if(i%2==0)
System.out.println(i);
}
break;
case 2:
for(int i=start;i<=end;i++)
{
if(i%2!=0)
System.out.println(i);
}
break;
}
}
}
Q8]Write a Java program to accept the number of lines from user and print the following pattern on the command prompt:
*
* *
* * *
* * * *
* * * * *
import java.util.*;
class Q8 {
public static void main(String args[]) {
int n = 5; // fixed number of rows
for (int i = 1; i <= n; i++) {
// print spaces
for (int j = i; j < n; j++) {
System.out.print(" ");
}
// print stars
for (int k = 1; k <= i; k++) {
System.out.print("* ");
}
// move to next line
System.out.println();
}
}
}
Q9]Write a Java program to accept a two-dimensional array from user and print the following options to the end user:
i. Display the sum of all diagonal elements
ii. Display the sum of all non-diagonal elements
Accept the choice from user and print the output according to the user’s choice.
import java.util.Scanner;
class Q9
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.println("enter no of rows and columns:");
int n=s1.nextInt();
int [][]arr=new int [n][n];
System.out.println("enter no. of matrix:");
for(int i=0;i= j)
System.out.print(a[i][j] + " ");
else
System.out.print(" ");
}
System.out.println();
}
}
else if (ch == 2) {
System.out.println("\nUpper Triangle Elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (i <= j)
System.out.print(a[i][j] + " ");
else
System.out.print(" ");
}
System.out.println();
}
}
else {
System.out.println("Invalid choice!");
}
}
}
Q11]Write a Java program to create an Interface that consists of two methods called accept() and display().
Accept three numbers from user and display the max value among them on the command prompt.
import java.util.Scanner;
interface Demo
{
public abstract void accept(int a,int b,int c);
public abstract void display();
}
class DemoClass implements Demo
{
int n1,n2,n3;
public void accept(int a,int b,int c)
{
n1=a;
n2=b;
n3=c;
}
public void display()
{
if((n1>n2) && (n1>n3))
System.out.println("greater no:"+n1);
else if(n2>n3)
System.out.println("greater no:"+n2);
else
System.out.println("greater no:"+n3);
}
}
class Q11
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
int a,b,c;
System.out.println("enter 3 no:");
a=s1.nextInt();
b=s1.nextInt();
c=s1.nextInt();
DemoClass d1=new DemoClass();
d1.accept(a,b,c);
d1.display();
}
}
Q12]Write a Java program to accept the size of an array from user and accept the one-dimensional array from user, then print the following options to the end user:
i. Display all array elements
ii. Display all even numbers from that array
Accept the choice from user and print the output according to the user’s choice.
import java.util.*;
class Q12 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter " + n + " elements:");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("\n1. Display all array elements");
System.out.println("2. Display all even numbers");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
if (ch == 1) {
System.out.println("\nAll array elements:");
for (int i = 0; i < n; i++) {
System.out.print(a[i] + " ");
}
}
else if (ch == 2) {
System.out.println("\nEven numbers in array:");
for (int i = 0; i < n; i++) {
if (a[i] % 2 == 0)
System.out.print(a[i] + " ");
}
}
else {
System.out.println("Invalid choice!");
}
}
}
Q13]Write a Java program to create one Interface that consists of two methods accept() and display().
Create one base class that consists of emp_no and emp_name.
Then create one derived and implemented class that accepts the employee number and name from user and display employee details on the command prompt.
import java.util.Scanner;
interface Demo
{
public abstract void accept();
public abstract void display();
}
class Emp implements Demo
{
int empno;
String empname;
public void accept()
{
Scanner s1=new Scanner(System.in);
System.out.println("enter emp no:");
empno=s1.nextInt();
System.out.println("enter emp name:");
empname=s1.next();
}
public void display()
{
System.out.println("emp no:"+empno);
System.out.println("emp name:"+empname);
}
}
class Q13
{
public static void main(String args[])
{
Emp d1=new Emp();
d1.accept();
d1.display();
}
}
--------------------------------------------------
Q14]Write a Java program to accept the one-dimensional array from user and print the following options to the end user:
i. Display the sum of all numbers
ii. Display the sum of all even numbers
iii. Display the sum of all odd numbers
Accept the choice from user and print the output according to the user’s choice.
import java.util.*;
class Q14 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter array elements:");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("\n1. Sum of all numbers");
System.out.println("2. Sum of even numbers");
System.out.println("3. Sum of odd numbers");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
int sum = 0;
switch (choice) {
case 1:
for (int i = 0; i < n; i++)
sum += a[i];
System.out.println("Sum of all numbers = " + sum);
break;
case 2:
for (int i = 0; i < n; i++)
if (a[i] % 2 == 0)
sum += a[i];
System.out.println("Sum of even numbers = " + sum);
break;
case 3:
for (int i = 0; i < n; i++)
if (a[i] % 2 != 0)
sum += a[i];
System.out.println("Sum of odd numbers = " + sum);
break;
default:
System.out.println("Invalid choice!");
}
}
}
-------------------------------------------------------
Q15]Write a Java program to create one interface Vehicle with one method getWheel().
Create classes from Vehicle interface and return the number of wheels for two-wheeler and car.
Display the result in the main function.
import java.util.Scanner;
interface vehical
{
public abstract void getwheels();
}
class car implements vehical
{
public void getwheels()
{
System.out.println("car has 4 wheels");
}
}
class auto implements vehical
{
public void getwheels()
{
System.out.println("auto has 3 wheels");
}
}
class bike implements vehical
{
public void getwheels()
{
System.out.println("bike has 2 wheels");
}
}
class Q15
{
public static void main(String args[])
{
car c1=new car();
auto a1=new auto();
bike b1=new bike();
c1.getwheels();
a1.getwheels();
b1.getwheels();
}
}
-----------------------------------------------------------
Q16]Write a Java program to accept the single-dimensional array from user and print the following options to the end user:
i. Display the max value from an array
ii. Display the min value from an array
iii. Display the reverse of that array
Accept the choice from user and print the output according to the user’s choice.
import java.util.*;
class Q16 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter array elements:");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.println("\n1. Display Max Value");
System.out.println("2. Display Min Value");
System.out.println("3. Display Reverse Array");
System.out.print("Enter your choice: ");
int choice = sc.nextInt();
switch (choice) {
case 1:
int max = a[0];
for (int i = 1; i < n; i++) {
if (a[i] > max)
max = a[i];
}
System.out.println("Maximum value = " + max);
break;
case 2:
int min = a[0];
for (int i = 1; i < n; i++) {
if (a[i] < min)
min = a[i];
}
System.out.println("Minimum value = " + min);
break;
case 3:
System.out.println("Array in Reverse Order:");
for (int i = n - 1; i >= 0; i--) {
System.out.print(a[i] + " ");
}
break;
default:
System.out.println("Invalid choice!");
}
}
}
-----------------------------------------
Q17]Design a screen in Java to handle the Mouse Events such as MOUSE_MOVED and MOUSE_CLICK, and display the position of the mouse click in a TextField.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class xyz extends JFrame implements MouseListener, MouseMotionListener
{
JTextField textField;
public xyz()
{
setTitle("mouse event demo");
setSize(800,400);
setLayout(new FlowLayout());
textField= new JTextField(30);
textField.setEditable(false);
add(textField);
addMouseListener(this);
addMouseMotionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void mouseClicked(MouseEvent e)
{
textField.setText("mouse clicked at x:"+e.getX()+"mouse clicked at y:"+e.getY());
}
public void mouseMoved(MouseEvent e)
{
textField.setText("mouse moved at x:"+e.getX()+ " mouse moved at y:"+e.getY());
}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){}
public void mousePressed(MouseEvent e){}
public void mouseDragged(MouseEvent e){}
public void mouseExited(MouseEvent e){}
}
class Q17
{
public static void main(String args[])
{
new xyz();
}
}
-------------------------------------------
Q18]Write a Java program to design the following GUI using Swing:
Personal Information
---------------------
First Name: [_________]
Last Name : [_________]
Address : [_________]
Mobile No : [_________]
Gender: ( ) Male ( ) Female
Your Interests: [ ] Computer [ ] Sports [ ] Music
[Submit] [Reset]
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Q18 extends JFrame implements ActionListener
{
JLabel l1,l2,l3,l4,l5,l6;
JTextField t1,t2,t4;
JTextArea t3;
JRadioButton r1,r2;
JCheckBox c1,c2,c3;
JButton b1,b2;
public Q18()
{
setTitle("Form");
setSize(400,800);
setLayout(new GridLayout(9,2,5,5));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1=new JLabel("First name:");
l2=new JLabel("Last name:");
l3=new JLabel("address:");
l4=new JLabel("Mobile no:");
l5=new JLabel("Gender:");
l6=new JLabel("your interest:");
t1=new JTextField();
t2=new JTextField();
t3=new JTextArea();
t4=new JTextField();
r1=new JRadioButton("male");
r2=new JRadioButton("female");
c1=new JCheckBox("Computer");
c2=new JCheckBox("sports");
c3=new JCheckBox("music");
b1=new JButton("Submit");
b2=new JButton("Reset");
add(l1); add(t1);
add(l2); add(t2);
add(l3); add(t3);
add(l4); add(t4);
add(l5); add(r1);
add(new JLabel("")); add(r2);
add(l6);
add(c1); add(c2); add(c3);
add(b1); add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==b1)
{
JOptionPane.showMessageDialog(this,"info submitted");
}
else
{
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
r1.setSelected(false);
r2.setSelected(false);
c1.setSelected(false);
c2.setSelected(false);
c3.setSelected(false);
}
}
public static void main(String args[])
{
new Q18();
}
}
----------------------------------------------
Q19]Write a Java program to design a screen using Swing that will take a username and password.
If the username and password are not same, raise an exception with appropriate message.
The user can have 3 login chances only.
Use a Clear button to clear the text fields.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class InvalidLoginException extends Exception {
public InvalidLoginException(String message) {
super(message);
}
}
public class Q19 extends JFrame implements ActionListener {
JLabel l1, l2;
JTextField t1;
JPasswordField p1;
JButton b1, b2;
int attempts = 0;
public Q19() {
setTitle("Login Form");
setSize(400, 200);
setLayout(new GridLayout(3, 2, 10, 10));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l1 = new JLabel("Username:");
l2 = new JLabel("Password:");
t1 = new JTextField();
p1 = new JPasswordField();
b1 = new JButton("Login");
b2 = new JButton("Clear");
add(l1);
add(t1);
add(l2);
add(p1);
add(b1);
add(b2);
b1.addActionListener(this);
b2.addActionListener(this);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == b1) { // Login button clicked
try {
String user = t1.getText();
String pass = new String(p1.getPassword());
attempts++;
if (user.equals(pass)) {
JOptionPane.showMessageDialog(this, "Login Successful!");
attempts = 0; // reset attempts
} else {
throw new InvalidLoginException("Invalid Login! Username and Password must be same.");
}
} catch (InvalidLoginException ex) {
if (attempts >= 3) {
JOptionPane.showMessageDialog(this, "Maximum attempts reached! Access Denied.");
System.exit(0); // close the program
} else {
JOptionPane.showMessageDialog(this, ex.getMessage() + "\nAttempt " + attempts + " of 3.");
}
}
}
if (e.getSource() == b2) { // Clear button clicked
t1.setText("");
p1.setText("");
}
}
public static void main(String[] args) {
new Q19();
}
}
-------------------------------------------
Q20]Write a Java program to design the following frame and display the output accordingly in the text boxes:
Button Output
UPPER CASE INDIRA COLLEGE
Lower Case indira college
Bold Indira College
Italic Indira College
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Q20 extends JFrame implements ActionListener {
JTextField input, upperOut, lowerOut, boldOut, italicOut;
JButton upper, lower, bold, italic;
Q20() {
setTitle("Text Converter");
setLayout(new GridLayout(5, 2, 10, 10));
setSize(500, 300);
add(new JLabel("Enter Text:"));
input = new JTextField();
add(input);
upper = new JButton("UPPER CASE");
add(upper);
upperOut = new JTextField();
upperOut.setEditable(false);
add(upperOut);
lower = new JButton("Lower Case");
add(lower);
lowerOut = new JTextField();
lowerOut.setEditable(false);
add(lowerOut);
bold = new JButton("Bold");
add(bold);
boldOut = new JTextField();
boldOut.setEditable(false);
add(boldOut);
// Row 5: Italic
italic = new JButton("Italic");
add(italic);
italicOut = new JTextField();
italicOut.setEditable(false);
add(italicOut);
// Button actions
upper.addActionListener(this);
lower.addActionListener(this);
bold.addActionListener(this);
italic.addActionListener(this);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
String text = input.getText();
if (e.getSource() == upper)
upperOut.setText(text.toUpperCase());
else if (e.getSource() == lower)
lowerOut.setText(text.toLowerCase());
else if (e.getSource() == bold)
boldOut.setFont(new Font("Arial", Font.BOLD, 14));
else if (e.getSource() == italic)
italicOut.setFont(new Font("Arial", Font.ITALIC, 14));
if (e.getSource() == bold)
boldOut.setText(text);
if (e.getSource() == italic)
italicOut.setText(text);
}
public static void main(String args[]) {
new Q20();
}
}
----------------------------------------------------------
Q21]Write a Java program to accept the file name from user and display the content of that file on the command prompt.
import java.io.*;
import java.util.Scanner;
class Q21
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.println("enter file name");
String filename=s1.nextLine();
try
{
FileReader f1=new FileReader(filename);
BufferedReader r1=new BufferedReader(f1);
String line;
System.out.println("file content");
while((line=r1.readLine()) != null)
{
System.out.println(line);
}
r1.close();
f1.close();
}
catch(IOException e)
{
System.out.println("file not found");
}
}
}
----------------------------------
Q22]to accept an array from user & also accept another number from user & check whether newly entered number exists in that array or not.
import java.util.*;
class Q22 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of array: ");
int n = sc.nextInt();
int a[] = new int[n];
System.out.println("Enter elements of array:");
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
System.out.print("Enter number to search: ");
int num = sc.nextInt();
boolean found = false;
for (int i = 0; i < n; i++) {
if (a[i] == num) {
found = true;
break;
}
}
if (found)
System.out.println(num + " is present in the array.");
else
System.out.println(num + " is NOT present in the array.");
}
}
----------------------------------------
Q23]Write a Java program to create an ArrayList of strings, add elements, and display them.
import java.util.ArrayList;
class Q23
{
public static void main(String args[])
{
ArrayListlist= new ArrayList<>();
list.add("apple");
list.add("mango");
list.add("cat");
System.out.println("Array list:"+list);
}
}
-------------------------------------------
Q24]to accept a two-dimensional array from user & display that array & all diagonal elements.
import java.util.*;
class Q24 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter size of square matrix: ");
int n = sc.nextInt();
int a[][] = new int[n][n];
System.out.println("Enter matrix elements:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
a[i][j] = sc.nextInt();
}
}
System.out.println("\nMatrix:");
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
System.out.println("\nDiagonal Elements:");
for (int i = 0; i < n; i++) {
System.out.print(a[i][i] + " ");
}
}
}
Q24B]
import java.util.Vector;
class Q24B
{
public static void main(String args[])
{
Vectorv1=new Vector<>();
v1.add(10);
v1.add(20);
v1.add(30);
System.out.println("vectore brfore removal:"+v1);
v1.remove(0);
System.out.println("vector after removal:"+v1);
}
}
----------------------------------------------------
Q25]to create a LinkedList of integers, add elements at the beginning and end, and display the list.
import java.util.LinkedList;
class Q25
{
public static void main(String args[])
{
LinkedListlist=new LinkedList<>();
list.add(20);
list.add(30);
list.add(40);
System.out.println("before adding:"+list);
list.addFirst(10);
list.addLast(50);
System.out.println("after adding:"+list);
}
}
-----------------------------------------------------
Q26]Write a Java program to accept a one-dimensional array from user and print that array on the command prompt.
Also print all array elements in reverse order.
import java.util.Scanner;
class Q26
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.println("enter size of 1-dim array");
int n=s1.nextInt();
int arr[]=new int[n];
for(int i=0;i=0;i--)
{
System.out.println(""+arr[i]);
}
}
}
------------------------------------
Q27]Write a Java program to create one package that accepts values of length (L) and breadth (B) from user and display the area of rectangle.
create folder pradnya and put this code file inside pradnya because package nedd folder
package pradnya;
import java.io.*;
class a
{
int l=0;
int b=0;
int area=0;
public void accept()
{
try
{
BufferedReader r1= new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter length:");
l=Integer.parseInt(r1.readLine());
System.out.println("enter breadth:");
b=Integer.parseInt(r1.readLine());
}
catch(Exception e)
{
System.out.println(e);
}
}
public void display()
{
area=l*b;
System.out.println("area of rectangle:"+area);
}
}
class Q27
{
public static void main(String args[])
{
a a1=new a();
a1.accept();
a1.display();
}
}
----------------------------------------------------------------
Q28]Write a Java AWT program to create a student registration form using TextField, Checkbox, and Button.
import java.awt.*;
import java.awt.event.*;
class Q28 extends Frame {
Q28() {
setLayout(new FlowLayout());
Label l1 = new Label("Name:");
TextField t1 = new TextField(20);
Label l2 = new Label("Course:");
Checkbox c1 = new Checkbox("BBA(CA)");
Checkbox c2 = new Checkbox("BCA");
Checkbox c3 = new Checkbox("BCS");
Button submit = new Button("Submit");
add(l1); add(t1);
add(l2); add(c1); add(c2); add(c3);
add(submit);
setTitle("Student Registration Form");
setSize(300, 200);
setVisible(true);
}
public static void main(String args[]) {
new Q28();
}
}
-------------------------------------------------------------
Q29]Write a Java Swing program that accepts two numbers in text fields, and on button click check whether numbers are same or not, display the message.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Q29 extends JFrame implements ActionListener {
JTextField t1, t2;
JButton check;
Q29() {
setTitle("Number Checker");
setLayout(new FlowLayout());
add(new JLabel("Enter First Number:"));
t1 = new JTextField(10);
add(t1);
add(new JLabel("Enter Second Number:"));
t2 = new JTextField(10);
add(t2);
check = new JButton("Check");
add(check);
check.addActionListener(this);
setSize(300, 200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
int num1 = Integer.parseInt(t1.getText());
int num2 = Integer.parseInt(t2.getText());
if (num1 == num2)
JOptionPane.showMessageDialog(this, "Both numbers are SAME!");
else
JOptionPane.showMessageDialog(this, "Numbers are DIFFERENT!");
}
public static void main(String args[]) {
new Q29();
}
}
-------------------------------------------------
Q30]Write a Java applet program to design a login frame.
import java.applet.*;
import java.awt.*;
/*
*/
public class Q30 extends Applet {
TextField user, pass;
Button login, reset;
public void init() {
setLayout(new GridLayout(3, 2, 10, 10));
Label l1 = new Label("Username:");
user = new TextField(15);
Label l2 = new Label("Password:");
pass = new TextField(15);
login = new Button("Login");
reset = new Button("Reset");
add(l1); add(user);
add(l2); add(pass);
add(login); add(reset);
}
}
---------------------------------------
Q31]Write a Java program to accept a file name from user, convert all uppercase letters to lowercase, and display the lowercase text on the command prompt.
import java.io.*;
import java.util.Scanner;
class Q31
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.println("enter file name");
String filename=s1.nextLine();
int lower;
try
{
FileReader f1=new FileReader(filename);
int ch;
System.out.println("file content");
while((ch=f1.read())!=-1)
{
char c=(char) ch;
if(Character.isUpperCase(c)){
c=Character.toLowerCase(c);
}
System.out.println(c);
}
f1.close();
}
catch(IOException e)
{
System.out.println("file not found");
}
}
}
----------------------------------------------------------
Q32]to accept the number from user and if the number is 0 then throw user-defined exception “This number is 0” and if the number is not 0 then display the reverse of that number on the command prompt.
import java.util.*;
class ZeroNumberException extends Exception {
ZeroNumberException(String msg) {
super(msg);
}
}
class Q32 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
try {
if (num == 0)
throw new ZeroNumberException("This number is 0");
int rev = 0;
int temp = num;
while (temp != 0) {
int digit = temp % 10;
rev = rev * 10 + digit;
temp = temp / 10;
}
System.out.println("Reverse of number: " + rev);
}
catch (ZeroNumberException e) {
System.out.println(e.getMessage());
}
}
}
------------------------------------------------------
Q33]Write a Java program to accept a file name from user and print all the content of that file.If any digit is found, then display * instead of that digit on the command prompt.
import java.io.*;
import java.util.Scanner;
class Q33
{
public static void main(String args[])
{
Scanner s1=new Scanner(System.in);
System.out.println("enter file name:");
String filename=s1.nextLine();
try
{
FileReader f1=new FileReader(filename);
BufferedReader r1=new BufferedReader(f1);
System.out.println("file content:");
int ch;
while((ch=r1.read()) != -1)
{
char c=(char) ch;
if(Character.isDigit(c))
{
System.out.println('*');
}
else
{
System.out.println(c);
}
}
r1.close();
f1.close();
}
catch(IOException e)
{
System.out.println("file not found");
}
}
}
-------------------------------------------------
Q34]to accept a number from the user. If the number is 0 then throw a user-defined exception “Number is 0”, and if it is not 0 then display the sum of its first and last digit.
import java.util.*;
class ZeroNumberException extends Exception {
public ZeroNumberException(String message) {
super(message);
}
}
class Q34 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = sc.nextInt();
try {
if (num == 0) {
throw new ZeroNumberException("This number is 0");
}
else {
int last = num % 10;
int first = num;
while (first >= 10) {
first = first / 10;
}
int sum = first + last;
System.out.println("Sum of first and last digit: " + sum);
}
}
catch (ZeroNumberException e) {
System.out.println("Exception caught: " + e.getMessage());
}
}
}
-----------------------------------------------------
Q35]
Write a Java program to create one package that accepts three numbers from main function and displays the minimum value among those numbers.
create folder pradnya and put this code file inside pradnya because package nedd folder
package pradnya;
import java.util.*;
class minimum
{
public void accept(int a,int b,int c)
{
if((a