Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

directions Explain ever line of code with comment Identify and explain all bugs

ID: 3767599 • Letter: D

Question

directions

Explain ever line of code with comment

Identify and explain all bugs

// Makes String comparisons

public class DebugSeven1

{

   public static void main(String[] args)

   {

      String name1 = "Roger";

      String name2 = "Roger";

      String name3 = "Stacy";

      if(name1== name2)

        System.out.println(name1 + " and " + name2 +

          " are the same");

      if(name1 == name3)

        System.out.println(name1 + " and " + name3 +

          " are the same");

      if(name1 == "roger")

        System.out.println(name1 + " and 'roger' are the same");

      if(name1 == "Roger")

        System.out.println(name1 + " and 'Roger' are the same");

   }

}

// Program prompts user to enter a series of integers

// separated by spaces

// Program converts them to numbers and sums them

import java.util.*;

public class DebugSeven2

{

   public static void main(String[] args)

   {

      String str;

      int x;

      int length;

      int start = 0;

      int num;

      int lastSpace = -1;

      int sum = 0;

      String partStr;

      Scanner in = new Scanner(System.in);

      System.out.print("Enter a series of integers separated by spaces >> ");

      str = in.nextLine();

      length = length();

      for(x = 0; x <= length; ++x)

      {

         if(str.charAt(x) == " ")

         {

             partStr = str.substring(x, lastSpace + 1);    

             num = Integer.parseInt(partStr);

             System.out.println("                " + num);

             sum = num;

             lastSpace == x;

          }

      }

      partStr = str.substring(lastSpace + 1, length);

      num = Integer.parseInt(partStr);

      System.out.println("                " + num);

      sum = num;

      System.out.println("         -------------------" +

         " The sum of the integers is " + sum);

   }

}

// Program displays some facts about a string

public class DebugSeven3

{

   public static void main(String[] args)

   {

      String quote =

        "Honesty is the first chapter in the book of wisdom. - Thomas Jefferson";

      System.out.println("index.of('f') is: " + indexOf('f'));

      System.out.println("index.of('x') is: " + indexOf('x'));

      System.out.println("char.At(5) is: " + charAt(5));

      System.out.println("endsWith("daughter") is: " + quote.endswith("daughter"));

      System.out.println("endsWith("son") is: " + quote.endswith("son"));

      System.out.println("replace('e', '*') is: " + quote.replace('e', 'M'));

   }

}

// Converts a string to lowercase, and

// displays the string's length

// as well as a count of letters

public class DebugSeven4

{

   public static void main(String[] args)

   {

      String aString = "HELP!! I need to get 37 things DONE today!!";

      int numLetters = 0;

      int stringLength = aString.length;

      System.out.println("In all lowercase, the sentence is: ");

      for(int i = 0; i < length; i++)

      {

         char ch = Character.toLowerCase(aString.charAt(length));

         System.out.print(ch);

         if(Character.isLetter())

            numLetters++;

      }

      System.out.println();

      System.out.println

         ("The number of CHARACTERS in the string is " + stringLength);

      System.out.println("The number of LETTERS is " + stringLength);

   }

}

Explanation / Answer

1.

//Makes String comparisons
public class DebugSeven1
{
   // main method definition starts
public static void main(String[] args)
{
   // declare 3 strings and initialize them with any values
String name1 = "Roger";
String name2 = "Roger";
String name3 = "Stacy";

// compare name1 and name2
if(name1== name2)
System.out.println(name1 + " and " + name2 +
" are the same");
// compare name1 and name3
if(name1 == name3)
System.out.println(name1 + " and " + name3 +
" are the same");
// check whether name1 is eual to roger
if(name1 == "roger")
System.out.println(name1 + " and 'roger' are the same");
// check whether name1 is eual to Roger
if(name1 == "Roger")
System.out.println(name1 + " and 'Roger' are the same");
}
}

2.

//Program prompts user to enter a series of integers
//separated by spaces
//Program converts them to numbers and sums them
import java.util.*;
public class DebugSeven2
{
   // main method definition starts
public static void main(String[] args)
{
   // declare the variables
String str;
int x;
int length;
int start = 0;
int num;
int lastSpace = 0;
int sum = 0;
String partStr = null;
Scanner in = new Scanner(System.in);
// prompt the user for a series of integers
System.out.print("Enter a series of integers separated by spaces >> ");
str = in.nextLine();
length = str.length(); // 'str.' is missing
for(x = 0; x <= length; ++x)
{
if(str.charAt(x) == ' ') // charAt gives a character, so we should check it with ' ', not " "
{
   // parse a number and print it
partStr = str.substring(x, lastSpace + 1);
num = Integer.parseInt(partStr);
System.out.println(" " + num);
sum = num;
lastSpace = x; // assignment operator is =, not ==
}
}
if(lastSpace < str.length()) partStr = str.substring(lastSpace + 1, length);
num = Integer.parseInt(partStr);
System.out.println(" " + num);
// calculate the sum
sum += num;
// print the sum
System.out.println(" -------------------" +
" The sum of the integers is " + sum);
}
}

3.

public class DebugSeven3
{
public static void main(String[] args)
{
   // declare a string
String quote =
"Honesty is the first chapter in the book of wisdom. - Thomas Jefferson";

// print the index of f and x
System.out.println("index.of('f') is: " + quote.indexOf('f')); // indexOf is a property of String object
System.out.println("index.of('x') is: " + quote.indexOf('x')); // indexOf is a property of String object
// print the character at index 5
System.out.println("char.At(5) is: " + quote.charAt(5)); // charAt is a property of String object
System.out.println("endsWith("daughter") is: " + quote.endsWith("daughter")); // It should be endsWith
System.out.println("endsWith("son") is: " + quote.endsWith("son"));
System.out.println("replace('e', '*') is: " + quote.replace('e', 'M'));
}
}

4.

//Converts a string to lowercase, and
//displays the string's length
//as well as a count of letters
public class DebugSeven4
{
public static void main(String[] args)
{
String aString = "HELP!! I need to get 37 things DONE today!!";
int numLetters = 0;
// get the length of aString
int stringLength = aString.length(); // length is a method of String object
System.out.println("In all lowercase, the sentence is: ");
// iterate throught the String
for(int i = 0; i < stringLength; i++)
{
   // convet the character to lowercase
char ch = Character.toLowerCase(aString.charAt(i)); // the position is i
System.out.print(ch);
if(ch.isLetter())
numLetters++;
}
System.out.println();
System.out.println
("The number of CHARACTERS in the string is " + stringLength);
System.out.println("The number of LETTERS is " + stringLength);
}
}