讲解:Eclipse、UML diagram、Python、Java,C++R|Haskell

Lab 6 RequirementsCreate a new Eclipse workspace named Lab6_1234567890 on the desktop of your computer (replace1234567890 with your student ID number). For each question below, create a new project in that workspace. Calleach project by its question number: Question1, Question2, etc. If you do not remember how to create aworkspace or projects, read the Introduction to Eclipse document which is on iSpace. Answer all the questions below.At the end of the lab, create a ZIP archive of the whole workspace folder. The resulting ZIP file must be calledLab6_1234567890.zip (replace 1234567890 with your student ID number). Upload the ZIP file on iSpace.Question 1Create a class a class Cat with the following UML diagram:+-----------------------------------+| Cat |+-----------------------------------+| - name: String || - weight: double |+-----------------------------------+| + Cat(String name, double weight) || + getName(): String || + getWeight(): double || + feed(): void || + testCat(): void |+-----------------------------------+Feeding a cat adds 1.0 to its weight.The testCat method is static and is used for testing the Cat class. Here is the code for this testCat method:public static void testCat() {Cat c = new Cat(Meow, 2.0);System.out.println(c.getName() == Meow);System.out.println(c.getWeight() == 2.0);c.feed();// The name is still the same but the weight increased by 1.0:System.out.println(c.getName() == Meow);System.out.println(c.getWeight() == 3.0);}And here is the Start class to test the Cat class:public class Start {public static void main(String[] args) {Cat.testCat();}}Question 2Add to your program a class Dog with the following UML diagram:+-----------------------------------+| Dog |+-----------------------------------+| - name: String || - weight: double |+-----------------------------------+| + Dog(String name, double weight) || + getName(): String || + getWeight(): double || + feed(): void || + testDog(): void |+-----------------------------------+Feeding a dog adds 2.0 to its weight.The testDog method is static and is used for testing the Dog class. Here is the code for this testDog method:public static void testDog() {Dog d = new Dog(Woof, 2.0);System.out.println(d.getName() == Woof);System.out.println(d.getWeight() == 2.0);d.feed();// The name is still the same but the weight increased by 2.0:System.out.println(d.getName() == Woof);System.out.println(d.getWeight() == 4.0);}Do not forget to modify the main method of the Start class to test the new Dog class too!Question 3Add a Student class so that a student has a cat as a pet:+---------------------------------+| Student |+---------------------------------+| - name: String || - pet: Cat |+---------------------------------+| + Student(String name, Cat pet) || + getName(): String || + getPet(): Cat || + testStudent(): void |+---------------------------------+Do not forget to write the testStudent method of the Student class to test the getName and getPet methods,and to modify the main method of the Start class to test the new Student class too!Question 4If you look at the code of the Cat and Dog classes above, you will see that they are almost the same. The onlydifferences are that: the names of the classes are different; the names of the constructors are different (since the names of the classes are different); the feed methods add a different amount of weight; the testCat and testDog methods are different (since the names of the classes are different).Everything else (the name and weight instance variables, the getName and getWeight methods) is the same. Thismeans that there is a lot of code duplication between the two classes Cat and Dog. Therefore it is a good idea to createa new class Animal that will contain only one copy of that code, and have the Cat and Dog classes then inherit thecode from the Animal class.Add a class Animal to the program above, with has the following UML diagram:+--------------------------------------+| Animal |+--------------------------------------+| - name: String || - weight: double |+--------------------------------------+| + Animal(String name, double weight) || + getName(): String || + getWeight():代写Eclipse、代写UML diagram、代做Pyth double || + setWeight(double weight): void || + testAnimal(): void |+--------------------------------------+The Dog and Cat classes should then be derived classes from the Animal base class (in other words, the Dog and Catclasses should inherit from the Animal class).Which instance variables, constructors, and methods from the Dog and Cat classes can be moved to the Animal class?Which instance variables, constructors, and methods of the Dog and Cat classes must stay in these classes? Howshould they be modified?After adding the Animal class, modify the Student class so that the student has an animal as a pet, not a cat.Do not forget to: change the main method of the Start class to run the unit tests of the new Animal class; add new tests to the Student class to test that you can now use an Animal object as the pet of a student;Now that the Student class uses an animal as pet, can you still use a cat object as the pet of a student? Why or whynot?Can you now use a dog object as the pet of a student? Why or why not?Question 5Add a Bird class to your program. A bird is an animal, therefore your Bird class must be a class derived from theAnimal class. The UML diagram of the Bird class is as follows:+-----------------------------------------------------+| Bird |+-----------------------------------------------------+| - altitude: double |+-----------------------------------------------------+| + Bird(String name, double weight, double altitude) || + getAltitude(): double || + testBird(): void |+-----------------------------------------------------+The altitude instance variable represents the altitude at which the bird is flying. Cats and dogs do not fly so they donot have an altitude.The constructor for the Bird class takes three arguments: the name of the bird, the weight of the bird, and the altitudeat which the bird is flying. The altitude argument of the constructor is stored into the altitude instance variable ofthe Bird class. Where are the name and weight of the bird stored? How?Do not forget to: change the main method of the Start class to run the unit tests of the new Bird class; add new tests to the Student class to test that you can now use a Bird object as the pet of a student;Suppose a student has a bird as a pet. Can the student get the altitude of his pet?Question 6Add a Chicken class to your program. A chicken is a bird, therefore your Chicken class must be a class derived fromthe Bird class. The UML diagram of the Chicken class is as follows:+-------------------------+| Chicken |+-------------------------++-------------------------+| + Chicken(String name) || + testChicken(): void |+-------------------------+A chicken always has a weight of 5.0 and an altitude of 0.0 (chickens spend all their time on the ground).Do not forget to: change the main method of the Start class to run the unit tests of the new Chicken class; add new tests to the Student class to test that you can now use a Chicken object as the pet of a student;Question 7Both the Student class and the Animal class have a name instance variable and a getName method, which leads tocode duplication between these two classes. To solve this problem, add a new LivingThing class to your program,which becomes the superclass for the Student and Animal classes, and which contains only one copy of the code forthe name instance variable and the getName method. The LivingThing class has the following UML diagram:+----------------------------+| LivingThing |+----------------------------+| - name: String |+----------------------------+| + LivingThing(String name) || + getName(): String || + testLivingThing(): void |+----------------------------+Change the Student class and Animal class so that both classes are now derived from the LivingThing class.Then remove the name instance variables and the getName methods from the Animal and Student classes. Theother classes do not change.Check that all your tests still work.Do not forget to change the main method of the Start class to run the unit tests of the new LivingThing class.LivingThing is now the top-most class. It has two derived classes: Animal and Student. Animal has threederived classes: Cat, Dog, and Bird. Bird has one derived class: Chicken. Student, Cat, Dog, and Chicken donot have derived classes.转自:http://www.3daixie.com/contents/11/3444.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 219,701评论 6 508
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,649评论 3 396
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 166,037评论 0 356
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,994评论 1 295
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 68,018评论 6 395
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,796评论 1 308
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,481评论 3 420
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,370评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,868评论 1 319
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 38,014评论 3 338
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 40,153评论 1 352
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,832评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,494评论 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 32,039评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,156评论 1 272
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,437评论 3 373
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 45,131评论 2 356

推荐阅读更多精彩内容