Java程序辅导

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

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
CS 214 Lab 8: Java Exercise CS 214 Lab 8: Java Exercise Begin by copying the files NameTester.java and Makefile from the class directory into your new directory. The Makefile is relatively simple, so take a few moments to view it, to get a sense for its contents. Thanks to the Makefile, you can enter this command: make java and you should see NameTester.java compile. Verify that the file compiles correctly before continuing. If you use the ls command, you should see a new file named NameTester.class that the Java compiler produced. To run the program in NameTester.class, enter: java NameTester Verify that the program runs initially without any errors, then continue. Use a text editor to open NameTester.java and take a moment to study it, to see what it is doing. Uncomment the line: // Name aName = new Name("John", "Paul", "Jones"); Then use the same make command to rebuild your program. What happens? To fix this problem, we must build a Name class. Building a Name Class. A Java class can be defined using the form: ClassDec ::= modifier class identifier { Block } Modifier ::= public | � where identifier is the name of the new class. To support object-oriented programming, a Java aggregate can encapsulate both data components (called data fields) and function components (called methods). For operations, our class needs to encapsulate methods that perform the operations described in the introduction. One way to facilitate these operations is for our Name class to contain three data members, one of the first name, one for the middle name, and one for the last name, all of type String. Using this information, we can declare the shell of a Name class by writing class Name { private String myFirst, myMiddle, myLast; }; Add this declaration to NameTester.java. The portions of the class that are public defines its interface, while the portions of the class that are private defines its implementation. Save and rebuild your program. Is this sufficient for your program to build and run without errors? Initialization. Java objects are initialized by a constructor method: a method with no return type whose name is the name of the class (i.e., Name in this case). Since the task of this method is simply to initialize the three data members to the values the method receives via its parameters, we can write: public Name(String first, String middle, String last) { myFirst = first; myMiddle = middle; myLast = last; } Add this constructor definition to your Name class (inside the class) and continue. Given this constructor, our program's declaration Name aName = new Name("John", "Paul", "Jones"); should now construct and initialize the object aName as the name John Paul Jones. Rebuild your program; then run/test this much of your class. Continue when it builds and runs without any errors. Accessor Methods. Uncomment the following line in NameTester.java: // assert aName.getFirst().equals("John"); Try to build your program. What happens? Since a name is an aggregate of three components, our Name class needs three operations to access the values of those components. Such operations are called accessor methods, (or "getters"). Since NameTester.java is trying to access the first name of the object aName, we can write an accessor method to return this quantity: public String getFirst() { return myFirst; } Add this to your Name class, and verify that the program now builds and runs correctly. Note that by default, assertions are disabled in Java. To enable them, run your program using the -ea (enable assertions) switch: java -ea NameTester Using this as a model, define the other two accessor methods, for the Name class; then uncomment the next two assertions. Your program should now be able to execute Name aName = new Name("John", "Paul", "Jones"); ... assert aName.getFirst().equals("John"); assert aName.getMiddle().equals("Paul"); assert aName.getLast().equals("Jones"); ... and pass all three assertions. When all three accessors are correct, continue. We now have accessor methods (or "getters") that let us retrieve the value of a component. By contrast, operations that change the value of a component are called mutators (or "setters"), since they cause an object's state to change or mutate. Implementing the Name mutators is a part of this week's project. Output and String Conversion. In NameTester.java, uncomment the line: // System.out.println(aName); Rebuild your program. Do you get any errors? Run your program. What is displayed? To fix this, let's write a method that will aid in displaying a Name. If we call this method toString(), and have the method return a String representation of a Name object, then Java will automatically use this method when we attempt to print a Name object using one of the standard print() methods. We might define our new method as follows: public String toString() { return myFirst + ' ' + myMiddle + ' ' + myLast; } After adding this method to the Name class, rebuild and rerun your program. The statement: System.out.println(aName); should now cause the name John Paul Jones to appear on our screen. Verify that this works correctly before continuing. Thanks to the power of the toString() method in Java, there is no need to create separate methods for printing and getting the full name. However, either is possible. Consider the following print() method: public void print() { System.out.println( toString() ); } While this definition is valid, you can see that it is superfluous since we can print a Name object using Java's standard print() method. Likewise, our toString() method returns a person's full name. Verify this by uncommenting the final assertion in NameTester.java, rebuilding, and rerunning your program. Since toString() kills two birds with one stone, we will not implement separate methods for either of these operations. If you enter the command ls, you should see NameTester.java, NameTester.class, and Name.class. Note that the Java run-time environment finds and combines all of these files as necessary, provided they are in the same directory. Enter: make clean Then enter the ls command again. What is gone? make java and enter the ls command again. What is back? The make utility is thus very useful for distributing source code, since make clean can be used to remove everything except the source, and the Makefile is all that is needed to turn that source code into a working program. That concludes the Java part of this lab exercise. Use script to create a script.java file in which you cat your source file, show that its builds correctly, and show that your program runs correctly. Calvin > CS > 214 > Labs > 08 > Java This page maintained by Joel Adams.