篇一:解析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 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.
篇三:It's been so long since I've had sex I've forgotten who ties up whom.Joan Rivers
It's been so long since I've had sex I've forgotten who ties up whom.Joan Rivers
Sleep is an excellent way of listening to an opera.James Stephens. Anyone who cannot cope with mathematics is not fully human. At best he is a tolerable subhuman who has learned to wear shoes, bathe, and not make messes in the house.Robert Heinlein
Biologically speaking, if something bites you it's more likely to be female. Desmond Morris.
Human beings, who are almost unique in having the ability to learn from the experience of others, are also remarkable for their apparent disinclination to do so.Douglas Adams.
If you're sick and tired of the politics of cynicism and polls and principles, come and join this campaign. George W Bush
They fuck you up, your mum and dad.They may not mean to but they do.They fill up with the faults they hadAnd add some extra, just for you.Philip Larkin
When did I realize I was God? Well, I was praying and I suddenly realized I was talking to myself.Peter O'Toole.
The last time I was in Spain I got through six Jeffrey Archer novels. I must remember to take enough toilet paper next time.Bob Monkhouse. Any sufficiently advanced bug is indistinguishable from a feature.Bruce Brown
Women take clothing much more seriously than men. I've never seen a man walk into a party and say Oh, my God, I'm so embarrassed; get me out of here. There's another man wearing a black tuxedo. Rita Rudner
The British Secret Service was staffed at one point almost entirely by alcoholic homosexuals working for the KGB.Clive James.
Only one man ever understood me, and he didn't understand me.G.W. Hegel. In my house I'm the boss. My wife is just the decision maker.Woody Allen You're only as good as your last haircut.Fran Lebowitz
A lot of people are afraid of heights. Not me, I'm afraid of widths.Steven Wright
I don't know. I don't care. And it doesn't make any difference. Television! Teacher, mother, secret lover.Homer Simpson
Before software can be reusable it first has to be usable.Ralph Johnson I don't think anyone should write their autobiography until after they're dead.Samuel Goldwyn
Coding styles are like assholes, everyone has one and no one likes anyone elses.Eric Warmenhoven
A banker is a fellow who lends you his umbrella when the sun is shining and wants it back the minute it begins to rain.Mark Twain
Mr. Right is coming. But he's in Africa and he's walking.Oprah Winfrey Happiness is having a large, loving, caring, close-knit family in another city.George Burns
He's the kind of man a woman would have to marry to get rid of.Mae West Submitted by Veer sorout Thinking too much can only cause problems Mail your packages early so the post office can lose them in time for Christmas. Johnny Carson.
He taught me housekeeping; when I divorce I keep the house.Zsa Zsa Gabor In ancient times they had no statistics so they had to fall back on lies. Stephen Leacock.
The best doctors in the world are Doctor Diet, Doctor Quiet and Doctor Merryman.Jonathan Swift
Have enough sense to know, ahead of time, when your skills will not extend to wallpapering.Marilyn vos Savant
Women represent the triumph of matter over mind, just as men represent the triumph of mind over morals.Oscar Wilde
Some people think football is a matter of life and death. I assure you, it's much more serious than that. Bill Shankly.
Beer is living proof that God loves us and wants us to be happy.Benjamin Franklin
I knew I was an unwanted baby when I saw that my bath toys were a toaster and a radio.Joan Rivers
All women become like their mothers. That is their tragedy. No man does. That's his. Oscar Wilde.
It's worse than dog eats dog. It's dog doesn't return dog's phone calls.Woody Allen
Mick Jagger is about as sexy as a pissing toad.Truman Capote.
After The Wizard Of Oz I was typecast as a lion, and there aren't all that many parts for lions.Bert Lahr.
If the word 'No' was removed from the English language, Ian Paisley would be speechlessJohn Hume.
Television has brought back murder into the home - where it belongs.Alfred Hitchcock
I do not take drugs. I am drugs.Salvador Dali
A fool and his money are soon elected. Will Rogers.
Submitted by Anonymous You'll never be as good as I think I am.
If the code and the comments disagree, then both are probably wrong.Norm Schryer
By the time a man realizes that maybe his father was right, he usually has a son who thinks he's wrong. Charles Wadsworth.
What could you hope to achieve except to be sunk in a bigger and more expensive ship this timeOn Admiral Mountbatten Winston Churchill
All right everyone, line up alphabetically according to your height.Casey Stengel
Outside of the killings, Washington has one of the lowest crime rates in the country. Mayor Marion Barry, Washington, DC.
[The television is] an invention that permits you to be entertained in your living room by people you wouldn't have in your home.David Frost
Thank God I'm an atheist.Luis Bunuel
What's another word for thesaurus?Steven Wright
Writing in C or C++ is like running a chain saw with all the safety guards removed.Bob Gray
If men can run the world, why can't they stop wearing neckties? How intelligent is it to start the day by tying a little noose around your neck?Linda Ellerbee
Here's to alcohol: the source of, and answer to, all of life's problems.Homer Simpson
Only one thing is impossible for God: To find any sense in any copyright law on the planet. Mark Twain
A publisher who writes is like a cow in a milk bar.Arthur Koestler. Do not worry about your problems with mathematics, I assure you mine are far greater. Albert Einstein
The first man to compare the cheeks of a young woman to a rose was obviously a poet; the first to repeat it was possibly an idiot.Salvador Dali Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.Brian W. Kernighan
Older people shouldn't eat health food, they need all the preservatives they can get.Robert Orben
Bart, with $10,000, we'd be millionaires! We could buy all kinds of useful things like...love!Homer Simpson
Every man has his follies - and often they are the most interesting thing he has got.Josh Billings
I base most of my fashion taste on what doesn't itch.Gilda Radner The use of COBOL cripples the mind; its teaching should therefore be regarded as a criminal offenseEdsger Dijkstra
No human investigation can be called real science if it cannot be demonstrated mathematically.Leonardo da Vinci
I blame my mother for my poor sex life. All she told me was 'the man goes on top and the woman underneath.' For three years my husband and I slept in bunk beds.Joan Rivers
Critics are to authors what dogs are to lamp-posts.Jeffrey Robinson. The future isn't what it used to be.
I was recently on a tour of Latin America, and the only regret I have was that I didn't study Latin harder in school so I could converse with those people. Dan Quayle
C is quirky, flawed, and an enormous success.Dennis M. Ritchie Anything worth doing is worth doing slowly.Mae West
Sex is an emotion in motion.Mae West
Outside of a dog, a book is a man's best friend and inside of a dog, it's too dark to read.Groucho Marx
I do not belong to any organised political party: I'm a democrat. Will Rogers.
How did I get to Hollywood? By train.John Ford.
The Russians love Brooke Shields because her eyebrows remind them of Leonid Brezhnev.Robin Williams.
We have a firm commitment to NATO, we are a 'part' of NATO. We have a firm commitment to Europe. We are a 'part' of Europe. Dan Quayle
I never drink coffee at lunch. I find it keeps me awake for the afternoon.Ronald Reagan.
When I'm good, I'm very, very good, but when I'm bad, I'm better.Mae West
A suburban mother's role is to deliver children obstetrically once, and by car forever after. Peter De Vries.
Military intelligence is a contradiction in terms.Groucho Marx
The Americans will always do the right thing . . . After they've exhausted all the alternatives.Winston Churchill
My Father had a profound influence on me. He was a lunatic.Spike Milligan
《》 相关热词搜索: [db:gjc]