Download presentation
Presentation is loading. Please wait.
Published byΙδουμα Γεννάδιος Modified over 5 years ago
1
CMPE212 – Reminders Assignment 2 due today, 7pm.
Winter 2019 CMPE212 5/21/2019 CMPE212 – Reminders Assignment 2 due today, 7pm. Assignment 3 due March 8 – same week as Quiz 2. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod
2
Today Coverage Testing. Debugging in Eclipse.
Winter 2019 CMPE212 5/21/2019 Today Coverage Testing. Debugging in Eclipse. Upcoming Topics, Including: Packages, Modules, Libraries, Viewing API Source Code. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod
3
Coverage Testing in Eclipse
Run Debug Coverage Provides an easy way of seeing which lines of code have been covered by your testing code. Winter 2019 CMPE212 - Prof. McLeod
4
Coverage Testing in Eclipse, Cont.
Let’s run Coverage on Halloween5Test.java to see if we missed anything. Green – covered. Yellow – partially covered. (Conditional branch(s) missed, for example). Red – not covered. “We” did miss some tests! Winter 2019 CMPE212 - Prof. McLeod
5
Coverage Testing in Eclipse, Cont.
Getting rid of highlighting: Click here Uses a module called EclEmma. See: Winter 2019 CMPE212 - Prof. McLeod
6
Debugging Sometimes a mental “walk-through” of your code is not enough to figure out an error. With the debugger you can run to a breakpoint, stop your program and then execute one line at a time while watching the call stack, variables and custom expressions. Let’s fix UseDebugger.java Winter 2019 CMPE212 - Prof. McLeod
7
Debugging, Summary See “Java development users guide” > “Concepts” > “Debugger” and “Java development user guide” > “Getting Started” > “Basic tutorial” > “Debugging your programs” in the Eclipse help system. Place one or more breakpoints in your edit window. Run in debug mode. Use the “step” choices. View the method call stack, variables, and use expressions if needed. Winter 2019 CMPE212 - Prof. McLeod
8
Aside – Skipping JRE Code in Debugger
Goto Window > Preferences > Java > Debug > Step Filtering. “Select All”: Uncheck “Step through filters” Then “Apply and Close”. Winter 2019 CMPE212 - Prof. McLeod
9
Skipping JRE Code in Debugger, Cont.
When in Debug mode: Turn on “Use Step Filters”: Now, you won’t see as much API code when you are stepping through your code. Winter 2019 CMPE212 - Prof. McLeod
10
Upcoming Topics & Terms
Packages, Modules Enumerated Types Hierarchies, Inheritance, Polymorphism Interfaces, Anonymous Classes, Abstract Classes, Lambda Functions, Inner Classes Generic Classes Winter 2019 CMPE212 - Prof. McLeod
11
package package_name;
Fall 2013 CMPE212 Packages “package” is a Java keyword. It provides a means of grouping classes together into a package. All classes in a package share some common theme. It is used as in: package package_name; This line is at the top of a class definition, before the public class… Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod
12
Packages and Eclipse Eclipse would prefer that you to create classes in a package. When you create a new class, specify what package you want it to belong to. This will automatically add the package package_name; line to the top of the class, the proper folder is created in src, and the new class in saved in this folder. Eventually, you will create a *.jar (or *.zip) file with all the *.class files in your package in the same folder structure. Put this user library somewhere in or below your classpath and then another class will be able to import it. Winter 2019 CMPE212 - Prof. McLeod
13
Packages, Cont. The structure is: classpath\folder\packagename
“folder” can be a series of folders. The import statement looks like: import folder.packagename.*; The “.*” says to import all classes in the package, or you can import specific classes. Winter 2019 CMPE212 - Prof. McLeod
14
Packages and Eclipse, Cont.
You can add to the classpath by right clicking on Project - Properties to get the window: Then choose “Add Class Folder…” (to get a project folder) or “Add External Class Folder…” (to get a file folder). Winter 2019 CMPE212 - Prof. McLeod
15
Packages, Cont. We have been importing existing packages already
import java.util.Scanner; import java.io.*; … Winter 2019 CMPE212 - Prof. McLeod
16
Packages, Cont. Note that Java automatically imports the java.lang.* package for you. This package contains many classes fundamental to the Java language: String Math all Wrapper classes (Boolean, Character, Integer, Long, Float, and Double) System, and a few others… Winter 2019 CMPE212 - Prof. McLeod
17
import static java.lang.Math.*;
static Import Used to import all static methods in a class, so that they can be used as if they were declared within the class that uses them. Example: import static java.lang.Math.*; See StaticImportMath.java. We have also seen this in JUnit5 testing classes – for the annotations and assertion methods. Winter 2019 CMPE212 - Prof. McLeod
18
Aside – Protected Access in Packages
So far we have used public and private access modifiers. protected is what you get if you don’t specify an access modifier. This means “public inside the package, private outside the package”. Used in the API. Use sparingly! Winter 2019 CMPE212 - Prof. McLeod
19
Java Modules This new system of organizing large numbers of Java source code files was introduced in Java 9. Modules are a way to organize many packages into a single module. But there is a little more to it than just grouping packages and files together. Each module must have a module-info.java file: Winter 2019 CMPE212 - Prof. McLeod
20
module-info.java The module can be built with a large set of packages in a folder structure. The module-info file goes in the root folder of this structure. This file contains information about the name of the module, the packages that are available, and the packages that are required. Basically, the info file supplies all the needed information about how the module interacts with other modules. Winter 2019 CMPE212 - Prof. McLeod
21
module-info.java, Cont. The format of the file is:
module module_name { requires module_name; … exports package_name; } You can have as many requires and exports lines as needed (and some others). Winter 2019 CMPE212 - Prof. McLeod
22
module-info.java, Cont. requires identifies modules on which this module depends. exports identifies packages in the current module that are available to other modules. This provides encapsulation at the package level! uses identifies specific interfaces and abstract classes or “services” that are used by this module. Winter 2019 CMPE212 - Prof. McLeod
23
open, opens, provides, with, to, transitive
module-info.java, Cont. Other keywords that can be used in the info file: open, opens, provides, with, to, transitive See help docs such as the API docs for the java.lang.module package. Or: Winter 2019 CMPE212 - Prof. McLeod
24
Java Modules, Cont. You can also restrict what other modules can see using Reflection. (More on Reflection later…) Dependencies can be specified for use at compilation or at run time or both. Encapsulation is a big advantage. The specification of dependencies reduces the run time size of the program and will speed up execution as a result. A module name must be unique. Winter 2019 CMPE212 - Prof. McLeod
25
Java Modules, Cont. In this course we won’t need to create modules for our little projects… The Java platform itself is now organized into at least 95 modules. Each module can contain many packages. A lot of the stuff we have been using is in the java.base module. Winter 2019 CMPE212 - Prof. McLeod
26
Java Libraries A library is distributed and used from a *.jar file (“Java Archive”) on the classpath. It is a compressed file containing only *.class bytecode files organized into the package folder structure specified by the packages. Now, *.jar files also contain the required module-info.class files called “module descriptors”. The *.jar file does not contain any *.java files – these will be in a file called “src.zip” that only comes with the JDK: Winter 2019 CMPE212 - Prof. McLeod
27
Aside – Viewing Java API Source Code
Open up the library link. Most of what we are interested in, for now, will be in the java.base module. When you locate the class or interface of interest and are doing this for the first time, you will see: Winter 2019 CMPE212 - Prof. McLeod
28
Aside – Viewing Java API Source Code, Cont.
Click on “Attach Source…” Choose “External location”, and click on “External File”. Fill out the dialog by providing the location of the src.zip file for the JDK you are using: Winter 2019 CMPE212 - Prof. McLeod
29
Aside – Viewing Java API Source Code, Cont.
Let’s look something up! Winter 2019 CMPE212 - Prof. McLeod
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.