1.编写并测试一个代表地址的Address类,地址信息由国家,省份,城市,街道,邮编组成,并可以返回完整的信息:
这道题就是考我们一个toString的覆写和构造方法的初始化
public
class
Address {
private
String
country
;
private
String
city
;
private
String
street
;
private
String
province
;
private
String
postcode
;
@Override
public
String toString() {
return
"国家:"
+
this
.
country
+
"\n"
+
" 省份:"
+
this
.
province
+
"\n"
+
" 城市:"
+
this
.
city
+
"\n"
+
" 街道:"
+
this
.
street
+
"\n"
+
"邮编:"
+
this
.
postcode
;
}
public
Address(String country, String city, String street, String province,
String postcode) {
this
.
country
= country;
this
.
city
= city;
this
.
street
= street;
this
.
province
= province;
this
.
postcode
= postcode;
}
public
static
void
main(String[] args) {
Address address=
new
Address(
"中国"
,
"成都"
,
"天府大道"
,
"四川"
,
"123456"
);
System.
out
.println(address);
}
}
结果:
国家:中国
省份:四川
城市:成都
街道:天府大道
邮编:123456
2.定义并测试一个代表员工的Employee类。员工属性包括“编号”,“姓名”,“基本薪水”,“薪水增长额”,还包括计算薪水增长额及计算增长后的工资总额的操作方法
public
class
Employee {
private
String
number
;
private
String
name
;
private
double
salary
;
private
double
add
;
public
double
add(
double
salary,
double
add){
return
(salary+salary*add);
}
@Override
public
String toString() {
return
"姓名:"
+
this
.
name
+
"\n"
+
"编号"
+
this
.
number
+
"\n"
+
"基本工资:"
+
this
.
salary
+
"\n"
+
"工资增长度"
+
this
.
add
+
"\n"
+
"增长后的工资:"
+
this
.add(
this
.
salary
,
this
.
add
);
}
public
Employee(String number, String name,
double
salary,
double
add) {
this
.
number
= number;
this
.
name
= name;
this
.
salary
= salary;
this
.
add
= add;
}
public
static
void
main(String[] args) {
Employee employee=
new
Employee (
"123"
,
"like"
, 10000.0, 0.2);
System.
out
.println(employee);
}
}
结果:
姓名:like
编号123
基本工资:10000.0
工资增长度0.2
增长后的工资:12000.0
3.编写程序,统计出字符串“want you to know one thing”中字母n和字母o出现的次数
public
class
StringTest {
public
static
void
main(String[] args) {
String str=
"want you to know one thing"
;
char
a[]=str.toCharArray();
int
num=0;
int
num1=0;
for
(
int
i=0;i
length
;i++){
if
(a[i]==
'n'
){
num++;
}
if
(a[i]==
'o'
){
num1++;
}
}
System.
out
.println(
"n的个数:"
+num);
System.
out
.println(
"o的个数"
+num1);
}
}
结果:
n的个数:4
o的个数4
4.4题和1题一样,就不做了
5.5题就多了个getter,setter方法,也不做了
6.字符串操作:
(1)从字符串“Java技术学习班20070326”中提取开班日期;
(2)将字符串“MLDN JAVA”字符串中的java替换成“j2ee”;
(3)取出“Java技术学习班20070326”中第8个字符
(4)清除“Java技术学习班20070326”中所有的0
(5)清除“Java 技术学习班 20070326 MLDN 老师”中的所有空格
(6)从任意给的身份证中提取出此人的出生日期
public
class
StringTest {
public
static
void
main(String[] args) {
String str=
"Java技术学习班20070326"
;
System.
out
.println(str.substring(9));
String str1=
"MLDN JAVA"
;
System.
out
.println(str1.replace(
"JAVA"
,
"J2EE"
));
System.
out
.println(str.substring(7, 8));
System.
out
.println(str.charAt(7));
System.
out
.println(str.replace(
"0"
,
""
));
String str3=
"Java 技术学习班 20070326 MLDN 老师"
;
System.
out
.println(str3.replace(
" "
,
""
));
String str4=
"513822191111111888"
;
System.
out
.println(str4.substring(6,14));
}
}
结果:
20070326
MLDN J2EE
习
习
Java技术学习班27326
Java技术学习班20070326MLDN老师
19111111
7.编写一个员工类:
数据成员:员工号,姓名,工资,部门
方法:
利用构造方法来完成:
单参:只传递员工号,则员工姓名:无名氏,薪水:0,部门:未定
双参:传递员工号,姓名,薪水:1000,部门:后勤
4参:传递员工号,姓名,薪水,部门
无参:则为空值
要求显示信息:
public
class
Employee {
private
String
number
;
private
String
name
;
private
double
salary
;
private
String
department
;
@Override
public
String toString() {
System.
out
.println(
"--------------------------------------------------------------"
);
return
"姓名:"
+
this
.
name
+
"\n"
+
"编号"
+
this
.
number
+
"\n"
+
"基本工资:"
+
this
.
salary
+
"\n"
+
"部门"
+
this
.
department
;
}
public
Employee(String number, String name) {
this
.
number
= number;
this
.
name
= name;
this
.
salary
=1000;
this
.
department
=
"后勤"
;
}
public
Employee(String number) {
this
.
number
= number;
this
.
name
=
"无名氏"
;
this
.
salary
=0;
this
.
department
=
"未定"
;
}
public
Employee() {
this
.
number
=
null
;
this
.
name
=
null
;
this
.
salary
=0;
this
.
department
=
null
;
}
public
Employee(String number, String name,
double
salary, String department) {
this
.
number
= number;
this
.
name
= name;
this
.
salary
= salary;
this
.
department
= department;
}
public
static
void
main(String[] args) {
Employee employee=
new
Employee (
"123"
);
System.
out
.println(employee);
Employee employee2=
new
Employee (
"123"
,
"like"
);
System.
out
.println(employee2);
Employee employee3=
new
Employee (
"123"
,
"like"
, 10000.0,
"经理"
);
System.
out
.println(employee3);
Employee employee4=
new
Employee ();
System.
out
.println(employee4);
}
}
结果:
--------------------------------------------------------------
姓名:无名氏
编号123
基本工资:0.0
部门未定
--------------------------------------------------------------
姓名:like
编号123
基本工资:1000.0
部门后勤
--------------------------------------------------------------
姓名:like
编号123
基本工资:10000.0
部门经理
--------------------------------------------------------------
姓名:null
编号null
基本工资:0.0
部门null
8.9这里就不错了,和7题差不多的。。有兴趣的可以去做做