打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
关于第5章的全部习题答案总结!!! - 《JAVA开发实战经典》答疑专区 - 魔乐MLDN java学习社区-魔乐科技 JAVA论坛|JAVA培训论坛|MLDN论坛|MLDN社区|MLDN视频下载 -JAVA学习论坛
关于第5章的全部习题答案总结!!!
习题
一个菜鸟的作业
以下是我对面向对象基础课后作业理解,因为本人是JAVA菜鸟。以下的程序可能有很多不到的地方,希望高手能给出指点,希望交到更多喜欢Java的朋友,还有非常感谢李老师的编的这本书,让我有了梦想的追求。我准备明年的上半年去北京培训,有一起的朋友可以QQ183588002。现在做的就是努力预习好课程。
1.
编写并测试一个代表地址的Address类,地址信息由国家、省份、城市、街道、邮编组 成、并可以返回完整的地址信息。
class Address{
//定义地址类
private String country;
//国家信息
private String pro;
//省份信息
private String city;
//城市信息
private String street;
//街道信息
private String zipcode;
//邮编信息
/*
设置构造初始化
*/
public Address(String country,
String pro,String city,String street,String zipcode){
this.setCountry(country);
this.setPro(pro);
this.setCity(city);
this.setStreet(street);
this.setZipcode(zipcode);
}
/*
通过一系列的get和set方法来获得属性
*/
public void setCountry(String country){
this.country=country;
}
public String getCountry(){
return country;
}
public void setPro(String pro){
this.pro=pro;
}
public String getPro(){
return pro;
}
public void setCity(String city){
this.city=city;
}
public String getCity(){
return city;
}
public void setStreet(String street){
this.street=street;
}
public String getStreet(){
return street;
}
public void setZipcode(String zipcode){
this.zipcode=zipcode;
}
public String getZipcode(){
return zipcode;
}
public String getInfo(){
//取得信息
return
"地址信息:" + "\n" +
"\t|- 国家:" + this.getCountry() + "\n" +
"\t|- 省份:" + this.getPro() + "\n" +
"\t|- 城市:" + this.getCity() + "\n" +
"\t|- 街道:" + this.getStreet() + "\n" +
"\t|- 邮编:" + this.getZipcode() ;
}
}
public class AddressDemo{
//测试地址类
public static void main(String args[]){
Address add=new Address("中国","江苏","南京","江浦街道","211800");
System.out.println(add.getInfo());
}
}
2.
定义并测试一个代表员工的Employ类。员工的属性包括:编号、姓名、基本薪水、薪水增长额、还包括计算薪水增长额及计算增长后的工资总额的操作方法。
class Employee{
/*
封装所有的属性
*/
private String number;
private String name;
private float basicSalry;
private float rate;
public Employee(){}
//定义一个无参的构造
//定义一个有参的构造
public Employee(String number,String name,float basicSalry,float rate){
this.setNumber(number);
this.setName(name);
this.setBasicSalry(basicSalry);
this.setRate(rate);
}
//设置一系列的get和set方法访问被封装的属性
public void setNumber(String number){
this.number=number;
}
public String getNumber(){
return number;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return name;
}
public void setBasicSalry(float basicSalry){
this.basicSalry=basicSalry;
}
public float getBasicSalry(){
return basicSalry;
}
public void setRate(float rate){
this.rate=rate;
}
public float getRate(){
return rate;
}
public float sumSalry(){
return basicSalry*(1+rate);
}
public String getInfo(){
return "员工信息:" + "\n" +
"\t|- 编号:" + this.getNumber() + "\n" +
"\t|- 姓名:" + this.getName() + "\n" +
"\t|- 基本薪水:" + this.getBasicSalry() + "\n" +
"\t|- 薪水增长额:" + this.getRate() + "\n" +
"\t|- 总薪水:" + this.sumSalry() ;
}
}
public class EmployeeDemo{//测试员工类
public static void main(String args[]){
Employee emp1= new Employee("1032","张三",850.0f,0.2f);
System.out.println(emp1.getInfo());
}
}
3.
编写程序,统计出字符串 “want you
to know one thing”中字母n和字母o出现的次数。
public class StringDemo{
public static void main(String args[]){
String youStr="want you to know one thing";
char s[] =youStr.toCharArray();
int n=0;
int o=0;
for(int i = 0;i<youStr.length();i++){
if (s=='n'){
n++;
}
if(s=='o'){
o++;
}
}
System.out.println("你的字符串中出现n的次数是"+n);
System.out.println("你的字符串中出现o的次数是"+o);
}
}
4.设计一个Dog类,有名字、颜色、年龄等属性,定义构造方法来初始化类的这些属性,定义方法输出Dog信息,编写应用程序使用Dog类。
class Dog {
 
private String name;
 
private String age;
 
private String color;
 
public Dog(String name, String age, String color) {
 
this.name = name;
 
this.age = age;
 
this.color = color;
 
}
 
public String getName() {
 
return
this.name;
 
}
 
public String getAge() {
 
return
this.age;
 
}
 
public String getColor() {
 
return
this.color;
 
}
 
public String getInfo() {
 
return
"狗的信息:" + "\n" + "\t" + "名字:" + this.getName() + "\n" + "\t"
 
+ "年龄:" + this.getAge() + "\n" + "\t" + "颜色:" + this.getColor();
 
}
 
}
 
public
class DogDemo {
 
public
static
void main(String[] args) {
 
Dog d1 = new Dog("泥泥", "13", "黄色");
 
System.out.println(d1.getInfo());
 
}
 
}
5.设计一个表示用户的User类,类中变量有用户名,口令,和记录用户个数的变量,定义类的3个构造方法(无参,为用户赋值,为用户名和口令赋值),获取和设置口令的方法,和返回信息的方法。
class User{
private String name;
private String password;
private static int num=0;
public User(){
num++;
System.out.println("产生了第"+num+"对象");
}
public User(String name){
this();
this.name=name;
}
public User(String name,String password){
this(name);
this.setPassword(password);
}
public void setPassword(String password){
this.password=password;
}
public String getPassword(){
return password;
}
public String getInfo(){
return "用户信息是:"+"\n"+
"\t姓名:"+this.name+
"\t密码:"+this.getPassword();
}
}
public class UserDemo{
public static void main(String args[]){
User u1=new User("张三","123");
System.out.println(u1.getInfo());
User u2=new User("张四","113");
System.out.println(u2.getInfo());
User u3=new User("张五","133");
System.out.println(u3.getInfo());
User u4=new User("张六","143");
System.out.println(u4.getInfo());
User u5=new User("张七","153");
System.out.println(u5.getInfo());
User u6=new User("张八","163");
System.out.println(u6.getInfo());
}
}
6.关于字符串的操作。
(1).从字符串"java技术学习班20070326"中提取开班日期
解答:和最后一题类似
(2).将"MLDN JAVA"字符串中的"java"替换成"J2EE"
class
StringDemo1{
public static void main(String[] args) {
String str="MLDN JAVA";
String str1=str.replace("JAVA","J2EE");
System.out.println("替换后的结果是:"+str1);
}
}
(3).取出"java技术学习班20070326"中的第八个字符.
class
StringDemo1{
public static void main(String[] args) {
String str="java技术学习班20070326";
System.out.println(str.charAt(8));
}
}
(4).清除"java技术学习班20070326"中的所有0.
public class NewClass {
public static void main(String s[]){
String str="java技术学习班20070326";
char[] cs = str.toCharArray();
String nowChar = "0";
String a = "";
for(int i = 0 ;i<cs.length;i++){
if(nowChar.indexOf((int)cs) == -1){
a += String.valueOf(cs);
}
}
System.out.println(a);
}
}
(5).清除"java技术学习班20070326 MLDN 老师"中所有的空格.
public class NewClass {
public static void main(String[] args) {
String q="java技术学习班20070326 MLDN 老师";
String c[]=q.split(" ");
String j="";
for(int i=0;i<c.length;i++){
//System.out.println(c);
j+=c;
}
System.out.println(j);
}
}
实现方式2:
public class NewClass2{
public static void main(String s[])
{
String str="java技 术学习 班2007 03 26";
str = str.replaceAll(" ", "");
System.out.println(str);
}
}
(6).从任意给定的身份证号码中提取此人的出生日期.
public class NewClass2{
public static void main(String s[]) {
String str="320122198501103616";
str = str.substring(6, 13);
System.out.println(str);
}
}
7.编写一个公司员工类.数据成员包括:员工号、姓名、薪水、部门。利用构造设置以下信息
单参:只传递员工工号,则员工姓名:无名氏,薪水:0,部门:为定。
双参:传递员工号、姓名,则员工薪水为1000,部门:后勤。
4参:传递员工号、姓名、部门、薪水。
无参:则均为空值。
要求:显示信息。
class Employee{
private String empno ;
private String name ;
private float salary ;
private String dept ;
public Employee(){}
public Employee(String empno){
this(empno,"无名氏",0.0f,"未定") ;
}
public Employee(String empno,String name){
this(empno,name,1000,"后勤") ;
}
public Employee(String empno,String name,float salary,String dept){
this.setEmpno(empno) ;
this.setName(name) ;
this.setSalary(salary) ;
this.setDept(dept) ;
}
public void setEmpno(String empno){
this.empno = empno ;
}
public void setName(String name){
this.name = name ;
}
public void setSalary(float salary){
this.salary = salary ;
}
public void setDept(String dept){
this.dept = dept ;
}
public String getEmpno(){
return this.empno ;
}
public String getName(){
return this.name ;
}
public float getSalary(){
return this.salary ;
}
public String getDept(){
return this.dept ;
}
};
public class TestEmployeeDemo{
public static void main(String argsp[]){
Employee emp = new Employee("1001","张三",1000,"技术部") ;
System.out.println(emp.getDept()) ;
}
};
8.构造一个银行帐户类,类的构成包括如下信息:
(1).数据成员用户的帐户名称,用户的帐户余额.
(2).方法包括开户(设置帐户名称及余额),利用构造方法完成.
(3).查询余额.
class Bank{
private String name;
private float odd;
public Bank(String name,float odd){
this.setName(name);
this.setOdd(odd);
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setOdd(float odd){
this.odd=odd;
}
public float getOdd(){
return this.odd;
}
}
public class BankDemo{
public static void main(String args[]){
Bank b=new Bank("张三",100);
System.out.println(b.getName()+"的余额是"+b.getOdd());
}
}
9.声明一个图书类,其数据成员为书名、编号、(利用静态变量实现自动编号)、书价,并拥有静态数据成员册数、记录图书的总册书,在构造方法中利用此静态变量为对象的编号赋值,在主方法中第一对象数组,并求出总册数。
class Book{
private String title;
private static int count=0;
private float price;
public Book(){
count++;
System.out.println("图书的总数为"+count);
}
public Book(String title,float price){
this();
this.title=title;
this.price=price;
}
public String getTitle(){
return title;
}
public float getPrice(){
return price;
}
}
public class BookDemo{
public static void main(String args[]){
Book b[]={new Book("java",90.0f),new Book("javaweb",77.0f)};
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Java 关键字static详解
Java反射获取类和对象信息全解析 – 码农网
Java中this关键字的使用
查漏补缺,超详细的Java基础知识点总结,快看哪些你还不会?
最全的Java笔试题库之选择题篇-总共234道【1~60】
java去除字符串中的空格、回车、换行符、制表符
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服