教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 求职职场 >

java程序设计教程(第六版)课后习题答案(2)

来源:网络收集 时间:2026-09-08
导读: sphere1.setDiameter(scan.nextDouble()); System.out.println(\n + 更改过的sphere1: + sphere1); } } ////////////////////////////////////////////////////////// PP 4.2 方法1 import java.util.Scanner; publ

sphere1.setDiameter(scan.nextDouble());

System.out.println("\n" + "更改过的sphere1: " + sphere1);

}

}

//////////////////////////////////////////////////////////

PP 4.2

方法1

import java.util.Scanner;

public class CreateDog {

/**

* @param args

*/

public static void main(String[] args) {

// TODO 自动生成方法存根 System.out.println("请输入狗的年龄age:"); Scanner scan=new Scanner(System.in); int age=scan.nextInt(); System.out.println("请输入狗的姓名name:"); Scanner scan1=new Scanner(System.in); String name=scan1.next(); Kennel AGE=new Kennel(age); AGE.Age(); System.out.println(AGE.toString());

System.out.println("狗的名字是:" +name);

}

}

public class Kennel{

final int mul=7;

int age;

int agep;

char name;

Kennel(int age){

this.age=age;

=name; } public void Age(){ agep=age*mul; } public String toString(){ String s=""; String r1=Integer.toString(age); String r2=Integer.toString(agep);

s=("狗的年龄为:"+r1+" 对应人的年龄为:"+r2);

return s;

}

}

方法2

class Dog1

{

String name;

int age;

public Dog1()

{

name="heizi";

age=1;

}

public Dog1(String n, int a )

{

name=n;

age=a;

}

public int DogAge(int Age)

{

int age=Age/7;

return(age);

}

public void setName(String name1)

{

name=name1;

}

public String getName()

{ return(name); } public String toString() { Dog1 d=new Dog1();

return("这只狗叫"++"已经有"+d.age+"岁了。");

}

}

public class Kennel {

public static void main(String[] args) {

Dog1 dog1=new Dog1();

System.out.println("dog1的 name="+); System.out.println("dog1的 age="+dog1.age); Dog1 dog2=new Dog1("xiaobai",2); System.out.println("dog2的 name="+); System.out.println("dog2的age="+dog2.age); dog1.setName("diandian"); dog1.getName();

System.out.println("dog1的 name="+);

int age=dog1.DogAge(35);

System.out.println("age="+age);

String r=dog1.toString();

System.out.println("r="+r);

}

}

//////////////////////////////////////////////////////////

PP 4.4

方法1

public class Book {

/**

* @param args

*/

public static void main(String[] args) {

// TODO 自动生成方法存根 String info = null; Bookshelf book; Bookshelf INFO=new Bookshelf(info); INFO.BOOK(); System.out.println(INFO.toString());

}

public class Bookshelf {

String bookname;

String author;

String publish;

String date;

String books;

String info;

public Bookshelf(String info){

bookname="JAVA";

author="qwe";

publish="Tsing HUA";

date="19890322";

=info;

}

public String BOOK(){

info=bookname+author+publish+date;

return info;

}

public String toString(){

String bookinfo;

}

bookinfo="书名:"+bookname+" 作者:"+author+"\n"; bookinfo+="出版社:"+publish+" "+"出版日期:"+date; return bookinfo; }

方法2

class Book

{

String title,author,publisher,copyright;

Book()

{

title="JAVA 编程语言设计"; author="John Lewis"; publisher="电子工业出版社"; copyright="xuesheng";

Book(String t,String a,String p,String c)

{

title= t;

author=a;

publisher=p;

copyright=c;

}

void setBook(String t1,String a1,String p1,String c1)

{

title= t1;

author=a1;

publisher=p1;

copyright=c1;

}

String getBookTitle()

{

return(title);

}

String getBookAuthor()

{

return(author);

}

String getBookPublisher()

{

return(publisher);

}

String getBookCopyright()

{

return(copyright);

}

public String toString()

{

Book bo=new Book();

return ("这本名叫"+bo.title+" 书,作者是"+bo.author+" 出版社是"+bo.publisher+" 版权归"+bo.copyright+"所有");

}

}

public class Bookshelf {

public static void main(String[] args) {

Book book=new Book();

System.out.println(book.title+" "+book.author+" "+book.publisher+" "+book.copyright);// TODO Auto-generated method stub

Book book1=new Book("C语言程序设计","谭浩强","清华出版社","zuozhe");

System.out.println(book1.title+" "+book1.author+" "+book1.publisher+" "+book1.copyright);

book.setBook("JAVA 语言设计","William Loftus","西安电子出版社","sheng");

book.getBookTitle();

book.getBookAuthor();

book.getBookPublisher();

book.getBookCopyright();

System.out.println(book.title+" "+book.author+" "+book.publisher+" "+book.copyright);

String str=book.toString();

System.out.print("str="+str);

}

}

//////////////////////////////////////////////////////////

PP4.5

class Flight{

String name, origin,destination;

int number;

public Flight()

{

name="西安国际机场"; origin="西安"; destination="北京"; number= …… 此处隐藏:3527字,全部文档内容请下载后查看。喜欢就下载吧 ……

java程序设计教程(第六版)课后习题答案(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/121536.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)