• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
M C6java4

M C6java4






Total Views
Views on SlideShare
Embed Views



0 Embeds 0

No embeds


Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

    M C6java4 M C6java4 Presentation Transcript

    • Objecten Using Objects
    • Agenda
      • Swing vs. AWT
      • Object vs. Class
      • instance vs. Class
      • instance variable vs. Class variable
      • static vs. instance
      • main vs. new
    • Classes and objects
      • A class is the type of an object
      • Just as a variable counter may have type int , Color.red has type Color
      • Just as 5 is a literal of type int , "Hello" is a literal of type String
      • There are exactly eight primitive types
      • There are thousands of classes, and you can create more
    • Assignment statements
      • An assignment statement has the form:
        • variable = expression ;
      • Examples:
        • classSize = 40;
        • area = pi * radius * radius;
        • uglyGreen = new Color(175, 175, 30);
        • myName = "David Matuszek";
    • Combining declaration and assignment
      • Declaration and assignment can be combined into a single statement:
        • int classSize = 40;
        • String myName = "David Matuszek";
        • Color uglyGreen = new Color(175, 175, 30);
      • You can only declare a variable once, but you can assign to it many times in many places
        • A variable declared inside a method is declared only for that one method
          • Hence, a method can declare a variable with the same name as a variable in another method—but it’s a different variable
    • Data in classes and objects
      • A class is the type of an object
      • A class describes:
        • How to make a new object of that class
          • Example: new Color(175, 175, 30);
        • What kind of data is in an object
          • Example: a Color object contains three numbers representing the amount of red, green, and blue
        • The methods of an object (the actions it can perform)
          • Example: a Color object can tell you how much red it contains:
          • int amount = myColor.getRed();
    • Messages to a Graphics
      • If you have a Graphics , and its name is g , here are some things you can do with g :
        • Tell it to use a particular color:
          • g.setColor(Color.orange);
        • Ask it what color it is using:
          • Color currentColor = g.getColor();
        • Tell it to draw a line:
          • g.drawLine(14, 23, 87, 5);
    • Messages to a Color
      • Once you make a Color , you cannot change it; you can only ask it for information
      • The last method doesn’t change the color; it makes a new color
        • // Make a new purplish color Color myColor = new Color(100, 0, 255);
        • // Ask how much blue is in it int amountOfBlue = myColor.getBlue();
        • // Ask the color for a brighter version of itself Color brightColor = myColor.brighter();
    • The End “ If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.” --Anon.
    • “ I invented the term ‘Object-Oriented’, and I can tell you I did not have C++ in mind.” --Alan Kay, creator of Smalltalk.