Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMPE212 – Reminders Assignment 2 sample solution is posted.

Similar presentations


Presentation on theme: "CMPE212 – Reminders Assignment 2 sample solution is posted."— Presentation transcript:

1 CMPE212 – Reminders Assignment 2 sample solution is posted.
Winter 2019 CMPE212 4/5/2019 CMPE212 – Reminders Assignment 2 sample solution is posted. Assignment 3 due next Friday. Quiz 2 next week. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

2 Today Modules & Libraries. Viewing API Source Code.
Winter 2019 CMPE212 4/5/2019 Today Modules & Libraries. Viewing API Source Code. Enumerated Types or “enums”. Nested or Inner Classes. Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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 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

11 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

12 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

13 Aside – Viewing Java API Source Code, Cont.
Let’s look something up! Winter 2019 CMPE212 - Prof. McLeod

14 Fall 2013 CMPE212 Enumerated Types enum is a keyword that is new since Java 5.0, but C has had enum’s for a while! For example: enum IceCream {CHOCOLATE, VANILLA, STRAWBERRY, GOLD_MEDAL_RIBBON, WOLF_PAWS, BUBBLE_GUM}; Winter 2019 CMPE212 - Prof. McLeod Prof. Alan McLeod

15 Enumerated Types, Cont. Code that would use IceCream: Displays:
IceCream favourite = IceCream.GOLD_MEDAL_RIBBON; IceCream leastFavourite = IceCream.BUBBLE_GUM; System.out.println("Favourite is " + favourite); System.out.println("Least Favourite is " + leastFavourite); Displays: Favourite is GOLD_MEDAL_RIBBON Least Favourite is BUBBLE_GUM Winter 2019 CMPE212 - Prof. McLeod

16 Enumerated Types, Cont. The contents of IceCream are not Strings, but they are a kind of Object. IceCream is also an Object. They behave as named constants, so by convention, they should be in upper case. Once you have specified the possible values for IceCream in the enum declaration, you cannot change them. A variable of type IceCream can only be assigned to one of the possible types. Winter 2019 CMPE212 - Prof. McLeod

17 Enumerated Types, Cont. The objects in an enum object inherit a few methods, including: equals() toString() compareTo() ordinal() values() The first three look familiar! Winter 2019 CMPE212 - Prof. McLeod

18 ordinal() Method Returns the numeric location of the value in the list (numbering starts from zero.) For example System.out.println("Location = " + favourite.ordinal()); Displays: Location = 3 Winter 2019 CMPE212 - Prof. McLeod

19 values() Method Returns an array of the enum’s Objects in exactly the same order. For example: IceCream[] flavours = IceCream.values(); System.out.println(flavours[3]); Displays: GOLD_MEDAL_RIBBON Winter 2019 CMPE212 - Prof. McLeod

20 Enumerated Types, Cont. You can use equals() or == to test for equality. For example: System.out.println(leastFavourite == IceCream.BUBBLE_GUM); Prints out true to the console. compareTo() compares the members on the basis of their position in the enum collection. Winter 2019 CMPE212 - Prof. McLeod

21 Enumerated Types, Summary
So what’s the point of these things? enum’s are used when you need a short, simple list of things, or “named values”. The list has a finite size, will not grow and will not (and cannot) change, unlike arrays. You want to know that you can access only the defined members of the list and no others. Modern IDE’s (like NetBeans and Eclipse) will provide a list of all the values, when you type a period after the name of the enum. Yes, there are other ways to do this - but an enum is pretty easy to use! Winter 2019 CMPE212 - Prof. McLeod

22 Inner Classes Simply defined as a class defined within a class!
Fall 2013 CMPE212 Inner Classes Simply defined as a class defined within a class! public class OuterClass { private class InnerClass { Attributes of innerclass Methods of innerClass } // end InnerClass Attributes of OuterClass Methods of OuterClass } // end OuterClass Fall 2013 CMPE212 - Prof. McLeod Prof. Alan McLeod

23 Inner Classes, Scope Rules
See the demo program InnerClassDemo.java, used with TestInnerClassDemo.java. As usual, private/public does not matter within the class itself. The inner class can easily get at all the attributes and methods of the outer class. The outer class can also access the attributes and methods of the private inner class, after it instantiates the inner class. However, all private classes, methods and attributes are hidden outside InnerClassDemo. Fall 2013 CMPE212 - Prof. McLeod

24 Inner Classes, Why private?
If you are declaring a non-static object inside your class, you are only doing so because you want to hide it away. This object can only be used inside the outer class and is hidden everywhere else. How else can you make a class private? Fall 2013 CMPE212 - Prof. McLeod

25 Inner Classes, Cont. So, what’s the point of private inner classes?
(Used quite often with GUI coding…) One reason - When you wish to use a small class (in terms of the size of its definition), but don’t want to bother creating a separate class definition file. Fall 2013 CMPE212 - Prof. McLeod

26 Inner Classes, Cont. An inner class can be a “hidden” object!
This is often used with Linked List definitions to define the node object. Yes, you can have an inner class inside an inner class! Fun! Fall 2013 CMPE212 - Prof. McLeod

27 public static Inner Classes
What is the point of declaring an inner class public static? It would allow you to “categorize” methods into topical groups. Invoke them as below: First: TestClass tc = new TestClass(); Then, invoking methods from public static inner classes: tc.Group1.method(); tc.Group2.anotherMethod(); Fall 2013 CMPE212 - Prof. McLeod


Download ppt "CMPE212 – Reminders Assignment 2 sample solution is posted."

Similar presentations


Ads by Google