My,Dog,Sex

更新时间:2024-02-12 06:49:01 阅读量: 经典范文大全 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

篇一:解析JAVA程序设计第三章课后答案

第3章习题解答

1.如何定义方法?在面向对象程序设计中方法有什么作用?

答:方法的定义包括方法名、方法形参、方法的返回值类型和方法体四部分,方法只能在类中定义。方法是对象的动态特征的描述,对象通过方法操作属性,进而改变对象的状态,完成程序所预期的功能。

2.定义一个Dog类,有名字、颜色、年龄等属性,定义构造方法用来初始化类的这些属性,定义方法输出Dog的信息。编写应用程序使用Dog。

答:

public class Dog{

private String name;

private String color;

private String age;

Dog(String n,String c,String a){

name = n; color = c; age = a;

}

public String toString() {

return name + "," + color + "," + age;

}

public static void main(String args[]) {

Dog dog = new Dog("小白", "白色", "2岁");

System.out.println(dog.toString());

}

}

3.什么是访问控制修饰符?修饰符有哪些种类?它们各有何作用?

答:访问控制修饰符是对类、属性和方法的访问权限的一种限制,不同的修饰符决定了不同的访问权限。访问控制修饰符有

3个:private、protected、public,另外还有一种默认访问权限。各个修饰符的作用如下表所示:

B:包中的类

C:所有子类

D:本类

A:所有类

4.阅读程序,写出程序的输出结果

class A{

private int privateVar;

A(int _privateVar){

privateVar=_privateVar;

}

boolean isEqualTo(A anotherA){

if(this.privateVar == anotherA.privateVar)

return true;

else

return false;

}

}

public class B{

public static void main(String args[]){

A a = new A(1);

A b = new A(2);

System.out.println(a.isEqualTo(b));

}

}

程序的输出结果为: false

5.阅读程序,写出程序的输出结果

public class Test {

public static void main(String[] args) {

int x;

int a[] = { 0, 0, 0, 0, 0, 0 };

calculate(a, a[5]);

System.out.println("the value of a[0] is " + a[0]);

System.out.println("the value is a[5] is " + a[5]);

}

static int calculate(int x[], int y) {

for (int i = 1; i < x.length; i++)

if (y < x.length)

x[i] = x[i - 1] + 1;

return x[0];

}

}

程序的输出结果为:

the value of a[0] is 0

the value is a[5] is 5

6.阅读程序,写出程序的输出结果

public class Test {

public static void main(String[] args) {

String str1 = new String("Java");

String str2 = new String("Java");

System.out.println(str1 == str2);

}

}

程序的输出结果为:

false

7.阅读下列程序,程序中已经指明错误位置,请说出错误原因。

1.

package sample;

class A {

private int num;

A(){

num=0;

}

int get(){ return num; }

}

class Z {

public static void main(String[] args) {

A a1 = new A();

int t = a1.get();

int s = a1.num; //此处有错误

}

}

错误原因:私有变量只能在其所在类中直接使用,在其它类中不可以直接使用。

8.阅读下列程序,程序中已经指明错误位置,请说出错误原因。其中,方法m的功能是把形参的值赋给类的成员变量x。

class Alpha{

private int x;

public void m(int x){

x = x;//此处有错误

}

}

应该修改为:this.x = x;

9.下面定义了一个完整的类,包括有构造方法。阅读这段程序,程序中已经指明错误位置,请说出错误原因。

class Alpha{

private int x;

void Alpha(){ //此处有错误

x = 0;

}

public void getX(){

return x;

}

}

错误原因:构造方法不能有返回类型,也不能以void作为它的返回类型。

10.定义一个名字为MyRectangle的矩形类,类中有4个私有的整型成员变量,分别是矩形的左上角坐标(xUp,yUp)和右下角坐标(xDown,yDown);类中定义了无参数的构造方法和有4个int参数的构造方法,用来初始化类对象。类中还有以下方法:

getW()- 计算矩形的宽度;

getH()- 计算矩形的高度;

area()- 计算矩形的面积;

toString()- 把矩形的宽、高和面积等信息作为一个字符串返回。

编写应用程序使用MyRectangle类。

答:

public class MyRectangle{

private int xUp,yUp,xDown,yDown;

MyRectangle(){

xUp = 0; yUp = 0; xDown = 0; yDown = 0;

}

MyRectangle(x1, y1, x2, y2 ){

xUp = x1; yUp = y1; xDown = x2; yDown = y2;

}

public int getW(){

return xDown - xUp;

}

public int getH(){

return yDown - yUp;

}

public int area(){

return getW() * getH();

}

public String toString() {

return "矩形宽:" + getW() +"矩形高:" + getH() + "矩形面积:"+area(); }

public static void main(String args[]) {

MyRectangle rectangle = new MyRectangle(1,2,7,8);

System.out.println(rectangle.toString());

}

}

11.定义一个表示学生的类Student,包括的成员变量有:学号、姓名、性别、年龄;成员方法有:获得学号、姓名、性别、年龄;修改年龄。并书写Java程序创建Student类的对象及测试其方法的功能。

答:

public class Student{

private String number, name;

private boolean sex; //true表示“男”,false表示“女”

private int age;

Student(){

number = ""; ; sex = true; age = 0

}

Student(String num, String na, boolean s, int a){

number = num; sex = s; age = a;

}

public String getNumber(){

return number;

}

public String getName(){

return name;

}

public boolean getSex(){

return sex;

}

public int getAge(){

return age;

}

public void setAge(int a){

age = a;

}

public String toString() {

return "学号:"+ number +"姓名:"+ name +"性别:"+ sex +"年龄:"+ age;

}

篇二:My dog

My dog

What animal do you like?I like the dog .I have one.Her name is Cici.Her body is brown.She has big ears,round eyes and a short tail. Everyone will have temper,the dog is no excepion.Cici is.If someone met her tail,she will open her mouth to bite him.But she sometimes is very lovely.As long as someone gave her to eat,she will not only licking his hand,will he shook tail.How,Does Cici cute?

篇三:My favourite pet(dog)

英语大作战

英语大作战之作文《My favourite pet》 制作人:痴人笑看事无常 编号:2014510160213 My favourite pet I have a dog. He is my favorite pet. He is very lovely. His name is Mike and he is one year old. His fur is long and white. He has big black eyes. His nose is very good. He can smell very well. He is quite small. He weighs about two kilograms. Mike’s favorite food is meat. He also likes bones.

Mike is very friendly. I feed him every day. He never barks or bites. Mike likes lots of exercise. It is necessary to walk the dog in the park every day if you want it to be healthy. So I play with him every day in the park. Mike likes to run in the park. He often chases cats and birds. It is very interesting. Mike can find the wayback easily. I think he is the cleverest animal of all.

I like my dog and he loves me too. He is very healthy. All my family like him. We look after him very carefully. I’ll make a small and lovely housefor him. I think he will be happy to live there. Do you like my dog?

I have a lovely lttle dog named Dion. He looks pretty with short legs, big ears and short tail. He is my good friend and he is also easy to take care of. I walk him at least twice a day, feed him and spend time with him. He also gives his love to me in return. He is always there to lick me and lie on me. I like playing with him. I think he can tell when I am happy, sad, angry or toubled. Sometimes he can be noisy and run around the room.

In a word, he is not only my dog, but also my friend.

Many people like keeping an animal as a pet.,I like small animals very much, so I have a lovely little dog ,his name is Dion. (I have a lovely little dog named Dion). The dog is brown,he has four short legs,so he looks very pretty (漂亮).He has two big ears and a short tail. He is my good friend and he is also easy to take care of. He is loyal to my families and me,so we all like him. I often takes a walk with him after I finish my work, and I like playing with him ,feeding him and spending time with him. He also gives his love to me in return. He can help my family look after our house.I think he can tell when I am happy, sad, angry or in touble. But sometimes he can be noisy and run around the room. At night when he makes much noise,I can’t sleep well.And he likes holding things in his mouth here and there, In the morning when I get up ,I don’t often find my shoes ,That makes me unhappy.

In a word, he is not only my dog, but also my friend.

本文来源:https://www.bwwdw.com/article/o14b.html

Top