A Closer Look at Methods and Classes Flashcards
Given this fragment,
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;
No; a private member cannot be accessed outside of its class.
An access modifier must __________ a member’s declaration.
precede
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
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;
}
Is the following fragment correct?
class X {
int meth(int a, int b) { … }
String meth(int a, int b) { … }
No. Overloaded methods can have different return types, but they do not play a role in overload
resolution. Overloaded methods must have different parameter lists.
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); } }
If all objects of a class need to share the same variable, how must you declare that variable?
Shared variables are declared as static.
Why might you need to use a static block?
A static block is used to perform any initializations related to the class, before any objects are created.
What is an inner class?
An inner class is a nonstatic nested class.
To make a member accessible by only other members of its class, what access modifier must be used?
private
The name of a method plus its parameter list constitutes the method’s __________.
signature
An int argument is passed to a method by using call-by-__________.
value
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); } }
Can a varargs method be overloaded?
Yes.
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.