You are on page 1of 19

DSA LAB

QUESTION 1
import java.util.*;

class match //creating class match


{
   public static void main(String[] args) { //main method

       Random r = new Random();   //creating random object for creating random
number
       String teamA[][] = new String[11][2]; //2 dimmensionl arrays for team A and
team B
       String teamB[][] = new String[11][2];

       int out=0; //out variable for counting out counnt of players
       int balls[]=new int[120]; //balls array for storing every ball value
       int score=0; //variable storing score of a player
       int weather=0;//varible for storing weather
       int players=1;//players count
       int totalScore=0; //varible for total score of teamA
       int ballsbowled=0; //variable for balls bowled for team A

       for (int i=1;i<=120 ;i++ ) { //loop through all 120 balls

           if(i%12==0){         //find weather for every 12 balls
               weather =r.nextInt(5);
           }
           if (weather==4) { //if it is raining
               if(players==1){ //then include the players scores list
               teamA[0][0]="Player"+players;
               teamA[0][1]=Integer.toString(score);
               }
               else{
               teamA[players-1][0]="Player"+players;
               teamA[players-1][1]=Integer.toString(score);
               }
               ballsbowled=i-1; //assign the value for total balls bowled and break
               break;
              
           }
           else if(players>11){   //if all players are out then break
               ballsbowled=i;
               break;
           }
           else
           {                   //else we get score by random value
               int ballval;   
               if (weather==0) { //if weather is sunny
                   ballval= -1+r.nextInt(7);//(score between 0-6)
               }
               else if (weather==1) { //if weather is cloudy
                   ballval= -1+r.nextInt(5);   //(score between 0-4)
               }
               else if (weather==2) { //if weather id windy
                   ballval= -1+r.nextInt(4);//(score between 0-3)
                  
               }
               else{ //if weather is drizzle
                   ballval= -1+r.nextInt(3);//(score between 0-2)
               }
              
               if (ballval==-1) { //if given score is -1 means out
                   out+=1; //then increase out count
                   totalScore+=score; //increase totalscore
                   teamA[players-1][0]="Player"+players;   //store the score of the player in
the array
                   teamA[players-1][1]=Integer.toString(score);
                   players+=1; //change the player
                   score=0;//make the score 0
                  
               }
               else
               {       //if score is positive integer
                   score+=ballval; //add score to player
                   balls[i-1]=ballval; //add the ball score to the balls array
               }
           }

          
       }

       if (ballsbowled<18) {   ///if balls bowled for team a <18 then match is drawn
           System.out.println("Match drawn");
          
       }
       else{       //else team b will play the game

           int deduct=0; //varibale for deductions of balls due to rain
           int b=120; //varible for storing balls of team b
           int outB=0; //varible for counting outs of teamb
           score=0;   //score for player
           weather=0; //varibale for weather
           players=1;   //player variable for who is playing
           int totalScoreA=totalScore; //varible for team A score and assigning total score
initially
           int totalScoreB=0; //variable for totalscore of team B
           int v=0; //varible for storing number of balls bowled for team B

           for (int i=0;i<b ;i++ ) { //loop through b balls


               if(i%12==0){ //find weather for every 12 balls
               weather =r.nextInt(5);

                   if (weather==4) { //if it is raining


                       b-=12;       //then deduct 12 balls from both teamA and teamB
                       deduct+=12;
                       totalScoreA=0;
                                       //then find the total score of team A after deducting balls
                       for (int j=0;j<ballsbowled-deduct ;j++ ) {
                           totalScoreA+=balls[j]; //assign the score to totalscoreA
                          
                       }
                  
                   }
               }
              
               if(players>11){       //if all players are out
                   v=i;   //assign number of balls bowled to v varibale
                   break; //then break
               }
               else if (totalScoreB>totalScoreA) { //if Team B crosses the score of Team A
                   v=i;//assign number of balls bowled to v varibale
                   break;//then exit
               }
               else
               { //else we get score by random value

                       int ballval;


                       if (weather==0) {
                           ballval= -1+r.nextInt(7);
                       }
                       else if (weather==1) {
                           ballval= -1+r.nextInt(5);  
                       }
                       else if (weather==2) {
                           ballval= -1+r.nextInt(4);
                          
                       }
                       else{
                           ballval= -1+r.nextInt(3);
                       }
                      
                       if (ballval==-1) { //if ball score is -1
                           outB+=1; //increase out count
                           teamB[players-1][0]="Player"+players; //assign player score to array
                           teamB[players-1][1]=Integer.toString(score);
                           players+=1; //change player
                           score=0;//make score 0
                          
                       }
                       else
                       { //if ball value is positive value
                           score+=ballval;   //then add it to score
                           totalScoreB+=ballval;//and also to total score
                       }

               }

              


           }

           if (b<18) {           //if balls bowled for teamB <18 then it is a draw match
               System.out.println("Match Drawn");
              
           }
           else{ //other wise display all details of team A and teamB
               System.out.println("Total Balls Bowled for TeamA - "+ballsbowled);
               System.out.println("Team A Scores : \n");
               for (int i=0;i<out ;i++ ) {
                   System.out.println(teamA[i][0]+" - "+teamA[i][1]);
                  
               }
               System.out.println("\nTotal Score of TeamA - "+totalScore);

               System.out.println("\nTotal Balls Bowled for TeamB - "+v);


               System.out.println("Team B Scores : \n");
               for (int i=0;i<outB ;i++ ) {
                   System.out.println(teamB[i][0]+" - "+teamB[i][1]);
                  
               }
               System.out.println("\nTotal Score of TeamB - "+totalScoreB);
               System.out.println("\nDeductions of Balls Due to Rain = "+deduct);
               System.out.println("Final Score Of Team A after Deduction : " +
totalScoreA);
               System.out.println("Final Score Of Team B after Deduction : " +
totalScoreB);

               if (totalScoreA<totalScoreB) { //final result


                   System.out.println("\nTeam B Wins ");
                  
               }
               else{
                   System.out.println("\nTeam A Wins");
               }
              

           }
       }

      
   }
}

ANSWER NO 4

public class Employee {

    String id;

    String name;

    String department;

    String post;

    Employee(String id, String name, String department, String post) {

        this.id = id;

        this.name = name;

        this.department = department;

        this.post = post;
    }

    public String toString() {

        String info = "ID: " + id + "\nName: " + name + "\nDepartment: " + department + "\npost: "
+ post + "\n";

        return info;

    }

    public static void main(String[] args) {

        Employee e1 = new Employee("IT-6767", "Mr. Habib", "IT", "Junior procurer");

        Employee e2 = new Employee("LG-012", "Mr. John", "Legal", "XYZ");

        Employee e3 = new Employee("RD-012", "Mr. Habib", "Record", "ABC");

        Employee e4 = new Employee("DB-098", "Mr. Rom", "Database", "Administrator");

        Employee e5 = new Employee("IT-6707", "Mr. Habib", "IT", "Consultant");

        Employee arr[] = {e1, e2, e3, e4, e5};

        String id = "IT-6767";

        for (int i = 0; i < arr.length; i++) {

            if (arr[i].id.equals(id)) {

                arr[i].post = "Senior procurer";

                System.out.println("updated info of Mr. Habib: ");

                System.out.println(arr[i]);

            }

        }

    }

------------------------------------------------------------------------------------------------------------------------------------------

ANSWER NO 3
class DoublyLinkedList {   
    class Node{
        int data;
        Node previous;
        Node next;
  
        public Node(int data) {
            this.data = data;
        }
    }
  
    Node head, tail = null;
Node current=null;
  
    
    public void addNode(int data) {
        //Create a new node
        Node toAdd = new Node(data);
  
       
        if(head == null) {
            head = tail = toAdd;
            head.previous = null;
            tail.next = null;
        }
        else {
            
            tail.next = toAdd;
            toAdd.previous = tail;
            tail = toAdd;   
            tail.next = null;
        }
    }
public void delete_data(int data){
Node temp=null;
  
if (head==null){
return;}
if (head.data==data){
if(head.next!=null){
head.next.previous=null;
head=head.next;
return;}
else{
head=null;
return;
}
}
else if(head.data!=data && head.next==null){//if data is not present in list
return;}
current=head;
while(current.next!=null && current.data!=data){
temp=current;
current=current.next;
}
if(current.data==data){
temp.next=temp.next.next;
if(temp.next!=null){
temp.next.previous=temp;
}
else
tail=temp;
}
else
return;
}
boolean areSame(DoublyLinkedList L2){

Node a=this.head

Node b=L2.head;

while(a!=null && b!=null){

if(a.data!=b.data)

return false;

a=a.next;

b=b.next;

return (a==null && b==null);

}
    
    public void printNodes() {
  
        Node current = head;
        if(head == null) {
            System.out.println("Doubly linked list is empty");
            return;
        }
        System.out.println(" ");
        while(current != null) {
            System.out.print(current.data + " ");
            current = current.next;
        }
    }
}
class Main{
    public static void main(String[] args) {
        //create a DoublyLinkedList object
        DoublyLinkedList dl_List = new DoublyLinkedList();
   DoublyLinkedList dl2_List = new DoublyLinkedList();
  int []a={50,30,25,75,82,28,77};
int []b={50,40,25,75,80,21,37,30};
for (int i=0;i<a.length;i++){
dl_List.addNode(a[i]);

}
for (int i=0;i<b.length;i++){
dl2_List.addNode(b[i]);
}
        dl_List.printNodes();
dl2_List.printNodes();

dl2_List.delete_data(80);

dl2_List.printNodes();

if (dl_List.areSame(dl2.List)==true){

System.out.println(" L1 and L2 are same");

else{

System.out.println(" L1 and L2 are not same");

}
    }
}

You might also like