Java Practical Codes
Java Practical Codes
class Point {
protected int x, y;
public Point() {
this.x = 0;
this.y = 0;
}
r
class ColorPoint extends Point {
private String color;
public ColorPoint() {
super();
this.color = "undefined";
}
public Point3D() {
super();
this.z = 0;
}
p.display();
cp.display();
p3d.display();
}
}
-----------------------------------------------------------------------------------
------------------------------------------------------------------
2.
class Fibonacci
{
class Cube {
fib.printFibonacci(n);
cube.printCubes(n);
square.printSquares(n);
}
}
-----------------------------------------------------------------------------------
-------------------------------------------------------
3.
import java.util.Scanner;
class Employee {
protected int id;
protected String name;
protected double salary;
public Employee() {
this.id = 0;
this.name = "";
this.salary = 0.0;
}
public Manager() {
super(); // Call the base class (Employee) constructor
this.bonus = 0.0;
}
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------
4.
class CapitalString {
class Person {
private String name;
private String city;
person.display();
}
}
-----------------------------------------------------------------------------------
------------------------------------------
SET B :
->
interface Operation {
double PI = 3.142;
double area();
double volume();
}
2. Write a Java program to create a super class Employee (members – name, salary).
Derive a sub-class as Developer (member – projectname). Derive a sub-class
Programmer (member – proglanguage) from Developer. Create object of Developer and
display the details of it. Implement this multilevel inheritance with appropriate
constructor and methods.s
->
class Employee {
protected String name;
protected double salary;
developer.displayDetails();
}
}
3 . Define an abstract class Staff with members name and address. Define two sub-
classes of this class – FullTimeStaff (members - department, salary, hra - 8% of
salary, da – 5% of salary) and PartTimeStaff (members - number-of-hours, rate-per-
hour). Define appropriate constructors. Write abstract method as calculateSalary()
in Staff class. Implement this method in subclasses. Create n objects which could
be of either FullTimeStaff or PartTimeStaff class by asking the user‘s choice.
Display details of all FullTimeStaff objects and all PartTimeStaff objects along
with their salary.
->
import java.util.Scanner;
if (type == 'f') {
// Input details for FullTimeStaff
System.out.println("Enter name:");
String name = scanner.nextLine();
System.out.println("Enter address:");
String address = scanner.nextLine();
System.out.println("Enter department:");
String department = scanner.nextLine();
System.out.println("Enter basic salary:");
double salary = scanner.nextDouble();
scanner.nextLine(); // Consume newline
System.out.println("\nStaff Details:");
for (int i = 0; i < staffArray.length; i++) {
if (staffArray[i] != null) {
staffArray[i].displayDetails();
}
}
}
}
-----------------------------------------------------------------------------------
----------------------------------------------------------
ASSIGNMENT 3
SET A :
1.
Accept n integers from the user and store them in a collection. Display them in the
sorted order.
The collection should not accept duplicate elements. (Use a suitable collection).
Search for a
particular element using predefined search method in the Collection framework.
->
import java.util.Scanner;
import java.util.TreeSet;
if (numbers.contains(searchElement))
{
System.out.println("Element " + searchElement + " is found in the
collection.");
}
else
{
System.out.println("Element " + searchElement + " is not found in the
collection.");
}
}
}
----------------------------------------------------------
2.
Create a Hash table containing Employee name and Salary. Display the details of the
hash table. Also search for a specific Employee and display Salary of that
Employee.
=>
import java.util.HashMap;
import java.util.Scanner;
employeeSalaries.put("John", 50000.0);
employeeSalaries.put("Jane", 60000.0);
employeeSalaries.put("Emily", 70000.0);
employeeSalaries.put("Michael", 80000.0);
employeeSalaries.put("Sophia", 75000.0);
System.out.println("Employee Salaries:");
for (String employee : employeeSalaries.keySet())
{
System.out.println("Employee: " + employee + ", Salary: $" +
employeeSalaries.get(employee));
}
if (employeeSalaries.containsKey(searchName))
{
double salary = employeeSalaries.get(searchName);
System.out.println("Salary of " + searchName + " is: $" + salary);
}
else
{
System.out.println("Employee " + searchName + " is not found in the
record.");
}
}
}
-----------------------------
3.
Write a java program to accept a number from the user, if number is zero then throw
userdefined exception ―Number is 0, otherwise check whether no is prime or not.
=>
import java.util.Scanner;
try
{
System.out.print("Enter a number: ");
int number = scanner.nextInt();
if (number == 0)
{
throw new ZeroNumberException("Number is 0");
}
if (isPrime(number))
{
System.out.println(number + " is a prime number.");
} else {
System.out.println(number + " is not a prime number.");
}
}
catch (ZeroNumberException e)
{
System.out.println("Exception: " + e.getMessage());
}
catch (Exception e)
{
System.out.println("An error occurred: " + e.getMessage());
}
finally
{
scanner.close();
}
}
return true;
}
}
-------------------
4.Write a java program that displays the number of characters, lines and words of a
file.
->
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
--------------------------------------
-----------------------------------------------------------------------------
SET B :
1. Construct a linked List containing names of colours: red, blue, yellow and
orange. Then
extend your program to do the following:
i. Display the contents of the List using an Iterator
ii. Display the contents of the List in reverse order using a ListIterator
iii. Create another list containing pink and green. Insert the elements of this
list
between blue and yellow.
—>
import java.util.LinkedList;
import java.util.Iterator;
import java.util.ListIterator;
// iii. Create another list containing pink and green, insert between blue
and yellow
LinkedList<String> newColors = new LinkedList<>();
newColors.add("pink");
newColors.add("green");
----------------------------
2.Write a java program to accept Doctor Name from the user and check whether it is
valid
or not. (It should not contain digits and special symbol) If it is not valid then
throw user
defined Exception - Name is Invalid -- otherwise display it.
->
import java.util.Scanner;
if (!name.matches("[a-zA-Z\\s]+")) {
throw new InvalidNameException("Name is Invalid");
}
}
try {
validateName(doctorName);
--------------------------
->
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Scanner;
try {
System.out.print("Address: ");
String address = scanner.nextLine();
dos.close();
dis.close();
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
}
scanner.close();
}
}
-----------------------------------------------------------------------------------
---------------------------------------------------------------------
Assignment 4 :
Set A :
1.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
// Constructor
public LoginPage() {
setTitle("Login Page");
setBounds(300, 90, 400, 300); // Window position and size
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false); // Disable resizing
container = getContentPane();
container.setLayout(null);
// Title Label
title = new JLabel("Login Page");
title.setFont(new Font("Arial", Font.PLAIN, 20));
title.setSize(300, 30);
title.setLocation(120, 30);
container.add(title);
// Login Button
loginButton = new JButton("Login");
loginButton.setFont(new Font("Arial", Font.PLAIN, 15));
loginButton.setSize(100, 20);
loginButton.setLocation(70, 180);
loginButton.addActionListener(this);
container.add(loginButton);
// Reset Button
resetButton = new JButton("Reset");
resetButton.setFont(new Font("Arial", Font.PLAIN, 15));
resetButton.setSize(100, 20);
resetButton.setLocation(200, 180);
resetButton.addActionListener(this);
container.add(resetButton);
setVisible(true);
}
—----------------------------------------------------------------------------------
----------------------------------------+++++++++++++++++++++++++++++++++++++++++++
+++++++++++++++
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
showButton.addActionListener(new ActionListener()
{
// Add components to the frame. These lines add the combo box, button, and
label to the frame (window). Swing components are typically added to a container
like JFrame.
frame.add(comboBox);
frame.add(showButton);
frame.add(label);
// Set frame to be visible . This makes the window visible to the user.
Without this, the window would not appear on the screen.
frame.setVisible(true);
}
}
—-------++++++++++-------
import javax.swing.*;
import java.awt.*;
gbc.gridx = 0;
gbc.gridy = 2;
frame.add(lastNameLabel, gbc);
gbc.gridx = 1;
frame.add(lastNameText, gbc);
gbc.gridx = 0;
gbc.gridy = 3;
frame.add(emailLabel, gbc);
gbc.gridx = 1;
frame.add(emailText, gbc);
gbc.gridx = 2;
gbc.gridy = 1;
frame.add(usernameLabel, gbc);
gbc.gridx = 3;
frame.add(usernameText, gbc);
gbc.gridx = 2;
gbc.gridy = 2;
frame.add(passwordLabel, gbc);
gbc.gridx = 3;
frame.add(passwordText, gbc);
gbc.gridx = 2;
gbc.gridy = 3;
frame.add(mobileLabel, gbc);
gbc.gridx = 3;
frame.add(mobileText, gbc);
—--------------++++++++++++++------------
Q3.LoginGUI
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
—-------------++++++++++++++++=-----------
Q4.Customer Details
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
—----------------+++++++++++++++======-----------
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
class SimpleCalculator{
public static void main(String[] args) {
// Create the frame
JFrame frame = new JFrame("Calculator with Search Bar");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300, 500); // Increased size to accommodate the search bar
// Set the layout and add the main panel to the frame
frame.add(mainPanel);