0% found this document useful (0 votes)
6 views35 pages

Mayankajfile

The document contains various Java programming exercises from the Gateway Institute of Engineering & Technology (GIET), including customer detail storage, arithmetic operations, file reading, method overloading, method overriding, and inheritance examples. It also covers GUI applications using applets and swing, JDBC for database operations, and servlet creation for web applications. Each program is accompanied by source code and expected output.
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)
6 views35 pages

Mayankajfile

The document contains various Java programming exercises from the Gateway Institute of Engineering & Technology (GIET), including customer detail storage, arithmetic operations, file reading, method overloading, method overriding, and inheritance examples. It also covers GUI applications using applets and swing, JDBC for database operations, and servlet creation for web applications. Each program is accompanied by source code and expected output.
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/ 35

Gateway Institute of Engineering & Technology (GIET)

Program -1
Write a program to store customer detail as Acno, Name, Balance in a class. Compute Interest as 8% of
Balance >= 75000, Interest as 6% if Balance >= 40000 else 3%. Display detail for the customer.

Source Code:
package javaapplication2; import java.util.*;
class Customer
{
int ac,bal String nm,double interest;
Customer( )
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Account no ");
ac = in.nextInt( );
in.nextLine( ); // To clear the buffer
System.out.print("Enter name");
nm = in.nextLine( );
System.out.print("Enter Balance ");
bal = in.nextInt( );
}
void compute( )
{ if(bal >= 75000)
interest = bal * .08;
else if(bal >40000)
interest = bal * .06;
else

interest = bal * .03; }


void putdata( )
{
System.out.println("Acno = " + ac);
System.out.println("Name = " + nm);

Mayank pg. 1
21010001010
Gateway Institute of Engineering & Technology (GIET)

System.out.println("Balance = " + bal);


System.out.println("Interest = " + interest);
}}
public class JavaApplication2 {
public static void main(String[] args)
{ Customer cust = new Customer( );
cust.compute( );
cust.putdata( );
}
}

Mayank pg. 2
21010001010
Gateway Institute of Engineering & Technology (GIET)

Output

Mayank pg. 3
21010001010
Gateway Institute of Engineering & Technology (GIET)

Program - 2
Write a program to accept 2 numbers. Divide 1st no. by 2nd & display the result. Display user defined
error message if 2nd no. is zero.
Source Code:

package javaapplication3;

import java.util.*;

public class JavaApplication3{

public static void main(String[] args) {

int a,b,c;

Scanner in = new Scanner (System.in);

System.out.print("Enter 1st number ");

a = in.nextInt( );

System.out.print("Enter 2nd number "); b

= in.nextInt( );

try { c = a / b;

System.out.println(" Result = " + c);

catch(ArithmeticException e)

System.out.println("Divide by zero error");

Neha pg. 4
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 5
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program -3
Write a program to read data from disk file.
Source Code:

package javaapplication13;
import java.io.*;

public class JavaApplication13 { public

static void main(String[] args) throws

IOException{

int n;

FileInputStream fis = new FileInputStream("C:\\Users\\ASUS\\OneDrive\\Documents\\test.txt");

while( (n = fis.read( )) != -1)

System.out.print((char) n);

fis.close( );

}}

Neha pg. 6
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 7
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program -4
Write a program to illustrate method overloading.
Source Code:

package javaapplication4; class Sample {

void check(int n)

if(n%2 == 0)

System.out.println("No. is even");

else

System.out.println("No. is odd");

void check(int a,int b)

if(a%b== 0)

System.out.println("1st no. is div. by 2nd ");

else

System.out.println("Not divisible");

}}

public class JavaApplication4 {

public static void main(String[] args)

{ Sample obj = new Sample( );

obj.check(8, 2);

obj.check(7);

Neha pg. 8
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 9
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program -5
Write a program to illustrate method overriding.
Source Code:

package javaapplication5; class A { void display( )

System.out.println(" This is class A ");

}}

class B extends A

{ void display( )

{ super.display( );

System.out.println(" This is class B ");

}}

public class JavaApplication5

{ public static void main(String[] args) {

B obj b = new B( ); objb.display( );

Neha pg. 10
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 11
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program -6
Write a program to illustrate various forms of Inheritance.
Source Code:

SINGLE INHERITANCE:

package javaapplication81;

class A {

int a; void getdata(int x)

a=x; }

void putdata()

System.out.println("a = " +a);

class B extends A

{ int b,c;

void accept(int y)

{ b=y;

void display()

{ c=a+b;

System.out.println("Sum = "+c);

}}

public class JavaApplication81

{ public static void main(String[] args) { B

obj = new B();

obj.getdata(7);

obj.accept(5);

Neha pg. 12
21010001013
Gateway Institute of Engineering & Technology (GIET)

obj.putdata();

obj.display(); } }

Output

Neha pg. 13
21010001013
Gateway Institute of Engineering & Technology (GIET)

MULTILTVEL INHERITANCE:

package javaapplication81;

class A { int a;

void getdata(int x)

{ a=x; }

void putdata()

System.out.println("a = " +a);

}}

class B extends A

{ int b;

void accept(int y)

{ b=y; }

void display()

System.out.println("b = "+b);

}}

class C extends B

int c,d;

void contain(int z) {

c=z;

void show()

d=a+b+c;

System.out.println(" Sum = "+d);

}}

Neha pg. 14
21010001013
Gateway Institute of Engineering & Technology (GIET)

public class JavaApplication81 { public

static void main(String[] args) { C obj =

new C();

obj.getdata(7);

obj.accept(5);

obj.contain(8);

obj.putdata();

obj.display();

obj.show();

Output

Neha pg. 15
21010001013
Gateway Institute of Engineering & Technology (GIET)

MULTIPLE INHERITANCE:
package javaapplication85; interface

Product1

{ int pc = 101;

String nm = "abc";

void putdata();

class Product2

int qty;

double up;

void getdata(int q,double u)

{ qty = q;

up = u;

void display()

System.out.println("Quantity = "+qty);

System.out.println("Unit Price = "+up);

}}

class Product extends Product2 implements Product1

{ double amt;

void compute()

Neha pg. 16
21010001013
Gateway Institute of Engineering & Technology (GIET)

amt = qty * up;

void show()

System.out.println("Amount = "+amt);

public void putdata()

System.out.println("Product code = "+pc);

System.out.println("Product name = "+nm);

public class JavaApplication85 { public

static void main(String[] args) {

Product P = new Product();

P.getdata(10,1000);

P.compute();

P.putdata();

P.display(); P.show();

Neha pg. 17
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 18
21010001013
Gateway Institute of Engineering & Technology (GIET)

HIERARICHAL INHERITANCE:
package javaapplication85; abstract

class Shape

{ double a,b; void getdata(double

x, double y)

{ a = x; b = y; }

abstract void area();

class Rectangle extends Shape

double ar1;

void area() {

ar1 = a*b;

System.out.println("Area of reactangle = " +ar1);

}}

class Triangle extends Shape

{ double ar2;

void area()

{ ar2 = 0.5*a*b;

System.out.println("Area of triangle = " +ar2);

}}

public class JavaApplication85 { public

static void main(String[] args) { Rectangle

rect = new Rectangle(); Triangle tri = new

Triangle(); rect.getdata(7.5,6.1);

rect.area();

tri.getdata(5.2, 6.3); tri.area(); }

Neha pg. 19
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 20
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program -7
Write a program to display message Hello World on the Applet window.
Source Code:

import java.applet.Applet;

import java.awt.*;

public class NewApplet extends Applet {

public void init() {

public void paint(Graphics g)

g.drawString("Hello worls",10,100);

Neha pg. 21
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 22
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program -8
Write a program to demonstrate layout manager for positioning various swing controls.
Source Code:

import javax.swing.JOptionPane;

public class NewJFrame extends javax.swing.JFrame

{ public NewJFrame()

{ initComponents();

private void initComponents()

jLabel1 = new javax.swing.JLabel();

jButton1 = new javax.swing.JButton();

jLabel2 = new javax.swing.JLabel();

tf1 = new javax.swing.JTextField();

pf1 = new javax.swing.JTextField();

setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

jLabel1.setText("username"); jButton1.setText("Submit");

jButton1.addActionListener(new java.awt.event.ActionListener() ;

public void actionPerformed(java.awt.event.ActionEvent evt)

{ jButton1ActionPerformed(evt);

});

jLabel2.setText("Password");

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());

getContentPane().setLayout(layout); layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup() .addGroup(layout.createParallelGroup(javax.swing.GroupLay
out.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(97, 97, 97)

Neha pg. 23
21010001013
Gateway Institute of Engineering & Technology (GIET)

.addComponent(jButton1))

.addGroup(layout.createSequentialGroup()

.addGap(31, 31, 31)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel2)

.addGap(41, 41, 41)

.addComponent(pf1))

.addGroup(layout.createSequentialGroup()

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 65,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)

.addComponent(tf1, javax.swing.GroupLayout.PREFERRED_SIZE, 86,


javax.swing.GroupLayout.PREFERRED_SIZE)))))

.addContainerGap(208, Short.MAX_VALUE))

);

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)

.addGroup(layout.createSequentialGroup()

.addGap(19, 19, 19)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 28,


javax.swing.GroupLayout.PREFERRED_SIZE)

.addComponent(tf1, javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,

javax.swing.GroupLayout.PREFERRED_SIZE)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlace

ment.RELATED)

Neha pg. 24
21010001013
Gateway Institute of Engineering & Technology (GIET)

.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)

.addComponent(jLabel2)

.addComponent(pf1, javax.swing.GroupLayout.PREFERRED_SIZE,

javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)) .addGap(42, 42,

42)

.addComponent(jButton1)

.addContainerGap(162, Short.MAX_VALUE))

);

pack();

}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

String nm,pass; nm = tf1.getText(); pass = pf1.getText();

if(nm.equals("System")&& pass.equals("Manager"))

JOptionPane.showMessageDialog(null, "Authorized user"); else

JOptionPane.showMessageDialog(null, "unauthorized user");

public static void main(String args[])

{ java.awt.EventQueue.invokeLater(new Runnable() {

public void run() {

new NewJFrame().setVisible(true);

}); }

private javax.swing.JButton jButton1; private

javax.swing.JLabel jLabel1; private

javax.swing.JLabel jLabel2; private

Neha pg. 25
21010001013
Gateway Institute of Engineering & Technology (GIET)

javax.swing.JTextField pf1; private

javax.swing.JTextField tf1; }

Output

Neha pg. 26
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program-9

Write a program to display detail for all the Employees from the Emp table (Empno,

Ename, Job, Sal)in a Java program using JDBC concept..

Source Code:

package javaapplication1;

import java.sql.*;

public class JavaApplication1 {

public static void main(String[] args)

throws SQLException, ClassNotFoundException

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con=DriverManager.getConnection("jdbc:odbc:mydsn","","");

Statement stmt = con.createStatement( );

ResultSet rs = stmt.executeQuery("Select * from Emp");

System.out.println("Empno \tEname \tJob \tSal");

while(rs.next( ))

int en = rs.getInt("Empno");

String nm = rs.getString("Ename");

String j = rs.getString("Job");

int s = rs.getInt("Sal");

System.out.println(en + "\t" + nm + "\t" + j + "\t" + s);

con.close();

}}

Neha pg. 27
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 28
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program - 10
Write a program to increase salary by 1000 for all the Employees from the Emp table (Empno, Ename,
Job, Sal) using JDBC concept.
Source Code:

package javaapplication16;

import java.sql.*;

import java.util.*;

public class JavaApplication16 {

public static void main(String[] args)

throws ClassNotFoundException, SQLException{

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

Connection con = DriverManager.getConnection("jdbc:odbc:mydsn","","");

PreparedStatement ps = con.prepareStatement("Update emp Set Sal=Sal+1000");

ps.executeUpdate();

System.out.println("Record Updated");

con.close();

Output

Neha pg. 29
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program - 11
Write a program to create simple servlet for displaying hello message Source Code:

Index.html

<html>

<head>

<title>TODO supply a title</title>

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

</head>

<body>

<center>

<form method = "get" action ="NewServlet">

<input type ="Submit" value= "Click me!">

</form>

</center>

</body>

</html>

NewServlet.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;

Neha pg. 30
21010001013
Gateway Institute of Engineering & Technology (GIET)

public class NewServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8");

try (PrintWriter out = response.getWriter()) {

out.println("<center><b>Hello World</b></center>");

}}

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { processRequest(request, response);

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException { processRequest(request, response);

public String getServletInfo() { return

"Short description";

Neha pg. 31
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 32
21010001013
Gateway Institute of Engineering & Technology (GIET)

Program -12
Write a program to create JSP file displaying current system date and time.
Source Code:

Index.html

<html>

<head>

<title>Working with JSP</title>

</head>

<body>

<center><form name ="form" method ="get" action ="newjsp.jsp">

<input type ="Submit" value ="Click me">

</form>

</center>

</body>

</html>

newjsp.jsp

<%@page import ="java.util.*"%>

<%! Date dt = new Date();%>

<!DOCTYPE html>

<html>

<head>

<title>JSP Page</title>

</head>

<body>

<center>Current Syntax Date & Timex

<%=dt%>

Neha pg. 33
21010001013
Gateway Institute of Engineering & Technology (GIET)

</center>

</body>

</html>

Neha pg. 34
21010001013
Gateway Institute of Engineering & Technology (GIET)

Output

Neha pg. 35
21010001013

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy