1 Tracing Programs For each program below show what is displ

1. Tracing Programs
For each program below, show what is displayed on the screen when the code executes.

A. import java.util.Arrays;
public class ReferenceSemantics
{
public static int mystery(int a, int [] b)
{
a++;
b[0] = 0;
b = new int[4];
return a / 2;
}

public static void main(String [] args)

{
int x = 10;
int [] y = {3, -3, 3};
x = x + mystery(x, y);
System.out.println(x);
System.out.println(Arrays.toString(y));
}
}

B. public class NestedCalls
{
public static int mystery1(int a)
{
System.out.println(\"m 1\");
return a / 2;
}
public static String mystery2(int b)
{
String s = \"m 2\";
System.out.println(s);
for(int i=0; i<b ; i++) {
s = s + \" \" + i;
}
return s;
}
public static void main(String [] args)
{
System.out.println(\"main\");
System.out.println(mystery2(mystery1(4)));
System.out.println(\"end main\");
}
}

2. Method development walkthrough (StringWalkthrough.java) for
calculating the number of letter, say ‘i’, in a String, and return that
number.

a. Start with a method signature: what’s the return type, how many arguments does it take,
and what is the data type of each argument?


b. Next, decide on an algorithm, or recipe, for taking the inputs to this command (the
arguments) and computing the outputs (the return value). Write down a step-by-step
procedure, in English, for computing the number of ‘i’s in a String.


c. Next, translate your algorithm into code. Don’t forget a return value at the end of the
method (and make sure the data type of the return value matches the return type in your
method signature).


d. Create a program whose main() method repeatedly reads in Strings from the user and
prints out how many ‘i’s are in that String, until the user enters a word with no ‘i’.


e. Modify the method from (c) so that it accepts an argument of type char as well as of type
String, and counts how many of that character appear in the String.


f. Have the main() method read in a String and a letter from the user, and print out how
many of the letter appear in the String.


3. Another method development walkthrough (PrimeWalkthrough.java) for
printing all the prime numbers between 1 and 100.


a. Write a method to compute whether an integer is prime or not. Start with the method
signature: what is the return type, how many arguments does it need, and what type are
the arguments?


b. Next, decide on an algorithm, or recipe, for checking whether a number is prime.
Without thinking about the code, write down a simple step-by-step procedure for
deciding whether or not a number is prime.


c. Finally, translate your algorithm into code. Remember to return a value (whose type
matches the return type in your method signature) at the end.


d. Write the program (including main) that prints out all the prime numbers between 1 and
100. Make calls to the method you defined above.


4. Taste of generalizing Method (Taste.java)


a. Write methods to display a small triangle, a small square, and a small horizontal line in
with ASCII art. Make calls to these methods from the main() method so that you create
the following figure:
XX
XX
================
^
^^^
^^^^^
XX
XX


b. Modify your methods so that they accept a char argument, and the characters that make
up each figure match the value of the argument. Then add arguments to your method
calls in main() so that the program now displays:
XX
XX
----------------
u
uuu
uuuuu
OO
OO

5. Test of method development.
For the main() method below, come up with a way to break it into multiple methods (also known
as \"decomposing\"), so that the resulting program is better organized. The goal is to print out the
entire stairwell with any given number of step, length of run, and height of rise.
public class PrintSteps
{
public static void main(String [] args)
{
System.out.println(\"-----\");
System.out.println(\" |\");
System.out.println(\" |\");
System.out.println(\" -----\");
System.out.println(\" |\");
System.out.println(\" |\");
}
}

Solution

1 A)

Output:

15

[0, -3, 3]

First output reason:

x = x + mystery(x, y);

In above statement x is 10 mystery will return 5 (i.e., 11/2). So result is 15.

Second output reason:

int [] y = {3, -3, 3};

System.out.println(Arrays.toString(y));

Inside the function mystery we have a statement b[0] = 0; So y[0] becomes 0 and rest values are not changed and displayed.

1 B)

Output:

main
m 1
m 2
m 2 0 1
end main

1st output reason: main because: System.out.println(\"main\");

2nd output reason: m 1 because: System.out.println(mystery2(mystery1(4)));

First mystery1(4) within the function we have System.out.println(\"m 1\");

3rd output reason: m 2 0 1 because return a / 2; will return 2.

now it is mystery2(2)

inside the function we have String s = \"m 2\";   System.out.println(s);

after this we have

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

{
s = s + \" \" + i;
}

where b is 2 and i will be 0 and then 1. So afer m 2 we have 0 and 1

Last output because: System.out.println(\"end main\");

2) A) What’s the return type: Return type is integer
How many arguments does it take: Two
what is the data type of each argument: First one is String and second one is character

2 B)
1) Accept a string
2) Accept a character
3) Initialize the counter to 0
4) Calculate the length of the string
5) Start from the zero position of the string move till end of the string
6) Extract one character at a time from the string
7) Compare the character with the character accepted
8) If both are equal increase the counter.

2 C) E) and F)

class StringWalkthrough
{
   public static void main(String ss[])
   {
       System.out.println(\"Number of times = \" + count(\"checkc\", \'c\'));
   }
   static int count(String st, char cc)
   {
       int co = 0;
       for(int c = 0; c < st.length(); c++)
           if(st.charAt(c) == cc)
               co++;
       return co;
   }
}

Output:

Ente a String:
demo

Ente a Character to search and count:
d
Number of times = 1

Ente a String:
check

Ente a Character to search and count:
c
Number of times = 2

Ente a String:
tryit

Ente a Character to search and count:
m

2 D)

import java.util.*;
class StringWalkthrough
{
   public static void main(String ss[])
   {
       Scanner sc = new Scanner(System.in);
       String s;
       char ch;
       int c = 0;
       //Loops infinitely
       do
       {
           System.out.println(\"\ Ente a String: \");
           s = sc.nextLine();
           System.out.println(\"\ Ente a Character to search and count: \");
           ch = sc.nextLine().charAt(0);
           c = count(s, ch);
           //If character found zero times then comeout of the loop
           if(c == 0)
               break;
           else
               System.out.println(\"Number of times = \" + c);
       }while(true);
   }
   static int count(String st, char cc)
   {
       int co = 0;
       for(int c = 0; c < st.length(); c++)
           if(st.charAt(c) == cc)
               co++;
       return co;
   }
}

3 A) What is the return type: void

How many arguments does it need: Zero

What type are the arguments: Though zero arguments so there is no type requierd

3 B)

1) Set the flag value to zero
2) Set first counter value to 2 and move upto 99
3) Set second counte value to 2 and move upto first counter value divide by 2
4) Check whether the first counter value is divisible by second counter value
5) If divisible set the flag counter value to 1 and comeout of the second counter loop
6) If the flag value is zero then print it is prime
7) If the flag value is one then set it to zero

3 C)

void dispPrime()
   {
       int f = 0, c, d;
       //Loops from 2 to 99
       for(c = 2; c < 100; c++)
       {
           //Loops from 2 to number / 2
           for(d = 2; d < c / 2; d++)
           {
               //Checks for the divisibility
               if(c % d == 0)
               {
                   //If divisible then set flag to 1
                   f = 1;
                   break;
               }
           }
           //Check the flag if the flag is not changed then prime
           if(f == 0)  
               System.out.println(\"Prime: \" + c);
           else
               f = 0;
       }
   }

3 D)

class PrimeWalkthrough
{
   //Displays all prime number between 1 and 100
   void dispPrime()
   {
       int f = 0, c, d;
       //Loops from 2 to 99
       for(c = 2; c < 100; c++)
       {
           //Loops from 2 to number / 2
           for(d = 2; d < c / 2; d++)
           {
               //Checks for the divisibility
               if(c % d == 0)
               {
                   //If divisible then set flag to 1
                   f = 1;
                   break;
               }
           }
           //Check the flag if the flag is not changed then prime
           if(f == 0)  
               System.out.println(\"Prime: \" + c);
           else
               f = 0;
       }
   }
   public static void main(String ss[])
   {
       PrimeWalkthrough pw = new PrimeWalkthrough();
       pw.dispPrime();
   }
}

Output:

Prime: 2
Prime: 3
Prime: 4
Prime: 5
Prime: 7
Prime: 11
Prime: 13
Prime: 17
Prime: 19
Prime: 23
Prime: 29
Prime: 31
Prime: 37
Prime: 41
Prime: 43
Prime: 47
Prime: 53
Prime: 59
Prime: 61
Prime: 67
Prime: 71
Prime: 73
Prime: 79
Prime: 83
Prime: 89
Prime: 97

4 A)

public class Taste
{
   public static void main(String ss[])
   {
       square();
       horizontalLine();
       triangle();
       square();  
   }
   static void square()
   {
       for(int r = 0; r < 2; r++)
       {
           for(int c = 0; c < 2; c++)
               System.out.print(\"X\");
           System.out.println();
       }
   }
   static void triangle()
   {
       for(int r = 0; r <= 5; r+=2)
       {
           for(int c = 0; c <= r; c++)
           {
               System.out.print(\"^\");
           }
           System.out.println();
       }
   }
   static void horizontalLine()
   {
       for(int r = 0; r < 17; r+=2)
       {
           System.out.print(\"=\");
       }
       System.out.println();
   }
}

Output:

XX
XX
=========
^
^^^
^^^^^
XX
XX

4 B)

public class Taste
{
   public static void main(String ss[])
   {
       square(\'X\');
       horizontalLine(\'-\');
       triangle(\'u\');
       square(\'O\');  
   }
   static void square(char ch)
   {
       for(int r = 0; r < 2; r++)
       {
           for(int c = 0; c < 2; c++)
               System.out.print(ch);
           System.out.println();
       }
   }
   static void triangle(char ch)
   {
       for(int r = 0; r <= 5; r+=2)
       {
           for(int c = 0; c <= r; c++)
           {
               System.out.print(ch);
           }
           System.out.println();
       }
   }
   static void horizontalLine(char ch)
   {
       for(int r = 0; r < 17; r+=2)
       {
           System.out.print(ch);
       }
       System.out.println();
   }
}

Output:

XX
XX
---------
u
uuu
uuuuu
OO
OO

5)

import java.util.*;
public class PrintSteps
{
   public static void main(String [] args)  
   {
       Scanner sc = new Scanner(System.in);
       int l, h, t, s;
       System.out.println(\"\ Enter the Length: \");
       l = sc.nextInt();
       System.out.println(\"\ Enter the Height: \");
       h = sc.nextInt();
       System.out.println(\"\ Enter Number of times: \");
       t = sc.nextInt();
       //Number of times required
       for(int c = 0; c < t; c++)
       {
           line(l);          
           hori(h);
       }
   }
   //Print line
   static void line(int len)
   {
       for(int c = 0; c < len; c++)
           System.out.print(\"-\");
       System.out.println();
   }
   //Print pipe
   static void hori(int he)
   {
       for(int c = 0; c < he; c++)
       {
           System.out.println(\" |\");
       }
   }
}

Output:

Enter the Length:
5

Enter the Height:
3

Enter Number of times:
5
-----
|
|
|
-----
|
|
|
-----
|
|
|
-----
|
|
|
-----
|
|
|

1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman
1. Tracing Programs For each program below, show what is displayed on the screen when the code executes. A. import java.util.Arrays; public class ReferenceSeman

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site