Hey, I was wondering if someone could write these problems out for me having a h
ID: 674583 • Letter: H
Question
Hey, I was wondering if someone could write these problems out for me having a hard time with them. Do not modify how the program works. Just make the corrections necessary. For the most part the errors will be syntax or logic errors. Some errors may be spelling errors in the output. Some of the exercises may have information in the comments explaining how the program should work that will provide needed information on what needs to be corrected.
1.) // Makes String comparisons
public class DebugSeven1
{
public static void main(String[] args)
{
String name1 = "Roger";
String name2 = "Roger";
String name3 = "Stacy";
if(name1.equals(name2))
System.out.println(name1 + " and " + name3 +
" are the same");
if(name1.equals())
System.out.println(name1 + " and " + name3 +
" are the same");
if(name1.equals(roger))
System.out.println(name1 + " and 'roger' are the same);
if(name1.equals("Roger")
System.out.println(name1 and 'Roger' are the same");
}
}
2.)// Phone number conversion
// User enters 10 digits, for example 8005551212
// Output is nicely formatted as (800) 555-1212
import javax.swing.*;
public class DebugSeven2
{
public static void main(String[] args)
{
String inputString;
String newString;
final String QUIT = "999";
inputString = JOptionPane.showInputDialog(null,
"Enter an area code and phone number" +
" as a series of 10 digits"
" and I will display it in a nice format" +
" Enter " + QUIT + " to quit");
while(inputString.equals(QUIT))
{
newString = "(" + inputString.substring(0,3) + ") " +
inputString.substring(0,6) + "-" +
inputString.substring(4, inputString.length() + 1);
inputString = JOptionPane.showInputDialog(null,
"The number is " + newString +
" Enter an area code and phone number" +
" as a series of 10 digits" +
" and I will display it in a nice format" +
" Enter " + QUIT to quit");
}
}
}
3.) // Pig Latin is a made-up language in which you
// remove the first letter of a word and attach
// it to the end, along with "ay". For example,
// "Dog" becomes "ogDay"
import javax.swing.*;
public class DebugSeven3
{
public static void main(String[] args)
{
StringBuffer str = new StringBuffer("");
String userEntry;
char first;
userEntry = JOptionPane.showInputDialog(null, "Enter a word " +
"and I will convert it to Pig Latin");
str.insert(1,userEntry);
first = str.charAt(1);
str.deleteCharAt(2);
str.append(first);
str.append("ya");
JOptionPane.showMessageDialog(null,"Pig Latin version: " + str);
}
}
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;
int stringLength = aString.length();
System.out.println("In all lowercase, the sentence is: ");
for(int i = 0; i < stringLength; i++)
{
char ch = Character.toLowerCase(charAt(x));
System.out.print(ch);
}
System.out.println();
System.out.println
("The number of CHARACTERS in the string is " + length);
System.out.println("The number of LETTERS is " + letters);
}
}
Explanation / Answer
public class DebugSeven1
{
public static void main(String[] args)
{
String name1 = "Roger";
String name2 = "Roger";
String name3 = "Stacy";
if(name1.equals(name2))
System.out.println(name1 + " and " + name2 +
" are the same");
if(name1.equals(name3))
System.out.println(name1 + " and " + name3 +
" are the same");
if(name1.equals("roger"))
System.out.println(name1 + " and 'roger' are the same");
if(name1.equals("Roger"))
System.out.println(name1 +" and 'Roger' are the same");
}
}
import javax.swing.*;
public class DebugSeven2
{
public static void main(String[] args)
{
String inputString;
String newString;
final String QUIT = "999";
inputString = JOptionPane.showInputDialog(null,
"Enter an area code and phone number" +
" as a series of 10 digits"+
" and I will display it in a nice format"
+" Enter " + QUIT + " to quit");
while(!inputString.equals(QUIT))
{
newString = "(" + inputString.substring(0,3) + ") " +
inputString.substring(3,6) + "-" +
inputString.substring(6, inputString.length() );
inputString = JOptionPane.showInputDialog(null,
"The number is " + newString +
" Enter an area code and phone number" +
" as a series of 10 digits" +
" and I will display it in a nice format" +
" Enter QUIT to quit");
}
}
}
import javax.swing.*;
public class DebugSeven3
{
public static void main(String[] args)
{
StringBuffer str = new StringBuffer("");
String userEntry;
char first;
userEntry = JOptionPane.showInputDialog(null, "Enter a word " +
"and I will convert it to Pig Latin");
str.insert(0,userEntry);
first = str.charAt(0);
str.deleteCharAt(0);
str.append(first);
str.append("ay");
JOptionPane.showMessageDialog(null,"Pig Latin version: " + str);
}
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 < stringLength; i++)
{
char ch = aString.charAt(i);
ch=Character.toLowerCase(ch);
if(Character.isAlphabetic(ch)){
numLetters++;
}
System.out.print(ch);
}
System.out.println();
System.out.println
("The number of CHARACTERS in the string is " + stringLength);
System.out.println("The number of LETTERS is " + numLetters);
}
}