Professional Documents
Culture Documents
Program: BSCS
1. Write your answers directly on the Black Board (recommended) or upload word file
before the due date on Blackboard.
2. Write your name and registration ID on the first page of your Word file.
3. Answer scripts can only be uploaded on Blackboard only during the submission time.
4. To avoid any unforeseen problems, you are advised to follow the Guide lines
communicated by the Faculty Members.
8. Provide relevant, original and conceptual answers, as this exam aims to test your
ability to examine, explain, modify or develop concepts discussed in class.
9. Do not copy answers from the internet or other sources. The plagiarism of your
answers may be checked through Turnitin.
10. Double check your word file before uploading it on BlackBoard to ensure that you
have uploaded the correct file with your answers.
Question no 01: [15 Marks]
Write a simulation of T20 match. There are two teams, Team A and Team B. For every ball
delivered generate a random number from -1 to 6 (both inclusive). A -1 means that batsman is
out, any other number is the runs scored on that delivery. A 0 in this case represent a dot ball.
After every 12 balls, generate a random value which represents the weather conditions.
Team A plays continuously until all players are out or 120 balls are delivered or match is stopped
due to the rain.
Team B plays continuously until all players are out or 120 balls are delivered they have scored
more than Team A or match is stopped due to rain. Every time a rain event occurs, deduct 12
balls from both innings. A match result should be draw if any of the innings is less than 18
balls. You have to maintain 2d array, store player id and player score. Count total scores of
teams depending upon preceding information. At the end of match announce the wining team
and Man of the match also.
Answer 01:
CODE:
package Mahrukh_46889;
import java.util.*;
class match
{
public static void main(String[] args) {
int out=0;
int balls[]=new int[120];
int score=0;
int weather=0;
int players=1;
int totalScore=0;
int ballsbowled=0;
if(i%12==0){
weather =r.nextInt(5);
}
if (weather==4) {
if(players==1){
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;
break;
}
else if(players>11){
ballsbowled=i;
break;
}
else
{
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) {
out+=1;
totalScore+=score;
teamA[players-1][0]="Player"+players;
teamA[players-1][1]=Integer.toString(score);
players+=1;
score=0;
}
else
{
score+=ballval;
balls[i-1]=ballval;
}
}
if (ballsbowled<18) {
System.out.println("Match drawn");
}
else{
int deduct=0;
int b=120;
int outB=0;
score=0;
weather=0;
players=1;
int totalScoreA=totalScore;
int totalScoreB=0;
int v=0;
if(i%12==0){
weather =r.nextInt(5);
if (weather==4) {
b-=12;
deduct+=12;
totalScoreA=0;
}
}
if(players>11){
v=i;
break;
}
else if (totalScoreB>totalScoreA) {
v=i;
break;
}
else
{
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) {
outB+=1;
teamB[players-1][0]="Player"+players;
teamB[players-1][1]=Integer.toString(score);
players+=1;
score=0;
}
else
{
score+=ballval;
totalScoreB+=ballval;
}
if (b<18) {
System.out.println("Match Drawn");
}
else{
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) {
System.out.println("\nTeam B Wins ");
}
else{
System.out.println("\nTeam A Wins");
}}}}}
Mr. Patrick is fond of reading books he is looking for a computerized way to keep record of his
book collection, you have been given the responsibility to meet up his following requirements.
(Use appropriate data structure to accomplish your task), develop following methods:
1. Add(): adds a new book to the list ( A book contains name of the book, author name)
(4)
NOTE: every new book will have access to its neighboring books (previous and next)
2. Remove():in case any book is removed from the pile update your list (4)
3. countBooks(): displays the total number of books (3)
4. search(): returns true if the given book is present in the list else returns false (3)
5. display(): shows the book collection (1) Output:
Note: You are not allowed to use any built-in Data Structure classes to implement above scenario.
Answer 02:
Class Book
package Mahrukh_46889;
import java.util.Scanner;
class Book {
private String name;
private String author;
private Book next, prev;
package Mahrukh_46889;
import java.util.Scanner;
public class BookCollection {
head = newBook;
}
else {
Book curBook = head;
while(curBook.getNext() != null) {
curBook = curBook.getNext();
}
curBook.setNext(newBook);
newBook.setPrev(curBook);
}
System.out.println("Successfully added");
count++;
}
if(searchBook(name, author)) {
if(head == null) {
return false;
}
else {
Book current = head;
while(current != null) {
if(current.getBookName().equals(name) && current.getAuthorName().equals(author)) {
return true;
}
current = current.getNext();
}
return false;
}
}
if(head == null) {
System.out.println("No books in the collection");
return;
}
Book current = head;
System.out.println("Books in the Collection:\n");
while(current != null) {
System.out.println(current.getBookName() + " by " + current.getAuthorName());
current = current.getNext();
}
}
head = null;
count = 0;
Scanner sc = new Scanner(System.in);
String cont, name, author;
int choice;
System.out.println("Welcome Mr.Patrick! Please select anyone of the following option.");
do {
// MENU
System.out.println("1. Add a book");
System.out.println("2. Remove a book");
System.out.println("3. Show total number of books");
System.out.println("4. Search a book");
System.out.println("5. Show all books");
System.out.print("Please input number of your desired operation: ");
choice = sc.nextInt();
sc.nextLine();
switch(choice) {
case 1:
System.out.println("Please input following information");
System.out.print("Name of the book: ");
name = sc.nextLine();
System.out.print("Author of the book: ");
author = sc.nextLine();
addBook(name, author);
break;
case 2:
System.out.println("Please input following information");
System.out.print("Name of the book: ");
name = sc.nextLine();
System.out.print("Author of the book: ");
author = sc.nextLine();
removeBook(name, author);
break;
case 3:
countBooks();
break;
case 4:
System.out.println("Please input following information");
System.out.print("Name of the book: ");
name = sc.nextLine();
System.out.print("Author of the book: ");
author = sc.nextLine();
boolean found = searchBook(name, author);
if(found)
System.out.println("Book found in the collection");
else
System.out.println("Book not found in the collection");
break;
case 5:
display();
break;
default:
System.out.println("Invalid option!");
}
System.out.println("\nDo you wish to continue (Yes/No)?");
cont = sc.nextLine();
}while(!cont.equals("No") && !cont.equals("no") && !cont.equals("NO"));
sc.close();
}
}
Question no 03: [10 Marks]
Consider flight network having places of departure and destination. Flight network represent
place name as a node and flight trajectories between place names are the edges. Consider
Pakistan cities as a node and show the source and destination paths, show all the paths
separately with their labels and include their code as well.
Analyze this scenario that tree or graph should implement here, justify and also implement this
with appropriate data structure.
Example:
Network represents that there are three cities named Karachi, Islamabad and Lahore. Flight
schedule from Karachi to Islamabad, Islamabad to Lahore and Lahore to Karachi. Places save
as class having departure time and day also.
Note: You are not allowed to use any built-in Data Structure classes to implement above scenario.
Answer 03:
Datastructure for classes implementation
1-Create a HashMap of given pair of tickets.
HashMap be 'dataset'. Every entry of 'dataset' is of the form
"from->to" like "Karachi" -> "Lahore"
3) Start from above found starting point and traverse the 'dataset'
to print itinerary.
//CODE
package Mahrukh_46889;
import java.util.HashMap;
import java.util.Map;
public class Flight_Network
{
public static void main(String[] args)
{
Map<String, String> dataSet = new HashMap<String, String>();
dataSet.put("Karachi", "Lahore");
printResult(dataSet);
}
private static void printResult(Map<String, String> dataSet)
{
Map<String, String> reverseMap = new HashMap<String, String>();
for (Map.Entry<String,String> entry: dataSet.entrySet())
reverseMap.put(entry.getValue(), entry.getKey());
String isl = "Islamabad";
String start = null;
for (Map.Entry<String,String> entry: dataSet.entrySet())
{
if (!reverseMap.containsKey(entry.getKey()))
{
start = entry.getKey();
break;
}
}
if (start == null)
{
System.out.println("Invalid Input");
return;
}
String to = dataSet.get(start);
while (to != null)
{
System.out.print(start + "->" + to + "->" + isl + ".");
start = to;
to = dataSet.get(to);
}}}