Presentation is loading. Please wait.

Presentation is loading. Please wait.

CISC124 Assignment 3 due tomorrow at 7pm.

Similar presentations


Presentation on theme: "CISC124 Assignment 3 due tomorrow at 7pm."— Presentation transcript:

1 CISC124 Assignment 3 due tomorrow at 7pm.
Fall 2018 CISC124 1/16/2019 CISC124 Assignment 3 due tomorrow at 7pm. Quiz 2 next week, starting Monday at 8:30. Topics in yesterday’s notes. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

2 Today Debugging in Eclipse. (Last quiz 2 topic.) Packages.
Enumerated Types. Inner Classes. Fall 2018 CISC124 - Prof. McLeod

3 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 Fall 2018 CISC124 - Prof. McLeod

4 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. Fall 2018 CISC124 - Prof. McLeod

5 Aside – Skipping JRE Code in Debugger
Goto Window > Preferences > Java > Debug > Step Filtering. “Select All”: Then “Apply and Close”. Fall 2018 CISC124 - Prof. McLeod

6 Skipping JRE Code in Debugger, Cont.
When in Debug mode: Turn on “Use Step Filters”: Now, you won’t see as much JRE code when you are stepping through your code. Fall 2018 CISC124 - Prof. McLeod

7 Upcoming Topics & Terms
Packages, Enumerated Types Hierarchies, Inheritance, Polymorphism Interfaces, Anonymous Classes, Abstract Classes, Lambda Functions, Inner Classes Generic Classes Fall 2018 CISC124 - Prof. McLeod

8 package package_name;
Fall 2013 CISC124 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… Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

9 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. Put this user library somewhere in or below your classpath and then another class will be able to import it. Fall 2018 CISC124 - Prof. McLeod

10 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. Fall 2018 CISC124 - Prof. McLeod

11 Packages and Eclipse, Cont.
You can add to the classpath by right clicking on Project - Properties to get the following window: Then choose “Add…” Fall 2018 CISC124 - Prof. McLeod

12 Packages, Cont. We have been importing existing packages already
import java.util.Scanner; import java.io.*; Fall 2018 CISC124 - Prof. McLeod

13 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… Fall 2018 CISC124 - Prof. McLeod

14 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. For example: import static org.junit.jupiter.api.Assertions.*; Another example: import static java.lang.Math.*; See StaticImportMath.java. Fall 2018 CISC124 - Prof. McLeod

15 Aside – Protected Access
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! Fall 2018 CISC124 - Prof. McLeod

16 Fall 2013 CISC124 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}; Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod

17 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 Fall 2018 CISC124 - Prof. McLeod

18 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. Fall 2018 CISC124 - Prof. McLeod

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

20 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 Fall 2018 CISC124 - Prof. McLeod

21 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 Fall 2018 CISC124 - Prof. McLeod

22 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. Fall 2018 CISC124 - Prof. McLeod

23 Enumerated Types, Cont. Another practical example in the API is the Pos enum in the javafx.geometry package. See the API docs. Fall 2018 CISC124 - Prof. McLeod

24 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”, or constants with a theme. 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 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! Fall 2018 CISC124 - Prof. McLeod

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

26 Inner Classes, Scope Rules
See the demo program InnerClassDemo.java, used with TestInnerClassDemo.java. 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 CISC124 - Prof. McLeod

27 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 CISC124 - Prof. McLeod

28 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 CISC124 - Prof. McLeod

29 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 CISC124 - Prof. McLeod

30 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 CISC124 - Prof. McLeod


Download ppt "CISC124 Assignment 3 due tomorrow at 7pm."

Similar presentations


Ads by Google