求大神帮忙。一直没搞明白怎么错了
求各位大神指导一下怎么错了,然后如何去改? 谢谢了
1. Your PhoneNumber class has and only has 3 int variables in its data field (please do not add any other data variables):
int areacode;
int number;
int ext;
2. Your PhoneNumber class has 4 constructors with the specifications as below:
if no argument is given, all data fieds (areacode, number, ext) are initialized to zero;
if one int argument is given, the constructor initializes number by using this argument, and set default areacode to 216, and default ext to 0.
if three int arguments are given, initialize areacode, number, ext accordingly.
if one String argument "xxx-xxxxxxx" is given, where the first three characters are numbers and the last 7 characters are either numbers or letters, e.g., "800-GOFEDEX", convert the String to numbers and initialize the data field accordingly. Note the ext should be initialized to 0.
Your program can assume the String is delimited by "-". If the character is a letter, your program should convernt it to a number according to the key pad standard. If the character is neither a number nor a letter, your contrustor will output an error message and initialize the data fileds to zeros.
Hint: given a String, you may use charAt(index) method to extract the character one by one. The decoding method that converts a letter to the corresponding number is provided. In addition, you may find the following methods useful:
Character.isLetterOrDigit(char c): returns either true or false.
Character.isDigit(char c): returns either true or false.
String.valueOf(char digit): convert a character to a String.
String.valueOf(int number): convert an int to a String.
Integer.parseInt(String str): convert a String to an int.
3. Your PhoneNumber provides a method equals() which compares the current object to another PhoneNumber object.
4. Your PhoneNumber provides a method toString() which outputs a String format of the object.
1. Your PhoneNumber class has and only has 3 int variables in its data field (please do not add any other data variables):
int areacode;
int number;
int ext;
2. Your PhoneNumber class has 4 constructors with the specifications as below:
if no argument is given, all data fieds (areacode, number, ext) are initialized to zero;
if one int argument is given, the constructor initializes number by using this argument, and set default areacode to 216, and default ext to 0.
if three int arguments are given, initialize areacode, number, ext accordingly.
if one String argument "xxx-xxxxxxx" is given, where the first three characters are numbers and the last 7 characters are either numbers or letters, e.g., "800-GOFEDEX", convert the String to numbers and initialize the data field accordingly. Note the ext should be initialized to 0.
Your program can assume the String is delimited by "-". If the character is a letter, your program should convernt it to a number according to the key pad standard. If the character is neither a number nor a letter, your contrustor will output an error message and initialize the data fileds to zeros.
Hint: given a String, you may use charAt(index) method to extract the character one by one. The decoding method that converts a letter to the corresponding number is provided. In addition, you may find the following methods useful:
Character.isLetterOrDigit(char c): returns either true or false.
Character.isDigit(char c): returns either true or false.
String.valueOf(char digit): convert a character to a String.
String.valueOf(int number): convert an int to a String.
Integer.parseInt(String str): convert a String to an int.
3. Your PhoneNumber provides a method equals() which compares the current object to another PhoneNumber object.
4. Your PhoneNumber provides a method toString() which outputs a String format of the object.
public class phonenumber{
private int areacode;
private int number;
private int ext;
phonenumber(){
areacode=0;
number=0;
ext=0;
}
phonenumber(int newnumber){
areacode=216;
newnumber=number;
ext=0;
}
phonenumber(int newareacode, int newnumber, int newext){
newareacode=areacode;
newnumber=number;
newext=ext;
}
phonenumber(String newnumber){
ext=0;
String[] str=newnumber.split("-");
StringBuilder nun=new StringBuilder();
for(int i=0; i<7; i++){
char c=str[1].charAt(i);
if (!((c <=90 && c>=65)||(c>=48 && c<=57))){
invalid=true;
return;
}
if(c<=90 && c>=65){
nun.append(decode(c));
}else
{nun.append(c);}
}
number=Integer.parseInt(nun.toString());
areacode=Integer.parseInt(str[0]);
}
private String decode(char c){
switch (c){
case 'A': case 'B': case 'C': return "2";
case 'D': case 'E': case 'F': return "3";
case 'G': case 'H': case 'I': return "4";
case 'J': case 'K': case 'L': return "5";
case 'M': case 'N': case 'O': return "6";
case 'P': case 'Q': case 'R': case 'S': return "7";
case 'T': case 'U': case 'V': return "8";
case 'W': case 'X': case 'Y': case 'Z': return "9";
default:return "";
}}
public boolean equals(phonenumber pn){
return (areacode==pn.areacode && number==pn.number && ext==pn.ext);
}
public String toString(){
if(ext==0){
return areacode+"-"+number;
}else{
return areacode+"-"+number+" ext "+ext;
}
}}public class phone {
public static void main (String[] args) {
phonenumber mynumber = new phonenumber("800-GOFEDEX");
System.out.println("the number is: " + mynumber);
phonenumber mynumber2 = new phonenumber("800-46FEDEX");
System.out.println("the number is: " + mynumber2);
phonenumber mynumber3 = new phonenumber("800-&*(^&%@");
System.out.println("the number is: " + mynumber3);
mynumber = new phonenumber(800, 1234567, 123);
System.out.println("the number is: " + mynumber);
mynumber2 = new phonenumber(7654321);
System.out.println("the number is: " + mynumber2);
if (mynumber.equals(mynumber2))
System.out.println("mynumber and mynumber2 are the same");
else
System.out.println("mynumber and mynumber2 are different");
mynumber = new phonenumber();
System.out.println("the number is: " + mynumber);
}
}
...全文
相关推荐
一张图让你搞明白C#中DataSet如何使用
一张图让你搞明白C#中DataSet如何使用
1天可以完全搞明白什么是单片机
1天可以完全搞明白什么是单片机1天可以完全搞明白什么是单片机
没搞明白的JAva自动程序代码.rar
一个没怎么搞明白的JAva自动程序代码,真的没搞明白,不知道是如何自动登录,代码也是前些年写的,觉得有意思的就下载源码编译吧。
搞明白差模噪声与共模噪声
电磁干扰EMI大致可分为“传导噪声”和“辐射噪声”两种。其中,传导噪声根据传导方式可分为“差模(常模)噪声”和“共模噪声”两种。本文将对这两种噪声进行介绍。
3分钟搞明白信用评分卡模型
信用评分卡模型在国外是一种成熟的预测方法,尤其在信用风险评估以及金融风险控制领域更是得到了比较广泛的使用,其原理是将模型变量WOE编码 方式离散化之后运用logistic回归模型进行的一种二分类变量的广义线性模型。


Java SE

