急!!!!!!!!JAVA Program about index

来源:百度知道 编辑:UC知道 时间:2024/05/28 18:40:37
Suppose the following code is created:
Code code=new Code("ABCdef123ghi456jklMNO");
The following sequence of method calls results in the instance variable myCode having the indicated values.

code.hide(2,7); ABXXXXX23ghi456jklMNO
code.recover(5,9); ABXXXf123ghi456jilMNO

a Thinking ahead to implementing the hide and recover methods, declare in this part any additional instance variables you will need and any additional instance variables you will need and any additional code you may need in the constructor.

// instance variables
private String myCode;

public Code(String code)
{
myCode=code;

}

b implement the hide method.
public void hide(int p1, int p2)
{
//to be implemented
}
c implement the recover method.
public void recover (int p1, int p2)
{
//to be implemented
}

public class Code {
public static void main(String[] args) {
Code code = new Code("ABCdef123ghi456jklMNO");
System.out.println("原 码:"+code);
code.hide(2,7);
System.out.println("掩码后:"+code);
code.recover(5,9);
System.out.println("去码后:"+code);
}

private char mask = 'X';
private String code;
private String copy;

public Code(String code){
this.code=code;
this.copy=code;
}

//参数不当,可能会抛出异常:StringIndexOutOfBoundsException
public void hide(int p1,int p2){
try{
if(p1>p2){
p1^=p2;p2^=p1;p1^=p2;
}
String tmp = "";
while(tmp.length()<p2-p1)tmp+=mask;
code = code.substring(0,p1)+tmp+code.substring(p2);
}catch(Exception e){e.printStackTrace();}
}

//参数不当,可能会抛出异常:StringIndexOutOfBoundsException
public void recover(int p1