docx

advertisement

Solution to Practice Problems: Methods 2

1.

Tracing Programs

For each program below, show what is displayed on the screen when the code executes. import java.util.Arrays; public class ReferenceSemantics1

{

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));

}

} screen

15

[0, -3, 3] import java.awt.Point; public class ReferenceSemantics2

{

public static int mystery(char a, Point b)

{

a = (char) (a + 1);

b.setLocation(0, 2);

b = new Point(1, 3);

return b.x;

}

public static void main(String [] args)

{

char x = 'b';

Point y = new Point(5, 7);

y.y = mystery(x, y);

System.out.println(x);

System.out.println(y);

}

} screen b java.awt.Point[x=0,y=1] import java.io.File; public class ReferenceSemantics3

{

public static void mystery(boolean a, File f)

{

a = !a;

f.delete(); // this deletes the file (whose name is stored in f)

// from the hard drive, if it exists.

f = new File("output");

return;

}

public static void main(String [] args)

{

boolean x = false;

File y = new File("input");

mystery(x, y);

System.out.println(x);

System.out.println(y.getName());

}

} screen false input public class NestedCalls1

{

public static void mystery1(int a)

{

System.out.println("m 1");

if(a > 0) {

mystery2(-a);

System.out.println("here?");

}

System.out.println("and here?");

}

public static void mystery2(int b)

{

System.out.println("m 2");

if(b > 0) {

System.out.println("here 2?");

mystery1(b – 10);

}

System.out.println("and here 2?");

}

public static void main(String [] args)

{

System.out.println("main");

int x = 3;

mystery1(x);

mystery2(x);

System.out.println("what about here?");

}

} screen main m 1 m 2 and here 2? here? and here? m 2 here 2? m 1 and here? and here 2? what about here? public class NestedCalls2

{

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");

}

}

screen main m 1 m 2 m 2 0 1 end main

2.

Writing Short Methods

a.

Write a method that takes an integer A as an argument. The method should display the value of A squared on the screen.

// for these kinds of questions, you only need to write the method definition

// you do NOT need to write the import statements, or "public class <name>" public static void displaySquare(int A)

{

System.out.println("the square of " + A + " is " + (A * A));

} b.

Write a method that takes an integer N as an argument. The method should display a line with N copies of the '*' character on the screen. public static void displayLineOfStars(int N)

{

for(int i=0; i<N; i++) {

System.out.print("*");

}

System.out.println();

} c.

Write a method that takes two Strings, s1 and s2, as arguments. The method should return whether s1 contains a copy of s2 inside it. public static boolean stringContainsOtherString(String s, String other)

{

// I check this first to avoid a NullPointerException below

if(s1==null) {

return false; // null String can't contain anything inside of it

}

if(s1.indexOf(s2) >= 0) {

return true; // found s2 inside of s1, so return true

}

return false; // couldn't find s2 inside of s1, so return false

}

d.

Write a method that takes two Points, p1 and p2, and a double d as arguments. The method should return whether the distance between p1 and p2 is less than d. public static boolean distanceIsLessThan(Point p1, Point p2, double d)

{

// notice that this will cause a NullPointerException if p1 is null

// Unlike the example above, it's not clear what the method should return

// if p1 is null. So here it's often better to just let the program

// crash if p1 is null; the method calling this one will need to make

// sure that p1 is not null before calling this method.

double dist = p1.distance(p2);

return dist < d;

// alternatively, you could say if(dist<d) return true; else return false;

// the two versions are equivalent. Can you figure out why?

} e.

Write a method that takes an array of chars A, an integer j, and an integer k as arguments.

The method should return a String consisting of the characters in A between positions j and k. public static String subCharArray(char [] A, int j, int k)

{

String ret = "";

for(int i=j; i<k; i++)

{

ret = ret + char[i];

}

return ret;

}

Download