Program's By Shubham Sawant
Learn Programming With Shubham Sawant
In This Article,
We Provide Programs's On Java Programming Language (Basic To Advance) With Source Code(No Output)
import java.util.Scanner;
class Result{
public static void main(String [] args){
System.out.println("***********************************************************\n\tPractical 1(1) : Java Programming - / / \n***********************************************************");
System.out.println("\t\t\t**************\n\t\t\tGrading System\n\t\t\t**************");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
// RDBMS - OS - Java - Maths - SEPM
float marks[] = new float[5]; Scanner scan = new Scanner(System.in);
System.out.print("Enter Your Marks(Out of 100)\n");
System.out.print("For RDBMS: "); marks[0] = scan.nextFloat();
System.out.print("For OS: "); marks[1] = scan.nextFloat();
System.out.print("For Java Programming: "); marks[2] = scan.nextFloat();
System.out.print("For Maths: "); marks[3] = scan.nextFloat();
System.out.print("For SEPM: "); marks[4] = scan.nextFloat();
float result = 0;
for(int i = 0; i < marks.length; i++)
result += marks[i];
result = (result / (marks.length*100)) * 100;
System.out.println("Your Percentage : " + result);
if(result < 35 && result >= 0)
System.out.println("You Are Fail !");
else if(result >= 35 && result < 50)
System.out.println("You Are Pass.");
else if(result >= 50 && result < 65)
System.out.println("You Are Pass With C Garde");
else if(result >= 65 && result < 85)
System.out.println("You Are Pass With B Garde");
else if(result >= 85 && result < 95)
System.out.println("You Are Pass With A Garde");
else if(result >= 95 && result <= 100)
System.out.println("You Are Pass With A+ Garde");
else
System.out.println("Something Wrong ! OR Invalid Input");
scan.close();
}
}
import java.util.Scanner;
class MatrixCalculation{
void additionMatrix(int matrix1[][], int matrix2[][]){
System.out.println("\t ************************************\n\t\tAddition Of Two Matrix\n\t ************************************");
for(int i = 0; i < matrix1.length ; i++){
for(int j = 0; j < matrix2[0].length; j++) {
System.out.print("\t" + (matrix1[i][j] + matrix2[i][j]) + "\t");
}
System.out.println();
}
}
void substractionMatrix(int matrix1[][], int matrix2[][]){
System.out.println("\t ************************************\n\t\tSubstraction Of Two Matrix\n\t ************************************");
for(int i = 0; i < matrix1.length ; i++){
for(int j = 0; j < matrix2[0].length; j++) {
System.out.print("\t" + (matrix1[i][j] - matrix2[i][j]) + "\t");
}
System.out.println();
}
}
public static void main(String [] args){
System.out.println("***********************************************************\n\tPractical 1(2) : Java Programming - / / \n***********************************************************");
System.out.println("\t*****************************************\n\t\tMatrix Calculation System\n\t*****************************************");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
int rM1, rM2, cM1, cM2; Scanner scan = new Scanner(System.in);
System.out.print("Enter Row M1( Count ): "); rM1 = scan.nextInt();
System.out.print("Enter Column M1( Count ): "); cM1 = scan.nextInt();
System.out.print("Enter Row M2( Count ): "); rM2 = scan.nextInt();
System.out.print("Enter Column M2( Count ): "); cM2 = scan.nextInt();
if(rM1 == rM2 && cM1 == cM2){
int matrix1[][] = new int[rM1][cM1];
for(int i = 0; i < rM1; i++){
for(int j = 0; j < cM1; j++) {
System.out.print("Enter Values For M1 ("+ i +", "+ j + ") : ");
matrix1[i][j] = scan.nextInt();
}
}
int matrix2[][] = new int[rM2][cM2];
for(int i = 0; i < rM1; i++){
for(int j = 0; j < cM2; j++) {
System.out.print("Enter Values For M2 ("+ i +", "+ j + ") : ");
matrix2[i][j] = scan.nextInt();
}
}
System.out.println("Matrix 1 :");
for(int i = 0; i < rM1; i++){
for(int j = 0; j < cM1; j++) {
System.out.print("\t" + matrix1[i][j]);
}
System.out.println();
}
System.out.println("Matrix 2 :");
for(int i = 0; i < rM2; i++){
for(int j = 0; j < cM2; j++) {
System.out.print("\t" + matrix2[i][j]);
}
System.out.println();
}
MatrixCalculation matrixCalculation = new MatrixCalculation();
System.out.println("Select Option To Perform Calculation : \n0)Exit\n1)Addition\n2)Substraction"); int option = scan.nextInt();
switch(option){
case 0 : break;
case 1 : matrixCalculation.additionMatrix(matrix1, matrix2); break;
case 2 : matrixCalculation.substractionMatrix(matrix1, matrix2); break;
default : System.out.println("Invalid Option !"); break;
}
}
else
System.out.println("Order Of These Two Matrix Are Not Same !");
scan.close();
}
}
import java.util.Scanner;
class StudentDetails {
String name = "", course, address;
int rollNo;
long mobileNum;
void input() {
Scanner scan = new Scanner(System.in);
System.out.println("****** Enter Student Details ******");
System.out.print("Enter Name : ");
name = scan.nextLine();
System.out.print("Enter Course : ");
course = scan.nextLine();
System.out.print("Enter Roll No : ");
rollNo = scan.nextInt();
System.out.print("Enter Mobile No : ");
mobileNum = scan.nextLong();
scan.close();
}
void output() {
System.out.println("****** Student Details ******");
System.out.println("Name : " + name);
System.out.println("Course : " + course);
System.out.println("Address : " + address);
System.out.println("Roll No : " + rollNo);
System.out.println("Mobile No : " + mobileNum);
}
}
class StudentInfo {
public static void main(String[] args) {
System.out.println(
"***********************************************************\n\tPractical 2(3) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Class, Methods and Object *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
System.out.println("****** Student Details *****");
StudentDetails student1 = new StudentDetails();
student1.input();
student1.output();
}
}
import java.util.Scanner;
class Calculation{
void VolumeOfBox(){
Scanner scan = new Scanner(System.in);
float length, width, height;
System.out.println("***********************************************************\n\tPractical 2(2) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Class, Methods and Object *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
System.out.println(" ***** Calcualte Volume Of Box *****");
System.out.print("Enter Length : "); length = scan.nextFloat();
System.out.print("Enter Width : "); width = scan.nextFloat();
System.out.print("Enter Height : "); height = scan.nextFloat();
scan.close();
System.out.println("Volume : " + (length * width * height));
}
}
class CalBoxVolume {
public static void main(String[] args) {
Calculation volumeOfBox = new Calculation();
volumeOfBox.VolumeOfBox();
}
}
class ConstructorWithNoParameter{
ConstructorWithNoParameter(){
System.out.println("No Parameter Constructor Called...");
}
}
class ConstructorWithParameter{
ConstructorWithParameter(String name){
System.out.println("\nParameterized Constructor Called...");
System.out.println("Passed Parameter Is " + name);
}
}
class Constructor {
public static void main(String[] args) {
System.out.println("***********************************************************\n\tPractical 2(3) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Constructor *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
ConstructorWithNoParameter cNoParameter = new ConstructorWithNoParameter();
ConstructorWithParameter cParameter = new ConstructorWithParameter("Shubham Sawant");
}
}
class Area {
// for traingle
float area(float height, float base){
return (0.5f * height * base);
}
// for square
float area(float side){
return (side * side);
}
// for cube
double area(double side){
return (6 * side * side);
}
public static void main(String[] args) {
Area areaClass = new Area();
System.out.println("***********************************************************\n\tPractical 3(1) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Method Overloading *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
System.out.println("****** Calculate Area *****");
System.out.println("Area Of Traingle (Base : 20, Height : 14) : " + areaClass.area(20f, 14f));
System.out.println("Area Of Square (Side : 20) : " + areaClass.area(20f));
System.out.println("Area Of Cube (Side : 14) : " + areaClass.area(14d));
}
}
class ConstructorOverLoadingExample{
ConstructorOverLoadingExample(){
System.out.println("No Parameter - Constructor Called...");
}
ConstructorOverLoadingExample(String name){
System.out.println("\nParameter 1 - Constructor Called...");
System.out.println("Single Parameter Is " + name);
}
ConstructorOverLoadingExample(String name, String surname){
System.out.println("\nParameter 2 - Constructor Called...");
System.out.println("First Parameter Is " + name);
System.out.println("Second Parameter Is " + surname);
}
}
class ConstructorOverloading {
public static void main(String[] args) {
System.out.println("***********************************************************\n\tPractical 3(2) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Constructor Overloading *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
ConstructorOverLoadingExample cParameter0 = new ConstructorOverLoadingExample();
ConstructorOverLoadingExample cParameter1 = new ConstructorOverLoadingExample("Shubham");
ConstructorOverLoadingExample cParameter2 = new ConstructorOverLoadingExample("Shubham", "Sawant");
}
}
class DYPATU {
public void universityName() {
System.out.println("University : Dr. D. Y. Patil Agriculture & Technical University");
}
}
class College extends DYPATU {
public void collegeName() {
System.out.println("College : School Of Engineering & Technology");
}
}
class McaDepartment extends College {
public void departmentName() {
System.out.println("Department : Masters in Computer Application");
}
}
public class MultilevelInheritance {
public static void main(String args[]) {
System.out.println("***********************************************************\n\tPractical 4(1) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Multilevel Inheritance *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
McaDepartment mca = new McaDepartment();
mca.universityName();
mca.collegeName();
mca.departmentName();
}
}
class DYPATU_ {
public void universityName() {
System.out.println("University : Dr. D. Y. Patil Agriculture & Technical University");
}
}
class College_ extends DYPATU_ {
public void collegeName() {
System.out.println("College : School Of Engineering & Technology");
}
}
class McaDepartment_ extends College_ {
public void departmentName() {
System.out.println("Department : Masters in Computer Application");
}
}
class BtechDepartment_ extends College_ {
public void departmentName() {
System.out.println("Department : Bachelor Of Technology");
}
}
public class HierarchicalInheritance {
public static void main(String args[]) {
System.out.println("***********************************************************\n\tPractical 4(2) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Hierarchical Inheritance *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
McaDepartment_ mca = new McaDepartment_();
mca.universityName();
mca.collegeName();
mca.departmentName();
System.out.println();
BtechDepartment_ btch = new BtechDepartment_();
btch.universityName();
btch.collegeName();
btch.departmentName();
}
}
class CollegeEntity {
protected String name, location;
public CollegeEntity(String name, String location) {
this.name = name;
this.location = location;
}
public void printInfo() {
System.out.println("Name: " + name);
System.out.println("Location: " + location);
}
}
class StudentEntity extends CollegeEntity {
private String department;
public StudentEntity(String name, String location, String department) {
super(name, location);
this.department = department;
}
public void printInfo() {
super.printInfo();
System.out.println("Department: " + department);
}
}
class ProfessorEntity extends CollegeEntity {
private String title;
public ProfessorEntity(String name, String location, String title) {
super(name, location);
this.title = title;
}
public void printInfo() {
super.printInfo();
System.out.println("Title: " + title);
}
}
public class SuperKeyWord {
public static void main(String[] args) {
System.out.println("***********************************************************\n\tPractical 4(3) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Super Keyword *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
StudentEntity studentEntity = new StudentEntity("Shubham Sawant", "Kolhapur", "MCA");
ProfessorEntity professorEntity = new ProfessorEntity("Abhimanyu Sir", "Kolhapur", "MCA");
studentEntity.printInfo();
System.out.println();
professorEntity.printInfo();
}
}
interface CollegeInterface {
String getName(); String getLocation(); String getDepartment();
}
class Student implements CollegeInterface {
private String name, location, department;
public Student(String name, String location, String department) {
this.name = name;
this.location = location;
this.department = department;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public String getDepartment() {
return department;
}
}
class Professor implements CollegeInterface {
private String name, location, department;
public Professor(String name, String location, String department) {
this.name = name;
this.location = location;
this.department = department;
}
public String getName() {
return name;
}
public String getLocation() {
return location;
}
public String getDepartment() {
return department;
}
}
class InterfaceExample{
public static void main(String[] args) {
System.out.println("***********************************************************\n\tPractical 5(1) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Interface *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
Student student = new Student("Shubham Sawant", "Kolhapur", "MCA");
Professor professor = new Professor("Abhimanyu Sir", "Kolhapur", "MCA");
System.out.println(student.getName() + " is located in " + student.getLocation() + " and is part of the " + student.getDepartment() + " department.");
System.out.println(professor.getName() + " is located in " + professor.getLocation() + " and is part of the " + professor.getDepartment() + " department.");
}
}
interface Shape {
void draw();
}
interface Color {
void fill();
}
class Circle implements Shape, Color {
private int radius;
public Circle(int radius) {
this.radius = radius;
}
public void draw() {
System.out.println("Drawing a circle with radius " + radius);
}
public void fill() {
System.out.println("Filling a circle with color");
}
}
public class MultipleInheritance {
public static void main(String[] args) {
System.out.println("***********************************************************\n\tPractical 5(2) : Java Programming - / / \n***********************************************************");
System.out.println("****** Multiple Inheritance Using Interface *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
Circle circle = new Circle(10);
circle.draw();
circle.fill();
}
}
/** This Package Code Or Package File **/
package MyMaths;
public class MathsClass {
// System.out.println("***********************************************************\n\tPractical 6(1) : Java Programming - / / \n***********************************************************");
// System.out.println("Code By Sawant Infotech - Shubham Sawant");
public float addition(float num1, float num2) {
return num1 + num2;
}
public float substraction(float num1, float num2) {
return num1 - num2;
}
public float multiplication(float num1, float num2) {
return num1 * num2;
}
public float division(float num1, float num2) {
return num1 / num2;
}
}
/** Main File Code **/
import MyMaths.MathsClass;
class PackageExample {
public static void main(String[] args) {
System.out.println("***********************************************************\n\tPractical 6(1) : Java Programming - / / \n***********************************************************");
System.out.println("****** Demonstrate Package - User Define *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
MathsClass mathsClass = new MathsClass();
System.out.println("Addition (14.20, 20.14) : " + mathsClass.addition(14.20F, 20.14F));
System.out.println("Substraction (14.20, 20.14) : " + mathsClass.substraction(14.20F, 20.14F));
System.out.println("Multiplication (14.20, 20.14) : " + mathsClass.multiplication(14.20F, 20.14F));
System.out.println("Division (14.20, 20.14) : " + mathsClass.division(14.20F, 20.14F));
}
}
class ExceptionHandling {
public static void main(String[] args) {
System.out.println(
"***********************************************************\n\tPractical 7(1) : Java Programming - / / \n***********************************************************");
System.out.println("***** Demonstrate Exception Handling *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
// Handle ArithmeticException
try {
// Handle ArrayIndexOutOfBoundsException
try {
int array[] = new int[5];
array[10] = 10;
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("\nError : " + e.getMessage());
}
int data = 100 / 0;
} catch (ArithmeticException e) {
System.out.println("\nError : " + e.getMessage());
}
// Handle NumberFormatException and NullPointerException
try {
String str = null;
str.length();
int number = Integer.parseInt("abc");
} catch (NumberFormatException e) {
System.out.println("\nError : " + e.getMessage());
} catch (NullPointerException e) {
System.out.println("\nError : " + e.getMessage());
}
}
}
import java.util.Scanner;
class NegativeNumException extends Exception {
@Override
public String toString() {
return ("Number Is Negative !");
}
}
public class UserLevelException {
public static void main(String[] args) {
System.out.println("***********************************************************\n\tPractical 7(2) : Java Programming - / / \n***********************************************************");
System.out.println("***** Demonstrate Own User Level Exception Handling *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
Scanner scan = new Scanner(System.in);
try {
System.out.print("Enter Positive Number : ");
int num = scan.nextInt();
if (num < 0)
throw new NegativeNumException();
else
System.out.println("Given Number Is Postive.");
} catch (NegativeNumException e) {
System.out.println("Error : " + e);
} finally {
scan.close();
}
}
}
import java.io.FileWriter;
import java.io.FileReader;
import java.util.Scanner;;
class ConsoleFile {
public static void main(String[] args) {
System.out.println("***********************************************************\n\tPractical 8(1) : Java Programming - / / \n***********************************************************");
System.out.println("***** Demonstrate Read, Write And Append Operations Using Console And File *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
String fileName = "NewFile.txt";
String newIData = "", appendData = "";
try{
Scanner userIp = new Scanner(System.in);
FileWriter writer = new FileWriter(fileName);
System.out.println("File Created Successfully...");
System.out.print("Write Something To Store In File : ");
newIData = userIp.nextLine();
writer.write(newIData);
System.out.println("File Write Successfully...");
writer.close();
FileWriter writerAppend = new FileWriter(fileName, true);
System.out.print("Write Something To Append Data In File : ");
appendData = userIp.next();
writerAppend.write("\n" + appendData);
System.out.println("File Append Successfully...");
writerAppend.close();
userIp.close();
System.out.println("File Data Is (Read Data From Filre): ");
Scanner scan = new Scanner(new FileReader(fileName));
if(scan.hasNextLine())
System.out.println(scan.nextLine());
}catch(Exception ex){
System.out.println("Error Occured ! \nError : " + ex.getMessage());
}
}
}
import java.io.*;
class DataIOStream {
public static void main(String[] args) throws IOException {
System.out.println(
"***********************************************************\n\tPractical 8(2) : Java Programming - / / \n***********************************************************");
System.out.println("***** Demonstrate DataInputStream And DataOutputStream Classes *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
try {
DataOutputStream dout = new DataOutputStream(new FileOutputStream("file.bin"));
dout.writeDouble(1.1);
dout.writeInt(55);
dout.writeBoolean(true);
dout.writeChar('4');
dout.close();
}
catch (Exception ex) {
System.out.println("Error Occured ! \nError : " + ex.getMessage());
}
try {
DataInputStream din = new DataInputStream(new FileInputStream("file.bin"));
// Illustrating readDouble() method
double a = din.readDouble();
// Illustrating readInt() method
int b = din.readInt();
// Illustrating readBoolean() method
boolean c = din.readBoolean();
// Illustrating readChar() method
char d = din.readChar();
din.close();
// Print the values
System.out.println("Values: " + a + " " + b + " " + c + " " + d);
}catch (Exception ex) {
System.out.println("Error Occured ! \nError : " + ex.getMessage());
}
}
}
import java.io.FileWriter;
import java.io.FileReader;
import java.util.Scanner;
class ConsoleFileTsetFileTxt {
public static void main(String[] args) {
System.out.println(
"***********************************************************\n\tPractical 8(1) : Java Programming - / / \n***********************************************************");
System.out.println("***** Demonstrate Read, Write And Append Operations Using Console And File *****");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
String fileName = "Test.txt";
try {
FileWriter writer = new FileWriter(fileName);
System.out.println("File Created Successfully...");
writer.write("\"Welcome To DYP ATU\"");
System.out.println("File Write Successfully...");
writer.close();
System.out.println("File Data Is (Read Data From Filre): ");
Scanner scan = new Scanner(new FileReader(fileName));
while (scan.hasNextLine())
System.out.println(scan.nextLine());
} catch (Exception ex) {
System.out.println("Error Occured ! \nError : " + ex.getMessage());
}
}
}
package P9;
import java.awt.*;
import javax.swing.*;
public class GridLayoutExample {
GridLayoutExample() {
// Create the frame
JFrame frame = new JFrame("GridLayout");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the layout manager
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 3, 5, 10));
// Add buttons to the content pane
for (int i = 1; i <= 6; i++) {
panel.add(new JButton("Button " + i));
}
frame.add(panel);
// Display the frame
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) {
// System.out.println("Code By Sawant Infotech - Shubham Sawant");
new GridLayoutExample();
}
}
package P9;
import java.awt.*;
import javax.swing.*;
public class BorderFlowLayoutExample {
private static void createAndShowGUI() {
// Create the frame
JFrame frame = new JFrame("BorderFlowLayout Example");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set the layout manager for the content pane
Container contentPane = frame.getContentPane();
contentPane.setLayout(new BorderLayout());
// Add buttons to the content pane using BorderLayout
contentPane.add(new JButton("North"), BorderLayout.NORTH);
contentPane.add(new JButton("West"), BorderLayout.WEST);
contentPane.add(new JButton("East"), BorderLayout.EAST);
contentPane.add(new JButton("South"), BorderLayout.SOUTH);
// Create a panel for the center region and set its layout to FlowLayout
JPanel centerPanel = new JPanel();
centerPanel.setLayout(new FlowLayout());
// Add buttons to the center panel using FlowLayout
for (int i = 1; i <= 6; i++) {
centerPanel.add(new JButton("Button " + i));
}
// Add the center panel to the content pane
contentPane.add(centerPanel, BorderLayout.CENTER);
// Display the frame
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// System.out.println("Code By Sawant Infotech - Shubham Sawant");
createAndShowGUI();
}
}
package P10.Calculator;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class Main extends JFrame implements ActionListener {
JButton btn[] = new JButton[24];
JTextField inputBox;
JLabel problemMsg;
String getProblemMsg = "";
Font fnt = new Font("Ms Mincho", Font.BOLD, 30);
Color frmBackColor = new Color(33, 33, 33),
btnBackColor = new Color(64, 63, 63),
fontColor = new Color(255, 255, 255),
eqlButtonColor = new Color(224, 121, 17);
String operation = "";
public Main() {
super("Calculator - Sawant Infotech");
JPanel btnPanel = new JPanel();
JPanel inputBoxPanel = new JPanel();
JPanel footerPanel = new JPanel();
this.setLayout(new BorderLayout());
btnPanel.setLayout(new GridLayout(6, 4));
inputBoxPanel.setLayout(new GridLayout(2, 1));
setInputBoxUI(inputBoxPanel);
setButtonsUI(btnPanel);
setFontsUI(btnPanel, inputBoxPanel);
setFooterUI(footerPanel);
add(inputBoxPanel, BorderLayout.NORTH);
add(btnPanel, BorderLayout.CENTER);
add(footerPanel, BorderLayout.SOUTH);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setSize(400, 600);
// disabled buttons : also @ line - 224
btn[0].setEnabled(false);
btn[20].setEnabled(false);
}
private void setInputBoxUI(JPanel inputBoxPanel) {
inputBox = new JTextField(20);
inputBoxPanel.add(inputBox);
inputBox.setEditable(false);
inputBox.setHorizontalAlignment(JTextField.RIGHT);
inputBoxPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));
inputBox.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
problemMsg = new JLabel();
inputBoxPanel.add(problemMsg);
problemMsg.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
if (problemMsg.getText().length() > 0)
JOptionPane.showMessageDialog(null, getProblemMsg);
}
});
problemMsg.setHorizontalAlignment(JTextField.RIGHT);
problemMsg.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
inputBoxPanel.setBackground(frmBackColor);
}
private void setButtonsUI(JPanel btnPanel) {
btn[0] = new JButton("%");
btnPanel.add(btn[0]);
btn[0].addActionListener(this);
btn[1] = new JButton("CE");
btnPanel.add(btn[1]);
btn[1].addActionListener(this);
btn[2] = new JButton("C");
btnPanel.add(btn[2]);
btn[2].addActionListener(this);
btn[3] = new JButton("\u25C4");
btnPanel.add(btn[3]);
btn[3].addActionListener(this);
btn[4] = new JButton("1/x");
btnPanel.add(btn[4]);
btn[4].addActionListener(this);
btn[5] = new JButton("x\u00B2");
btnPanel.add(btn[5]);
btn[5].addActionListener(this);
btn[6] = new JButton("\u221A");
btnPanel.add(btn[6]);
btn[6].addActionListener(this);
btn[7] = new JButton("/");
btnPanel.add(btn[7]);
btn[7].addActionListener(this);
btn[8] = new JButton("7");
btnPanel.add(btn[8]);
btn[8].addActionListener(this);
btn[9] = new JButton("8");
btnPanel.add(btn[9]);
btn[9].addActionListener(this);
btn[10] = new JButton("9");
btnPanel.add(btn[10]);
btn[10].addActionListener(this);
btn[11] = new JButton("*");
btnPanel.add(btn[11]);
btn[11].addActionListener(this);
btn[12] = new JButton("4");
btnPanel.add(btn[12]);
btn[12].addActionListener(this);
btn[13] = new JButton("5");
btnPanel.add(btn[13]);
btn[13].addActionListener(this);
btn[14] = new JButton("6");
btnPanel.add(btn[14]);
btn[14].addActionListener(this);
btn[15] = new JButton("-");
btnPanel.add(btn[15]);
btn[15].addActionListener(this);
btn[16] = new JButton("1");
btnPanel.add(btn[16]);
btn[16].addActionListener(this);
btn[17] = new JButton("2");
btnPanel.add(btn[17]);
btn[17].addActionListener(this);
btn[18] = new JButton("3");
btnPanel.add(btn[18]);
btn[18].addActionListener(this);
btn[19] = new JButton("+");
btnPanel.add(btn[19]);
btn[19].addActionListener(this);
btn[20] = new JButton("+/-");
btnPanel.add(btn[20]);
btn[20].addActionListener(this);
btn[21] = new JButton("0");
btnPanel.add(btn[21]);
btn[21].addActionListener(this);
btn[22] = new JButton(".");
btnPanel.add(btn[22]);
btn[22].addActionListener(this);
btn[23] = new JButton("=");
btnPanel.add(btn[23]);
btn[23].addActionListener(this);
btnPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
btnPanel.setBackground(frmBackColor);
}
private void setFontsUI(JPanel btnPanel, JPanel inputBoxPanel) {
inputBox.setFont(fnt);
inputBox.setBackground(btnBackColor);
inputBox.setForeground(fontColor);
problemMsg.setFont(new Font("Ms Mincho", Font.BOLD, 15));
problemMsg.setForeground(Color.red);
problemMsg.setBackground(Color.BLUE);
for (Component c : btnPanel.getComponents()) {
c.setFont(fnt);
c.setBackground(btnBackColor);
c.setForeground(fontColor);
}
btn[23].setBackground(eqlButtonColor);
btn[23].setForeground(Color.black);
}
private void setFooterUI(JPanel footerPanel) {
JLabel footerLbl = new JLabel("Sawant Infotech");
footerLbl.setFont(new Font("Ms Mincho", Font.BOLD, 40));
footerLbl.setSize(footerPanel.getWidth(), footerPanel.getHeight());
footerPanel.add(footerLbl);
footerPanel.setBackground(new Color(255, 240, 25));
}
@Override
public void actionPerformed(ActionEvent e) {
JButton clickBtn = (JButton) e.getSource();
String input = inputBox.getText();
if (clickBtn == btn[0]) {
} else if (clickBtn == btn[1] || clickBtn == btn[2]) {
inputBox.setText("");
problemMsg.setText("");
disableBtn(0);
btn[22].setEnabled(true); // . button enabled
} else if (clickBtn == btn[3]) {
try {
inputBox.setText(input.substring(0, input.length() - 1));
} catch (Exception ex) {
}
problemMsg.setText("");
} else if (clickBtn == btn[4]) {
inputBox.setText("1/");
disableBtn(4);
problemMsg.setText("");
} else if (clickBtn == btn[5]) {
if (input.length() > 0) {
try {
inputBox.setText(squ(Double.parseDouble(input)) + "");
problemMsg.setText("");
} catch (Exception ex) {
problemMsg.setText("Unable To Handle ! (Invalid Inputs)");
}
}
} else if (clickBtn == btn[6]) {
inputBox.setText("\u221A");
problemMsg.setText("");
} else if (clickBtn == btn[7]) {
inputBox.setText(inputBox.getText() + "/");
disableBtn(7);
problemMsg.setText("");
} else if (clickBtn == btn[8]) {
inputBox.setText(input + "7");
problemMsg.setText("");
} else if (clickBtn == btn[9]) {
inputBox.setText(input + "8");
problemMsg.setText("");
} else if (clickBtn == btn[10]) {
inputBox.setText(input + "9");
problemMsg.setText("");
} else if (clickBtn == btn[11]) {
inputBox.setText(inputBox.getText() + "*");
disableBtn(11);
problemMsg.setText("");
} else if (clickBtn == btn[12]) {
inputBox.setText(input + "4");
problemMsg.setText("");
} else if (clickBtn == btn[13]) {
inputBox.setText(input + "5");
problemMsg.setText("");
} else if (clickBtn == btn[14]) {
inputBox.setText(input + "6");
problemMsg.setText("");
} else if (clickBtn == btn[15]) {
inputBox.setText(inputBox.getText() + "-");
disableBtn(15);
problemMsg.setText("");
} else if (clickBtn == btn[16]) {
inputBox.setText(input + "1");
problemMsg.setText("");
} else if (clickBtn == btn[17]) {
inputBox.setText(input + "2");
problemMsg.setText("");
} else if (clickBtn == btn[18]) {
inputBox.setText(input + "3");
problemMsg.setText("");
} else if (clickBtn == btn[19]) {
inputBox.setText(inputBox.getText() + "+");
disableBtn(19);
problemMsg.setText("");
} else if (clickBtn == btn[20]) {
problemMsg.setText("");
disableBtn(20);
} else if (clickBtn == btn[21]) {
inputBox.setText(input + "0");
problemMsg.setText("");
} else if (clickBtn == btn[22]) {
if (inputBox.getText().length() == 0)
inputBox.setText("0.");
else
inputBox.setText(input + ".");
problemMsg.setText("");
btn[22].setEnabled(false);
} else if (clickBtn == btn[23]) {
problemMsg.setText("");
inputBox.setText(eval(input, problemMsg));
disableBtn(0);
}
}
private void disableBtn(int btnNum) {
if (btnNum == 4) { // +
btn[7].setEnabled(false);
btn[11].setEnabled(false);
btn[15].setEnabled(false);
btn[19].setEnabled(false);
btn[20].setEnabled(false);
operation = "/";
} else if (btnNum == 19) { // +
btn[4].setEnabled(false);
btn[7].setEnabled(false);
btn[11].setEnabled(false);
btn[15].setEnabled(false);
btn[20].setEnabled(false);
operation = "\\+";
} else if (btnNum == 7) { // /
btn[4].setEnabled(false);
btn[19].setEnabled(false);
btn[11].setEnabled(false);
btn[15].setEnabled(false);
btn[20].setEnabled(false);
operation = "/";
} else if (btnNum == 11) { // *
btn[4].setEnabled(false);
btn[7].setEnabled(false);
btn[19].setEnabled(false);
btn[15].setEnabled(false);
btn[20].setEnabled(false);
operation = "\\*";
} else if (btnNum == 15) { // -
btn[4].setEnabled(false);
btn[7].setEnabled(false);
btn[11].setEnabled(false);
btn[19].setEnabled(false);
btn[20].setEnabled(false);
operation = "-";
} else if (btnNum == 20) { // +/-
btn[4].setEnabled(false);
btn[7].setEnabled(false);
btn[11].setEnabled(false);
btn[15].setEnabled(false);
btn[19].setEnabled(false);
} else { // enables all buttons
btn[4].setEnabled(true);
btn[7].setEnabled(true);
btn[11].setEnabled(true);
btn[15].setEnabled(true);
btn[19].setEnabled(true);
btn[20].setEnabled(true);
operation = "";
}
// disabled buttons : also @ line - 37
btn[0].setEnabled(false);
btn[20].setEnabled(false);
}
private String findRes(String exp, String operation) {
String nNum[] = exp.split(operation);
double res = Double.parseDouble(nNum[0]);
for (int i = 1; i < nNum.length; i++) {
if (operation == "/")
res /= Double.parseDouble(nNum[i]);
else if (operation == "\\*")
res *= Double.parseDouble(nNum[i]);
else if (operation == "-")
res -= Double.parseDouble(nNum[i]);
else if (operation == "\\+")
res += Double.parseDouble(nNum[i]);
}
return res + "";
}
private String eval(String input, JLabel pbMsg) {
String res = "";
if (input.length() > 0) {
try {
if (input.substring(0, 1).equals("\u221A")) // square root "\u221A"
res = (Math.sqrt(Double.parseDouble(input.substring(1))) + "");
else if (operation == "")
res = input;
else
res = findRes(input, operation);
} catch (Exception ex) {
pbMsg.setText("Unable To Handle ! (Invalid Inputs)");
getProblemMsg = ex.getMessage();
}
}
return res;
}
private double squ(double num) {
return (num * num);
}
public static void main(String[] args) {
// System.out.println("Code By Sawant Infotech - Shubham Sawant");
new Main();
}
}
import javax.swing.*; import java.awt.*;
import java.awt.event.*;
import java.io.*;
class CustomerFrame {
private JFrame frm;
private JPanel pan, genPan, btnPan;
private JLabel fNameLbl, lNameLbl, mobileLbl, genderLbl;
private JTextField fNameTxtField, lNameTxtField, mobileTxtField;
private JRadioButton maleRBtn, femaleRBtn;
private ButtonGroup grpBtn;
private JButton submitBtn, resetBtn;
private String gender;
public void initializeComponent() {
frm = new JFrame(); frm.setLayout(new BorderLayout());
pan = new JPanel(); genPan = new JPanel(); btnPan = new JPanel();
pan.setLayout(new GridLayout(4,2, 0,5));
genPan.setLayout(new GridLayout(1,2));
btnPan.setLayout(new GridLayout(1,2, 20, 0));
fNameLbl = new JLabel("Enter First Name : "); pan.add(fNameLbl);
fNameTxtField = new JTextField(); pan.add(fNameTxtField);
lNameLbl = new JLabel("Enter Last Name : "); pan.add(lNameLbl);
lNameTxtField = new JTextField(); pan.add(lNameTxtField);
mobileLbl = new JLabel("Enter Mobile Number : ");pan.add(mobileLbl);
mobileTxtField = new JTextField(); pan.add(mobileTxtField);
genderLbl = new JLabel("Select Gender : ");pan.add(genderLbl);
maleRBtn = new JRadioButton("Male"); genPan.add(maleRBtn);
femaleRBtn = new JRadioButton("Female");genPan.add(femaleRBtn);
maleRBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) { gender = maleRBtn.getText(); }
});
femaleRBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
gender = femaleRBtn.getText();
}
});
pan.add(genPan);
grpBtn = new ButtonGroup(); grpBtn.add(maleRBtn); grpBtn.add(femaleRBtn);
submitBtn = new JButton("Submit"); btnPan.add(submitBtn);
resetBtn = new JButton("Reset"); btnPan.add(resetBtn);
submitBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
validateForm();
}
});
resetBtn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
clrForm();
}
});
pan.setBorder(BorderFactory.createEmptyBorder(10,15,0,20));
btnPan.setBorder(BorderFactory.createEmptyBorder(10,215,10,20));
frm.add(pan, BorderLayout.CENTER); frm.add(btnPan, BorderLayout.SOUTH);
frm.setTitle("Registration Form - By Shubham Sawant");
frm.setSize(450, 200);
frm.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frm.setResizable(false); frm.setLocationRelativeTo(null); frm.setVisible(true);
}
private void validateForm() {
if ((!fNameTxtField.getText().isEmpty() && !lNameTxtField.getText().isEmpty()
&& !mobileTxtField.getText().isEmpty()) && (maleRBtn.isSelected() || femaleRBtn.isSelected())) {
if (toTxtFile(fNameTxtField.getText(), lNameTxtField.getText(), mobileTxtField.getText())) {
JOptionPane.showMessageDialog(null, "Your Data Successfully Saved...", "Success...",
JOptionPane.INFORMATION_MESSAGE);
clrForm();
} else {
JOptionPane.showMessageDialog(null, "Failure To Save Data !", "Error",
JOptionPane.ERROR_MESSAGE);
}
} else {
JOptionPane.showMessageDialog(null, "All Fields Are Compulsory...", "Warning",
JOptionPane.WARNING_MESSAGE);
}
}
private void clrForm() {
fNameTxtField.setText(null);lNameTxtField.setText(null);
mobileTxtField.setText(null); grpBtn.clearSelection();
}
private boolean toTxtFile(String fName, String lName, String mobNum) {
String data = "{" + fName + ", " + lName + ", " + mobNum + ", " + gender + "}";
String fileName = "data-shubhams1401.txt";
try {
java.io.FileWriter fw = new FileWriter(fileName, true);
if (new File(fileName).length() != 0)
data = "," + data;
fw.write(data); fw.close(); return true;
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
JOptionPane.ERROR_MESSAGE);
return false;
}
}
}
class RegistrationForm {
public static void main(String[] args) {
// System.out.println("Code By Sawant Infotech - Shubham Sawant");
new CustomerFrame().initializeComponent();
}
}
package p11;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventHandlingExample extends JFrame {
EventHandlingExample() {
setTitle("Event Handling Example - By Shubham Sawant");
setLayout(new BorderLayout());
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(2, 1));
JLabel label = new JLabel("No button clicks yet");
label.setHorizontalAlignment(JLabel.CENTER);
JButton button = new JButton("Click Me!");
button.addActionListener(new ActionListener() {
private int count = 0;
@Override
public void actionPerformed(ActionEvent e) {
count++;
label.setText("Button clicked " + count + " times");
}
});
panel.add(label);
panel.add(button);
add(panel, BorderLayout.CENTER);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(800, 100);
setVisible(true);
}
public static void main(String[] args) {
// System.out.println("Code By Sawant Infotech - Shubham Sawant");
new EventHandlingExample();
}
}
import java.awt.*;
import java.applet.*;
public class SmileyApplet extends Applet {
@Override
public void paint(Graphics g) {
// Draw the face
g.setColor(Color.YELLOW);
g.fillOval(10, 10, 200, 200);
// Draw the eyes
g.setColor(Color.BLACK);
g.fillOval(55, 65, 30, 30);
g.fillOval(135, 65, 30, 30);
// Draw the mouth
g.fillArc(50, 105, 120, 60, 180, 180);
// System.out.println("Code By Sawant Infotech - Shubham Sawant");
}
}
/* <applet code="SmileyApplet.class" width="500" height="500"></applet>*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class EventHandlingApplet extends Applet implements ActionListener {
private Button button;
private Label label;
public void init() {
button = new Button("Click Me!");
add(button);
button.addActionListener(this);
label = new Label("");
add(label);
}
public void actionPerformed(ActionEvent e) {
label.setText("Button clicked!");
// System.out.println("Code By Sawant Infotech - Shubham Sawant");
}
}
/* <applet code="EventHandlingApplet.class" width="500" height="500"></applet>*/
// Server Side Code
import java.io.*;
import java.net.*;
public class ChatServer {
public static void main(String[] args) throws IOException {
int count = 0, port = 1401;
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(port);
System.out.println("Code By Sawant Infotech - Shubham Sawant");
System.out.println("Server Start : " + new java.util.Date(System.currentTimeMillis()));
System.out.println("Current Port : " + serverSocket.getLocalPort());
} catch (IOException e) {
System.err.println("Could not listen on port: " + port + ".");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
System.out.println("Client Host : " + clientSocket.getInetAddress().getHostAddress());
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(
clientSocket.getInputStream()));
String inputLine, outputLine;
while ((inputLine = in.readLine()) != null) {
outputLine = "Server: " + inputLine;
out.println(outputLine);
count++;
if (outputLine.toLowerCase().equals("bye"))
break;
}
out.close();
in.close();
clientSocket.close();
serverSocket.close();
System.out.println("Server Total Response Count : " + count);
System.out.println("Server Close : " + new java.util.Date(System.currentTimeMillis()));
}
}
// Client Side Code
import java.io.*;
import java.net.*;
public class ChatClient {
public static void main(String[] args) throws IOException {
System.out.println(
"***********************************************************\n\tPractical 13 : Java Programming - / / \n***********************************************************");
System.out.println(
"\t***********************************************\n\t\t Client & Server Communication\n\t***********************************************");
System.out.println("Code By Sawant Infotech - Shubham Sawant");
Socket kkSocket = null;
PrintWriter out = null;
BufferedReader in = null;
int port = 1401;
try {
kkSocket = new Socket("localhost", port);
out = new PrintWriter(kkSocket.getOutputStream(), true);
System.out.println("The Server Returns What You Sent");
System.out.println("Enter 'Bye' To Exit");
in = new BufferedReader(new InputStreamReader(kkSocket.getInputStream()));
} catch (UnknownHostException e) {
System.err.println("Don't know about host: localhost.");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to: localhost.");
System.exit(1);
}
BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
String fromServer;
String fromUser;
while (true) {
System.out.print("Client : ");
if ((fromUser = stdIn.readLine()) != null) {
out.println(fromUser);
fromServer = in.readLine();
System.out.println(fromServer);
if (fromUser.toLowerCase().equals("bye"))
break;
}
}
out.close();
in.close();
stdIn.close();
kkSocket.close();
}
}
class MultithreadingDemo extends Thread {
public void run() {
try {
// Displaying the thread that is running
System.out.println(
"Thread " + Thread.currentThread().getId()
+ " is running");
} catch (Exception e) {
// Throwing an exception
System.out.println("Exception : " + e);
}
}
}
public class MultithreadExample {
public static void main(String[] args) {
System.out.println("Code By Sawant Infotech - Shubham Sawant");
int n = 7; // Number of threads
for (int i = 0; i < n; i++) {
MultithreadingDemo object = new MultithreadingDemo();
object.start();
}
}
}
// Code Not Available
// Code Not Available
*Note : If Any Query Please Comment Down 🙏