Chapter 6: A Close Look at Methods and Classes

```
class X {
 private int count;

```

**is the following fragment correct?**

```
class Y {
 public static void main(String args[]) {
 X ob = new X();
 ob.count = 10;

```

 **2. An access modifier must \_\_\_\_\_\_\_\_\_\_ a member’s declaration.**
 **3. The complement of a queue is a stack. It uses first-in, last-out accessing and is often likened to a stack of plates. The first plate put on the table is the last plate used. Create a stack class called *Stack* that can hold characters. Call the methods that access the stack *push( )* and *pop( )*. Allow the user to specify the size of the stack when it is created. Keep all other members of the *Stack* class private. (Hint: You can use the *Queue* class as a model; just change the way that the data is accessed.)**// A stack class for characters.

```
class Stack {
 private char stck[]; // this array holds the stack
 private int tos; // top of stack

 // Construct an empty Stack given its size.
 Stack(int size) {
 stck = new char[size]; // allocate memory for stack
 tos = 0;
 }

 // Construct a Stack from a Stack.
 Stack(Stack ob) {
 tos = ob.tos;
 stck = new char[ob.stck.length];

 // copy elements
 for(int i=0; i < tos; i++)
 stck[i] = ob.stck[i];
 }

 // Construct a stack with initial values.
 Stack(char a[]) {
 stck = new char[a.length];
 for(int i = 0; i < a.length; i++) {
 push(a[i]);
 }
 }

 // Push characters onto the stack.
 void push(char ch) {
 if(tos==stck.length) {
 System.out.println(" -- Stack is full.");
 return;
 }

 stck[tos] = ch;
 tos++;
 }

 // Pop a character from the stack.
 char pop() {
 if(tos==0) {
 System.out.println(" -- Stack is empty.");
 return (char) 0;
 }
 tos--;
 return stck[tos];
 }
}

// Demonstrate the Stack class.
class SDemo {
 public static void main(String args[]) {
 // construct 10-element empty stack
 Stack stk1 = new Stack(10);
 char name[] = {'T', 'o', 'm'};

 // construct stack from array
 Stack stk2 = new Stack(name);
 char ch;
 int i;

 // put some characters into stk1
 for(i=0; i < 10; i++)
 stk1.push((char) ('A' + i));

 // construct stack from another stack
 Stack stk3 = new Stack(stk1);

 // show the stacks.
 System.out.print("Contents of stk1: ");
 for(i=0; i < 10; i++) {
 ch = stk1.pop();
 System.out.print(ch);
 }
 System.out.println("\n");
 System.out.print("Contents of stk2: ");
 for(i=0; i < 3; i++) {
 ch = stk2.pop();
 System.out.print(ch);
 }
 System.out.println("\n");
 System.out.print("Contents of stk3: ");
 for(i=0; i < 10; i++) {
 ch = stk3.pop();
 System.out.print(ch);
 }
 }
}

Here is the output from the program:

Contents of stk1: JIHGFEDCBA
Contents of stk2: moT
Contents of stk3: JIHGFEDCBA

 **4. Given this class,**

```
class Test {
 int a;
 Test(int i) { a = i; }
}

```

**write a method called *swap( )* that exchanges the contents of the objects referred to by two *Test* object references.**

```
void swap(Test ob1, Test ob2) {
 int t;
 t = ob1.a;
 ob1.a = ob2.a;
 ob2.a = t;
}

 **5. Is the following fragment correct?**

```
class X {
 int meth(int a, int b) { ... }
 String meth(int a, int b) { ... }

```

 **6. Write a recursive method that displays the contents of a string backwards.**

```
// Display a string backwards using recursion.
class Backwards {
 String str;
 Backwards(String s) {
 str = s;
 }

 void backward(int idx) {
 if(idx != str.length()-1) backward(idx+1);
 System.out.print(str.charAt(idx));
 }
}

class BWDemo {
 public static void main(String args[]) {
 Backwards s = new Backwards("This is a test");
 s.backward(0);
 }
}

 **7. If all objects of a class need to share the same variable, how must you declare that variable?**
 **8. Why might you need to use a *static* block?**
 **9. What is an inner class?**
 **10. To make a member accessible by only other members of its class, what access modifier must be used?**
 **11. The name of a method plus its parameter list constitutes the method’s \_\_\_\_\_\_\_\_\_\_.**
 **12. An int argument is passed to a method by using call-by-\_\_\_\_\_\_\_\_\_\_.**
 **13. Create a varargs method called *sum( )* that sums the *int* values passed to it. Have it return the result. Demonstrate its use.**
There are many ways to craft the solution. Here is one:

```
class SumIt {
 int sum(int ... n) {
 int result = 0;
 for(int i = 0; i < n.length; i++)
 result += n[i];
 return result;
 }
}

class SumDemo {
 public static void main(String args[]) {
 SumIt siObj = new SumIt();
 int total = siObj.sum(1, 2, 3);
 System.out.println("Sum is " + total);
 total = siObj.sum(1, 2, 3, 4, 5);
 System.out.println("Sum is " + total);
 }
}

 **14. Can a varargs method be overloaded?**
 **15. Show an example of an overloaded varargs method that is ambiguous.**Here is one example of an overloaded varargs method that is ambiguous:

```
double myMeth(double ... v ) { // ...
double myMeth(double d, double ... v) { // ...

```

If you try to call *myMeth( )* with one argument, like this,

```
myMeth(1.1);

```

the compiler can’t determine which version of the method to invoke.