Exercise 2
Exercise 2
1. Write a program that displays a frame window 800 pixels wide and 600
pixels high. Set the title of the frame to Welcome to Java.
3. Write a program that accepts a string input and outputs the number of
characters in the string and the first and last character in separate lines.
For 80 Chapter 2 Getting Started with Java example, if the input is I like
Java then the output would be 11 I a
4. At the McGraw-Hill book website, you will find a Java package called
galapagos. The galapagos package includes a Turtle class that is
modeled after Seymour Papert’s logo. This Turtle has a pen, and when
you move the Turtle, its pen will trace the movement. So by moving a
Turtle object, you can draw many different kinds of geometric shapes. For
example, this program commands a Turtle to draw a square:
import galapagos.*;
class Square {
public static void main( String[] arg ) {
Turtle turtle;
turtle = new Turtle( );
turtle.move( 50 ); //move 50 pixels
turtle.turn( 90 ); //turn 90 deg counterclockwise
turtle.move( 50 );
turtle.turn( 90 );
turtle.move( 50 );
turtle.turn( 90 );
turtle.move( 50 );
}
}
Write a program to draw a triangle. Read the documentation and see if you
can find a way to draw the square in a different color and line thickness.
5. Using the Turtle introduced in number 3, display the text Hello five times
at the approximate positions shown below: