CS51 - Fall 2009 - Lecture 39
Given the class definitions below:
public class A{
public void method1(){
...
}
}
public class B extends A{
public void method1(String a){
...
}
public void method2(){
...
}
}
public class C extends B{
public void method1(){
...
}
}
private A someA;
private B someB;
private C someC;
- what assignments are legal?
- Is someC.method2() a legal call?
- Which classes method1 is called for:
---- someB.method1();
---- someC.method1("this is a string");
What would the call mystery(5) return?
public String mystery(int num){
if( num <= 1 ){
return "1";
}else{
String returnMe = "";
for( int i = 0; i < num; i++ ){
returnMe += num
}
return returnMe + mystery(num-1);
}
}
What does the following method return with the input: [1, 2, 3, 4, 5, 6, 7, 8]?
// length of a is assumed to be even
public int mystery(int[] a){
int returnVal = 0;
for( int i = 0; i < a.length/2; i++ ){
returnVal += a[i] * a[a.length-i-1];
}
return returnVal;
}
what does the method below do?
public int[] mystery(int[][] a){
int[] returnMe = new int[a.length];
for( int i = 0; i < a.length; i++ ){
int s = 0;
for( int j = 0; j < a[i].length; j++ ){
s += a[i][j];
}
if( s > 100 ){
returnMe[i] = s;
}else{
returnMe[i] = 0;
}
}
return returnMe;
}
Why should you use the StringBuffer class?
One of the methods below will not compile. Indicate which one and explain why.
public void methodA(){
...
throw new IOException();
}
public void methodB(){
...
throw new NumberFormatException();
}