Hello, Pict!
Circle and Rect make shapes? What’s a Pict?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.Prof: Sure!
definition: A statement that looks like
SomeType name = value;We will see a lot of types;
Pictis just the first one we’ve seen written down. You may have seen types likeintordoubleorStringin other programming languages.constructor use:
CircleandRectare the names of constructors. We use constructors in Java with the codenew ConstructorName(argument1, argument2, ...)That creates a new object. in this case those objects represent images, and have the type
Pict.arguments: I said
argumentas part of using a constructor. These are the values we give to a constructor. For the circle here we’re giving25and"red", for the rectangle20,30, and"blue".
"red" and "blue" strings?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?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)
Prof: Oh, that’s a helpful thing to ask. I would read them this way:
- The
Circleconstructor takes an int radius and a string color - The
Rectconstructor 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).
int and String values, images which are called Picts.Practice
Triangle(int width, int height, String color). Use it to create any triangle you like below, in a variable called t with type Pict.