Uploaded by Harleen kaur

Himanshu Kumar Dutt 094 JAVA (1)

advertisement
STUDENT NAME : HIMANSHU KUMAR DUTT
Institute of Innovation in Technology and Management, New Delhi
(Affiliated to GGSIP University)
List of questions for Practical – VIII Java Lab
Programme:
BCA
Date of commencement
of classes: 23rd Semester:
July’ 2007 IV
Academic Year: 2022-23
Serial No
Paper Code: BCA 272
Question Statement
Date on
which
executed
Write a program declaring a class Rectangle with data
member’s length and breadth and member functions Input,
Output and CalcArea.
16/03/23
1
2
Write a program to find the greatest of three numbers (take
the input from the user) in java.
21/03/23
3
Write the program to show switch case by taking the input
from the user.
Write a program to enter 10 numbers in an array from the
user and show in ascending order.
Write a program to demonstrate use of method overloading
to calculate area of square, rectangle and triangle.
Write a program to call method using static keyword and
non-static keyword.
Write a program of method overloading using
DecimalFormatclass.
22/03/23
Write a program to perform constructor overloading.
28/03/23
Write a program to demonstrate the use of static variable,
static method and static block.
Write a program to demonstrate concept of ``this``.
29/03/23
Write a program to demonstrate multi-level and hierarchical
inheritance.
Write a program to use super() to invoke base class
constructor.
Write a program to demonstrate the concept of interface
when two interfaces have unique methods and same data
members.
Write a program to demonstrate the concept of abstract
class with constructor and ``final`` method.
Write a program to perform package operation using
multiple class
1/04/23
4
5
6
7
8
9
10
11
12
13
14
15
Page 1
23/03/23
23/03/23
25/03/23
25/03/23
29/03/23
5/04/23
11/04/23
11/04/23
12/04/23
Signature
STUDENT NAME : HIMANSHU KUMAR DUTT
Write a program to demonstrate unchecked exception.
18/04/23
Write a program to demonstrate the concept of aggregation.
20/04/23
18
Write a program to demonstrate run-time polymorphism.
20/04/23
19
Write a program to demonstrate checked exception during file
handling
25/04/23
16/05/23
24
Write a program to use Byte stream class to read from a text
file and display the content on the output screen
Write a program to use character stream class to read from a
text file and display the content on the output screen
Write a program to make use of BufferedStream to read lines
from the keyboard until 'STOP' is typed
Write a program to demonstrate creation of multiple child
threads
Write a program to demonstrate any event handling.
25
Write a swing application that uses atleast 5 swing controls
1/06/23
26
Write a program to implement border layout using Swing.
5/06/23
Write program that uses swings to display combination of
RGB using 3 scrollbars.
Write a Java program to perform basic Calculator operations.
Make a menu driven program to select operation to perform
(+ - * / ). Take 2 integers and perform operation as chosen by
user e.
Write a java program to retrieve data from database
8/6/23
16
17
20
21
22
23
27
28
27/04/23
18/05/23
30/05/23
7/06/23
8/6/23
9/6/23
29
30
Write a java program to insert data to database using GUI.
Page 2
9/6/23
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 16/03/23.
Roll No. 09490302021
Program/ Experiment No.1
AIM: Write a program declaring a class Rectangle with data member’s length and
breadth and member functions Input, Output and CalcArea.
CODING
import java.util.Scanner;
class Rectangle {
double length;
double breadth;
double area;
void Input(){
Scanner sc = new Scanner(System.in);
System.out.print("Enter the length: ");
length = sc.nextDouble();
System.out.print("Enter the breadth: ");
breadth = sc.nextDouble();
sc.close();
}
void Output(){
System.out.println("Length: " + length);
System.out.println("Breadth: " + breadth);
System.out.println("Area: " + area);
}
void CalcArea(){
area = length * breadth;
}
}
class Main {
public static void main(String[] args) {
Rectangle rectangle = new Rectangle();
rectangle.Input();
rectangle.CalcArea();
rectangle.Output();
}
}
Page 3
STUDENT NAME : HIMANSHU KUMAR DUTT
OUTPUT
Page 4
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 21/03/23
Roll No. 09490302021
Program/ Experiment No.2
AIM: Write a program to find the greatest of three numbers (take the input from the
user) in java.
CODING
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = sc.nextInt();
System.out.print("Enter the second number: ");
int num2 = sc.nextInt();
System.out.print("Enter the third number: ");
int num3 = sc.nextInt();
int greatest = num1;
if(num2 > greatest){
greatest = num2;
}
if(num3 > greatest){
greatest = num3;
}
sc.close();
System.out.println("The greatest number is: " + greatest);
}
}
OUTPUT
Date of Experiment */ Program: 22/03/23
Page 5
Program/ Experiment No.3
STUDENT NAME : HIMANSHU KUMAR DUTT
AIM: Write the program to show switch case by taking the input from the user.
CODING
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the month number (1-12): ");
int month = sc.nextInt();
switch(month){
case 1:
System.out.println("January");
break;
case 2:
System.out.println("February");
break;
case 3:
System.out.println("March");
break;
case 4:
System.out.println("April");
break;
case 5:
System.out.println("May");
break;
case 6:
System.out.println("June");
break;
case 7:
System.out.println("July");
break;
case 8:
System.out.println("August");
break;
case 9:
System.out.println("September");
break;
case 10:
System.out.println("October");
break;
case 11:
System.out.println("November");
break;
case 12:
System.out.println("December");
break;
default:
System.out.println("Invalid month number.");
Page 6
STUDENT NAME : HIMANSHU KUMAR DUTT
}
sc.close();
}
}
OUTPUT
Page 7
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 23/03/23
Program/ Experiment No.4
AIM: Write a program to enter 10 numbers in an array from the user and show in
ascending order.
CODING
import java.util.Arrays;
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[10];
System.out.println("Enter 10 numbers: ");
for(int i=0; i<10; i++){
arr[i] = sc.nextInt();
}
Arrays.sort(arr);
System.out.println("Numbers in ascending order: ");
for(int i=0; i<10; i++){
System.out.println(arr[i]);
}
sc.close();
}
}
OUTPUT
Page 8
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 23/03/23
Program/ Experiment No.5
AIM: Write a program to demonstrate use of method overloading to calculate area of
square, rectangle and triangle.
CODING
import java.util.Scanner;
class Main {
public static int area(int side){
return side*side;
}
public static int area(int length, int breadth){
return length*breadth;
}
public static double area(int base, double height){
return 0.5*base*height;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter side length of square: ");
int side = sc.nextInt();
System.out.println("Area of square: " + area(side));
System.out.println("Enter length and breadth of rectangle: ");
int length = sc.nextInt();
int breadth = sc.nextInt();
System.out.println("Area of rectangle: " + area(length, breadth));
System.out.println("Enter base and height of triangle: ");
int base = sc.nextInt();
int height = sc.nextInt();
System.out.println("Area of triangle: " + area(base, height));
sc.close();
}}
OUTPUT
Page 9
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 25/03/23
Program/ Experiment No.6
AIM: Write a program to call method using static keyword and non-static keyword.
CODING
class Main {
static void staticMethod(){
System.out.println("This is a static method.");
}
void nonStaticMethod(){
System.out.println("This is a non-static method.");
}
public static void main(String[] args) {
staticMethod();
Main obj = new Main();
obj.nonStaticMethod();
}
}
OUTPUT
Page 10
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 25/03/23
Program/ Experiment No.7
AIM: Write a program of method overloading using DecimalFormatclass.
CODING
import java.text.DecimalFormat;
class Main {
static void print(double num){
DecimalFormat df = new DecimalFormat("#.00");
System.out.println(df.format(num));
}
static void print(String str){
System.out.println(str);
}
public static void main(String[] args) {
print(3.14159);
print("Hello, world!");
}
}
OUTPUT
Page 11
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 28/03/23
Program/ Experiment No.8
AIM: Write a program to perform constructor overloading.
CODING
class Rectangle {
double length;
double breadth;
double area;
Rectangle(){
length = 0;
breadth = 0;
}
Rectangle(double side){
length = side;
breadth = side;
}
Rectangle(double l, double b){
length = l;
breadth = b;
}
void CalcArea(){
area = length * breadth;
}
void Output(){
System.out.println("Length: " + length);
System.out.println("Breadth: " + breadth);
System.out.println("Area: " + area);
}
public static void main(String[] args) {
Rectangle rectangle1 = new Rectangle();
rectangle1.CalcArea();
rectangle1.Output();
Rectangle rectangle2 = new Rectangle(5);
rectangle2.CalcArea();
rectangle2.Output();
Rectangle rectangle3 = new Rectangle(3, 4);
rectangle3.CalcArea();
rectangle3.Output();
}}
OUTPUT
Page 12
STUDENT NAME : HIMANSHU KUMAR DUTT
Page 13
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 29/03/23
Program/ Experiment No.9
AIM: Write a program to demonstrate the use of static variable, static method and
static block.
CODING
class Main {
static int num1 = 10;
static int num2;
static {
System.out.println("Static block is initialized.");
num2 = num1 * 2;
}
static void staticMethod(){
System.out.println("Value of num1: " + num1);
System.out.println("Value of num2: " + num2);
}
public static void main(String[] args) {
staticMethod();
}
}
OUTPUT
Page 14
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 29/03/23
Program/ Experiment No.10
AIM: Write a program to demonstrate concept of ``this``.
CODING
class Person {
String name;
int age;
public Person(String name, int age){
this.name = name;
this.age = age;
}
public void display(){
System.out.println("Name: " + this.name);
System.out.println("Age: " + this.age);
}
public static void main(String[] args) {
Person person = new Person("John", 25);
person.display();
}
}
OUTPUT
Page 15
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 01/04/23
Program/ Experiment No.11
AIM: Write a program to demonstrate multi-level and hierarchical inheritance.
CODING
class Animal {
void eat() {
System.out.println("Eating...");
}}
class Dog extends Animal {
void bark() {
System.out.println("Barking...");
}}
class Labrador extends Dog {
void color() {
System.out.println("Labrador is black in color.");
}}
class Cat extends Animal {
void meow() {
System.out.println("Meowing...");
}}
class Kitten extends Cat {
void color() {
System.out.println("Kitten is white in color.");
}}
class Main {
public static void main(String args[]) {
Labrador lab = new Labrador();
lab.color();
lab.bark();
lab.eat();
Kitten kit = new Kitten();
kit.color();
kit.meow();
kit.eat();
}}
OUTPUT
Page 16
STUDENT NAME : HIMANSHU KUMAR DUTT
Page 17
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 05/04/23
Program/ Experiment No.12
AIM: Write a program to use super() to invoke base class constructor.
CODING
class Animal {
String name;
Animal(String name) {
this.name = name;
System.out.println("Animal constructor called.");
}
}
class Dog extends Animal {
Dog(String name) {
super(name);
System.out.println("Dog constructor called.");
}
}
class SuperDemo {
public static void main(String[] args) {
Dog dog = new Dog("Tommy");
System.out.println("Dog name is " + dog.name);
}
}
OUTPUT
Page 18
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 11/04/23
Program/ Experiment No.13
AIM: Write a program to demonstrate the concept of interface when two interfaces
have unique methods and same data members.
CODING
interface Animal {
int legs = 4;
void move();
}
interface Bird {
int wings = 2;
void fly();
}
class Eagle implements Animal, Bird {
public void move() {
System.out.println("Eagle moves on its legs.");
}
public void fly() {
System.out.println("Eagle flies using its wings.");
}
}
class InterfaceDemo {
public static void main(String[] args) {
Eagle eagle = new Eagle();
eagle.move();
eagle.fly();
System.out.println("Eagle has " + Animal.legs + " legs and " + Bird.wings + " wings.");
}
}
OUTPUT
Page 19
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 11/04/23
Program/ Experiment No.14
AIM: Write a program to demonstrate the concept of abstract class with constructor
and ``final`` method.
CODING
abstract class Animal {
String name;
Animal(String name) {
this.name = name;
System.out.println("Animal constructor called.");
}
final void display() {
System.out.println("Name: " + name);
}
abstract void move();
}
class Dog extends Animal {
Dog(String name) {
super(name);
System.out.println("Dog constructor called.");
}
void move() {
System.out.println("Dog moves on its legs.");
}
}
class AbstractDemo {
public static void main(String[] args) {
Dog dog = new Dog("Tommy");
dog.display();
dog.move();
}
}
OUTPUT
Page 20
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 12/04/23
Program/ Experiment No.15
AIM: Write a program to perform package operation using multiple class.
CODING
ClassA.java :
package myPackage;
public class ClassA {
public void methodA() {
System.out.println("Method A called from ClassA.");
}
}
ClassB.java :
package myPackage;
public class ClassB {
public void methodB() {
System.out.println("Method B called from ClassB.");
}
}
Package.java
import myPackage.ClassA;
import myPackage.ClassB;
class PackageDemo {
public static void main(String[] args) {
ClassA a = new ClassA();
ClassB b = new ClassB();
a.methodA();
b.methodB();
}
}
OUTPUT
Page 21
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 18/04/23
Program/ Experiment No.16
AIM: Write a program to demonstrate unchecked exception.
CODING
class UncheckedExceptionDemo {
public static void main(String[] args) {
int[] arr = {1, 2, 3};
System.out.println(arr[3]); // ArrayIndexOutOfBoundsException
}
}
OUTPUT
Page 22
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 20/04/23
Program/ Experiment No.17
AIM: Write a program to demonstrate the concept of aggregation.
CODING
class Address {
String street, city, state;
Address(String street, String city, String state) {
this.street = street;
this.city = city;
this.state = state;
}
}
class Employee {
int id;
String name;
Address address;
Employee(int id, String name, Address address) {
this.id = id;
this.name = name;
this.address = address;
}
void display() {
System.out.println("ID: " + id);
System.out.println("Name: " + name);
System.out.println("Address: " + address.street + ", " + address.city + ", " +
address.state);
}
}
class AggregationDemo {
public static void main(String[] args) {
Address addr = new Address("123 Main St", "Anytown", "CA");
Employee emp = new Employee(101, "John Doe", addr);
emp.display();
}
}
OUTPUT
Page 23
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 20/04/23
Program/ Experiment No.18
AIM: Write a program to demonstrate run-time polymorphism.
CODING
class Animal {
void makeSound() {
System.out.println("Animal is making a sound.");
}
}
class Dog extends Animal {
void makeSound() {
System.out.println("Dog is barking.");
}
}
class Cat extends Animal {
void makeSound() {
System.out.println("Cat is meowing.");
}
}
class PolymorphismDemo {
public static void main(String[] args) {
Animal animal = new Animal();
animal.makeSound(); // Animal is making a sound.
animal = new Dog();
animal.makeSound(); // Dog is barking.
animal = new Cat();
animal.makeSound(); // Cat is meowing.
}
}
OUTPUT
Page 24
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 25/04/23
Program/ Experiment No.19
AIM: Write a program to demonstrate checked exception during file handling.
CODING
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
class FileHandlingDemo {
public static void main(String[] args) {
try {
File file = new File("input.txt");
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
}
}
}
OUTPUT
Page 25
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 16/05/23
Program/ Experiment No.20
AIM: Write a program to use Byte stream class to read from a text file and display
the content on the output screen
CODING
import java.io.FileInputStream;
import java.io.IOException;
class ByteStreamDemo {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("input.txt");
int data;
while ((data = fis.read()) != -1) {
System.out.print((char) data);
}
fis.close();
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
OUTPUT
Page 26
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 27/04/23
Program/ Experiment No.21
AIM: Write a program to use character stream class to read from a text file and
display the content on the output screen
CODING
import java.io.FileReader;
import java.io.IOException;
class CharacterStreamDemo {
public static void main(String[] args) {
try {
FileReader fr = new FileReader("input.txt");
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
fr.close();
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}
OUTPUT
Page 27
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 18/05/23
Program/ Experiment No.22
AIM: Write a program to make use of BufferedStream to read lines from the
keyboard until 'STOP' is typed.
CODING
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class BufferedStreamDemo {
public static void main(String[] args) {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line;
try {
while (!(line = br.readLine()).equals("STOP")) {
System.out.println("\nline input is: " + line);
}
br.close();
} catch (IOException e) {
System.out.println("Error reading input: " + e.getMessage());
}
}
}
OUTPUT
Page 28
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 30/05/23
Program/ Experiment No.23
AIM: Write a program to demonstrate creation of multiple child threads
CODING
class rst extends Thread{
public void run(){
for(int i=1;i<5;i++){
try{Thread.sleep(500);}catch(InterruptedException e){System.out.println(e);}
System.out.println(i);
}
}
public static void main(String args[]){
rst t1=new rst();
rst t2=new rst();
t1.start();
t2.start();
}
}
OUTPUT
Page 29
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 07/06/23
Program/ Experiment No.24
AIM: Write a program to demonstrate any event handling.
CODING
import java.awt.event.*;
import javax.swing.*;
class rst {
public static void main(String[] args) {
JFrame f=new JFrame("Button Example");
JTextField tf=new JTextField("hello");
tf.setBounds(50,50, 150,20);
JButton b=new JButton("Click Here");
b.setBounds(50,100,95,30);
b.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("Welcome to Javatpoint.");
}
});
f.add(b);f.add(tf);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
} }
OUTPUT
Page 30
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 1/06/23
Program/ Experiment No.25
AIM: Write a swing application that uses atleast 5 swing controls
CODING
import javax.swing.*;
import java.awt.*;
class SwingControls {
public static void main(String[] args) {
JFrame frame = new JFrame("Swing Controls Demo");
JLabel label = new JLabel("Label:");
label.setBounds(20, 20, 50, 20);
JTextField textField = new JTextField();
textField.setBounds(80, 20, 150, 20);
JCheckBox checkBox = new JCheckBox("Checkbox");
checkBox.setBounds(20, 50, 150, 20);
JRadioButton radioButton1 = new JRadioButton("Radio button 1");
JRadioButton radioButton2 = new JRadioButton("Radio button 2");
ButtonGroup radioGroup = new ButtonGroup();
radioGroup.add(radioButton1);
radioGroup.add(radioButton2);
radioButton1.setBounds(20, 80, 120, 20);
radioButton2.setBounds(140, 80, 120, 20);
JComboBox<String> comboBox = new JComboBox<>(new String[]{"Option 1",
"Option 2", "Option 3"});
comboBox.setBounds(20, 110, 150, 20);
JSlider slider = new JSlider(0, 100, 50);
slider.setBounds(20, 140, 200, 40);
JButton button = new JButton("Button");
button.setBounds(80, 200, 100, 30);
frame.add(label);
frame.add(textField);
frame.add(checkBox);
frame.add(radioButton1);
frame.add(radioButton2);
frame.add(comboBox);
frame.add(slider);
frame.add(button);
frame.setSize(250, 300);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Page 31
STUDENT NAME : HIMANSHU KUMAR DUTT
}
}
OUTPUT
Page 32
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 5/06/23
Program/ Experiment No.26
AIM: Write a program to implement border layout using Swing.
CODING
import java.awt.*;
import javax.swing.*;
class brder
{
JFrame jframe;
brder()
{
jframe = new JFrame();
// create buttons
JButton btn1 = new JButton("NORTH");
JButton btn2 = new JButton("SOUTH");
JButton btn3 = new JButton("EAST");
JButton btn4 = new JButton("WEST");
JButton btn5 = new JButton("CENTER");
jframe.setLayout(new BorderLayout(20, 15));
jframe.add(btn1, BorderLayout.NORTH);
jframe.add(btn2, BorderLayout.SOUTH);
jframe.add(btn3, BorderLayout.EAST);
jframe.add(btn4, BorderLayout.WEST);
jframe.add(btn5, BorderLayout.CENTER);
jframe.setSize(300,300);
jframe.setVisible(true);
}
public static void main(String argvs[])
{ new brder();
} }
OUTPUT
Page 33
STUDENT NAME : HIMANSHU KUMAR DUTT
Page 34
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 8/6/23
Roll No.09490302021
Program/ Experiment No.27
AIM:Write a program to implement border layout using Swing.Write program that uses
swings to display combination of RGB using 3 scrollbars.
CODING
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
class pppp implements AdjustmentListener {
public static void main(String[] args) {
JFrame frame = new JFrame("JScrollBar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(20,30,200,250);
frame.setLayout(null);
pppp app = new pppp();
app.sbar1 = new JScrollBar(java.awt.Adjustable.VERTICAL, 0,0,0,255);
app.sbar1.setBounds(10,20, 10, 200);
app.sbar1.setBackground(Color.red);
app.sbar1.addAdjustmentListener(app);
frame.add(app.sbar1);
app.sbar2 = new JScrollBar(java.awt.Adjustable.VERTICAL, 0,0,0,255);
app.sbar2.setBounds(30,20, 10, 200);
app.sbar2.setBackground(Color.green);
app.sbar2.addAdjustmentListener(app);
frame.add(app.sbar2);
app.sbar3 = new JScrollBar(java.awt.Adjustable.VERTICAL, 0,0,0,255);
app.sbar3.setBounds(50,20, 10, 200);
app.sbar3.setBackground(Color.blue);
app.sbar3.addAdjustmentListener(app);
frame.add(app.sbar3);
app.panel = new JPanel();
app.panel.setBounds(80,20,50,200);
app.panel.setBackground(new Color(0,0,0));
frame.add(app.panel);
frame.setVisible(true);
}
public void adjustmentValueChanged(AdjustmentEvent e)
{
panel.setBackground(new Color(sbar1.getValue(),sbar2.getValue(),
sbar3.getValue()));
}
JScrollBar sbar1;
JScrollBar sbar2;
JScrollBar sbar3;
JPanel panel;}
Page 35
STUDENT NAME : HIMANSHU KUMAR DUTT
OUTPUT
Page 36
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 8/6/23
Roll No.09490302021
Program/ Experiment No.28
AIM: Write a Java program to perform basic Calculator operations. Make a menu
driven program to select operation to perform (+ - * / ). Take 2 integers and perform
operation as chosen by user.
CODING
import javax.swing.*;
class CalculatorDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Calculator");
JTextField num1Field = new JTextField();
num1Field.setBounds(20, 20, 150, 20);
JTextField num2Field = new JTextField();
num2Field.setBounds(20, 50, 150, 20);
JLabel resultLabel = new JLabel("");
resultLabel.setBounds(20, 80, 150, 20);
JButton addButton = new JButton("+");
addButton.setBounds(20, 110, 50, 30);
JButton subtractButton = new JButton("-");
subtractButton.setBounds(80, 110, 50, 30);
JButton multiplyButton = new JButton("*");
multiplyButton.setBounds(140, 110, 50, 30);
JButton divideButton = new JButton("/");
divideButton.setBounds(200, 110, 50, 30);
addButton.addActionListener(e -> {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
int result = num1 + num2;
resultLabel.setText("Result: " + result);
});
subtractButton.addActionListener(e -> {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
int result = num1 - num2;
resultLabel.setText("Result: " + result);
});
multiplyButton.addActionListener(e -> {
Page 37
STUDENT NAME : HIMANSHU KUMAR DUTT
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
int result = num1 * num2;
resultLabel.setText("Result: " + result);
});
divideButton.addActionListener(e -> {
int num1 = Integer.parseInt(num1Field.getText());
int num2 = Integer.parseInt(num2Field.getText());
double result = (double) num1 / num2;
resultLabel.setText("Result: " + result);
});
frame.add(num1Field);
frame.add(num2Field);
frame.add(resultLabel);
frame.add(addButton);
frame.add(subtractButton);
frame.add(multiplyButton);
frame.add(divideButton);
frame.setSize(270, 200);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
OUTPUT
Page 38
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 9/6/23
Roll No.09490302021
Program/ Experiment No.29
AIM: Write a java program to retrieve data from database
CODING
import java.sql.*;
class RetrieveData {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/himanshu";
String user = "root";
String password = "";
try {
Connection conn = DriverManager.getConnection(url, user, password);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM himanshu");
while (rs.next()) {
String name = rs.getString("name");
int age = rs.getInt("age");
int rollno = rs.getInt("rollno");
String batch = rs.getString("course");
System.out.println(" Name: " + name + ", Age: " + age + "Roll No: " + rollno +
", Batch: " + batch);
}
rs.close();
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace(); }
}
}
OUTPUT
Page 39
STUDENT NAME : HIMANSHU KUMAR DUTT
Date of Experiment */ Program: 9/6/23
Roll No.09490302021
Program/ Experiment No.30
AIM: Write a java program to insert data to database using GUI.
CODING
import javax.swing.*;
import java.sql.*;
class DatabaseInsertDemo {
public static void main(String[] args) {
JFrame frame = new JFrame("Database Insert Demo");
JLabel nameLabel = new JLabel("Name:");
nameLabel.setBounds(20, 20, 50, 20);
JTextField nameField = new JTextField();
nameField.setBounds(80, 20, 150, 20);
JLabel ageLabel = new JLabel("Age:");
ageLabel.setBounds(20, 50, 50, 20);
JTextField ageField = new JTextField();
ageField.setBounds(80, 50, 150, 20);
JLabel rollnoLabel = new JLabel("Roll No:");
rollnoLabel.setBounds(20, 80, 50, 20);
JTextField rollnoField = new JTextField();
rollnoField.setBounds(80, 80, 150, 20);
JLabel courseLabel = new JLabel("Course:");
courseLabel.setBounds(20, 110, 50, 20);
JTextField courseField = new JTextField();
courseField.setBounds(80, 110, 150, 20);
JButton addButton = new JButton("Add");
addButton.setBounds(80, 150, 100, 30);
addButton.addActionListener(e -> {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/himanshu", "root", "");
PreparedStatement stmt = con.prepareStatement("insert into himanshu(name,
age, rollno, course) values(?, ?, ?, ?)");
stmt.setString(1, nameField.getText());
stmt.setInt(2, Integer.parseInt(ageField.getText()));
stmt.setInt(3, Integer.parseInt(rollnoField.getText()));
stmt.setString(4, courseField.getText());
int result = stmt.executeUpdate();
JOptionPane.showMessageDialog(frame, "Data inserted successfully");
con.close();
} catch (Exception ex) {
JOptionPane.showMessageDialog(frame, "Error: " + ex.getMessage());
}
Page 40
STUDENT NAME : HIMANSHU KUMAR DUTT
});
frame.add(nameLabel);
frame.add(nameField);
frame.add(ageLabel);
frame.add(ageField);
frame.add(rollnoLabel);
frame.add(rollnoField);
frame.add(courseLabel);
frame.add(courseField);
frame.add(addButton);
frame.setSize(250, 220);
frame.setLayout(null);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
OUTPUT
Page 41
Download