Java lab
Java lab
import java.util.Scanner;
int empNo;
String empName;
double basic, da, hra, cca = 240, pf, pt = 100, grossSalary, netSalary;
da = 0.70 * basic;
hra = 0.30 * basic;
pf = 0.10 * basic;
double a, b, c;
double discriminant, root1, root2;
discriminant = (b * b) - (4 * a * c);
if (discriminant > 0) {
root1 = -b / (2 * a);
System.out.println("The equation has one real solution (double root):");
System.out.println("Root: " + root1);
} else {
Rectangle
import java.util.Scanner;
class Area {
rectangle.setDim(length, breadth);
Chaining
import java.util.Scanner;
class Parent {
public Parent() {
System.out.println("Parent class constructor called.");
}
}
class Child extends Parent {
private int a, b;
public Child() {
super();
System.out.println("Default constructor of Child class called.");
}
public Child(int a, int b) {
super();
this.a = a;
this.b = b;
System.out.println("Parameterized constructor of Child class called with a = " + a
+ " and b = " + b);
}
public void display() {
System.out.println("a: " + a + ", b: " + b);
}
}
public class ConstructorChainingDemo {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter value for a: ");
int a = scanner.nextInt();
System.out.print("Enter value for b: ");
int b = scanner.nextInt();
Child childObject = new Child(a, b);
childObject.display();
}
}
Student
import java.util.Scanner;
class Student {
public Student() {
this.name = "unknown";
this.age = 0;
this.address = "not available";
}
if (choice.equalsIgnoreCase("yes")) {
System.out.print("Enter address: ");
String address = scanner.nextLine();
students[i].setInfo(name, age);
}
}
System.out.println("\nStudent Information:");
for (int i = 0; i < students.length; i++) {
System.out.println("Student " + (i + 1) + ":");
students[i].printInfo();
System.out.println();
}
}
}
Employee
import java.util.Scanner;
class Employee {
private int empNo;
private String name;
private double salary;
class Department {
System.out.println("\nInitial Details:");
dept.printDepartmentDetails();
System.out.println();
employees[0].printEmployeeDetails();
System.out.println();
scanner.close();
}
}
Professor
import java.util.Scanner;
class Teacher {
System.out.println("\nProfessor Details:");
prof.displayInfo();
System.out.println();
scanner.close();
}
}
Vehicle
interface Vehicle {
void start();
void stop();
void speedUp(int increment);
}
public Bicycle() {
this.speed = 0;
}
public Car() {
this.speed = 0;
}
public Bike() {
this.speed = 0;
}
System.out.println("Car:");
car.start();
car.speedUp(20);
car.stop();
System.out.println();
System.out.println("Bike:");
bike.start();
bike.speedUp(10);
bike.stop();
}
}
Words
import java.util.Scanner;
if (number > 0) {
result += units[number];
}
return result.trim();
}
if (amount > 0) {
result += convertThreeDigitNumber(amount);
}
return result.trim();
}
scanner.close();
}
}
Login
import java.util.Scanner;
if (!hasDigit || !hasSpecialChar) {
throw new InvalidPasswordException("Password must contain at least one
digit and one special character.");
}
}
try {
validatePassword(password);
System.out.println("Login Successful for user: " + loginID);
} catch (InvalidPasswordException e) {
System.out.println(e.getMessage());
System.out.println("Please enter a valid password of length 8, containing at
least one digit and one special symbol.");
}
scanner.close();
}
}
Thread
import java.util.Scanner;
class NumberPrinter {
private int number = 1;
private int limit;
oddThread.start();
evenThread.start();
}
}
Swing
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.tree.DefaultMutableTreeNode;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public BookInfoApp() {
setTitle("Book Information Form");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(800, 600);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
createMenuBar();
createToolBar();
add(mainPanel, BorderLayout.NORTH);
menuBar.add(fileMenu);
setJMenuBar(menuBar);
}
@Override
public void actionPerformed(ActionEvent e) {
String title = titleField.getText().trim();
String author = authorField.getText().trim();
String genre = (String) genreComboBox.getSelectedItem();
String year = yearField.getText().trim();
Student Profile
import java.awt.*;
import java.awt.event.*;
add(nameLabel);
add(nameField);
add(ageLabel);
add(ageField);
add(genderLabel);
add(maleCheckbox);
add(new Label(""));
add(femaleCheckbox);
add(addressLabel);
add(addressField);
add(contactLabel);
add(contactField);
add(submitButton);
add(resetButton);
add(academicLabel);
add(displayArea);
submitButton.addActionListener(this);
resetButton.addActionListener(this);
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent we) {
dispose();
}
});
}
displayArea.setText("Student Profile:\n");
displayArea.append("Name: " + name + "\n");
displayArea.append("Age: " + age + "\n");
displayArea.append("Gender: " + gender + "\n");
displayArea.append("Address: " + address + "\n");
displayArea.append("Contact: " + contact + "\n");
}
public LibraryLoginForm() {
setTitle("Library Login Form");
setSize(300, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new GridBagLayout());
gbc.gridx = 0;
gbc.gridy = 0;
add(new JLabel("Username:"), gbc);
gbc.gridx = 0;
gbc.gridy = 1;
add(new JLabel("Password:"), gbc);
if (isValid(username, password)) {
showMessage("Login Successful", "Welcome to the Library, " + username +
"!");
usernameField.setText("");
passwordField.setText("");
} else {
showMessage("Login Failed", "Invalid username or password.");
}
}
}
Shapes
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
switch (currentShape) {
case CIRCLE:
g2d.drawOval(200, 100, 200, 200);
break;
case RECTANGLE:
g2d.drawRect(150, 100, 300, 150);
break;
case POLYGON:
int[] xPoints = {300, 450, 300, 150};
int[] yPoints = {50, 150, 250, 150};
int nPoints = 4;
g2d.drawPolygon(xPoints, yPoints, nPoints);
break;
}
}
shapeSelector.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String selectedShape = (String) shapeSelector.getSelectedItem();
switch (selectedShape) {
case "Circle":
panel.setCurrentShape(ShapeType.CIRCLE);
break;
case "Rectangle":
panel.setCurrentShape(ShapeType.RECTANGLE);
break;
case "Polygon":
panel.setCurrentShape(ShapeType.POLYGON);
break;
}
}
});
frame.setLayout(new BorderLayout());
frame.add(shapeSelector, BorderLayout.NORTH);
frame.add(panel, BorderLayout.CENTER);
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}