Free Response Test #1 Name



Download 19.42 Kb.
Date28.01.2017
Size19.42 Kb.
#9511

AP Computer Science

Free Response Test #1 Name:_______________________

Using the String class methods;

String name = “Wangler”;
System.out.println(name.length( )); //prints the number 7

System.out.println(name.substring(0, 3); //prints the string “Wan”

System.out.println(name.substring(4); //prints the string “ler”
Notes:


  • Assume that declarations of variables and methods appear within the context of an enclosing class.

  • Assume that method calls that are not prefixed with an object or class name and are not shown within a complete class definition appear within the context of an enclosing class.

  • Unless otherwise noted in the question, assume that parameters in method calls are not null.

Assume that the Name class, which is partially defined below, is used to represent people’s first names.


public class Name{

private String myName;

public Name(String s) { //constructor

myName = s;

}

public int length( ) {



return myName.length( );

}

public String prefix(int k) {



/* part (a) */

}

public String suffix(int k) {



/* part (b) */

}

public boolean isNickname(String nick) {



Name n = new Name(nick);

/* part (c) */

}

}


Part (a)

Write the prefix method of the Name class. The prefix method should return a string containing the first k characters in the name. If the name has fewer than k characters, the prefix method should return the entire string.


For example, assume that name N represent the name “Bill”. Below are some examples of calls to N’s prefix method.
k Result of the call N.prefix(k)

0 “”


1 “B”

2 “Bi”


3 “Bil”

4 “Bill”


5 “Bill”
Complete method prefix below.
//precondition: k >= 0

//postcondition: returns a string containing the first k letters in myName; if

// myName has fewer than k letters, returns the whole name
public String prefix(int k){

Part (b)

Write the suffix method of the Name class. The suffix method should return a string containing the last k characters in the name. If the name has fewer than k characters, the suffix method should return the entire string.


For example, assume that name N represents the name “Bill”. Below are some example of calls to N’s suffix method.
k Result of the call N.suffix(k)

0 “”


1 “l”

2 “ll”


3 “ill”

4 “Bill”


5 “Bill”
Complete the method suffix below.
//precondition: k >= 0

//postcondition: returns a string containing the last k letters in myName; if

// myName has fewer than k letters, returns the whole name
public String suffix(int k) {

Part (c)

Write the isNickname method of the Name class. This isNickname method should return true if and only if its parameter nick is made up of two parts:



  1. A nonempty string that is a prefix of myName

  2. The suffix “ie”

Below are some example



Value return by the call

Name represented by N Name represented by nick N.isNickname(nick)____

Susan Susie true

David Davie true

Ann Annie true

Susan Sus false

Ann Robbie false

David Davy false
In writing method isNickname, you may include calls to methods prefix and suffix. Assume that both methods work as specified, regardless of what you wrote in parts (a) and (b).
Complete method isNickname below.

//precondition: nick is not null


public boolean isNickname(String nick) {

Name n = new Name(nick);
Download 19.42 Kb.

Share with your friends:




The database is protected by copyright ©ininet.org 2024
send message

    Main page