JAVA PROGRAMMING PRACTICAL FILE 2222akash
JAVA PROGRAMMING PRACTICAL FILE 2222akash
PRACTICAL FILE
CSE504-22
INDEX
2
Write a Java program to design an applet that performs the basic
14. functionality of a calculator
20
3
1) Write a Java Program of create Classes and Objects
class Car {
String model;
String color;
int year;
void displayInfo() {
System.out.println("Model: " + model);
System.out.println("Colour: " + color);
System.out.println("Year: " + year);
}
}
car1.displayInfo();
car2.displayInfo();
}
}
Output
4
B. Write the Java program to find the greatest number among
three numbers
switch (choice) {
case 1:
System.out.println("You chose option 1.");
break;
case 2:
System.out.println("You chose option 2.");
break;
case 3:
System.out.println("You chose option 3.");
break;
default:2
System.out.println("Invalid choice.");
}
}
}
5
OUTPUT
OUTPUT
B)
6
3) A. Write a Java Program to use constructors, passing
arguments to constructors
B. Write a Java program to implement Switch statements to
perform the
following operations :
1) Addition 2) subtraction 3) multiplication 4) division
A)class Rectangle {
private double length;
private double width;
double result;
switch (choice) {
case 1:
result = num1 + num2;
System.out.println("Addition: " + result);
break;
case 2:
result = num1 - num2;
System.out.println("Subtraction: " + result);
break;
case 3:
result = num1 * num2;
System.out.println("Multiplication: " + result);
break;
case 4:
if (num2 != 0) {
result = num1 / num2;
System.out.println("Division: " + result);
} else {
System.out.println("Error: Division by zero is not allowed.");
}
break;
default:
System.out.println("Invalid choice.");
}
}
8
Output
Output
9
10
5) Write a program to perform following operations on
strings:
11
6) Write a Java program to illustrate multi-level
inheritance where a subclass inherits from another
subclass. Create a hierarchy of classes to showcase the
concept
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
Output
12
7) Write a program to implement Multiple Inheritance using
interface
interface Animal {
void eat();
}
interface Pet {
void play();
}
Output
Output
class Vehicle {
public String move() {
14
return "The vehicle moves.";
}
}
System.out.println(vehicle.move());
System.out.println(car.move());
}
}
Output
Output
import java.util.Scanner;
scanner.close();
}
}
16
12) Write a Java program to create a thread by extending
the Thread class or implementing the Runnable interface
import java.applet.Applet;
import java.awt.*;
Output
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
add.addActionListener(this);
sub.addActionListener(this);
}
19
15) Write a Java program to create a GUI with basic AWT
components including a label, text field, button, checkbox,
and radio button.
import java.awt.*;
import java.awt.event.*;
// Create a Label, TextField, Button, Checkbox, and CheckboxGroup for radio buttons
Label label = new Label("Enter Text:");
TextField textField = new TextField(20);
Button button = new Button("Submit");
Checkbox checkbox = new Checkbox("Accept Terms");
import java.awt.*;
import java.awt.event.*;
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
Output
21
17) Create an application that uses BorderLayout to place
components in the five regions (North, South, East, West,
Center)
import java.awt.*;
import java.awt.event.*;
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
Output
import java.awt.*;
import java.awt.event.*;
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
label.setText("Button clicked");
}
});
f.setLayout(new FlowLayout());
f.add(button);
f.add(label);
f.setSize(300, 200);
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
System.exit(0);
}
});
}
}
Output
Initially, the GUI window will display:
When you click the Click Me button, the label changes to:
import java.io.*;
import java.sql.*;
Output
If successful
24