Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005.

Similar presentations


Presentation on theme: "Week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005."— Presentation transcript:

1 week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005

2 week102 More about Classes & Design Need to be consistent with our models and design of classes. One way to do this is to use UML (Unified Modeling Language) diagrams to visualize programs Use box to represent class  Top box - Class name  Middle box - instance variables  Bottom box - methods Song length : int artist : Artist album : Album play() : void toString(): String

3 week103 Encapsulation The instance data of an object should only be modified by that object  Keep the data private Make other objects use getter (accessor) and setter (mutator) methods to access and change data This guarding of data is called encapsulation Violate encapsulation Provide services to clients Support other methods in class Enforce encapsulation public private variables methods Figure 4.5

4 week104 Class Relationships Dependency (one class “uses” another)  But how does a class gain access to another object? One class can instantiate (create) the other object One class can gain access to another by getting that object as a parameter to a method  The more classes depend on one another, the more changes in one class can impact another (which can be troublesome) Classes can depend upon objects of the same class (ie one object of a class interacts with another object of the same class)

5 week105 Class Relationships Composition (aggregation) - one object is made up of other objects  Can be described as a “has-a” relationship  A special type of dependency Next week we will being talking about inheritance and hierarchical class relationships…

6 week106 Math Class Defined in the java.lang package All methods in the Math class are static methods (aka class methods)  This means that you do not need an object of the class to call the methods - you invoke them directly through the name of the class Some useful methods:  static double sqrt (double power)  static double pow (double num, double power)  static double random ()  static double cos (double num)  static double sin (double num)  static double tan (double num)

7 week107 static Static Methods - invoked by class name, no object needed  Can be used when no object state is needed to do an action  The Binary class that I gave you could have had static methods, since there were no instance variables in that class Static Variables (also called Class Variables) are shared among all instances of a class  There is one copy of the variable for all objects in the class (if the variable is public, then you can have one copy for all classes in that project)  This is different from an instance variable in which each instance (object) has its own version of the variable

8 week108 One use of static For a project you may want to have the option of printing debugging information  You could put a final static boolean variable in one class to indicate if debugging should be on or off Any other class in that project could then access that variable (through the class) and use it as an indication if it should print the information  The final is so that it can’t be changed once the program is running A control such as this is a called a Singleton design pattern  Exactly one item with global access to it

9 week109 enum Enumerated type, lists all the possible values of a variable of the type  The values given are identifiers enum is a special kind of class  It can be declared either on its own (in BlueJ when you create a new class, enum is an option)  It can be declared as part of a class (used like an instance variable)

10 week1010 enum examples Examples: enum Season {winter, spring, summer, fall} enum Grade {A, B, C, D, F} enum STA-grade {Aplus, A, Bplus, B, Cplus, C, Dplus, D, F} Using an enum type: STA-grade myGrade = STA-grade.Aplus; Season time = Season.fall;

11 week1011 Full enum example public class IceCream { enum Flavor{ vanilla, chocolate, strawberry} Flavor myFlavor; public void chooseFlavor(Flavor choice){ if(choice.getName() == “vanilla”) System.out.println(“Chocolate is better”); else if(choice == Flavor.chocolate) System.out.println(“You choose wisely”); else System.out.println(“Strawberry is ok.”); }

12 week1012 Method Overloading Having the same method name with different types of parameters  String convertToBinary(String s)  String convertToBinary(char c) The compiler knows which method to call based on the type of the argument that you pass in

13 week1013 Graphical User Interfaces To make a GUI, you need at least:  Components - the object on the screen that displays information or provides interactivity Like button, label, textfield, menu, etc (As opposed to the containers with hold and organize components)  Events - an object that represents something that has happened which may be of interest (like user actions of pressing or typing) Most GUI components generate events  Listeners - an object that waits for an event to occur and responds in some way (usually that the programmer defines)

14 week1014 To Create a Java program that uses a GUI Steps as specified in book: Instantiate and set up the necessary components Implement listener classes that define what happens when particular events occur Establish the relationship between the listeners and the components that generate the events of interest

15 week1015 ActionListener ActionListener is an interface (basically a contract that says that you have defined all the methods defined in the interface)  The ActionListener interface specifies one method Any gui item that you create can set a listener: addActionListener (new MyListener()); Then you can make an internal class: private class MyListener implements ActionListener { public void actionPerformed(ActionEvent e){} } In that method you can do whatever you need to, print the value, call another method, etc

16 week1016 Sudoku and Files Scanner class can read in a File  Scanner sc = new Scanner(new File("myFile")); We can get a line of the file and split it up in tokens based on white space  Will need to do hasNext() to make sure that there is another token on the way before you try to actually read it in Then we can look at each of those string tokens  if it is a period, we know that is an empty cell  if it is a number, we can make a non-changeable cell with that number in it

17 week1017 Sudoku Project Program the Sudoku game as designed in class  Part 1: Due Monday/Tuesday - You write Sudoku.java and Cell.java as specified in class Monday we will discuss any additional methods you may discover you need while programming so that everyone has the same class/method signatures (for ease of testing and integration) Monday night you can debug your code and finish up anything based on class discussion, Part 1 code due Tuesday in class.  Part 2: Due Thursday - I will provide skeleton gui files early next week (CellPanel.java and SudokuGui.java) for you to fill in and to make work with Part 1, all of this will be due on Thursday Additional specifications for part 2 will be provided next week.


Download ppt "Week101 APCS-AB: Java Miscellaneous Topics: Snippets we missed in Chapters 1-6 of book November 11, 2005."

Similar presentations


Ads by Google