Question: Last week, you wrote a first, simple Java program: the traditional Hello World. It had two methods: main() called printGreeting(), with both in the same
Last week, you wrote a first, simple Java program: the traditional Hello World. It had two methods: main() called printGreeting(), with both in the same Java source code file. This week, youll refactor (transform) that program into a new version. This new version will now have 2 classes: a client application class HelloAgain that uses a new component class Hello. Your new program will behave identically to last weeks program, except that now most of the underlying details will be buried away in a separate class file, which is used by the client application file. This is more representative of how a typical O-O program would be structured. Java Objectives: 1) Refactor an existing Java source code file into a more O-O structure (one application class using one utility class component). [CSLO-2] 2) Gain some initial exposure to Java classes and objects. [CSLO-2] 3) Continue gaining experience with the jGRASP IDE. [CSLO-4] 4) Continue applying good programming conventions/style. [CSLO-3] Submitting the assignment: Turn in two text Java source code files called HelloAgainFL.java and HelloFL.java (not the .class files) where: F=your first initial, L=your last initial. Submit both Java source files in Canvas, under this assignment. Individually is preferred, no zip files. Grading: See the rubric associated with this assignment in Canvas, or below. LAB02: TOTAL POINTS POSSIBLE: 20 1.00 Program Elements/Structure 10 1.01 HelloAgain class: main() method only, create object, 2 print calls 5 1.02 Hello class: 2 instance variables, constructor, printGreeting, 2 sets 5 2.00 Program Execution 5 2.01 Runs as-submitted, default message and name message 5 3.00 Program Style 5 3.01 Header block, braces, code nesting, whitespace, as shown in assignment examples (both files) 5 4.00 Late Deduction 0 4.01 -1.5 pts/day, prorated 0 As you move through this exercise, note that key language or IDE terms are in BOLD. In all cases when you are creating a file or a class, for FL substitute YOUR first/last initials. Steps: 1) In a file browser, copy last weeks file HelloWorldFL.java and rename it to HelloAgainFL.java. 2) Double-click on HelloAgainFL.java, to invoke jGRASP. 3) Edit HelloAgainFL.java, replacing all instances of HelloWorld with HelloAgain. You can use the Edit menu Find/Replace Replace All option to do this. Next, update the date, filename, and purpose in the header comment block. Finally, gut this file by eliminating the printGreeting() method entirely, and deleting the call to printGreeting() within main(). Your file is now an empty application shell. Well add code back to it in another step. Your entire updated file now should look like this (with updated data): 4) Save your changes, and test compile this file. Make sure there are no compilation errors. You can try and execute this file, but it wont do anything yet. 5) Next, create a new utility class HelloFL by selecting FileNewJava. Save this file right away as HelloFL.java. 6) Starting from the outside in, create an empty class shell for HelloFL.java. Remember, everything in Java is a class. Add the header comment block as shown. This must be provided for ALL code submitted for grading in this course. You may want to copy-and-paste from the previous file. Or at this point, perhaps consider setting up a header block template? Your new file should look like this. Note that this file does not need a main() method, as it is intended to be used as a reusable component in within another client application. Before proceeding, save your work and test-compile, to make sure no errors have been added. 7) Next, lets start sketching out the rough outline of our new class. We can use non-executable single-line comments for this purpose as pseudocode, which can give us the rough draft of our code without actually having to write it yet. Your HelloFL.java code should now look like the following: Before proceeding, save your work and test-compile, to make sure no errors have been added. 8) Next, set up the interfaces to all of your needed methods. An interface defines the inputs and outputs of any method. It is a contract with any client application that wants to use that method. An interface says, If you give me these things, I will do such-and-such for you. A method is a named container for some related instructions to achieve some well-defined purpose. It can be reused wherever needed, without having to reinvent the wheel. Get into the good habit now of always providing a brief comment describing what each method does. Take note of the consistent indentations. 3 or 4 spaces is typical - I like to use 4 spaces but pick ONE value and be consistent with it everywhere. Also, the closing braces should be aligned with the beginning of the statement which opens that particular block, which is all the code between a pair of braces {...}. Your HelloFL.java code should now look like the following: Before proceeding, save your work and test-compile, to make sure no errors have been added. 9) Next, define your instance variables and use the constructor to initialize their values. The instance variables describe whatever the object IS. In this case, the class knows the users first and last names. Note that instance variables typically have private visibility, because we want to protect instance variables from uncontrolled outside alteration. The constructor is a special method, named the same as the class itself and its file. It is used to set initial values for those instance variables. In this case, we are setting some default values. Remember that a String is Javas designation for some text within double quotes. Note that constructors typically have public visibility, because we want the outside world to be able to see and use them. The first part of your HelloFL.java code should now look like the following: Before proceeding, save your work and test-compile, to make sure no errors have been added. Note that here we have begun to move away from last weeks line of code, blank line, line of code, blank line... coding style. That was just a Week 1 starting point. Arranging your code more like this into commented groups of related statements, with some whitespace in between is more along the lines of what we would like to see your code look like for all coming programs. Otherwise, many individual statements can get lost in a forest of code, and we lose sight of the bigger picture. 10) Finally, add the internals of the other methods. We already saw the printGreeting() method in use last week, and its similar this week. However, this time there are no parameters in its interface, because now it can use the instance variables directly. (Of course, its now in a separate file). You could copy-and-paste printGreeting() from last weeks HelloWorldFL.java, if you wish. Just remember to remove the two input parameters. However, its easy enough to recreate here. The other 2 methods are mutator methods, which are used to update the values of the instance variables from their default settings. They simply copy the input value to its corresponding instance variable. All of these methods have public visibility, because we want the outside world to be able to see and use them. The last part of your HelloFL.java code should now look like the following: Before proceeding, save your work and test-compile, to make sure no errors have been added. Make sure all code within any brace pairs is indented consistently. 11) Your HelloFL.java file should now look like below. At this point, it is ready to be used within the HelloAgainFL class, so we can set it aside and forget about it for now. You can try and execute this file, but you will just get a warning message saying that it has no main() method. Any standalone Java application must have at least one main() method to show where execution is to begin. This Java class is meant to be a component used elsewhere. 12) Return to your (refactored) HelloAgainFL.java class, and add the following remaining code to main(), but be sure to specify YOUR name in the code and not mine! 13) Save your changes, compile, and execute this code. Your output should look similar to this: Notice that the first print message displays the default values for the instance variables. We used the mutator methods to change the first and last name instance variables into what we really wanted. Once we have created an object in our code (here, person), we use dot notation to access its methods. The object person is an instance of the class HelloFL. Heres a visual summary of the transformation from last weeks program into this weeks: Compare this weeks HelloAgain.java application to last weeks HelloWorld.java. Whats different? The new application is leaner and shorter, and the print method is relocated. We now have two classes at work, instead of just one. One class is the application client, the other is a reusable software component. Most of the details are now offloaded (buried away) into the new Hello.java utility class. Printing is easier, because the object already knows who it is, no parameters are needed. The Hello class can be easily reused elsewhere without any changes, which is the whole benefit of using O-O. Build it once, then forget about it. It might seem like a lot of work to write software this way. But actually, writing software classes such as this one represents only a one-time investment of effort. Once a software class is well-built, we can use and reuse it endlessly. This makes for a long-term savings of effort. Your colleagues or development organization may already have many commonly-used classes written. The Java language provides THOUSANDS of pre-built classes, so its very likely that any needed common capability has already been written. All you have to do is leverage someone elses efforts. Now you and your colleagues can focus your efforts on the domain (problem-specific) aspects of your software. This is code reuse at its finest!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
