☰ Contents Programs and Values

Hello, Pict!

Hello, Pict!

Student: Professor, I’m eager to get started, but I don’t want just another introduction to variables and strings. I already have some programming experience, I am an expert in so many things, I don’t think you are ready for how much I know.
Prof: That sounds very impressive. I think it’s cool when we can program with pictures. How about we start with something like this?
Student: Interesting, so Circle and Rect make shapes? What’s a Pict?
Prof: Great question. This deserves a little explanation. These are both definitions, one for the name dot and one for the name box. Definitions in Java have a type, a name, and a value. The Java in this book has ways to make images, and the type of the images in this book is Pict. Then, Circle and Rect are constructors that create Picts. The book shows you the image values and what they look like when you run the program.
Student: OK that’s a lot at once. Can you break down some of those terms for me?

Prof: Sure!

  • definition: A statement that looks like

    SomeType name = value;

    We will see a lot of types; Pict is just the first one we’ve seen written down. You may have seen types like int or double or String in other programming languages.

  • constructor use: Circle and Rect are the names of constructors. We use constructors in Java with the code

    new ConstructorName(argument1, argument2, ...)

    That creates a new object. in this case those objects represent images, and have the type Pict.

  • arguments: I said argument as part of using a constructor. These are the values we give to a constructor. For the circle here we’re giving 25 and "red", for the rectangle 20, 30, and "blue".

Student: OK, I recognize some of these ideas. Are "red" and "blue" strings?
Prof: That’s right, those are string values. And they give the Circle and Rect constructors information about the color of the resulting shape. What do you think the other numbers mean to the Circle and Rect constructors?
Student: Well, they probably have to do with sizes. Like the 25 is maybe the width of the circle, and the 20 and 30 are the sides of the rectangle?

Prof: Right. Specifically 25 is the radius of the circle, and 20 and 30 are the width and height of the rectangle. This helps us describe what these constructors do. I’ll write them down now with their specific types:

  • Circle(int radius, String color)
  • Rect(int width, int height, String color)
Student: OK, that’s the Java notation, how do we talk about it?

Prof: Oh, that’s a helpful thing to ask. I would read them this way:

  • The Circle constructor takes an int radius and a string color
  • The Rect constructor takes an int width, an int height, and a string color

So we think of “giving” the values to the constructor, and it produces the image (or the Pict, to use the Java type).

Student: OK, so we have constructors, int and String values, images which are called Picts.
Prof: Yup, you’ve got it
Student: OK, this is cool. I’m curious to see what else we can do with these images, and what other values we can construct.

Practice

There is another constructor Triangle(int width, int height, String color). Use it to create any triangle you like below, in a variable called t with type Pict.