oops edited
oops edited
AIM:
To write a Java program to calculate the area of rectangle, circle and triangle using
the concept of abstract class.
ALGORITHM:
Step 1: Start the program.
Step 2: Create an abstract class named shape that contains two integers and an
empty method named area().
Step 3: Create three classes named rectangle, triangle and circle such that each
one of the classes extends the class Shape.
Step 4: Each of the inherited class from shape class should provide the
implementation for the method area().
Step 5: In area() method get the input from user and calculate the area of
rectangle, circle and triangle.
Step 6: In the AbstractDDemo, create the objects for the three inherited classes
and invoke the methods and display the area values of the different shapes.
Step 7: Stop the program.
import java.util.*;
abstract class shape
{
int x,y;
abstract void area(double x,double y);
}
class Rectangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of rectangle is :"+
(x*y));
}
}
class Circle extends shape
{
void area(double x,double y)
{
System.out.println("Area of circle
is :"+(3.14*x*x));
}
}
class Triangle extends shape
{
void area(double x,double y)
{
System.out.println("Area of triangle
is :"+(0.5*x*y));
}
}
public class AbstactDDemo
{
public static void main(String[] args)
{
Rectangle r=new Rectangle();
r.area(2,5);
Circle c=new Circle();
c.area(5,5);
Triangle t=new Triangle();
t.area(2,5);
}
}
5.Write a Java Program to create an abstract class named
Shape
that contains two integers and an empty method named
printArea(). Provide three classes named Rectangle, Triangle and
Circle such that each one of the classes extends the class Shape.
Each one of the classes contains only the method printArea( ) that
prints the area of the given shape. Solve the above problem using
an interface
AIM:
To write a Java program to calculate the area of rectangle, circle and triangle by
implementing the interface shape.
ALGORITHM:
Step 2: Create an interface named shape that contains an empty methodnamed printArea().
Step 3: Create three classes named rectangle, triangle and circle such that each
one of the classes implements the class Shape.
Step 4: Each of the class should provide the implementation for the method printArea().
Step 5: In printArea() method get the input from user and calculate the area of
rectangle, circle and triangle.
Step 6: In the AbstractClassExample, create the objects for the three classes
and invoke the MethodprintArea() and display the area values of the different
shapes.
Step 7: Stop the program.
import java.util.*;
abstract class Shape {
int length, breadth, radius;
Scanner input = new Scanner(System.in);
abstract void printArea();
}
class Rectangle extends Shape {
void printArea() {
System.out.println("*** Finding the Area of Rectangle ***");
System.out.print("Enter length and breadth: ");
length = input.nextInt();
breadth = input.nextInt();
System.out.println("The area of Rectangle is: " + length *
breadth);
}
}
RESULT:
Thus, the Java program to calculate the area of rectangle, circle and
triangle using the concept of interfacewasdeveloped and executed successfully.
6.IMPLEMENT EXCEPTION HANDLING AND CREATION OF USER DEFINED EXCEPTIONS
AIM:
To write a Java program to implement user defined exception handling.
ALGORITHM:
AIM:
To write a java program to implement a multi-threaded application.
ALGORITHM:
Step 2: Create a class even which implements first thread that computes the
square of the number.
Step 3:Therun() method implements the code to be executed when thread gets
executed. Step 4:Create a class odd which implements second thread that
computes the cube of the number.
Step 5:Create a third thread that generates random number. If the random number is even,
it displaysthe square of the number. If the random number generated is odd, it displays
the cube of thegiven number.
Step 6: The Multithreading is performed and the task switched between multiple threads.
Step 7: The sleep () method makes the thread to suspend for the specified time.
Step 8: Stop the program.
import java.util.Random;
if((randomInteger%2) == 0) {
sThread.start();
else {
cThread.start();
try {
Thread.sleep(1000);
System.out.println(ex);
int number;
SquareThread(int randomNumbern) {
number = randomNumbern;
int number;
CubeThread(int randomNumber) {
number = randomNumber;
rnThread.start();
}
EX.NO.: 8
WRITE A PROGRAM TO PERFORM FILE OPERATIONS
AIM:
To write a java program to copy the contents of one file to another file using file
operations.
ALGORITHM:
Step 2: Create a class FileCopy. Get the source and destination file names from the user.
Step 3:Create object of FileInputStream by passing the source file name.
Step 4:Using read() method read the contents of the file till end of the file.
Step 5:Create object of FileOutputStream by passing the second file to the constructor.
Step 6:Use write() method to write the contents to the destination file.
Step 7:Close the file input and output stream using close() method.
Step 8:Display the contents of both files.
Step 9: Stop the program.
PROGRAM
PackagejavaTpoint.javacodes;
import java.io.*;
importjava.util.*;
publicclassFileExample {
// create instances of FileInputStream and FileOutputStream classes for file1 and file2
FileInputStreaminputStream = newFileInputStream(file1);
FileOutputStreamoutputStream = newFileOutputStream(file2);
// use try-catch-finally block to read and write bytes data into file
try {
inti;
// use while loop with read() method of FileInputStream class to read bytes data
from file1
// use write() method of FileOutputStream class to write the byte data into file2
outputStream.write(i);
catch(Exception e) {
finally {
if (inputStream != null) {
inputStream.close();
if (outputStream != null) {
outputStream.close();
}
}
System.out.println("File Copied");
Scanner sc = newScanner(System.in);
System.out.println("Enter the name of the file from where the data would be
copied :");
File a = newFile("C:\\Users\\pc\\OneDrive\\Desktop\\"+file1);
System.out.println("Enter the name of the file from where the data would be
written :");
File b = newFile("C:\\Users\\pc\\OneDrive\\Desktop\\"+file2);
sc.close();
copyData(a, b);
}
10.a.DEVELOP APPLICATIONS USING JAVAFX CONTROLS
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.RadioButton;
import javafx.scene.control.ToggleGroup;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
@Override
radio1=new RadioButton("10");
radio1.setToggleGroup(question1);
radio2.setToggleGroup(question1);
radio3.setToggleGroup(question1);
radio4.setToggleGroup(question1);
button.setDisable(true);
button.setOnAction(e ->
if (radio3.isSelected())
labelresponse.setText("Correct answer");
button.setDisable(true);
else
labelresponse.setText("Wrong answer");
button.setDisable(true);
);
primaryStage.setScene(scene1);
primaryStage.show();
launch(args);
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Menu;
import javafx.scene.control.MenuBar;
import javafx.scene.control.MenuItem;
import javafx.scene.layout.VBox;
l.setText("\t\t\t\t\t\t" +
((MenuItem)e.getSource()).getT
ext() + " menu item selected");
}
};
m1.setOnAction(event);
m2.setOnAction(event);
m3.setOnAction(event);
m4.setOnAction(event);
m5.setOnAction(event);
m6.setOnAction(event);
primaryStage.setScene(scene);
primaryStage.setTitle("JavaFX Menu");
primaryStage.show();
Application.launch(args);
DEVELOP A MINI PROJECT FOR ANY APPLICATION USING JAVA CONCEPTS (LIBRARY
MANAGEMENT SYSTEM)
package library;
import javax.swing.JOptionPane;
public Login() {
initComponents();
}
@SuppressWarnings("unchecked")
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jLabel1.setText("Library Login");
jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Login"));
jLabel2.setText("Username");
jLabel3.setText("Password");
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(4, 4, 4)
.addComponent(txtpass))
.addContainerGap(114, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
jPanel1Layout.createSequentialGroup()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(jLabel2)
.addComponent(txtusername, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G)
.addComponent(jLabel3)
.addComponent(txtpass, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(31, Short.MAX_VALUE))
);
jButton2.setText("Cancel");
jButton2.addActionListener(new java.awt.event.ActionListener() {
});
jButton1.setText("Login");
jButton1.addActionListener(new java.awt.event.ActionListener() {
jButton1ActionPerformed(evt);
});
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGap(4, 4, 4))
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap(18, Short.MAX_VALUE)
.addComponent(jLabel1)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addContainerGap())))
);
this.hide();
m.setVisible(true);
}
else
txtusername.setText("");
txtpass.setText("");
txtusername.requestFocus();
Category.java
package library;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
public category() {
initComponents();
Connect();
table_update();
Connection con;
PreparedStatement pst;
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Category",
javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 14)));
jLabel9.setText("Category");
jLabel10.setText("Status");
jButton1.setText("Add");
jButton1.addActionListener(new java.awt.event.ActionListener() {
});
jButton2.setText("Edit");
jButton2.addActionListener(new java.awt.event.ActionListener() {
jButton2ActionPerformed(evt);
});
jButton3.setText("Delete");
jButton3.addActionListener(new java.awt.event.ActionListener() {
jButton3ActionPerformed(evt);
});
jTable1.setModel(new javax.swing.table.DefaultTableModel(
},
new String [] {
){
};
});
jTable1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
jTable1MouseClicked(evt);
});
jScrollPane1.setViewportView(jTable1);
jButton4.setText("Cancel");
jButton4.addActionListener(new java.awt.event.ActionListener() {
jButton4ActionPerformed(evt);
});
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILIN
G)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING)
.addComponent(jLabel9)
.addComponent(jLabel10))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEA
DING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(8, 8, 8)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 73,
Short.MAX_VALUE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(txtcat, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel9))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELIN
E)
.addComponent(txtstatus, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel10))
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADIN
G)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASEL
INE)
.addComponent(jButton3, javax.swing.GroupLayout.Alignment.TRAILING,
javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)))
.addContainerGap(23, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 43,
Short.MAX_VALUE)
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/slibr","root","");
int c;
try {
ResultSet rs = pst.executeQuery();
c = rsd.getColumnCount()
DefaultTableModel d = (DefaultTableModel)jTable1.getModel();
d.setRowCount(0);
while(rs.next())
v2.add(rs.getString("id"));
v2.add(rs.getString("category"));
v2.add(rs.getString("status"));
}
d.addRow(v2);
try {
pst.setString(1, category);
pst.setString(2, status);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Category Adddeddd");
table_update();
txtcat.setText("");
txtstatus.setSelectedIndex(-1);
txtcat.requestFocus();
DefaultTableModel d1 = (DefaultTableModel)jTable1.getModel();
try {
pst.setString(1, category);
pst.setString(2, status);
pst.setInt(3, id);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Category Updateddd");
table_update();
txtcat.setText("");
txtstatus.setSelectedIndex(-1);
txtcat.requestFocus();
jButton1.setEnabled(true);
DefaultTableModel d1 = (DefaultTableModel)jTable1.getModel();
if(dialogResult == JOptionPane.YES_OPTION)
try {
pst = con.prepareStatement("delete from category where id =?");
pst.setInt(1, id);
pst.executeUpdate();
JOptionPane.showMessageDialog(null,"Category Deletedd");
table_update();
txtcat.setText("");
txtstatus.setSelectedIndex(-1);
txtcat.requestFocus();
DefaultTableModel d1 = (DefaultTableModel)jTable1.getModel();
txtcat.setText(d1.getValueAt(selectIndex, 1).toString());
txtstatus.setSelectedItem(d1.getValueAt(selectIndex, 2).toString());
jButton1.setEnabled(false);
this.setVisible(false);
try {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
java.util.logging.Logger.getLogger(category.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
java.util.logging.Logger.getLogger(category.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
java.util.logging.Logger.getLogger(category.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
java.util.logging.Logger.getLogger(category.class.getName()).log(java.util.logging.Level.SEVERE, null,
ex);
java.awt.EventQueue.invokeLater(new Runnable() {
new category().setVisible(true);
});