JAVA Practicle Khushdeep Kaur
JAVA Practicle Khushdeep Kaur
_______________________________________________________________________________________
BASICS CORE JAVA -
A. Write steps of installation of java
D. Write a program to create the object & initialize the object through instance variable
class Student {
int rollNo;
String name;
}
class Car {
String model;
int year;
// Constructor
Car(String model, int year) {
this.model = model;
this.year = year;
}
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 2 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
// Function to display details
void display() {
System.out.println("Car Model: " + model + ", Year: " + year);
}
}
public class CarTest {
public static void main(String[ ] args) {
Car c1 = new Car("Tesla", 2022);
c1.display();
}
}
F. Write a program to take 5 students and print their roll no, name & marks.
import java.util.Scanner;
class StudentDetails {
int rollNo;
String name;
float marks;
// Constructor
StudentDetails(int rollNo, String name, float marks) {
this.rollNo = rollNo;
this.name = name;
this.marks = marks;
}
// Display Function
void display() {
System.out.println("Roll No: " + rollNo + ", Name: " + name + ", Marks: " +
marks);
}
}
System.out.println("Student Details:");
for (StudentDetails student : students) {
student.display();
}
sc.close();
}
}
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 4 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
import java.util.Scanner;
public class PrimeCheck {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
scanner.close();
if (isPrime(num)) {
System.out.println(num + " is a prime number.");
} else {
System.out.println(num + " is not a prime number.");
}
}
public static boolean isPrime(int num) {
if (num <= 1) {
return false;
}
for (int i = 2; i <= Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}
OUTPUT :-
Test Case 1:
Enter a number: 2
2 is a prime number.
Test Case 2:
Enter a number: 10
10 is not a prime number.
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 5 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
import java.util.Scanner;
public class HCF_LCM_Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = scanner.nextInt();
System.out.print("Enter second number: ");
int num2 = scanner.nextInt();
scanner.close();
OUTPUT :-
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 6 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
// Default Constructor
public Student() {
System.out.println("Default constructor called.");
name = " khushdeep kaur";
age = 19;
}
// Parameterized Constructor
public Student(String name, int age) {
System.out.println("Parameterized constructor called.");
this.name = name;
this.age = age;
}
// Copy Constructor
public Student(Student other) {
System.out.println("Copy constructor called.");
this.name = other.name;
this.age = other.age;
}
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 7 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
// Using Copy Constructor
Student student3 = new Student(student2);
student3.display();
}
}
OUTPUT :-
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 8 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
class CalculateArea {
// Method to calculate the area of a rectangle
double area(double length, double breadth) {
return length * breadth;
}
System.out.println(rectangleArea);
System.out.println(circleArea);
System.out.println(triangleArea);
}
OUTPUT :-
50.0
153.93804002589985
20.0
Explanation:
Rectangle Area = 10 * 5 = 50.0
Circle Area = π * 7 * 7 ≈ 153.94
Triangle Area = 0.5 * 8 * 5 = 20.0
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 9 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
// Derived class from the Mammal class (Leaf class in the hierarchy)
class Dog extends Mammal {
void bark() {
System.out.println("This dog can bark.");
}
}
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 10 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
OUTPUT :-
OUTPUT :
Behavior inherited from Animal class:
This animal can eat.
This animal can sleep.
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 11 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
// Base class
class Shape {
// Method to calculate the area (to be overridden by subclasses)
void calculateArea() {
System.out.println("This is the base class. Define specific shapes to calculate
the area.");
}
}
@Override
void calculateArea() {
double area = length * width;
System.out.println("Area of Rectangle: " + area);
}
}
Circle(double radius) {
this.radius = radius;
}
@Override
void calculateArea() {
double area = Math.PI * radius * radius;
System.out.println("Area of Circle: " + area);
}
}
@Override
void calculateArea() {
double area = 0.5 * base * height;
System.out.println("Area of Triangle: " + area);
}
}
OUTPUT :-
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 14 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
// Interface definition
interface Animal {
// Abstract methods (implicitly public and abstract)
void eat();
void sleep();
}
// Interface definition
interface Pet {
void play();
}
@Override
public void sleep() {
System.out.println("Dog is sleeping.");
}
@Override
public void play() {
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 15 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
System.out.println("Dog is playing fetch.");
}
}
OUTPUT :-
Dog is eating.
Dog is sleeping.
Dog is playing fetch.
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 16 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 8 |> Write a program to create your own packages and utilize
those packages in a class.
In Java, a package is essentially a way to group related classes together. Let's create a package called
mypackage that contains a class for basic arithmetic operations.
Now we'll create another class outside the package to use the mypackage package and its
ArithmeticOperations class.
File: MainApp.java
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 17 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
public static void main(String[] args) {
// Create an object of the ArithmeticOperations class
ArithmeticOperations operations = new ArithmeticOperations();
// Perform arithmetic operations
int sum = operations.add(10, 5);
int difference = operations.subtract(10, 5);
// Display results
System.out.println("Sum: " + sum);
System.out.println("Difference: " + difference);
}
}
javac -d . ArithmeticOperations.java
The -d flag ensures the compiled class is placed in the correct directory structure for the package.
javac MainApp.java
java MainApp
OUTPUT :-
Sum: 15
Difference: 5
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 18 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
import java.util.StringTokenizer;
OUTPUT :-
Total number of tokens: 7
Welcome
to
the
world
of
Java
programming!
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 19 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 10 |> Write a program to implement applets in java.
import java.applet.Applet;
import java.awt.Graphics;
/*
<applet code="MyApplet" width="300" height="200">
</applet>
*/
OUTPUT :-
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 20 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 11 |> Write a program to apply the concept of exception
handling in classes.
import java.util.Scanner;
class Divider {
private int numerator;
private int denominator;
OUTPUT :-
Enter numerator: 10
Enter denominator: 0
Exception caught: Division by zero is not allowed!
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 21 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 12 |> Write a program to create a frame, button, text box and
label.
import java.awt.*;
import java.awt.event.*;
// Create a label
Label label = new Label("Enter your name:");
label.setBounds(50, 80, 120, 30);
// Create a button
Button button = new Button("Click Me");
button.setBounds(130, 130, 80, 30);
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 22 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
OUTPUT :-
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 23 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
| 13 |> Write a program to create a button. When the button is
click, an appropriate message should be displayed in the
dialog box.
import javax.swing.*;
import java.awt.event.*;
// Create a button
JButton button = new JButton("Click Me");
button.setBounds(120, 80, 150, 40);
// Frame settings
frame.setSize(400, 250);
frame.setLayout(null); // Absolute layout
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 24 )@$ | +-------+
+----+ | khushdeep kaur | +----+ | 1323601 | +----+ | BCA-405 Java Programming Lab |
_______________________________________________________________________________________
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
OUTPUT :-
#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*#>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
+-------+ | $@( 25 )@$ | +-------+