slip.4

1.
import java.util.Scanner;
public class Transpose
{
 public static void main(String args[])
 {
int i, j;
System.out.println("Enter total rows and columns: ");
Scanner s = new Scanner(System.in);
int row = s.nextInt();
int column = s.nextInt();
int array[][] = new int[row][column];
System.out.println("Enter matrix:");
for(i = 0; i < row; i++)
{
 for(j = 0; j < column; j++)
 {
 array[i][j] = s.nextInt();
 System.out.print(" ");
 }
}
System.out.println("The above matrix before Transpose is ");
for(i = 0; i < row; i++)
{
 for(j = 0; j < column; j++)
 {
 System.out.print(array[i][j]+" ");
 }
 System.out.println(" ");
 }
System.out.println("The above matrix after Transpose is ");
for(i = 0; i < column; i++)
{
 for(j = 0; j < row; j++)
 {
 System.out.print(array[j][i]+" ");
 }
 System.out.println(" ");
 }
 }
}

2.
import java.awt.*;
import java.awt.event.*;

class InvalidLoginException extends Exception {
 public InvalidLoginException(String msg) {
 super(msg);
 }
}
public class LoginScreen extends Frame implements ActionListener {
 Label l1, l2, msg;
 TextField tfUser, tfPass;
 Button btnLogin, btnClear;
 int attempts = 0;
 public LoginScreen() {
 setLayout(new FlowLayout());
 l1 = new Label("User Name: ");
 l2 = new Label("Password: ");
msg = new Label("");
 tfUser = new TextField(15);
 tfPass = new TextField(15);
 tfPass.setEchoChar('*'); 
 btnLogin = new Button("Login");
 btnClear = new Button("Clear");
 add(l1); add(tfUser);
 add(l2); add(tfPass);
 add(btnLogin); add(btnClear);
 add(msg);
 btnLogin.addActionListener(this);
 btnClear.addActionListener(this);
 setTitle("Login Screen");
 setSize(300, 200);
 setVisible(true);
 
 addWindowListener(new WindowAdapter() {
 public void windowClosing(WindowEvent we) {
 System.exit(0);
 }
 });
 }
 @Override
 public void actionPerformed(ActionEvent ae) {
 if (ae.getSource() == btnClear) {
 tfUser.setText("");
 tfPass.setText("");
 msg.setText("");
 return;
 }
 if (ae.getSource() == btnLogin) {
 try {
 String user = tfUser.getText();
 String pass = tfPass.getText();
 if (!user.equals(pass)) {
attempts++;
 throw new InvalidLoginException("Invalid Login! Attempts left: "
+ (3 - attempts));
 } else {
 msg.setText("Login Successful!");
 }
 } catch (InvalidLoginException e) {
 msg.setText(e.getMessage());
 if (attempts >= 3) {
 msg.setText("3 Attempts Over! Access Denied.");
 btnLogin.setEnabled(false);
 }
 }
 }
 }
 public static void main(String[] args) {
 new LoginScreen();
 }
}


Comments

Popular posts from this blog

slip 25

slip.1

slip.14