1.7 Solutions

  1.  
    Here we are (finally),
    
    at the end
    of
    
    Chapter 1
    
  2.  
    println("The handsome stranger said,\n\"My name is Kim, Paul Kim.\""); 
    1. int
    2. double (would accept float)
    3. char
    4. illegal, exponent for E notation must be int
    5. char
    6. String
    7. String
    8. float
    9. illegal, should be '\''
    10. illegal, only 1 char allowed inside char
    11. long
    12. illegal, comma not allowed
    1. short
    2. int
    3. byte
    4. long
    1. 0.000877
    2. 299.04
    3. -44300.0
    4. -0.0023
  3. a) illegal, starts with a number
    b) doesn't follow convention since starts with a capital
    c) doesn't follow convention since uses currency symbol ($)
    e) illegal, space is not allowed
    f) illegal, reserved word
  4. b, e, f
    1. x: 0.0093y: -0.003
    2. first is 5
      second is -77
  5. void setup()
    /* another mess */
    {
       int i = 7, j = 3;
       print("Smaller is " + i);
       println(" Larger is " + j);
    }
  6.  
    void setup()
    {
      String s;
      char c;
      int i;
      float f;
      
      s = getString("Enter a string");
      c = getChar("Enter a char");
      i = getInt("Enter an int");
      f = getFloat("Enter a float");
      
      println("The string was " + s);
      println("The char was " + c);
      println("The int was " + i);
      println("The float was " + f);
    } 
  7.  
    size(500,500);
    
    // the rectangle
    stroke(255,0,0);
    fill(255,0,0);
    rect(400,400,100,100);
    
    //the circle
    stroke(0,255,0);
    fill(0,255,0);
    ellipse(50,50,100,100);
    
    // the triangle
    stroke(0);
    fill(0,0,255);
    beginShape();
    vertex(150,150);
    vertex(150,250);
    vertex(300,200);
    endShape(CLOSE); 
    or if you wanted to use the predefined colours (in another tab):
    void setup()
    {
      size(500,500);
    }
    
    void draw()
    {
      // the rectangle
      stroke(RED);
      fill(RED);
      rect(400,400,100,100);
      
      //the circle
      stroke(GREEN);
      fill(GREEN);
      ellipse(50,50,100,100);
      
      // the triangle
      stroke(BLACK);
      fill(BLUE);
      beginShape();
      vertex(150,150);
      vertex(150,250);
      vertex(300,200);
      endShape(CLOSE);
    }