0% found this document useful (0 votes)
42 views19 pages

Java Outputs 3,7,8,9,10

The document describes creating a GUI application in Java to calculate student grades. It accepts student details like ID, name, section and marks in different subjects. It calculates the total, percentage and grade and displays the results. The code provided creates the GUI using Swing components like JFrame, JPanel, JLabels, JTextFields and JButtons. It takes input from the text fields, performs calculations and displays output on button click.

Uploaded by

sujaykulkarni755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views19 pages

Java Outputs 3,7,8,9,10

The document describes creating a GUI application in Java to calculate student grades. It accepts student details like ID, name, section and marks in different subjects. It calculates the total, percentage and grade and displays the results. The code provided creates the GUI using Swing components like JFrame, JPanel, JLabels, JTextFields and JButtons. It takes input from the text fields, performs calculations and displays output on button click.

Uploaded by

sujaykulkarni755
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Aim: - Develop a GUI which accepts the information regarding the marks for all the

subjects of a student in the examination. Display the result for a student in a


separate window.
Ans)
The Code: -
package grade_calcu;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.FlowLayout;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Grade_Calcu
{
private JFrame frame;
private JTextField tf;
private JTextField tf_1;
private JTextField tf_2;
private JTextField tf_3;
private JTextField tf_4;
private JTextField tf_5;
private JTextField tf_6;
private JTextField tf_7;
private JTextField tf_8;
private JTextField tf_9;
public static void main(String[] args)
{
try
{
Grade_Calcu window = new Grade_Calcu();
window.frame.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
}
}
public Grade_Calcu()
{
initialize();
}
private void initialize()
{
frame = new JFrame();
frame.setBounds(100, 100, 537,341);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JPanel panel = new JPanel();
panel.setBounds(0, 0, 521,302);
frame.getContentPane().add(panel);
panel.setLayout(null);
JLabel lb = new JLabel("Modern College of Engg.");
lb.setFont(new Font("Times New Roman", Font.BOLD, 18));
lb.setBounds(200, 11, 231, 34);
panel.add(lb);
JLabel lb_1 = new JLabel("Student ID ");
lb_1.setBounds(10, 45, 85, 24);
panel.add(lb_1);
JLabel lb_2 = new JLabel("Student Name ");
lb_2.setBounds(10, 70, 92, 24);
panel.add(lb_2);
JLabel lb_3 = new JLabel("Section ");
lb_3.setBounds(10, 90, 96, 14);
panel.add(lb_3);
JLabel lb_4 = new JLabel("Physics ");
lb_4.setBounds(45, 132, 56, 24);
panel.add(lb_4);
JLabel lb_5 = new JLabel("Chemistry ");
lb_5.setBounds(140, 132, 65, 24);
panel.add(lb_5);
JLabel lb_6 = new JLabel("Maths");
lb_6.setBounds(240, 132, 46, 14);
panel.add(lb_6);
JLabel lb_7 = new JLabel("English");
lb_7.setBounds(343, 132, 46, 14);
panel.add(lb_7);
JLabel lb_8 = new JLabel("Total Marks");
lb_8.setBounds(415, 132, 81, 14);
panel.add(lb_8);
JLabel lb_9 = new JLabel("Percentage");
lb_9.setBounds(40, 193, 65, 24);
panel.add(lb_9);
JLabel lb_10 = new JLabel("Grade");
lb_10.setBounds(140, 193, 46, 14);
panel.add(lb_10);
tf = new JTextField();
tf.setBounds(101, 46, 143, 20);
panel.add(tf);
tf.setColumns(10);
tf_1 = new JTextField();
tf_1.setBounds(101, 71, 143, 20);
panel.add(tf_1);
tf_1.setColumns(10);
tf_2 = new JTextField();
tf_2.setBounds(101, 96, 143, 20);
panel.add(tf_2);
tf_2.setColumns(10);
tf_3 = new JTextField();
tf_3.setBounds(40, 157, 65, 20);
panel.add(tf_3);
tf_3.setColumns(10);
tf_4 = new JTextField();
tf_4.setBounds(130, 157, 65, 20);
panel.add(tf_4);
tf_4.setColumns(10);
tf_5 = new JTextField();
tf_5.setBounds(230, 157, 65, 20);
panel.add(tf_5);
tf_5.setColumns(10);
tf_6 = new JTextField();
tf_6.setBounds(324, 157, 65, 20);
panel.add(tf_6);
tf_6.setColumns(10);
tf_7 = new JTextField();
tf_7.setBounds(415, 157, 65, 20);
panel.add(tf_7);
tf_7.setColumns(10);
tf_8 = new JTextField();
tf_8.setBounds(40, 218, 65, 20);
panel.add(tf_8);
tf_8.setColumns(10);
tf_9 = new JTextField();
tf_9.setBounds(130, 218, 65, 20);
panel.add(tf_9);
tf_9.setColumns(10);
JButton button= new JButton("Calculate");
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int s_id = Integer.parseInt(tf.getText());
String s_name = tf_1.getText();
String s_sec = tf_2.getText();
double Physics= Double.parseDouble(tf_3.getText());
double Chemistry= Double.parseDouble(tf_4.getText());
double Maths= Double.parseDouble(tf_5.getText());
double English= Double.parseDouble(tf_6.getText());
double Total = Physics + Chemistry + Maths + English;
tf_7.setText(""+Total);
double Percentage = (Total/400)*100;
tf_8.setText(""+Percentage);
String Grade = null;
if(Percentage > 90) {
Grade = "A";
}
else if((Percentage > 85) && (Percentage <90)) {
Grade = "B";
}
else if((Percentage > 80) && (Percentage <85)) {
Grade = "C";
}
else if((Percentage > 70) && (Percentage <80)) {
Grade = "D";
}
else if((Percentage > 60) && (Percentage <70)) {
Grade = "E";
}
else if((Percentage > 50) && (Percentage <60)) {
Grade = "Poor";
}
else if((Percentage > 40) && (Percentage <50)) {
Grade = "Very Poor";
}
else if(Percentage <40) {
Grade = "Fail";
}
tf_9.setText(""+Grade);
}
});
button.setBounds(71,256,89,23);
panel.add(button);
JButton button_2 = new JButton("Clear");
button_2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
tf.setText("");
tf_1.setText("");
tf_2.setText("");
tf_3.setText("");
tf_4.setText("");
tf_5.setText("");
tf_6.setText("");
tf_7.setText("");
tf_8.setText("");
tf_9.setText("");
}
});
button_2.setBounds(213, 256, 89, 23);
panel.add(button_2);
JButton button_3 = new JButton("Exit");
button_3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
System.exit(0);
}
});
button_3.setBounds(346, 256, 89, 23);
panel.add(button_3);
}
}
The Output: -

Aim: - Create a simple calculator application using servlet.


Ans)

The Code: -

CalculatorServlet.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CalculatorServlet extends HttpServlet {
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
double num1, num2, result;
String opt;
num1 = Double.parseDouble(request.getParameter("txtNum1"));
num2 = Double.parseDouble(request.getParameter("txtNum2"));
opt = request.getParameter("opt");
if (opt.equals("+"))
{
result = num1+num2;
}
else if (opt.equals("-"))
{
result = num1-num2;
}
else if (opt.equals("*"))
{
result = num1*num2;
}
else
{
result = num1/num2;
}
out.println("Result is = "+ result);
}
}

Index.html
<html>
<head>
<title>Calculator</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action ="CalculatorServlet">
Enter First Number:<input type ="text" name="txtNum1"><br><br>
Enter Second Number:<input type ="text" name="txtNum2"><br><br>
Select Operation:<br>
<input type ="radio" name="opt" value ="+">Addition<br>
<input type ="radio" name="opt" value ="-">Subtraction<br>
<input type ="radio" name="opt" value ="*">Multiplication<br>
<input type ="radio" name="opt" value ="/">Division<br>
<input type ="submit" value ="Calculate">
<input type ="reset" value ="Clear">

</form>
</body>
</html>

The Output: -
Addition:

Subtraction:

Multiplication:
Division:
Aim: - Write a simple JSP page to display a simple message. (It may be a simple
html page).

Ans)

The Code:
Index.jsp
<%@page import = "java.util.Date"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Today's Date</title>
</head>
<% Date date = new Date(); %>
<body>
<h1>Today's Date</h1>
<p> Today's date is <%=date%></p>
</body>
</html>

The Output: -
Aim: - Login application using mysql database and JDBC driver.

Ans)

The Code:
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = {"/LoginServlet"})
public class LoginServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con =
DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb?useSSL=false",
"root","root");
Statement stm = con.createStatement();
ResultSet rs= stm.executeQuery("select * from login where username =
'"+username+"' and password = '"+password+"'");
if (rs.next())
{
response.sendRedirect("Home.html");
}
else
{
System.out.println("Wrong Username and Password");
}
con.close();
}catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

The Output:
Aim: - Login and Logout using HTTP session in servlet.

Ans)
The Code:

LoginServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String email = request.getParameter("email");
String pass = request.getParameter("pass");
if(email.equals("[email protected]") && pass.equals("java")) {
HttpSession session = request.getSession();
session.setAttribute("email", email);
response.sendRedirect("home");
}
else {
response.sendRedirect("index.html");
}
}
}
LogoutServlet.java

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/logout")
public class LogoutServlet extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
session.invalidate();
response.sendRedirect("index.html");
}
}

HomeServlet.java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@WebServlet("/home")
public class HomeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter pr = response.getWriter();
HttpSession session = request.getSession(false);
if(session != null) {
String email = (String) session.getAttribute("email");
pr.print("Welcome " + email);
pr.print("<br/><a href=\"logout\">Logout</a>");
}
else {
response.sendRedirect("index.html");
}
pr.close();
}
}

Index.html

<html>
<head>
<title>Login and Logout With Session Example</title>
</head>
<body>
<form action="login" method="post">
Email: <input type="email" name="email" required/><br/><br/>
Password: <input type="password" name="pass" required/><br/><br/>
<input type="submit" value="Login"/>
</form>
</body>
</html>
The Output:

You might also like