Docsity

Java Code Snippets and Explanations for CS100J Prelim I, Exams of Computer Science

Toggle sidebar
bg1
6
1. /** See Prelim for the spec.*/
public static String fixZip(String s) {
if (s.indexOf("Cornell") == -1) {
return s;
}
int k= s.indexOf("14850");
if (k == -1) {
return s;
}
return s.substring(0,k) + "14853" +
s.substring(k+5);
}
2a.
s _null__
m(String) setX(int)
c1
P
5a. Parameter: a variable that is declared within the
parentheses of a method header.
5b. 1. Create (draw) a new manila folder (object) of the
class, in this case, C. 2. Execute the constructor call in the
new-expression, in this case, C(5, 3). 3. Yield as value of the
new-expression he name (on the tab) of the new object.
5c. Evaluate the <expression> and store its value in the
<variable>.
5d. Local variable: a variable declared in a method body. Its
scope is the sequence of statements that follow the
declaration, until the end of the block.
5e. return <expression>;.
3a. Name, netId, Student, getName, getNetId,
toString, equals (you do not have to remember
this one; it is in class Object).
3b. toString.
3c. "Johnny, JD123, A&S".
3d. /** An instance: info about a freshman */
public class Freshman extends Undergrad
private int APcredits; // no. of AP credits
/** Constructor: a freshman named n with
netId id, in college c, and with x AP credits */
public Freshman(String n, String id,
String c, int x) {
super(n, id, c);
APcredits= x;
}
/** = number of APCredits of this student */
public int getAPCredits() {
return APcredits;
}
/** = set the number of APCredits to ap */
public void setAPCredits(int ap) {
APcredits= ap;
}
/** = a representation of this student */
public String toString() {
return super.toString() + ", " +
APcredits + " AP credits";
}
}
CS100J Prelim I answers Netid _________ Name __________________
4. Below is the output. We also show the final
state of variables b, c, d.
d (0, 5)
b (9, 5)
c (6, 7)
d (9, 5)
b (0, 0)
d (9, 5)
s _null__
m(String) setX(int)
c2
P
S
two __null__
one __c3__
s _null__
m(String) setX(int)
c3
P
2b.
a0
x
y
Point
9
5
ba2
ca1
da0
a1
x
y
Point
6
7
a2
x
y
Point
0
0

Partial preview of the text

Download Java Code Snippets and Explanations for CS100J Prelim I and more Exams Computer Science in PDF only on Docsity!

1. /** See Prelim for the spec.*/

public static String fixZip(String s) {

if (s.indexOf("Cornell") == -1) {

return s;

int k= s.indexOf("14850");

if (k == -1) {

return s;

return s.substring(0,k) + "14853" +

s.substring(k+5);

2a.

s _ null __

m(String) setX( int )

c

P

5a. Parameter: a variable that is declared within the

parentheses of a method header.

5b. 1. Create (draw) a new manila folder (object) of the

class, in this case, C. 2. Execute the constructor call in the

new-expression, in this case, C(5, 3). 3. Yield as value of the

new-expression he name (on the tab) of the new object.

5c. Evaluate the < expression > and store its value in the

< variable >.

5d. Local variable: a variable declared in a method body. Its

scope is the sequence of statements that follow the

declaration, until the end of the block.

5e. return < expression >;.

3a. Name, netId, Student, getName, getNetId,

toString, equals (you do not have to remember

this one; it is in class Object).

3b. toString.

3c. "Johnny, JD123, A&S".

3d. /** An instance: info about a freshman */

public class Freshman extends Undergrad

private int APcredits; // no. of AP credits

/** Constructor: a freshman named n with

netId id, in college c, and with x AP credits */

public Freshman(String n, String id,

String c, int x) {

super (n, id, c);

APcredits= x;

/** = number of APCredits of this student */

public int getAPCredits() {

return APcredits;

/** = set the number of APCredits to ap */

public void setAPCredits( int ap) {

APcredits= ap;

/** = a representation of this student */

public String toString() {

return super .toString() + ", " +

APcredits + " AP credits";

CS100J Prelim I answers Netid _________ Name __________________

4. Below is the output. We also show the final

state of variables b, c, d.

d (0, 5)

b (9, 5)

c (6, 7)

d (9, 5)

b (0, 0)

d (9, 5)

s _ null __

m(String) setX( int )

c

P

S

two __ null __

one c3

s _ null __

m(String) setX( int )

c

P

2b.

a

x

y

Point

b a

c a

d a

a

x

y

Point

a

x

y

Point