Professional Documents
Culture Documents
QUESTION 1
import java.util.*;
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
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);
}
}
}
}
ANSWER NO 4
String id;
String name;
String department;
String post;
this.id = id;
this.name = name;
this.department = department;
this.post = post;
}
String info = "ID: " + id + "\nName: " + name + "\nDepartment: " + department + "\npost: "
+ post + "\n";
return info;
}
String id = "IT-6767";
if (arr[i].id.equals(id)) {
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;
if(a.data!=b.data)
return false;
a=a.next;
b=b.next;
}
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){
else{
}
}
}