Download presentation
Presentation is loading. Please wait.
Published byMarilynn McGee Modified over 8 years ago
1
Fundamentals of Software Development 1Slide 1 Recap: Key ideas in BallWorlds so far Writing a class:Writing a class: –structure –process Writing methods:Writing methods: –Using parametersparameters FieldsFields local variables\local variables\ –Scope and lifetime. –Invoking methods: The dot notationThe dot notation Import statements. JDK Help.Import statements. JDK Help. The next several slides review each of these ideas Interactions with other classes:Interactions with other classes: –UML class diagrams Writing them, reading themWriting them, reading them –X extends Y –X implements A, B, C –The distinction between a class anda class and an object that is an instance of that classan object that is an instance of that class Object Creation and ManipulaitonObject Creation and Manipulaiton –constructors –the new keyword –the this object
2
Fundamentals of Software Development 1Slide 2 Exercise to review these key concepts from BallWorlds so far Concepts Writing a class: structure and processWriting a class: structure and process Writing methodsWriting methods Interactions with other classesInteractions with other classes Constructors, the new keyword and the this objectConstructors, the new keyword and the this object Exercise: Stand up & walk far away from your computer Find a partner with whom you have NOT worked before and sit next to each other –Don’t sit at either of your computers The next slides include questions. As your instructor shows each slide: –One partner, answer the questions aloud –Other partner, make sure that the answers are clear and correct –Both partners: ask your instructor and/or assistants for more explanation if you are unsure about the answers or the concepts behind them! –Switch roles after each slide Questions about how to do this exercise?
3
Fundamentals of Software Development 1Slide 3 Recap: Writing a class – structure public class NameDropper extends StringTransformer implements StringTransformable { private String name; private String name; public NameDropper(String whatMyNameIs) { public NameDropper(String whatMyNameIs) { this.name = whatMyNameIs; this.name = whatMyNameIs; } public String transform(String whatToSay) { public String transform(String whatToSay) { return this.name + " says " + whatToSay; return this.name + " says " + whatToSay; }} Questions? Field(s): Data that is associated with the object Scope: rest of the class Lifetime: lifetime of the object Method(s): operations that the object can do Constructor(s): code that runs when the object is constructed Tell your partner: What are the 3 sections of a class? What is the role / purpose of each?
4
Fundamentals of Software Development 1Slide 4 By using documented stubs, for each method: You get its form (syntax) correct before working on its content (semantics) You write its specification before trying to implement it Questions? Recap: Writing a class – process Step 1: Add the class to the projectStep 1: Add the class to the project Step 2: Implement the classStep 2: Implement the class –Step 2a: Write the Javadoc specifications with stubs –Step 2b: Convert stubs to the actual code Obey Sun’s code conventionsObey Sun’s code conventions Step 3: Test the classStep 3: Test the class –For larger classes, repeat steps 2 and 3 several times, implementing more of the class each time Explain to your partner: What are documented stubs? Why is it helpful to use them?
5
Fundamentals of Software Development 1Slide 5 Recap: Interactions with other classes For each of the circled interactions, explain to your partner: What does it mean? Why is it useful / important? Questions?
6
Fundamentals of Software Development 1Slide 6 Recap: The distinction between a class and an object that is an instance of that class public class Mover extends Ball implements Animate, Drawable { private Point2D.Double position; private ManagerOfBalls manager;... public Mover(ManagerOfBalls mob) { this.manager = mob; } public Shape getShape() { return new Ellipse2D.Double( this.position.getX(),...); }... } ManagerOfBalls world1, world2; world1 = new World(); world2 = new World(); Ball ball1, ball2, ball3; ball1 = new Mover(world2); ball2 = new Mover(world2); ball3 = new Mover(world1); Briefly explain the portion of the Mover class shown to the left. Then explain each statement below in detail. What do they say about the title of this slide? This box defines a class called Mover The new Mover expressions in the statements above and to the right create instances of that class. Questions?
7
Fundamentals of Software Development 1Slide 7 Recap: Using parameters, fields, local variables; Scope and lifetime public class NameDropper extends StringTransformer implements StringTransformable { private String ; private String name; public NameDropper(String whatMyNameIs) { public NameDropper(String whatMyNameIs) { = whatMyNameIs; this.name = whatMyNameIs; } public String transform(String whatToSay) { public String transform(String whatToSay) { String middle = " says "; String middle = " says "; return + middle + whatToSay; return this.name + middle + whatToSay; }} What are the fields in the following? Parameters? Local variables? What is the scope of each? Lifetime? What does the this keyword mean? Why use it? How do you decide when to use a parameter? When to use a field versus a local variable? Use a field when you need data whose: Scope is the entire class Lifetime is the life of the instance Use a local variable when you need data whose: Scope is a single method Lifetime is the life of the method Use this to refer to this instance of the class. Questions?
8
Fundamentals of Software Development 1Slide 8 Recap: method invocation expressions Suppose that you have an object from one of the Java library classes (e.g. a JTextField object). How do you find out: What that object can do? What import statement is needed? Explain each of the expressions to the right. What appears before the dot? After the dot? Questions? Select JDK Help, index tab, look up the class, see its list of methods. Shortcut: Type the variable, then dot, and see what pops up. As above, but see the first line of the class description Questions? Always use the dot notation to invoke a method. For example: whatToSay.toUpperCase() this.position.setLocation(...) Math.random() this.die()
9
Fundamentals of Software Development 1Slide 9 Recap: Constructors, the new keyword and the this object public class MusicPanel extends JPanel implements Runnable { public MusicPanel(Color color) { this.setBackground(color); }... } Consider the MusicPanel class to the right. Write expression(s) that declare and construct a new blue MusicPanel called panel MusicPanel panel; panel = new MusicPanel(Color.BLUE); or equivalently: MusicPanel panel = new MusicPanel(Color.BLUE); Another solution: MusicPanel panel = new MusicPanel(new Color(0, 0, 255))); Color in RGB (RedGreenBlue) format (range 0-255) BLUE: Constant values usually appear in all CAPS, but in this case blue and Blue will also work!
10
Fundamentals of Software Development 1Slide 10 Recap: Constructors, the new keyword and the this object public class MusicPanel extends JPanel implements Runnable { public MusicPanel(Color color) { this.setBackground(color); }... } A Thread is an independent instruction- follower. From JDK Help: public Thread(Runnable target, String name) Allocates a new Thread object. Parameters: target - the object whose run method is called. name - the name of the new thread. Write an expression that constructs a new Thread whose target is panel and whose name is “Lola”. MusicPanel panel = new MusicPanel(Color.BLUE); new Thread(panel, “Lola”); Write an expression that, placed in the MusicPanel constructor, would construct a new Thread whose target is the MusicPanel being constructed and whose name is the MusicPanel’s color (as a String). From the previous slide new Thread(this, color.toString()); Questions?
11
Fundamentals of Software Development 1Slide 11 Questions? Questions on anything?Questions on anything? –Reminder: BallWorlds (all of it – parts 1 to 6) is due tomorrow You will have some time to work on it in class todayYou will have some time to work on it in class today Before you leave today:Before you leave today: –Tell your instructor where you are on BallWorlds –Arrange to get help as needed to complete it –Student assistants are available to you Sunday through Thursday evenings 7 to 9 p.m. in the CSSE lab F-217, as well as at other times
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.