Question: ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab Lab Instructions Each student must complete their own lab assignment. However, you are free

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

Lab Instructions

Each student must complete their own lab assignment. However, you are free to discuss with other students.

IMPORTANT! I cannot overemphasize the importance of following these instructions EXACTLY! 99% of the time, if you have a problem with the lab, it will be because you skipped a sentence, didnt copy something precisely, or skimmed the instructions. Read every sentence completely and follow the instructions to the letter. If you find an error, please let me know.

OVERVIEW

  1. 1) Part A: Browsing the Java API Documentation (1point)

  2. 2) Part B: Using the Rectangle class (1 point)

  3. 3) Part C: Adding JavaDoc to a class(1 point)

  4. 4) Par D: Scanner and String methods (1 point)

Part A: Browsing the Java API Documentation Objective: Be able to use the Java API documentation for different Java classes

1) Java provides thousands of built-in classes that you can use. It is neither necessary nor useful to memorize the details of each class. Instead, it is important to get comfortable with using the Java API Documentation, which describes how to use these classes. You can find the API documentation at: https://docs.oracle.com/en/java/javase/

For example, clicking the JDK 15 will lead you to the documentation for that version

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

  1. 2) Given the large number of built-in classes, Java organizes related classes into packages, and related packages into modules. It is important to know the package that a given class belongs to because to use the class, one needs to write an import statement that includes the package name.

  2. 3) Open the Javadoc for the Rectangle class. To do this, go to the above website and select the JDK version you are using Then, in the search bar on the top right corner of the page type Rectangle.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

  1. 4) Look through the Javadoc for the Rectangle class. As you can see, it starts with the names of the module and the package that the class belongs to. Note that, for this class, the package is java.awt. To use this class in a Java program, you will need to include the statement import java.awt.Rectangle; or import java.awt.*;. The difference is that the first import statement only enables use of the Rectangle class, while the second enables use of any class defined in this package. The asterisk is commonly used to represent a wild- card type of value and here it means include all the classes that are a part of the java.awt package.

  2. 5) In addition, the Javadoc includes a section that describes the purpose of the class, a list of fields, and a summary of the constructors and methods. You can click on any method to get more details, which include: method declaration, a description of the method, a list of parameters (if any), and a description of the method's return value.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

  1. 6) Spend several minutes reading the Javadoc of the Rectangle class and answer each of the following questions:

    • In which package is the Rectangle class located?

    • What do you need to do in order to use the Rectangle class in your program?

    • How many constructors does the Rectangle class have?

    • Find the errors in the following statements:

      r = new Rectangle(); r.translate(10, Hello);

  2. 7) Save your answers in a document called lab2_partA to submit with this lab.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

Part B: Using the Rectangle class Objective: Be able to create and work with objects of the Rectangle class.

In part A, you got familiar with browsing the Java API Documentation. In this part, you will consult the Javadoc and write a program using the Rectangle class. Note that the Rectangle class is usually used in conjunction with a GUI (Graphical User Interface), which you will learn about later this semester. Today, you will use it in a regular console application. The goal of this part is for you to get more practice with using built-in Java classes.

  1. 1) Open NetBeans, choose File New Project..., then choose Java with Ant (or Java in Netbeans 8), then choose Java Application, and click Next >.

  2. 2) Create a project called 1213Module2. Leave the Create Main Class checkbox checked. Click Finish.

3) Create a new Java class called RectangleTest.java. Make sure that it is added to the 1213Module2 package. Recall from Module 1 lab how we added new Java classes to a project, choose File New File. Select Java under Categories and for the file type you can select either a Java Class, Java Main Class or an Empty Java File. Selecting the Java Main Class type will include the main method declaration while the Java Class type only include the class declaration. In the popup, name your class RectangleTest (in the Class

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

Name box). Click the Package dropdown arrow and select 1213Module2 package. Click Finish. Java creates this class, adds it to the 1213Module2 package, and opens the RectangleTest.java source code file.

  1. 4) Now, inside of the main method, type the following statement to create a rectangle.

    Rectangle box1 = new Rectangle(10, 10, 40, 30);

  2. 5) You will see that Rectangle has a red underline and if you move your cursor to the lightbulb on the left. You will see the following message: cannot find symbol: class Rectangle. This is because the Rectangle class is defined in a different package, and the compiler does not know where to locate it.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

  1. 6) There are two ways to fix this error

    • - add an import statement right below: package pkg1213Module2;

    • - click on the lightbulb and select the first suggestion, which will add the import statement

      automatically.

  2. 7) Once you fix the error, check if the box is created properly by adding the following statement:

    System.out.println(box1: + box1);

  3. 8) Run the RectangleTest by clicking Run Run File or right clicking inside the file viewer and then Run File. You should see this:

  4. 9) Now use the Rectangle class to complete the following tasks:

    • - Create another object of the Rectangle class named box2 with a width of 100 and height

      of 50. Note that we are not specifying the x and y position for this Rectangle object. Hint:

      look at the different constructors)

    • - Display the properties of box2 (same as step 7 above).

    • - Call the proper method to move box1 to a new location with x of 20, and y of 20.

    • - Call the proper method to change box2s dimension to have a width of 50 and a height of

      30.

    • - Display the properties of box1 and box2.

    • - Call the proper method to find the smallest intersection of box1 and box2 and

      store it in reference variable box3.

    • - Calculate and display the area of box3. Hint: call proper methods to get the values of

      width and height of box3 before calculating the area.

    • - Display the properties of box3.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

10) Sample output of the program is as follow:

11) When your program works take a screenshot of your NetBeans window and output and save the file as lab2_partB to include with your submission.

Part C: Creating Java Documentation Objective: Be able to add JavaDoc comments and create the Java documentation for a class

1) Create a new Empty Java class called Circle.java. Make sure that it is added to the 1213Module2 package.

  1. 2) Copy the contents from this code snippet into the Circle.java source code file.

  2. 3) Make sure to add the package declaration to the beginning of the file

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

4) Examine the code and develop an understanding of the code you just copied. At this point you should be able to follow along each line in this code and know what it means and its purpose inside this class. Take a few minutes to note the different components of this class and identify all the fields (instance variables), constructors, and methods.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

5) Now, add in the appropriate Java comments. Be sure to include:

  • A documentation comment for the overall class, including an @author tag

  • Documentation comments for each of the methods and constructors (a summary that

    describes the purpose of the method)

  • @param tags for all parameters (describes each of the parameters in the parameter list

    (if any)).

  • @return tags for all return values (describes the information that is returned to the

    calling statement (if any)). Refer to this video for an introduction to JavaDoc by Dr. Margret Bosch:

    https://youtu.be/CJxMwbJPisw

  1. 6) A description of anything that someone using the class might need to know. When you are done adding comments to the Circle class you are ready to generate the Java doc. NetBeans supports generating HTML files that can be viewed using any web browser. To generate the Java doc select the 1213Module2 project in the Projects Pane, then choose Run Generate Javadoc (1213Module2).

  2. 7) This will open a web browser window. On the left panel of this window will be a list of classes. Select the Circle class to load its Java doc. Check the Java doc to ensure your comments were put in correctly.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

8) In the future, if you need to access the Javadoc of the Circle class, simply click on the Files tab in the upper left (beside the Projects tab). Expand 1213Module2 dist javadoc 1213Module2, right-click Circle.html then click View.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

9) There is nothing to capture for this part. Once you are satisfied with the JavaDoc you are ready to move on to the next part.

Part D: User Input, and string methods

1) In this part we will work with the Scanner and String methods. Open the Main.java source file and modify the main method to prompt the user to enter the information (values) that will be stored in the variables listed in the following table:

Variable Description

First Name Last Name 800 number College Major GP A

Credits Earned

Java Data Type

String String int String double int

Java Identifier (variable name)

firstName lastName studentId major gpa credits

Refer to the Scanner class JavaDoc for more information. For examples on how to use the Scanner class to capture user input see https://www.w3schools.com/java/java_user_input.asp

  1. 2) Display to the console all of the information the user entered. Your output should be formatted to look like:

  2. 3) Note how the values for the name and major are displayed using uppercase letters and the student details are indented under the main heading. To ensure that the name and major is displayed in all caps regardless of how this information was entered by the user we can use one of the String class methods to convert all characters in the String to uppercase.

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

  1. 4) For example, to convert the letters stored under the firstName variable we add a statement that looks like this:

    Update your code to use this method to convert the first name, last name and major to include all uppercase characters. To include the indentation in the output you can use the escape sequence \t to provide a tab space in the displayed output. Review this video on inserting new line and tabs in Strings https://youtu.be/r4kuDIcAte8

  2. 5) Once your program compiles without errors and you run it for the first time, you might see that you are unable to enter the major and the program skips to the prompt for GPA. This occurs if you used the nextInt() method to read in the user input. nextInt() does not capture the new line and skips the next scanner code. To fix this problem, you can clear the buffer by using nextLine() immediately after each nextInt().

  3. 6) When your program works and you are satisfied with the result, take a screenshot of your NetBeans output window and save the file as lab2_partD to include with your submission. Your output might look something like this:

ITSC 1213 Introduction to Computer Science 1 - Module 2 Lab

Youre done! Now export your 1213Module2 NetBeans project. Refer to the lab instructions in module 1 (part C) if you dont recall how this process works.

In summary, in this lab, we discussed:

  1. 1) How to browse Java API documentation

  2. 2) How to read Javadoc of a class

  3. 3) Why and how to use import statement

  4. 4) How to create an object of a class

  5. 5) How to call methods on an object

  6. 6) How to add and generate JavaDoc to a class

  7. 7) How to use the Scanner class to capture user input

  8. 8) How to use the String methods to work with String data types

Module 2 Lab Canvas submission

1. In this weeks module, you will find the link to the lab completion submission. Submit the following files

  1. your answers for part A

  2. screenshots for parts B and D

  3. 1213Module2.zip NetBeans project export

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!