










































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
a JAVA Practicals with answer is good thing gtu is dumb
Typology: Exercises
1 / 82
This page cannot be seen from the preview
Don't miss anything!
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:
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()));
} } }
/Client class/
raj.txt kanagaraj
D:\java\EXNO-7>java client
/server class/
D:\java\EXNO-7>java server
Kanagaraj
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:
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: