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

public class WhatzitRevisited { public static void main(String[] args) { int i =

ID: 3544295 • Letter: P

Question

public class WhatzitRevisited {


public static void main(String[] args) {

inti =50;

intj =0;

while(i >0) {

intk = whatzitTheFirst(i);

j = j + k;

i = i /10;

System.out.println("Main Loop"); line 10

}

System.out.println("The final value is: "+j);

}


private static int whatzitTheFirst(intj) {

inti =50;

intk =5;

while(k < j) {

System.out.println("First Whatzit");

i = i - whatzitTheSecond(j); line 20

k = k*10;

}

returni;

}


private static intwhatzitTheSecond(intk) {

intj =2;

inti =0;

while(j >0) {

System.out.println("Second Whatzit"); line 30

i = i + k;

j = j -1;

}

returni;

}

}

Explanation / Answer

public class WhatzitRevisited {

    public static void main(String[] args) {
        int i = 50;
        int j = 0;
        while (i > 0) {
            int k = whatzitTheFirst(i);
            j = j + k;
            i = i / 10;
            System.out.println("Main Loop");
        }
        System.out.println("The final value is: "+j);
    }
    
    private static int whatzitTheFirst(int j) {
        int i = 50;
        int k = 5;
        while (k < j) {
            System.out.println("First Whatzit");
            i = i - whatzitTheSecond(j);
            k = k*10;
        }
        return i;
    }
    
    private static int whatzitTheSecond(int k) {
        int j = 2;
        int i = 0;
        while (j > 0) {
            System.out.println("Second Whatzit");
            i = i + k;
            j = j - 1;
        }
        return i;
    }
}

    a) What lines are in the scope of the variable i declared at line 16?
   
    Ans: since i declared at line 16(begining of method). it will be valid through out that method.
    so lines 17 to 23 are in score of variable i.
   
    What lines are in the scope of the variable k declared at line 7?
   
    Ans: since k declared inside while loop. it will be valid only inside while loop. thus
    lines 8 to 10 are in scope of variable k.
   
    What lines are in the scope of the variable k declared at line 26?
   
    Ans: since k is passed argument. it is valid through out that method.
    thus lines 27 to 34 are in scope of variable k.
       
    Provide a trace table for this program. What is the final output?
    Keep in mind the scopes of the variables while making your trace table.
    How many columns will it need? (Trace the program first, then type it into Eclipse to verify your answer).
   
    j will be in 50 in First Whatzit method.
    j will be 2 in Second Whatzit method.
    j will be 1 in Second Whatzit method.
    j will be -50 in Main Loop.
    j will be 0 in Main Loop.
    so final value of j is 0.
   
    First Whatzit
    Second Whatzit
    Second Whatzit
    Main Loop
    Main Loop
    The final value is: 0

Write a method that takes a String as input and returns the reverse of that String.
Then write a second method that uses the first method to test whether or not an input String is a palindrome.
Your method should return true if the String is a palindrome, and false otherwise.
Palindromes were discussed in Exercise 2 of Closed Lab 06, and you should use the same definition here.
In addition, write a main method that tests your method with a variety of Strings that should return true and false values.

public class palindrome
{
public static String reverse(String str)
{
String rev="";
for(int i=str.length()-1; i>=0; i--)
rev= rev + str.charAt(i);
return rev;
}
public static boolean is_palindrome(String str)
{
if(str.equalsIgnoreCase(reverse(str))) return true;
return false;
}
public static void main(String[] args)
{
System.out.println("String malayalam is palindrome ?" +(is_palindrome("malayalam")?"true":"false"));
System.out.println("String ramar is palindrome ?" +(is_palindrome("ramar")?"true":"false"));
System.out.println("String ramas is palindrome ?" +(is_palindrome("ramas")?"true":"false"));
}
}

Using the methods you wrote for the above question,
write a method that takes a Scanner as an input parameter and returns a String as a result.
The method should print a prompt to the screen asking the user to enter a palindrome.
It should then test the input. If it is not a palindrome,
it should print an error message and continue asking for a palindrome until the user provides one.
Once a palindrome has been input, the method should return it.
In addition, write a main method that uses this method and displays the final palindrome to the screen.

import java.util.*;
public class palindrome
{
public static String reverse(String str)
{
String rev="";
for(int i=str.length()-1; i>=0; i--)
rev= rev + str.charAt(i);
return rev;
}
public static boolean is_palindrome(String str)
{
if(str.equalsIgnoreCase(reverse(str))) return true;
return false;
}
public static String input(Scanner in)
{
String str;
System.out.println("Enter a palindrome :");
str = in.next();
while(!is_palindrome(str))
{
System.out.println("Input String is not a Palindrome.Re-adEnter a palindrome :");
str = in.next();
}
return str;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.println("Final Palindrome is " + input(in));
}
}


Write a method named rowOfStars that takes a single integer parameter n and returns a String with that many stars in it.
For example, calling the method with a value of 3 would cause the String "***" to be returned.

public static String rowOfStars(int n)
{
String str = "";
for(int i=0; i<n; i++)
str = str + '*';
return str;
}

Using the method you wrote for the previous question, write a method that takes a single integer parameter n and prints a triangle
of stars to the screen that has a height and base size equal to n. For example, calling the method with a value of
4 will produce the triangle:
*
**
***
****

public static void print_triangle(int n)
{
for(int i=0; i<n; i++)
System.out.println(rowOfStars(i+1));
}