You are on page 1of 11

COMSATS UNIVERSITY ISLAMABAD ABBOTTABAD CAMPUS

Name Ameer Hamza Khan

Registration No FA19-BSE-079

Class BSE-6B
Assignment 2nd
Teacher Sir Mazhar Bukhari
Subject Software Testing
Dated April 24th, 2022

1
CODE

import java.util.Scanner;

/**

* @author fa19-bse-079

*/

public class StringBugs{

public static void main(String[] args) {

Scanner stdin=new Scanner(System.in);

System.out.println("Enter a String: ");

String str = stdin.next();

System.out.println("Enter a char: ");

2
char ch = stdin.next().charAt(0);

System.out.println("Character " + ch + " occurs "

+ countOccurrences(str, ch) + " times in string \""

+ str + "\".");

System.out.println("\n\nEnter another String: ");

str = stdin.next();

System.out.println("The reverse of string \""

+ str + "\" is " + reverseString(str));

/**

* Counts the number of occurrences of char ch in String str.

* @param s - String to search for ch

* @param c - char whose occurrence should be counted

* @return int - the number of occurrences

* @bugs There are TWO minor errors. Can you find and fix them?

*/

public static int countOccurrences(String s, char c) {

int count = 0;

if (s != null) {

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

if (s.charAt(i) == c) {

count++;

return count;

/**

* Reverses the String.

* @param s - String to reverse

* @return String - the reversed String

* @bugs There are TWO minor errors. Can you find and fix them?

*/

public static String reverseString(String s) {

//toCharArray converts string s to an array of characters

char[] swap = s.toCharArray();

//bugs are in the for loop

int j=1;

for (int i = 0; i < swap.length/2; i++) {

char cTmp = swap[i];

4
swap[i] = swap[swap.length-j];

swap[swap.length-j] = cTmp;

j++;

//converts the swap array of characters back to a String

return new String(swap);

GAME CODE

package debuggingassignment;

import java.util.Scanner;

/**

* @author fa19-bse-079

*/

public class StringBugs{

public static void main(String[] args) {

5
Scanner stdin = new Scanner(System.in);

System.out.println("Enter a String: ");

String str = stdin.next();

System.out.println("Enter a char: ");

char ch = stdin.next().charAt(0);

System.out.println("Character " + ch + " occurs "

package debuggingassignment;

import java.util.Random;

import java.util.Scanner;

/**

* @author fa19-bse-079

*/

public class Guess3 {

public static void main(String[] args) {

Random ranGen = new Random(8);

final int sides = 6;

Scanner userInput = new Scanner(System.in);

int userguess = -1;

int rolled = -1;

6
int computerPoints = 0;

int humanPoints = 0;

boolean rightGuess = false;

System.out.println("Welcome to the Guess Game!\n\n RULES:");

System.out.println("1. We will play five rounds.");

System.out.println("2. Each round you will guess the number rolled on a six-sided die.");

System.out.println("3. If you guess the correct value in three or fewer tries\n" +

" then you score a point, otherwise I score a point.");

System.out.println("4. Whoever has the most points after five rounds wins.");

for (int r = 0; r < 5; r++) {

System.out.println("\n\nROUND " + r);

System.out.println("-------");

rolled = ranGen.nextInt(sides+1);

System.out.println("The computer has rolled the die.");

System.out.println("You have three guesses.");

rightGuess = false;

int numGuesses = 0;

while (numGuesses < 3 && !rightGuess) {

do {

System.out.print("\nWhat is your guess [1-6]? ");

userguess = userInput.nextInt();

if ((userguess < 1) || (userguess > 6)) {

System.out.println(" Please enter a valid guess [1-6]!");

} while (userguess < 1 || userguess > 6);

7
if (rolled == userguess) {

System.out.println(" Correct!");

rightGuess=true;

} else {

System.out.println(" Incorrect guess.");

numGuesses++;

if (rightGuess) {

humanPoints++;

} else {

computerPoints++;

// display the answer and scores

System.out.println("\n** The correct answer was: " + rolled + " **\n");

System.out.println("Scores:");

System.out.println(" You: \t\t" + humanPoints);

System.out.println(" Computer: \t" + computerPoints);

System.out.println("");

if (computerPoints > humanPoints) {

System.out.println("* You Lose! *");

} else {

System.out.println("* You Win! *");

8
System.out.println("Thanks for playing the Guess Game!");

Screenshots

9
10
11

You might also like