Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 214 Lab 10: Java Exercise CS 214 Lab 10: Java Exercise Begin by creating a new subdirectory named java within your lab10 subdirectory. Then copy the files from the course labs/10/java directory into your new directory. Using a text editor, open these files, customize the opening documentation, and take a moment to study them. Use the provided Makefile to build the program; then verify that it runs correctly: java Birds A Bird Class In the file Bird.java, you should see the basic framework for class Bird: public class Bird { private String myName; } Given this much, the program in Birds.java can say: Bird bird0 = new Bird(); and compile correctly because the instance varible myName will be initialized using the default String constructor. The Bird Constructors. It is the responsibility of class Bird to provide the means of initializing its data members. In Java, this is done via a constructor method, whose name is always the same as the name of the class. A default constructor initializes an object to default values: public Bird() { myName = ""; } while an explicit constructor initializes an object to values received via its parameters: public Bird(String name) { myName = name; } Technically, we only need the explicit constructor here, but let's include both, just to see Java's syntax for them. Add these definitions within the declaration of class Bird. Given these, a programmer can write: Bird bird1 = new Bird("Hawkeye"); and the myName member of the Bird pointed to by bird1 will be initialized to Hawkeye. To test this much of your code, uncomment the line in Birds.java that passes the name "Hawkeye" to the Bird constructor. Continue when your program builds and runs correctly. The Accessor method. Once we are able to initialize the myName member, it is useful if we can access its value. But since myName is a private instance variable, there is no access allowed via the dot operator. Instead, we must define an accessor method: public String getName() { return myName; } Given this definition, a programmer can now write: bird1.getName() to retrieve myName from a Bird pointed to by bird2. The Call method. Since every bird has a bird call, our Bird class should provide at least a default as a part of its interface. Here is a simple definition: public String call() { return "Squaaaaaaaaaawk!"; } Given this definition, a programmer can write: bird1.call() to determine the bird call of the Bird pointed to by bird1. The Print method. To display the information about a Bird, we might write the following method definition: public void print() { System.out.println( getName() + ' ' + getClass().getName() + " says " + call() ); } Note that the getClass() method returns our current class, and we send it the getName() message, which returns a string description of the class. Given this defintion, we can write: bird1.print() to display the information of the Bird pointed to by bird1. Add the print() method and uncomment the line in Birds.java that prints the information about bird1. Compile and test that your program works correctly before proceeding. Note that the command used to build the program: javac -deprecation Birds.java does not just build Birds.java; it also builds Bird.java. More generally, javac will compile the specified file and each file on which that file depends, -- provided they are in the same folder -- even if the original file does not import those classes! The Duck Class Since we invested so much work in building our Bird class, we need only derive Duck from Bird to recoup our investment. Modify the declaration of Duck in Duck.java accordingly: public class Duck extends Bird { } Doing so specifies that class Duck is derived from Bird, and so inherits all of its data and methods, except its constructor. The Duck Constructors. Our Duck constructors trivial, because our Duck class has no data members beyond those it inherits from class Bird. Since it is the responsibility of Bird to initialize its data members, our Duck constructor passes the appropriate information on to the Bird constructor: public Duck() { super(); } public Duck(String name) { super(name); } Note the use of super, which is a reference to the superclass. In the methods above, we are asking the superclass to use its constructor to initialize its instance variables with default values and explicitly-supplied values, respectively. Given this definition, a programmer can now write: Duck bird2 = new Duck("Donald"); and bird2 will point at a correctly constructed Duck named Donald. Add these constructors and uncomment the lines in Birds.java that creates bird2 and prints its information. Compile and run your program. What do you observe? The Duck Bird Call. In order for a Duck object to produce the correct bird call, our Duck class must override the definition of call() it inherits from Bird with a Duck-appropriate definition: public String call() { return "Quack!"; } Given this definition, a programmer can now write: bird2.call() to elicit the bird call of the Duck to which bird2 points. Recompile and run your program to verify that after adding the Duck-specific version of call(), the print() method correctly reports that a duck quacks. It is worthwhile tracing the behavior of a call to: bird2.print(); The bird2 object is a reference to a Duck, and since there is no print() defined in Duck, this call is bound to Bird.print(). Within that call, print() executes calls to: getName() // bound to Bird.getName() getClass().getName() // bound to Object.getClass, Class.getName() call() // bound to Duck.call() Stop and take a moment to realize how powerful this is: we have a Duck, we called a method that is only defined in Bird.java, but that method invoked a method that ended up using a definition from Duck.java! The Goose Class Our Goose class is similar to our Duck class. We begin by deriving Goose from Bird: public class Goose extends Bird { } and then we define the interface methods as appropriate for a Goose: public Goose() { super(); } public Goose(String name) { super(name); } public String call() { return "Honk!"; } The remaining functionality is inherited from class Bird. Uncomment the lines in Birds.java that define and print a Goose object. Continue when your code builds and runs correctly. The Owl Class The Owl class is similar to that of the Duck and Goose classes. Take a few minutes to derive it from Bird, and define its interface methods that are different from those of Bird. Testing Test your code by uncommenting the remaining lines of Birds.java and then compiling and executing it. Continue when it builds and runs correctly. Turn In. When Birds.java works correctly, create a script file in which you run your program, use cat to display the contents of each of your files, and show that they build and run correctly. That concludes the Java introduction for this lab. Calvin > CS > 214 > Labs > 10 > Java This page maintained by Joel Adams.