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

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
L3:CSC © Dr. Basheer M. Nasef Lecture #3 By Dr. Basheer M. Nasef.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Road Map Introduction to object oriented programming. Classes
COMP 110 Introduction to Programming Mr. Joshua Stough October 8, 2007.
Aalborg Media Lab 26-Jun-15 Software Design Lecture 5 “ Writing Classes”
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Chapter 4 Writing Classes. We've been using predefined classes. Now we will learn to write our own classes to define objects Chapter 4 focuses on:  class.
Chapter 4: Writing Classes Presentation slides for Java Software Solutions Foundations of Program Design Third Edition by John Lewis and William Loftus.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
Unified Modeling Language
Programming Task: Task 1 Controlled Assessment Practice.
UML Basics & Access Modifier
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
Writing Classes (Chapter 4)
Week101 APCS-A: Java November 8, week102 Java Packages All Java classes are grouped into libraries (or packages)  String is part of the java.lang.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
An Introduction to Java Chapter 11 Object-Oriented Application Development: Part I.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
Method Overriding Remember inheritance: when a child class inherits methods, variables, etc from a parent class. Example: public class Dictionary extends.
Object-Oriented Modeling Chapter 10 CSCI CSCI 1302 – Object-Oriented Modeling2 Outline The Software Development Process Discovering Relationships.
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Centre for Computer Technology ICT214 Object Oriented Design and Programming Week 02 – Classes, Objects, Instances Richard Salomon and Umesh Patel Centre.
Chapter 4 -2 part Writing Classes 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
An Object-Oriented Approach to Programming Logic and Design Chapter 3 Using Methods and Parameters.
Chapter 12 1 TOPIC 13B l Buttons and Action Listeners Window Interfaces Using Swing Objects.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSSE501 Object-Oriented Development. Chapter 4: Classes and Methods  Chapters 4 and 5 present two sides of OOP: Chapter 4 discusses the static, compile.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved September 12, 2007 Encapsulation ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Swing GUI Components So far, we have written GUI applications which can ‘ draw ’. These applications are simple, yet typical of all Java GUI applications.
CSCI 51 Introduction to Programming Dr. Joshua Stough February 26, 2009.
© 2004 Pearson Addison-Wesley. All rights reserved November 2, 2007 Class Relationships ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Writing Classes : Review Java Software Solutions Foundations of Program Design Seventh Edition John.
CSC 205 Programming II Lecture 5 AWT - I.
More Sophisticated Behavior
Java Programming: Guided Learning with Early Objects
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Encapsulation & Visibility Modifiers
OBJECT ORIENTED PROGRAMMING II LECTURE 8 GEORGE KOUTSOGIANNAKIS
IFS410: Advanced Analysis and Design
Outline Anatomy of a Class Encapsulation Anatomy of a Method
Object Oriented Programming in java
Classes, Objects and Methods
Presentation transcript:

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

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

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

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)

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…

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)

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

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

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)

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;

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.”); }

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

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)

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

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

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

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.