Write a program using swing components to add and subtract two numbers. Use Text Fields for inputs and output. Your program should display the result when the user press button
package JavaLab10;
import javax.swing.*;
import java.awt.event.*;
public class AddSub implements ActionListener {
JFrame f;
JTextField t1,t2,t3;
JLabel l1,l2,l3;
JButton b1,b2;
public AddSub(){
f=new JFrame("Add and Subtract");
l1=new JLabel("First Number:");
l2=new JLabel("Second Number:");
l3=new JLabel("Result:");
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
b1=new JButton("Add");
b2=new JButton("Subtract");
b1.addActionListener(this);
b2.addActionListener(this);
l1.setBounds(20,20,100,20);
t1.setBounds(150 ,20,100,20);
l2.setBounds(20,50,100,20);
t2.setBounds(150,50,100,20);
l3.setBounds(20,110,100,20);
t3.setBounds(150,110,100,20);
b1.setBounds(20,80,80,20);
b2.setBounds(150,80,100,20);
f.setSize(300,400);
f.setVisible(true);
f.setDefaultCloseOperation(3);
f.setLayout(null);
f.add(l1); f.add(t1);
f.add(l2); f.add(t2);
f.add(l3); f.add(t3);
f.add(b1); f.add(b2);
}
public void actionPerformed(ActionEvent e){
double num1=Double.parseDouble(t1.getText());
double num2=Double.parseDouble(t2.getText());
if(e.getSource()==b1){
t3.setText(String.format("%2f",num1+num2));
l3.setText("add");
}
else if(e.getSource()==b2){
t3.setText(String.format("%2f",num1-num2));
l3.setText("sub");
}
}
public static void main(String[]args){
new AddSub();
}
}
Write a java program to find the smallest and largest among three numbers using swing components. Use text Fields for input and output your program should display the result when the user press button
package JavaLab10;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class SmallLargest implements ActionListener {
JFrame frame;
JTextField t1, t2, t3, t4, t5;
JLabel l1, l2, l3, l4, l5;
JButton b2;
public SmallLargest() {
frame = new JFrame("Find Smallest & Largest");
l1 = new JLabel("Number 1:");
l2 = new JLabel("Number 2:");
l3 = new JLabel("Number 3:");
l4 = new JLabel("Smallest:");
l5 = new JLabel("Largest:");
t1 = new JTextField();
t2 = new JTextField();
t3 = new JTextField();
t4 = new JTextField();
t5 = new JTextField();
// Output fields should be non-editable
t4.setEditable(false);
t5.setEditable(false);
b2 = new JButton("Find");
b2.addActionListener(this);
frame.setSize(350, 350);
frame.setLayout(null);
// Positioning components
l1.setBounds(50, 30, 100, 30);
t1.setBounds(150, 30, 150, 30);
l2.setBounds(50, 70, 100, 30);
t2.setBounds(150, 70, 150, 30);
l3.setBounds(50, 110, 100, 30);
t3.setBounds(150, 110, 150, 30);
l4.setBounds(50, 190, 100, 30);
t4.setBounds(150, 190, 150, 30);
l5.setBounds(50, 230, 100, 30);
t5.setBounds(150, 230, 150, 30);
b2.setBounds(150, 150, 120, 30);
// Adding components to frame
frame.add(l1); frame.add(t1);
frame.add(l2); frame.add(t2);
frame.add(l3); frame.add(t3);
frame.add(l4); frame.add(t4);
frame.add(l5); frame.add(t5);
frame.add(b2);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
// Parse input values
double num1 = Double.parseDouble(t1.getText());
double num2 = Double.parseDouble(t2.getText());
double num3 = Double.parseDouble(t3.getText());
// Calculate smallest and largest
double smallest = Math.min(num1, Math.min(num2, num3));
double largest = Math.max(num1, Math.max(num2, num3));
// Set the results
t4.setText(String.format("%.2f", smallest));
t5.setText(String.format("%.2f", largest));
}
public static void main(String[] args) {
new SmallLargest();
}
}
Write a java program to compute simple interest. The input and output required are taken from to prebuild dialog box.
package JavaLab10;
import javax.swing.*;
public class SimInterest {
public static void main(String[]args){
String Principle =JOptionPane.showInputDialog("Enter a Principle");
int p=Integer.parseInt(Principle);
String time =JOptionPane.showInputDialog("Enter a Time");
int t=Integer.parseInt(time);
String rate =JOptionPane.showInputDialog("Enter a Rate");
int r=Integer.parseInt(rate);
int SI=(p*t*r)/100;
JOptionPane.showInputDialog(null,"Simple Interest is"+SI);
}
}
Write a java program using swing component to create student registration from with field(text field for name, address, email, password, radio button for gender, checkbox for Hobbies, country as dropdown list, opinion as text area, one buttone for submit) Your program display the student information when user click on submit button
package JavaLab10;
import javax.swing.*;
import java.awt.event.*;
public class Registration implements ActionListener{
JFrame f;
JLabel l1,l2,l3,l4,l5,l6,l7;
JTextField t1,t2,t3;
JRadioButton r1,r2;
JCheckBox c1,c2,c3;
JComboBox cb;
JTextArea ta;
JButton b1;
JPasswordField pw;
public Registration(){
f=new JFrame("Student Registration Form");
l1=new JLabel("Name:");
t1=new JTextField(20);
l2=new JLabel("Address:");
t2=new JTextField(30);
l3=new JLabel("Email:");
t3=new JTextField();
l4=new JLabel("Password:");
pw=new JPasswordField(20);
l5=new JLabel("Gender:");
r1=new JRadioButton("Male");
r2=new JRadioButton("Female");
ButtonGroup bg=new ButtonGroup();
bg.add(r1); bg.add(r2);
l6=new JLabel("Hobbies:");
c1=new JCheckBox("Running");
c2=new JCheckBox("ReadingBook");
c3=new JCheckBox("Circket");
l7=new JLabel("Country:");
String[]countries={"Nepal","India","USA","China"};
cb=new JComboBox(countries);
b1=new JButton("Submit");
b1.addActionListener(this);
ta=new JTextArea(500,500);
ta.setEditable(false);
l1.setBounds(30,30,80,23);
t1.setBounds(130,30,200,25);
l2.setBounds(30,70,80,25);
t2.setBounds(130,70,200,25);
l3.setBounds(30,110,80,25);
t3.setBounds(130,110,200,25);
l4.setBounds(30,150,80,25);
pw.setBounds(130,150,200,25);
l5.setBounds(30,190,80,25);
r1.setBounds(130,190,70,25);
r2.setBounds(200,190,80,25);
l6.setBounds(30,230,80,25);
c1.setBounds(130,230,80,25);
c2.setBounds(210,230,80,25);
c3.setBounds(290,230,80,25);
l7.setBounds(30,270,80,25);
cb.setBounds(130,270,200,30);
ta.setBounds(30,360,340,150);
b1.setBounds(130,310,100,30);
f.setSize(500,700);
f.setVisible(true);
f.setDefaultCloseOperation(3);
f.setLayout(null);
f.add(l1); f.add(t1);
f.add(l2); f.add(t2);
f.add(l3); f.add(t3);
f.add(l4); f.add(pw);
f.add(l5); f.add(r1);f.add(r2);
f.add(l6); f.add(c1);f.add(c2);f.add(c3);
f.add(l7); f.add(cb);
f.add(b1);
f.add(ta);
}
public void actionPerformed(ActionEvent e){
String name=t1.getText();
String address=t2.getText();
String email=t3.getText();
String password=new String(pw.getPassword());
String gender=r1.isSelected()?"Male": r2.isSelected()?"Female":"Not selected";
String hobbies="";
if(c1.isSelected())hobbies+="Running";
if(c2.isSelected())hobbies+="Reading";
if(c3.isSelected())hobbies+="Circket";
String country=(String)cb.getSelectedItem();
ta.setText("Name:"+name+"\n"
+ "Address:"+address+"\n"
+ "Email:"+email+"\n"
+"Password:"+password+"\n"
+"Gender:"+gender+"\n"
+"Hobbies:"+hobbies+"\n"
+"Country:"+country);
}
public static void main(String[]args){
new Registration();
}
}
Write a java Program to illistrate the following swing component jMenu
package JavaLab10;
import javax.swing.*;
import java.awt.event.*;
public class MenuApp {
JFrame f;
JMenuBar mb;
JMenu m1,m2;
JMenuItem l1,l2,l3;
JRadioButtonMenuItem r1,r2;
JCheckBoxMenuItem cm;
public MenuApp(){
f=new JFrame("Menu Example");
mb=new JMenuBar();
m1=new JMenu("File");
m2=new JMenu("Edit");
l1=new JMenuItem("New");
l2=new JMenuItem("Open");
l3=new JMenuItem("Save");
r1=new JRadioButtonMenuItem("Redo");
r2=new JRadioButtonMenuItem("Undo");
ButtonGroup bg=new ButtonGroup();
bg.add(r1); bg.add(r2);
cm=new JCheckBoxMenuItem("Copy");
m1.add(l1);m1.add(l2);m1.add(l3);
m2.add(r1); m2.add(r2); m2.add(cm);
mb.add(m1);mb.add(m2);
f.setJMenuBar(mb);
f.setSize(400,400);
f.setVisible(true);
f.setDefaultCloseOperation(3);
l1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(f,"New Menu item is clicked");
}
});
}
public static void main(String[]args){
new MenuApp();
}
}
Write a java Program to illistrate the following swing component Jtable
package JavaLab10;
import javax.swing.*;
public class JTableEx {
JFrame f;
JTable tb;
JScrollPane sp;
public JTableEx(){
f=new JFrame("Table Example");
String[]Colname={"Roll","Name","Address"};
String[][] data = {
{ "101", "Bhuban Subedi", "Dolakha" },
{ "102", "Basanta", "Kathmandu" },
{ "103", "Bibek", "Pyuthan" }
};
tb=new JTable(data,Colname);
sp=new JScrollPane(tb);
f.setSize(300,300);
f.setVisible(true);
f.setDefaultCloseOperation(3);
f.add(sp);
}
public static void main(String[]args){
new JTableEx();
}
}
Write a java Program to illistrate the following swing component JInternal Frame and JDesktop
You Must Need Xampp Install in your Compur First.
Start Apache and Sql from Xampp Control Panel
Go to MySQL Admin then type code or you can create database and table manually easy way
CREATE DATABASE EmployeeDB;
USE EmployeeDB;
CREATE TABLE Employee (
eid INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
address VARCHAR(255),
department VARCHAR(100)
);

package JavaLab10;
import javax.swing.*;
import java.awt.*;
public class DesktopApp {
JFrame f;
JDesktopPane dp;
JInternalFrame f1,f2;
public DesktopApp(){
f=new JFrame("MDI Application");
dp=new JDesktopPane();
f.add(dp);
f.setSize(500,400);
f.setVisible(true);
f.setDefaultCloseOperation(3);
f1=new JInternalFrame("Frame1",true,true,true,true);
f1.setSize(200,200);
f1.setVisible(true);
f2=new JInternalFrame("Frame2",true,true,true,true);
f2.setSize(200,200);
f2.setVisible(true);
dp.add(f1);
dp.add(f2);
}
public static void main(String[]args){
new DesktopApp();
}
}
Write a java Program to connect to the database and Perform Crud Operation
package JavaLab10;
import java.sql.*;
public class StoreSql {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/bhuban", "root", " ");
String sql = "INSERT INTO employees VALUES (12, 'Bitulla', 'jorpati', 'BA'),(17, 'Bibek', 'Anamnagar', 'BCA'),(16, 'bhuban', 'BKT', 'BCA'),(17, 'Rachana', 'Kathmandu', 'BA')";
Statement st = con.createStatement();
int row = st.executeUpdate(sql);
if (row > 0) {
System.out.println(row+"data inserted successfully");
} else {
System.out.println(row+"error in insertion");
}
con.close();
}
}
Write a java Program to connect to the database and Perform Delete
package JavaLab10;
import java.sql.*;
public class DeleteSql {
public static void main(String[] args) throws Exception {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost/bhuban", "root", "");
String sql = "delete from (17, 'Ram', 'Anamnagar', 'BCA') where eid ='112'";
Statement st = con.createStatement();
int row = st.executeUpdate(sql);
if (row > 0) {
System.out.println(+row+"deleted successfully");
} else {
System.out.println(+row+"error in deletion");
}
con.close();
}
}
Write a java program using swing Component to find Area and Perimeter of rectangle use text field for inputs and output. Your Program Should display the result When the user click a button.
package JavaLab10;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
public class Q1AreaParameter implements ActionListener {
JFrame f;
JLabel l1, l2, l3;
JTextField t1, t2, t3;
JButton b1, b2;
public Q1AreaParameter() {
f = new JFrame("Area and Perimeter");
l1 = new JLabel("Length:");
l1.setBounds(30, 30, 80, 25);
t1 = new JTextField();
t1.setBounds(120, 30, 120, 25);
l2 = new JLabel("Breadth:");
l2.setBounds(30, 70, 80, 25);
t2 = new JTextField();
t2.setBounds(120, 70, 120, 25);
l3 = new JLabel("Result:");
l3.setBounds(30, 110, 80, 25);
t3 = new JTextField();
t3.setBounds(120, 110, 120, 25);
t3.setEditable(false);
b1 = new JButton("Area");
b1.setBounds(30, 160, 100, 30);
b2 = new JButton("Perimeter");
b2.setBounds(140, 160, 100, 30);
b1.addActionListener(this);
b2.addActionListener(this);
f.add(l1); f.add(t1);
f.add(l2); f.add(t2);
f.add(l3); f.add(t3);
f.add(b1); f.add(b2);
f.setSize(300, 250);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(3);
}
@Override
public void actionPerformed(ActionEvent e) {
double length = Double.parseDouble(t1.getText());
double breadth = Double.parseDouble(t2.getText());
if (e.getSource() == b1) {
double area = length * breadth;
l3.setText("Area: ");
t3.setText(String.valueOf(area));
} else if (e.getSource() == b2) {
double perimeter = 2 * (length + breadth);
l3.setText("Perimeter: ");
t3.setText(String.valueOf(perimeter));
}
}
public static void main(String[] args) {
new Q1AreaParameter();
}
}
Write a java program to illustrate the Following class java.util package -Random Class
package Lab9;
import java.util.*;
public class Q5Random {
public static void main (String[] args){
Random random= new Random();
int a = random.nextInt(9);
System.out.println("Integer random number="+a);
double b = random.nextDouble(1);
System.out.println("Double random number="+b);
boolean c= random.nextBoolean();
System.out.println("Boolean random value ="+c);
}
}
Write a java program to illustrate the Following class java.util package -Hashtable Class
package Lab9;
import java.util.Hashtable;
public class Q5Hashtable {
public static void main(String[]args){
Hashtable htable = new Hashtable<>();
htable.put(201,"DSA");
htable.put(202,"Statistics");
htable.put(203,"SAD");
htable.put(204,"OOP in java");
htable.put(205,"Web Technology");
System.out.println("Total elements = "+htable.size());
System.out.println(htable);
htable.remove(203);
System.out.println("Hash element after remove 203:"+htable);
}
}
Write a java program to illustrate the Following class java.util package -Stack Class
package Lab9;
import java.util.Stack;
public class Q5Stack {
public static void main(String[] args) {
Stack stack = new Stack<>();
stack.push(50);
stack.push(70);
stack.push(90);
System.out.println("Stack: " + stack);
System.out.println("Top element: " + stack.peek());
System.out.println("Popped elements: " + stack.pop());
System.out.println("Stack after pop: " + stack);
System.out.println("Is stack empty? " + stack.empty());
}
}
Write a java program to illustrate the Following class java.util package -Vector Class
package Lab9;
import java.util.*;
public class Q5Vector {
public static void main(String []args){
Vector v= new Vector();
v.add(15);
v.add(20);
v.add(25);
v.add(30);
v.add(35);
System.out.println("Total Element in vector="+v.size());
System.out.println("Element in vector="+v);
System.out.println("Element Index 4: "+v.get(4));
v.remove(3);
System.out.println("Remaining Element:"+v);
v.clear();
System.out.println("vector Elements:"+v);
}
}
Write a java program to perform autoboxing and Autounboxing using wrapper Class
package Lab9;
import java.io.*;
public class Q4AutoBoxing {
public static void main(String[] args) {
int x = 99;
Integer num = x;
System.out.println("The value of x "+x);
System.out.println("The value of num = "+num);
Double y = 77.77;
double d =y;
System.out.println("The value of y ="+y);
System.out.println("The value of d ="+d);
}
}
Write a java program to save 3 student record (such as roll, name, address, and phone number ) into file student.txt and display the student record whose address is kritipur
package Lab9;
import java.io.*;
import java.util.*;
class Student implements Serializable {
int roll;
String name;
String address;
String phoneNumber;
public Student(int roll, String name, String address, String phoneNumber) {
this.roll = roll;
this.name = name;
this.address = address;
this.phoneNumber = phoneNumber;
}
}
public class Q3Student {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
Student[] std = new Student[3];
FileOutputStream fout = new FileOutputStream("student.txt");
ObjectOutputStream os = new ObjectOutputStream(fout);
for (int i = 0; i < 3; i++) {
System.out.println("Enter info of Student " + (i + 1));
System.out.print("Roll: ");
int roll = sc.nextInt();
sc.nextLine();
System.out.print("Name: ");
String name = sc.nextLine();
System.out.print("Address: ");
String address = sc.nextLine();
System.out.print("Phone Number: ");
String phoneNumber = sc.nextLine();
std[i] = new Student(roll, name, address, phoneNumber);
os.writeObject(std[i]);
}
os.close();
fout.close();
System.out.println("File Write Successfully");
FileInputStream fin = new FileInputStream("student.txt");
ObjectInputStream oin = new ObjectInputStream(fin);
System.out.println("\nStudent Details from Kritipur:");
for (int i = 0; i < 3; i++) {
Student st = (Student) oin.readObject();
if (st.address.equalsIgnoreCase("Kritipur")) {
System.out.println(st.roll + "\t" + st.name + "\t" + st.address + "\t" + st.phoneNumber);
}
}
oin.close();
fin.close();
}
}
Write a java program to write and read file using FileWriter and FileReader class
package Lab9;
import java.io.*;
public class ReadWrite2 {
public static void main(String[] args) throws Exception {
String data = "My name is Bhuban Subedi";
FileWriter fw = new FileWriter("vuone.txt");
fw.write(data);
fw.close();
System.out.println("File Write Successfully");
FileReader fr = new FileReader("vuone.txt");
int i;
while ((i = fr.read()) != -1) {
System.out.print((char) i);
}
System.out.println("");
fr.close();
}
}
Write a java program to write a sentence "java is object Oriented Programming " using FileOutput Stream class and read this file using FileInputStream class and find total number of vowel in the file
package Lab9;
import java.io.*;
public class Q1Vowel {
public static void main(String[]args)throws Exception
{
String str="Java is object oriented programming";
byte[] letter= str.getBytes();
FileOutputStream fout= new FileOutputStream("Bhuban.txt");
fout.write(letter);
fout.close();
System.out.println("file written successfully");
FileInputStream fin = new FileInputStream("Bhuban.txt");
int i;
while ((i = fin.read()) != -1) {
char vowels = (char) i;
if (vowels == 'a' || vowels == 'e' || vowels == 'i' || vowels == 'o' || vowels == 'u') {
System.out.print(vowels + " ");
}
}
System.out.println();
fin.close();
}
}
WAP to demonstrate the concept of Synchronization in multithreading application
package Lab8;
class PrintStr {
public synchronized void display() {
try {
System.out.println("Java is");
Thread.sleep(1500);
System.out.println("Object Oriented Programming");
System.out.println("Thread priority: " + Thread.currentThread().getPriority());
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
class CallerThread extends Thread {
PrintStr ps;
public CallerThread(PrintStr ps) {
this.ps = ps;
}
public void run() {
ps.display();
}
}
public class Q6 {
public static void main(String[] args) {
PrintStr ps = new PrintStr();
CallerThread t1 = new CallerThread(ps);
CallerThread t2 = new CallerThread(ps);
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}
WAP to set and get Thread Priorities as multithread application
package Lab8;
class Multithread extends Thread {
public void run() {
System.out.println("Thread name: " + Thread.currentThread().getName() +","+
"Thread Priority: " + Thread.currentThread().getPriority());
}
}
public class Q5 {
public static void main(String[] args) {
Multithread t1 = new Multithread();
Multithread t2 = new Multithread();
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}
WAP to develop Multithreaded application by following ways a) By implementing Runable Interface b) By extending Thread Class
package Lab8;
class FirstThread implements Runnable {
public void run() {
System.out.println("FirstThread by implementing Runnable interface:");
for (int i = 0; i < 11; i++) {
if (i % 2 != 0) {
System.out.println("First \t" + i);
}
}
}
}
class SecondThread extends Thread {
public void run() {
System.out.println("SecondThread by extending Thread class:");
for (int i = 0; i < 18; i++) {
if (i % 2 == 0) {
System.out.println("Second \t" + i);
}
}
}
}
public class DevelopMultiThreadApp {
public static void main(String[] args) {
Thread t1 = new Thread(new FirstThread());
SecondThread t2 = new SecondThread();
t1.start();
t2.start();
}
}
WAP that will read the balance and withdraw amount from keyboard and display the remaning balance on screen if the balance is greater than withdraw amount otherwise throw an exception with approprate message
package Lab8;
import java.util.Scanner;
class BalanceException extends Exception {
public BalanceException(String message) {
super(message);
}
}
public class Q2 {
public static void main(String[] args) {
try {
Scanner sc = new Scanner(System.in);
System.out.println("Enter balance:");
double balance = sc.nextDouble();
System.out.println("Enter withdraw amount:");
double withdraw = sc.nextDouble();
if (balance >= withdraw) {
balance = balance - withdraw;
System.out.println("Remaining Balance = " + balance);
} else {
throw new BalanceException("Insufficient balance");
}
} catch (BalanceException ex) {
System.out.println("Exception: " + ex.getMessage());
}
}
}
WAP to set and get Thread Priorities as multithread application
package Lab8;
class Multithread extends Thread {
public void run() {
System.out.println("Thread name: " + Thread.currentThread().getName() +","+
"Thread Priority: " + Thread.currentThread().getPriority());
}
}
public class Q5 {
public static void main(String[] args) {
Multithread t1 = new Multithread();
Multithread t2 = new Multithread();
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}
WAP to demonstrate the concept of synchronization in multithreading application
package Lab8;
class PrintStr {
public synchronized void display() {
try {
System.out.println("Java is");
Thread.sleep(1500);
System.out.println("Object Oriented Programming");
System.out.println("Thread priority: " + Thread.currentThread().getPriority());
} catch (Exception e) {
System.out.println("Exception: " + e.getMessage());
}
}
}
class CallerThread extends Thread {
PrintStr ps;
public CallerThread(PrintStr ps) {
this.ps = ps;
}
public void run() {
ps.display();
}
}
public class Q6 {
public static void main(String[] args) {
PrintStr ps = new PrintStr();
CallerThread t1 = new CallerThread(ps);
CallerThread t2 = new CallerThread(ps);
t1.setPriority(Thread.MAX_PRIORITY);
t2.setPriority(Thread.MIN_PRIORITY);
t1.start();
t2.start();
}
}
WAP to demonstrate Arithmetic exception ArrayIndexOutofBoundException, And NumberFormatException Class
package Lab8;
public class Q11 {
public static void main(String[] args) {
try {
String A="123CBA";
int num=Integer.parseInt(A);
System.out.println("The number:"+num);
}
catch(NumberFormatException ex){
System.out.println("Exception:"+ex.getMessage());
}
finally{
System.out.println("Finally block excuted");
}
}
}
Write a Java program to demonstrate ArithmeticException, ArrayIndexOutOfBoundsException, and NumberFormatException using try, catch, and finally blocks.
package Lab8;
import java.util.*;
public class q12 {
public static void main(String[]args){
try{
Scanner sc=new Scanner(System.in);
System.out.println("Enter two number:");
int n1=sc.nextInt();
int n2=sc.nextInt();
int div=n1/n2;
System.out.println("Division="+div);
}
catch(ArithmeticException ex){
System.out.println("Error Message="+ex.getMessage());
}
finally{
System.out.println("Finally block is Executed");
}
}
}
Write a java program to create a class student having property name address and roll set default and use defined value of these property using constructors and then desplay of these property
package lab6;
class Student {
int roll;
String name,address;
public Student(int r, String n, String a)
{
roll=r;
name=n;
address=a;
}
public Student()
{
}
public void display()
{
System.out.println("Student Name="+name);
System.out.println("Student Roll="+roll);
System.out.println("Student Address="+address);
}
public void display2(){
System.out.println("Student Name="+name);
System.out.println("Student Roll="+roll);
System.out.println("Student Address="+address);
}
}
public class MyStudent
{
public static void main(String[]args)
{
Student s1=new Student();
s1.display2();
Student s2=new Student(16,"Bhuban Subedi","Bhaktapur");
s2.display();
}
}
Write a class box with private varible width height and depth and methods to find volume and surface area Use suitable constructors Implements the class to find volume and surface area of two boxes
package lab6;
class Box{
private int width;
private int height;
private int depth;
public Box()
{
}
public Box( int w,int h,int d)
{
width=w;
height=h;
depth=d;
}
public int areaVolume()
{
return (depth*width*height);
}
public int areaSurface()
{
return 2*(width*height + width*depth + height*depth);
}
}
public class MyBox
{
public static void main(String []args)
{
Box b1 = new Box();
Box b2 = new Box(4,10,8);
Box b3 = new Box(7,7,7);
int a1 = b1.areaVolume();
int av =b1.areaSurface();
System.out.println("Area Volume = "+a1+ " Area Surface = "+av);
int a2 = b2.areaVolume();
int av2 =b2.areaSurface();
System.out.println("Box-1 Area Volume = "+a2 + " Area Surface="+av2);
int a3 = b3.areaVolume();
int av3 =b3.areaSurface();
System.out.println("Box-2 Area Volume = "+a3 + " Area Surface="+av3);
}
}