TYBBA(CA) PRACTICAL SLIPS SOLUTIONS

 JAVA SLIPS 1 TO 10

JAVA SLIPS 11 TO 20

JAVA SLIPS 21 TO 30

VB.NET PROGRAM (9)

VB.NET PROGRAM (11)


TYBBA(CA) SLIPS ZIP FILE


GANESH ZIP FILE

prathi slip


Slip No:15

A) Write a java program to display each alphabet after 2 seconds between ‘a’ to ‘z’.


public class Slip15A {


    public static void main(String args[]) {


        alpha a1 = new alpha();


        a1.start();


    } }


class alpha extends Thread {


    public void run() {


        try {


            for(int i=97; i<=122; i++){


                System.out.println((char)i);


                sleep(2000); } }


            

 catch (Exception e) {


        }


    }


}


B) Write a Java program to accept the details of Student (RNo, SName, Per, Gender,


Class) and store into the database. (Use appropriate Swing Components and


PreparedStatement Interface).


import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;


public class StudentDetails extends JFrame implements ActionListener {


    JLabel l1, l2, l3, l4, l5;

    JTextField t1, t2, t3, t4;

    JButton b1, b2;

    JComboBox<String> comboBox;

    Connection conn = null;

    PreparedStatement pst = null;


    public StudentDetails() {

        setTitle("Student Details");

        setSize(400, 300);

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setResizable(false);

        setLocationRelativeTo(null);


        l1 = new JLabel("Roll No:");

        l2 = new JLabel("Name:");

        l3 = new JLabel("Percentage:");

        l4 = new JLabel("Gender:");

        l5 = new JLabel("Class:");


        t1 = new JTextField();

        t2 = new JTextField();

        t3 = new JTextField();


        String[] genders = {"Male", "Female", "Other"};

        comboBox = new JComboBox<>(genders);


        t4 = new JTextField();


        b1 = new JButton("Save");

        b2 = new JButton("Cancel");


        JPanel p = new JPanel(new GridLayout(6, 2));

        p.add(l1);

        p.add(t1);

        p.add(l2);

        p.add(t2);

        p.add(l3);

        p.add(t3);

        p.add(l4);

        p.add(comboBox);

        p.add(l5);

        p.add(t4);

        p.add(b1);

        p.add(b2);


        add(p);


        b1.addActionListener(this);

        b2.addActionListener(this);


        setVisible(true);


        // Connect to the database

        try {

            Class.forName("com.mysql.cj.jdbc.Driver");

            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "password");

        } catch (Exception ex) {

            System.out.println("Error: " + ex.getMessage());

        }

    }


    public static void main(String[] args) {

        new StudentDetails();

    }


    public void actionPerformed(ActionEvent ae) {

        if (ae.getSource() == b1) { // Save button is clicked

            try {

                String query = "insert into students(RNo, SName, Per, Gender, Class) values (?, ?, ?, ?, ?)";

                pst = conn.prepareStatement(query);

                pst.setInt(1, Integer.parseInt(t1.getText()));

                pst.setString(2, t2.getText());

                pst.setDouble(3, Double.parseDouble(t3.getText()));

                pst.setString(4, (String) comboBox.getSelectedItem());

                pst.setInt(5, Integer.parseInt(t4.getText()));

                pst.executeUpdate();

                JOptionPane.showMessageDialog(null, "Data saved successfully!");

                t1.setText("");

                t2.setText("");

                t3.setText("");

                t4.setText("");

            } catch (SQLException ex) {

                System.out.println("Error: " + ex.getMessage());

            }

        } else if (ae.getSource() == b2) { // Cancel button is clicked

            System.exit(0);

        }

    }

}


.net

Slip 15 b
using System;

namespace Slip15b
{
    class Customer
    {
        public int customer_no;
        public string name;
        public string address;
        public int itemno;
        public int quantity;
        public double price;
        public double total;

        // Accept Customer Details
        public void AcceptDetails()
        {
            Console.Write("Enter Customer No: ");
            customer_no = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter Customer Name: ");
            name = Console.ReadLine();

            Console.Write("Enter Address: ");
            address = Console.ReadLine();

            Console.Write("Enter Item No: ");
            itemno = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter Quantity: ");
            quantity = Convert.ToInt32(Console.ReadLine());

            Console.Write("Enter Price per Item: ");
            price = Convert.ToDouble(Console.ReadLine());

            total = quantity * price; // Calculate Total Price
        }

        // Display Customer Details
        public void DisplayDetails()
        {
            Console.WriteLine("Customer No: " + customer_no);
            Console.WriteLine("Name: " + name);
            Console.WriteLine("Address: " + address);
            Console.WriteLine("Item No: " + itemno);
            Console.WriteLine("Quantity: " + quantity);
            Console.WriteLine("Price per Item: " + price);
            Console.WriteLine("Total Price: " + total);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Enter number of Customers: ");
            int n = Convert.ToInt32(Console.ReadLine());

            Customer[] cust = new Customer[n];
            double grandTotal = 0;

            // Accept details for each customer
            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("\nEnter details of Customer {0}:", i + 1);
                cust[i] = new Customer();
                cust[i].AcceptDetails();
                grandTotal += cust[i].total;
            }

            // Display details of each customer
            Console.WriteLine("\n======== Customer Details ========");

            for (int i = 0; i < n; i++)
            {
                Console.WriteLine("\nCustomer {0} Details:", i + 1);
                cust[i].DisplayDetails();
            }

            Console.WriteLine("\nGrand Total Price of all items: " + grandTotal);

            Console.ReadLine();
        }
    }
}

Post a Comment

Previous Post Next Post