Setting the Commenting and Formatting Standards in Eclipse
- Download the three xml files provided with this lab and place them on your desktop.
- CSCI1260_Code_Formatting.xml
- This file contains standard formatting data for curly braces and other things.
- CSCI1260_Commenting_Template.xml
- This file contains standard formatting dataforyour code’s comments.
- Templates.xml
- This file contains a template to generate a toString method in a new class.
- Launch Eclipse and select your CSCI1260 workspace.
- SelectWindows > Preferences…
- Navigate to General/Editors/Text Editors. Check Show Line Numbers if not already checked.
- In the Preferences dialog, navigate the Preferencestree to ‘CodeTemplates’ as shown on the next page.
- Next, selectComments, and then click the Import… button.
- Navigate to the ‘CSCI1260_Commenting_Templates.xml’ file, select it, and select Open.
- In the Preferences dialog, expand Comments, select Files, and click the Edit button. This will allow you to change the name of the user, the user’s email address and the section number to your information:
- Repeat the process with Types and change my name to your name.
- Back in the Preferences dialog, navigate the tree to ‘Formatter,’ and then select Import…
- Navigate to the ‘CSCI1260_Code_Formatting.xml’ file, select it, and select Open.
- Take note of the name change of the Active Profile and then select ‘Ok.’
- You can now generate comments automatically and the curly braces will be automatically aligned for you.
- Now, navigate to Editor under Java. Expand Editor and go to Templates under Editor. That should present a dialog that looks something like this.
- Now click Import on the right-hand side and browse to where you saved Templates.xml. Select it. This will add an editor template that will allow you to generate the skeleton of a fully commented toString method in a class you are creating by typingtostr followed by pressing ctrl and space together. This will show
Double-click on the selectedline in the dialog and the toString method skeleton such as below will appear.
- In the preferences dialog, navigate to Run/Debug/Console as shown below. Modify the colors to suit your preferences, making sure to choose colors that are easy to see when your program is running. “Standard Out” is for text displayed by your program by System.out.print or System.out.println commands. “Standard In” is for text you type at the keyboard that is being input to your program via the program’s use of a Scanner object.
- Click the OK button to exit the Preferences dialog.
- To see the results of our changes in action, create a Java project called HelloWorld2.
- Add a class called HelloWorldto the project. Here is the New Java Class dialog, take note of the checked boxes. Then click Finish.
- Eclipse should generate the following commented code (some of the comment blocks may be collapsed and you will have to click on the + in the circle next to the line number to expand them to look as below):
- Examine the comments, and make changes to the filler text (such as “Enter type purpose here” or “Enter method description here”) in all appropriate locations. Add additional information to comments as needed throughout all projects.
- Retrieve and work your way through Some Tips for Using Eclipse.pptx on the website.
- Add a second class named Person with 3 attributes: firstName, lastName, and age of appropriate types. Use the Source menu in Eclipse to generate constructor methods, getters and setters, and the toString method. Using the Source menu in this way creates the skeleton of the methods and the headercomments for the methods. This saves you a lot of work instead of typing everything from scratch. Of course, there may be methods not on the Source menu that you must type from scratch, but if you put the cursor on the method and click Source/Generate Element Comment, Eclipse will write the skeleton of the comments for you.
0
- Complete the program by modifying the driver to create at least 2 Person objects, one using the defaultconstructor for Person and the other using the parameterizedconstructor. Display the two Person objects on the screen.
- All files, classes, methods, and attributes MUST be properly commented before submission.
- Submit the results of this exercise. Be sure to follow the instructions and name the zipped folder as instructed in the document named SomeCourseFacts.docx. Make sure all of the .java files submitted are documented consistently with the Documentation Standards document on the course website.
Part II
- Create a new Java project named HW1bYourName
- Add a driver class to the project. Name the driver class HW1bDriver
- Add a second class to the project. Name this class Person.
- Add 3 (private) attributes to the class: firstName, lastName, and age of appropriate types.
- Add a defaultconstructor method that initializes each of the attributes to some defaultvalue (such as blanks or zero)
- Add a parameterizedconstructor method that initializes each of the attributes to values passed in as parameters
- Add getters and setters for each attribute
- Add a method called formattedPerson that returns a string with a value consisting of the first name, the last name, and the age formatted as shown by the example:
“I am Willy Makit and I am 24 years old.” - Comment each of the methods as well as the class and the file appropriately.
- In the main method in the driver class,
- Promptthe user to input his/her first and last name and age
- Create a Person object using the information the user input
- Output the formattedPerson information for this Person object
- Document the entirefile and its components accordingly
- Show your work to the TA and get him/her to check off your name before you leave