Slip25

 Q1) Create a class Student(rollno, name ,class, per), to read student information from the console and display them (Using BufferedReader class) [10 marks]

import java.io.*;

class Student_25_1 

{

    int rollno;

    String name,studentClass;

    double percentage;

    public Student_25_1(int rollno, String name, String studentClass, double percentage) 

    {

        this.rollno = rollno;

        this.name = name;

        this.studentClass = studentClass;

        this.percentage = percentage;

    }

    public String toString() 

    {

        System.out.println("Student Details:");

        System.out.println("Roll No: " + rollno);

        System.out.println("Name: " + name);

        System.out.println("Class: " + studentClass);

        System.out.println("Percentage: " + percentage + "%");

return "";

    }


    public static void main(String[] args) throws IOException 

    {

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Enter Roll No: ");

        int rollno = Integer.parseInt(reader.readLine());

        System.out.print("Enter Name: ");

        String name = reader.readLine();

        System.out.print("Enter Class: ");

        String cls= reader.readLine();

        System.out.print("Enter Percentage: ");

        double p= Double.parseDouble(reader.readLine());

        Student_25_1 s1= new Student_25_1(rollno, name, cls, p);

        System.out.print(s1);

    }

}


Q2) Create the following GUI screen using appropriate layout manager. Accept the name, class, hobbies from the user and display the selected options in a textbox. [20 marks]

import java.awt.*;

import java.awt.event.*;   

import javax.swing.*;

   

public class Slip25_2 extends JFrame implements ActionListener

{

        JTextField t1,t2;

        JCheckBox c[]=new JCheckBox[3];

      JRadioButton r[]=new JRadioButton[3];

public static void main(String arg[])

{

    Slip25_2 c =new Slip25_2();

    c.setSize(250,200);

    c.setVisible(true);

    c.setLocation(500,200);

    c.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

        public Slip25_2()

        {

         setLayout(new FlowLayout());

JPanel p1=new JPanel();

p1.setLayout(new GridLayout(4,1));

ButtonGroup b1 = new ButtonGroup();

ButtonGroup b2 = new ButtonGroup();

         //add radio buttons

JPanel p2=new JPanel();

p2.setLayout(new GridLayout(4,1));

        JLabel l1=new JLabel("Your Name : ");

JLabel l2=new JLabel(" Your Class ");

JLabel l3=new JLabel(" Your Hobbies ");

        t1=new JTextField(10);

t2=new JTextField(22);

r[0]=new JRadioButton("FY");r[0].setSelected(true);

         r[1]=new JRadioButton("SY");

r[2]=new JRadioButton("TY");

for(int i=0;i<3;i++)

        b1.add(r[i]);

        c[0]=new JCheckBox("Music");c[0].setSelected(true);

c[1]=new JCheckBox("Dance");

c[2]=new JCheckBox("Sport");

  for(int i=0;i<3;i++)

  b2.add(c[i]);

          add(l1);add(t1);

p1.add(l2);

for(int i=0;i<3;i++)

  p1.add(r[i]);

p2.add(l3);

for(int i=0;i<3;i++)

  p2.add(c[i]);

add(p1);add(p2);

add(t2);

for(int i=0;i<3;i++)

{

        r[i].addActionListener(this);  

        c[i].addActionListener(this);

        }

       } 

 

  public void actionPerformed(ActionEvent ae)

  {

String na=t1.getText();

String hobby="",cl="";

for(int i=0;i<3;i++)

if(c[i].isSelected())

hobby=c[i].getText()+" ";

for(int i=0;i<3;i++)

if(r[i].isSelected())

cl=r[i].getText()+" ";

t2.setText("Name : "+na+" Class : "+cl+" Hobbies : "+hobby);  

  }

 }




Comments

Popular posts from this blog

Slip1

Slip3

Slip2