Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes:  Ch 2:  Data and Expressions (2 of 2)


Logistics

  1. Schedule
    1. Quiz 1 returned today
    2. Hw2 due tomorrow
      1. Tonight's Office Hours
        DK  4:30 -  5:30  (5215)
        JE  7:30 -  8:30  (5419)
        GB  9:30 - 10:30  (5419)
    3. DK:  "off the grid" from late tonight until Tuesday morning
    4. Quiz 2 on Tuesday
      Covers all material through hw2 and last lecture (not this lecture)
    5. Hw3 due next Friday
  2. Reading:
    1. L&L Chapter 2:  Data and Expressions
      Sections 2.1 - 2.2 and 2.6 (last lecture)
      Sections 2.3 - 2.5, 2.7, 2.9 (today)
      Note:  while we will be drawing graphics at times, we will not use applets (unlike the book) to do so.
    2. Also see:  BasicGraphics.java and BasicGraphicsDemo1.java

Topic Outline:

  • Data and Expressions (cont)
     
    1. Different-sized integers
      public class MyCode {
         public static void main(String[] args) {
            // Demonstrate the different integer primitive data types
            long  xlMax = Long.MAX_VALUE;
            int   xiMax = Integer.MAX_VALUE;
            short xsMax = Short.MAX_VALUE;
            char  xcMax = Character.MAX_VALUE;
            byte  xbMax = Byte.MAX_VALUE;
      
            long  xlMin = Long.MIN_VALUE;
            int   xiMin = Integer.MIN_VALUE;
            short xsMin = Short.MIN_VALUE;
            char  xcMin = Character.MIN_VALUE;
            byte  xbMin = Byte.MIN_VALUE;
      
            // Note:  you do not yet need to know about "printf" (soon...)
            System.out.printf("%5s %25s %25s\n","type","max","min");
            System.out.printf("%5s %25d %25d\n","long", xlMax,xlMin);
            System.out.printf("%5s %25d %25d\n","int",  xiMax,xiMin);
            System.out.printf("%5s %25d %25d\n","short",xsMax,xsMin);
            System.out.printf("%5s %25d %25d\n","char", (int)xcMax, (int)xcMin);
            System.out.printf("%5s %25d %25d\n","byte", xbMax,xbMin);
         }
      }
    2. Floating-point numbers
      public class MyCode {
         public static void main(String[] args) {
            // Demonstrate the different integer primitive data types
            float  xfMax = Float.MAX_VALUE;
            double xdMax = Double.MAX_VALUE;
      
            float  xfMin = Float.MIN_VALUE;  // Not what you may expect!!!
            double xdMin = Double.MIN_VALUE;
      
            // Note:  you do not yet need to know about "printf" (soon...)
            System.out.printf("%5s %25s %25s\n","type","max","min");
            System.out.printf("%5s %25g %25g\n","float",  xfMax,xfMin);
            System.out.printf("%5s %25g %25g\n","double", xdMax,xdMin);
         }
      }
    3. Characters
      class MyCode {
        public static void main(String[] args) {
          char c1 = 'A';
          char c2 = (char)66;
          char c3 = "ABCD".charAt(2);
          System.out.println("c1 = '" + c1 + "'");
          System.out.println("c2 = '" + c2 + "'");
          System.out.println("c3 = '" + c3 + "'");
        }
      }
    4. Booleans
      class MyCode {
        public static void main(String[] args) {
           boolean b1 = true;
           boolean b2 = false;
           boolean b3 = (b1 && b2);  // b1 AND b2
           boolean b4 = (b1 || b2);  // b1 OR b2
           boolean b5 = !b1;         // NOT b1
           System.out.println("b1 = " + b1);
           System.out.println("b2 = " + b2);
           System.out.println("b3 = " + b3);
           System.out.println("b4 = " + b4);
           System.out.println("b5 = " + b5);
       }
      }
    5. integer vs floating-point division
      class MyCode {
        public static void main(String[] args) {
          int i = 10;
          double d = 10;
          System.out.println( 10 / 3 );
          System.out.println(  i / 3 );
          System.out.println(  d / 3 );
          System.out.println( 10 / 3.0 );
          System.out.println(  i / 3.0 );
        }
      }
    6. Approximate values of floating-point numbers
      class MyCode {
        public static void main(String[] args) {
          double d1 = (29 / 7.0) * 7.0;
          double d2 = 29;
          System.out.println(d1 == d2);
          System.out.println(d2 - d1);
        }
      }
    7. Operator Precedence and Association

      See Figure 2.4 (p. 78)
       

    8. Increment and Decrement Operators

      a)  As Statements

      class MyCode {
        public static void main(String[] args) {
          int x = 5;
          System.out.println(x); // 5
          x++;
          System.out.println(x); // 6
          ++x;
          System.out.println(x); // 7
          x--;
          System.out.println(x); // 6
          --x;
          System.out.println(x); // 5
        }
      }

      b)  As Expressions

      class MyCode {
        public static void main(String[] args) {
          int x = 5, y = 0;
          System.out.println(x + "," + y); // 5,0
          y = x++;
          System.out.println(x + "," + y); // 6,5
          y = ++x;
          System.out.println(x + "," + y); // 7,7
          y = 10 + x--;
          System.out.println(x + "," + y); // 6,17
          y = 10 + --x;
          System.out.println(x + "," + y); // 5,15
        }
      }
    9. Assignment Operators
      class MyCode {
        public static void main(String[] args) {
          int x = 5;
          System.out.println(x); // 5
          x += 2;
          System.out.println(x); // 7
          x *= 2;
          System.out.println(x); // 14
          x %= 9;
          System.out.println(x); // 5
          x /= 2;
          System.out.println(x); // 2
          x -= 5;
          System.out.println(x); // -3
        }
      }
    10. Data Conversion

      a)  Widening (automatic) versus Narrowing (manual, lossy)

      b)  Assignment Conversion

      class MyCode {
        public static void main(String[] args) {
           int a = 5;
           double b = a; // Assignment conversion!  
           System.out.println("a = " + a); // 5
           System.out.println("b = " + b); // 5.0
        }
      }

      c)  Promotion

      class MyCode {
        public static void main(String[] args) {
           int a = 5;
           double b = (2.0 + a); // Promotion!  
           System.out.println("a = " + a); // 5
           System.out.println("b = " + b); // 7.0
        }
      }

      d)  Casting

      class MyCode {
        public static void main(String[] args) {
           double a = 5.0;
           int b    = a; // Will not compile!
           System.out.println("a = " + a);
           System.out.println("b = " + b);
        }
      }

      So we cast the double into an int:

      class MyCode {
        public static void main(String[] args) {
           double a = 5.0;
           int b    = (int) a; // Casting!
           System.out.println("a = " + a); // 5.0
           System.out.println("b = " + b); // 5
        }
      }

      Note that casting truncates:

      class MyCode {
        public static void main(String[] args) {
           int a = (int)5.9;
           int b = (int)-5.9;
           System.out.println("a = " + a); // 5
           System.out.println("b = " + b); // -5
        }
      }
    11. More on the Scanner

      a)  From p. 87:  Just need to know the methods next***()
      next(), nextLine(), nextBoolean(), nextByte(), etc...

      b)  next() versus nextLine()

      Does not work quite as you might expect:
        * next returns next input token as a String
        * nextLine returns the rest of the current line as a String
        * they do not mix-and-match well!

      class MyCode {
        public static java.util.Scanner scanner = new java.util.Scanner(System.in);
        public static void main(String[] args) {
           String s1, s2;
           System.out.print("Enter a few words: ");
           s1 = scanner.next();
           System.out.print("Enter a few other words: ");
           s2 = scanner.nextLine();
           System.out.println("s1 = '" + s1 + "'");
           System.out.println("s2 = '" + s2 + "'");
        }
      }

      Do this again, but just use next, not nextLine:

      class MyCode {
        public static java.util.Scanner scanner = new java.util.Scanner(System.in);
        public static void main(String[] args) {
           String s1, s2, s3;
           System.out.print("Enter a few words: ");
           s1 = scanner.next();
           s2 = scanner.next();
           s3 = scanner.next();
           System.out.println("s1 = '" + s1 + "'");
           System.out.println("s2 = '" + s2 + "'");
           System.out.println("s3 = '" + s3 + "'");
        }
      }

      Once more, but just use nextLine, not next:

      class MyCode {
        public static java.util.Scanner scanner = new java.util.Scanner(System.in);
        public static void main(String[] args) {
           String s1, s2;
           System.out.print("Enter a few words: ");
           s1 = scanner.nextLine();
           System.out.print("Enter a few other words: ");
           s2 = scanner.nextLine();
           System.out.println("s1 = '" + s1 + "'");
           System.out.println("s2 = '" + s2 + "'");
        }
      }


    Graphics

     

    1. Applet  vs  Application
       
    2. Writing graphics code in an Application:  BasicGraphics.java
      // BasicGraphics.java
      
      // A simple shell for drawing graphics as described in our textbook.
      // Note that this is an *application* whereas the book uses *applets*.
      // This should have little impact on you, however.
      
      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import javax.swing.*;
      
      public class BasicGraphics extends JComponent {
         
         public static void main(String[] args) {
            JComponent myGraphics = new BasicGraphics(); // Your class name goes here!
            launch(myGraphics, 500, 300); // Set the initial dimensions here!
         }
         
         public void paint(Graphics page) {
            int width = getWidth();
            int height = getHeight();
      
            // paint a blue rectangle as large as the window      
            page.setColor(Color.blue);
            page.fillRect(0,0,width,height);
            
            // paint a yellow oval inscribed in the blue rectangle
            page.setColor(Color.yellow);
            page.fillOval(0,0,width,height);
         }
      
         //////////////////////////////////////////////////////////////////////////////
         /////////////////           END OF YOUR CODE             /////////////////////
         /////////////////  (you may ignore all the code below!!! /////////////////////
         //////////////////////////////////////////////////////////////////////////////
      
         public static void launch(JComponent jc, int width, int height) {
            JFrame frame = new JFrame(jc.getClass().getName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel cp = new JPanel();
            cp.setLayout(new BorderLayout());
            cp.add(jc);
            frame.setContentPane(cp);
            frame.setSize(new Dimension(width,height));
            frame.setVisible(true);
         }
      }
    3. More Graphics:  BasicGraphicsDemo1.java

      // BasicGraphicsDemo1.java

      // A demo of some simple graphics operations, starting from BasicGraphics.java

      import java.awt.*;
      import java.awt.event.*;
      import java.util.*;
      import javax.swing.*;

      public class BasicGraphicsDemo1 extends JComponent {
        
         public static void main(String[] args) {
            JComponent myGraphics = new BasicGraphicsDemo1(); // Your class name goes here!
            launch(myGraphics, 500, 300); // Set the initial dimensions here!
         }
        
         public void paint(Graphics page) {
            int width = getWidth();
            int height = getHeight();

            // filled blue rectangle as large as the window     
            page.setColor(Color.blue);
            page.fillRect(0,0,width,height);
           
            // filled yellow oval inscribed in the blue rectangle
            page.setColor(Color.yellow);
            page.fillOval(0,0,width,height);
           
            // unfilled (drawn) black rectangle on the left
            page.setColor(Color.black);
            page.drawRect(10,10,width/2-20,height-20);
           
            // unfilled thick-lined black ovals on the right
            ((Graphics2D)page).setStroke(new BasicStroke(5)); // 5 pixels wide
            page.drawOval(width/2+10,10,width/2-20,height/2-20);
            page.drawOval(width/2+10,height/2+10,width/2-20,height/2-20);

            // unfilled thin-lined red triangle on the right
            ((Graphics2D)page).setStroke(new BasicStroke(1)); // set back to 1 pixels wide
            page.setColor(Color.red);
            Polygon p1 = new Polygon();
            p1.addPoint(3*width/4,height/3);  // addPoint(x,y)
            p1.addPoint(width/2+40,2*height/3);
            p1.addPoint(width-40,2*height/3);
            page.drawPolygon(p1);
           
            // filled green pentagon in the middle
            page.setColor(Color.green);
            int pWidth = width/5;
            int pHeight = height/10;
            int pLeft = width/2  - pWidth/2;
            int pTop  = height/2 - pHeight/2;
            Polygon p2 = new Polygon();
            p2.addPoint(pLeft           , pTop);
            p2.addPoint(pLeft+pWidth/3  , pTop+pHeight);
            p2.addPoint(pLeft+2*pWidth/3, pTop+pHeight);
            p2.addPoint(pLeft+pWidth    , pTop);
            page.fillPolygon(p2);
           
            // custom-colored red rectangles in the middle
            page.setColor(new Color(100, 0, 0));  // RGB (Red, Green, Blue)
            page.fillRect(width/2-10, 10, 30, 30);
            page.setColor(new Color(200, 25, 25));
            page.fillRect(width/2-10, 50, 30, 30);
            page.setColor(new Color(255, 50, 50));
            page.fillRect(width/2-10, 90, 30, 30);
           
            // using trig to place a small blue circle at 70 degrees
            // in the first quadrant of an orange circle
            int radius = 50;
            page.setColor(Color.orange);
            // oval will be centered at (100,100)
            page.fillOval(100-radius,100-radius,2*radius,2*radius);
            // here we use 70 degrees
            int blueCenterX = 100 + (int)(radius*Math.cos(Math.PI*70/180));
            int blueCenterY = 100 - (int)(radius*Math.sin(Math.PI*70/180));
            int blueRadius = radius/10;
            page.setColor(Color.blue);
            page.fillOval(blueCenterX-blueRadius,blueCenterY-blueRadius,
                          2*blueRadius,2*blueRadius);
                         
            // draw a line connecting the centers of the orange and blue circles
            page.drawLine(100,100,blueCenterX,blueCenterY);
         }

         //////////////////////////////////////////////////////////////////////////////
         /////////////////           END OF YOUR CODE             /////////////////////
         /////////////////  (you may ignore all the code below!!! /////////////////////
         //////////////////////////////////////////////////////////////////////////////

         public static void launch(JComponent jc, int width, int height) {
            JFrame frame = new JFrame(jc.getClass().getName());
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JPanel cp = new JPanel();
            cp.setLayout(new BorderLayout());
            cp.add(jc);
            frame.setContentPane(cp);
            frame.setSize(new Dimension(width,height));
            frame.setVisible(true);
         }
      }

       

    carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem