a)(2pts) abcdefg
b)(1pts) b
c) (2pts)17
d)(8 pts)
a: x = 7 y = 6 z = 1
b: x = 8 y = 7 z = 1
c: x = 9 y = 4 z = 4
d: x = 14 y = 4 z = 4
e)(6 pts)
(a) Exception. no output.
Question 2
1 pt for finding an error, 2 pts for explanation, -1 for finding a wrong mistake.
(a) cancel, no mistake
(b)(6pts)
public class ClassB extends ClassA {
public int x;
public int b;
public int c;
public ClassB(int x, int b, int c) {
super(b,c);
this.x = x;
}
public int methodA() {
return a + b;
}
public int methodB() {
z.changeMe();
}
public final int sum() {
return x + b + c;
}
public final int sum() {
return x + b + c;
}
}
1. return type show be void
2. can't override a final method.
(c)(3pts)
No access to private members.
(d)(6pts)
import java.awt.Graphics;
import java.awt.Color;
public class ColorTest extends javax.swing.JFrame {
public void paint(Graphics g) {
Color color= new Color(1.5f, 1.5f, 1.5f);
g.setColor(color);
g.drawRect(0,0,50,50);
}
}
Question 3
public class Question3 {
// here is a partial list of java keywords, I assume this set is ALL the java keywords
static String[] javaKeywords= {"if", "else", "while", "do", "switch",
"case", "int", "float", "double", "char"};
public static boolean isJavaIdentifier(String s) {
if(!(Character.isLetter(s.charAt(0)) || (s.charAt(0) =='_')
|| (s.charAt(0) == '$'))) {
return false;
}
for(int i = 1; i < s.length(); i++) {
if(!(Character.isLetter(s.charAt(i)) || (s.charAt(i) =='_')
|| (s.charAt(i) == '$') || Character.isDigit(s.charAt(i)))) {
return false;
}
}
for(int i = 0; i < javaKeywords.length; i++ ) {
if(s.equals(javaKeywords[i])) {
return false;
}
}
return true;
}
// for testing
public static void main(String[] args) {
System.out.println(isJavaIdentifier(args[0]));
}
}