Professional Documents
Culture Documents
A914145022016
Java Assignment
Q1) Design a form in Java to implement calculator to perform the basic arithmetic operation
(+, -, *, /). Use appropriate GUI components, layout and event handling mechanism.
CODE:
// Java program to create a simple calculator
// with basic +, -, /, * using java swing elements
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
// create a textfield
static JTextField l;
// default constructor
calculator() {
s0 = s1 = s2 = "";
}
// main function
public static void main(String args[]) {
// create a frame
f = new JFrame("calculator");
try {
// set look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception e) {
System.err.println(e.getMessage());
}
// create a textfield
l = new JTextField(16);
// equals button
beq1 = new JButton("=");
// create operator buttons
ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");
// create . button
be = new JButton(".");
// create a panel
JPanel p = new JPanel();
f.setSize(200, 220);
f.show();
}
double te;
// convert it to string
s0 = Double.toString(te);
s1 = s2 = "";
} else {
// if there was no operand
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;
// convert it to string
s0 = Double.toString(te);
Q2) Write a program in Java to accept Employee_Code and Employee_Name from the user
and to store the data in a file.
CODE:
import java.io.*;
import java.util.Scanner;
public class question2 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int Employee_code;
String Employee_name;
System.out.println();
try {
File file = new File("vishalfirst.txt");
file.createNewFile();
System.out.println("File Created");
} catch (IOException e) {
e.printStackTrace();
}
} catch (Exception e) {
System.out.println(e);
}
}
}
Q3) Write a program in Java to read the content of a file and to show it on the screen.
CODE:
import java.io.*;
import java.util.Scanner;
}
}
Q4) Write a program in Java to add the content of two files in a third file.
CODE:
import java.io.*;
line = br.readLine();
pw.flush();
// closing resources
br.close();
pw.close();
CODE:
import java.util.Scanner;
import java.sql.*;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost/employee", "root", "");
Statement stmt = con.createStatement();
int x = stmt.executeUpdate(s1);
int y = stmt.executeUpdate(s2);
int z = stmt.executeUpdate(s3);
ResultSet rs = stmt.executeQuery(s4);
while (rs.next()) {
System.out.println(rs.getString(1) + " " + rs.getString(2));
}
stmt.close();
con.close();
}
}
Q6) Write a program in Java to accept the URL of a web page from the user and show the
content of the page on the screen.
CODE:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;