Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Ajava practical with answer sffgreger, Exercises of Software Engineering

a JAVA Practicals with answer is good thing gtu is dumb

Typology: Exercises

2019/2020

Uploaded on 06/01/2020

chirag-jani
chirag-jani šŸ‡®šŸ‡³

5

(1)

2 documents

1 / 82

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
ADVANCED JAVA - 2160707 170280107042
1
PRACTICAL: 1
To develop application using swing component.
Write java programs for following:
1. Create 4 Jbuttons with label 1,2,3,4 and display them on
Jframe window.When you push a button,the number on
it is display on Jlabel.
2. An objective type question has three choice.one of the
choice is correct answer.There are five questions Each
correct ans got 2 marks.when the user pushes finish
jbuttonHis score should be displayed.
Part 1:
package practical_1;
import java.awt.Font; import
java.awt.event.MouseEvent; import
java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class Gui_1 {
public static JButton getButton(String text,JPanel jp){
JButton jb=new JButton(text);
jb.setSize(60,40);
jp.add(jb);
jb.addMouseListener(new MouseListener() {
@Override
public void mouseClicked(MouseEvent e) {
JLabel jl=(JLabel)jp.getComponent(0);
jl.setText("Entered Number : " + text);
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52

Partial preview of the text

Download Ajava practical with answer sffgreger and more Exercises Software Engineering in PDF only on Docsity!

PRACTICAL: 1

To develop application using swing component.

Write java programs for following:

1. Create 4 Jbuttons with label 1,2,3,4 and display them on

Jframe window.When you push a button,the number on

it is display on Jlabel.

2. An objective type question has three choice.one of the

choice is correct answer.There are five questions Each

correct ans got 2 marks.when the user pushes finish

jbuttonHis score should be displayed.

Part 1:

package practical_1;

import java.awt.Font; import java.awt.event.MouseEvent; import java.awt.event.MouseListener;

import javax.swing.JButton;

import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; public class Gui_1 {

public static JButton getButton(String text,JPanel jp){ JButton jb=new JButton(text); jb.setSize(60,40); jp.add(jb); jb.addMouseListener(new MouseListener() { @Override public void mouseClicked(MouseEvent e) { JLabel jl=(JLabel)jp.getComponent(0); jl.setText("Entered Number : " + text); } @Override public void mousePressed(MouseEvent e) { // TODO Auto-generated method stub } @Override public void mouseReleased(MouseEvent e) {

Output:

Part 2:

package practical_1;

import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.MouseEvent; import java.awt.event.MouseListener;

import javax.swing.BoxLayout; import javax.swing.ButtonGroup; import

javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.JRadioButton; public class Gui_2 { public static void addQuestion(JPanel jp, ButtonGroup bg, String[] option) {

jp.setLayout(new GridLayout(4, 1)); JLabel jl = new JLabel(option[0]); jp.add(jl);

JRadioButton init = new JRadioButton(); init.setSelected(true); init.setActionCommand("null"); init.setVisible(false); bg.add(init);

for (int i = 2; i < option.length; i++) { JRadioButton jr = new JRadioButton(option[i]);

jr.setSelected(false); jr.setActionCommand(String.valueOf(i - 1)); jr.setVisible(true); jp.add(jr); bg.add(jr); } }

public static JPanel getPanel() { JPanel jp = new JPanel(); jp.setMaximumSize(new Dimension(760, 200)); jp.setMinimumSize(new Dimension(760, 100)); jp.setVisible(true); return jp; }

private static void createUI() {

JFrame jf = new JFrame("Hardik"); jf.setSize(800, 800); jf.setVisible(true); jf.setLayout(new BoxLayout(jf.getContentPane(), BoxLayout.Y_AXIS));

JPanel jp = getPanel(); JPanel jp1 = getPanel(); JPanel jp2 = getPanel(); JPanel jp3 = getPanel(); JPanel jp4 = getPanel();

jf.add(jp); jf.add(jp1); jf.add(jp2); jf.add(jp3); jf.add(jp4);

ButtonGroup bg = new ButtonGroup(); ButtonGroup bg1 = new ButtonGroup(); ButtonGroup bg2 = new ButtonGroup(); ButtonGroup bg3 = new ButtonGroup(); ButtonGroup bg4 = new ButtonGroup();

String al[] = { "Are java and javascript same?", "2", "yes", "no", "don't know" }; String al1[] = { "MVC stands for?", "1", "Model View Controller", "Model vs

Computation", "don't know" }; String al2[] = { "who will kill thanos?", "3", "captain marvel", "thor", "all

together" };

String al3[] = { "Relation between ML,AI and DL", "3", "ML>AI>DL", "DL>AI>ML", "AI>ML>DL" };

String al4[] = { "what is spring?", "3", "season", "framework", "both" };

public static void main(String args[]) {

javax.swing.SwingUtilities.invokeLater(new Runnable() {

public void run() { createUI();

Output:

PRACTICAL: 2

To develope UDP client server application.

Write a Java program to using UDP to transfer text file from host

to another.

/CLIENT CLASS/

import java.net.; import java.io.; public class client { public static void main(String args[])throws Exception { byte b[]=new byte[1024]; FileInputStream f=new FileInputStream("D:/raj.txt"); DatagramSocket dsoc=new DatagramSocket(2000); int i=0; while(f.available()!=0) { b[i]=(byte)f.read(); i++; } f.close(); dsoc.send(new DatagramPacket(b,i,InetAddress.getLocalHost(),1000)); }

} /SERVER CLASS/ import java.net.; import java.io.; public class server { public static void main(String args[])throws IOException { byte b[]=new byte[3072]; DatagramSocket dsoc=new DatagramSocket(1000); FileOutputStream f=new FileOutputStream("D:/nandha.txt"); while(true) { DatagramPacket dp=new DatagramPacket(b,b.length); dsoc.receive(dp); System.out.println(new String(dp.getData(),0,dp.getLength()));

} } }

OUTPUT:

/Client class/

raj.txt kanagaraj

D:\java\EXNO-7>java client

/server class/

D:\java\EXNO-7>java server

Kanagaraj

PRACTICAL: 3

To develope TCP client server application.

Write a Java program where client sends a string as a message

and server counts the number of characters in string.server

sends back this value to client.server should be able to serve

multiple clients simultaneously.

ServerSideSocket.java

import java.net.; import java.io.;

class ServerSideSocket implements Runnable

{ private Socket s=null; public ServerSideSocket(Socket s) { this.s=s; }

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

ServerSocket ss=new ServerSocket(6666); while(true) { Socket s=ss.accept(); System.out.println("Request is coming from "+s.getPort()); new Thread(new ServerSideSocket(s)).start(); } } @Override public void run(){

DataOutputStream dout=null; DataInputStream din=null; try{

dout=new DataOutputStream(s.getOutputStream()); din=new DataInputStream(s.getInputStream()); BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

String str1=br.readLine(); dout.writeUTF(str1);

dout.flush();

10

int ans=din.readInt(); System.out.println("server says: "+ans);

dout.close(); din.close(); br.close(); s.close();

Output:

ADVANCED JAVA - 2160707 170280107101

try{ Connection con=DbConnection.getConnection(); Statement st=con.createStatement(); st.executeUpdate("CREATE TABLE book(id int,title varchar(25), author

varchar(40),price float)");

} catch(SQLException e){ e.printStackTrace(); } } }

Part2: package practical_2;

import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.util.Scanner;

public class App_2 {

private static int id=100;

public static void main(String args[])

{ try{ Connection con=DbConnection.getConnection(); Statement st=con.createStatement(); Scanner in=new Scanner(System.in); int i=1; while(i<=20){ System.out.println("record no : " +i); System.out.println("enter title without space:"); String title=in.next(); System.out.println("enter author name without space:"); String author=in.next(); System.out.println("enter price :");

float price=in.nextFloat();

st.executeUpdate("insert into book values(" + id + ", '"+title+"','"+author+"',"+price+")"); id++; i++;

catch(SQLException e) {

Part3: package practical_2;

import java.sql.Connection; import

java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Scanner; public class App_3 {

public static void main(String[] args){ Connection con; Scanner in=new Scanner(System.in); try { con = DbConnection.getConnection(); PreparedStatement ps=con.prepareStatement("select * from book

where title=? or author=?");

System.out.println("enter keyword: "); String keyword=in.next(); ps.setString(1, keyword); ps.setString(2, keyword); ResultSet rs=ps.executeQuery(); while(rs.next()){ System.out.println("id :"+rs.getInt(1)); System.out.println("title :"+rs.getString(2)); System.out.println("author : "+rs.getString(3)); System.out.println("price :"+rs.getFloat(4)); } } catch (SQLException e) { e.printStackTrace(); }

Output:

Part4: package practical_2;

} catch (SQLException e) { e.printStackTrace(); }

Output:

Part5: package practical_2;

import java.sql.Connection; import

java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; public class App_5 {

public static void main(String[] args) { Connection con;

try { con = DbConnection.getConnection(); Statement st = con.createStatement(); ResultSet rs = st.executeQuery("select * from book"); ResultSetMetaData rsmd = rs.getMetaData(); int c = rsmd.getColumnCount(); while (c > 0) { System.out.print("name : "+rsmd.getColumnName(c)+" "); System.out.print("type : "+rsmd.getColumnTypeName(c)+" "); System.out.println("size : "+rsmd.getColumnDisplaySize(c)); c--; } } catch (SQLException e) { e.printStackTrace(); } }

}

Output: