CS 162 Lecture-Module #04:
More Details about Statements etc. (Chapter 2)


(Chapter 2 of our textbook)

actually first note official, thorough documentation for Java is at http://java.sun.com/
or more specifically http://java.sun.com/javase/6/docs/
particularly http://java.sun.com/javase/6/docs/api/ (API)
and http://java.sun.com/javase/6/docs/api/ (syntax)
plus http://java.sun.com/docs/books/tutorial/index.html (tutorials)
(the API is most useful)

primitive output

  •  System.out.println(...);
    produces output
    not graphical (NOT drawing circles/colors/...)
    just text
    where? 
    that name and this primitiveness reflects that this is
    very old fashioned




    those monitors with keyboards were called "terminals"
    screens had only characters

    versus more modern output: graphics, dialogs
    e.g. could add the following to  Picture.java:
    import javax.swing.*;
    JOptionPane.showMessageDialog(null, "It's a beautiful day!");

    anyway, you can apply  System.out.println(...);  to  Strings,  ints,  doubles,  floats,  chars, ...


  • by the way, to have single  char  in Java use 
    e.g.  


  • to have material continue on same line in output,
    use  System.out.print()  instead of  System.out.println()
    e.g. adding to  Circle.java:
     .
      .
       .
        private void draw()
        {
            if(isVisible) {
                
    


  • by the way, can include quotation marks inside  String  using  e.g.:
            System.out.print();
            ...
            System.out.println();
    alternatively rem.  "  is a single char so could use :
            System.out.println();


  • by contrast with that joining lines, to have more separated lines in output, do this to produce an empty line:
           


    numeric operations

    basic arithmetic ones as usual:
    +   -   *   / 
    e.g.  7 + 6 - 4 * 5 / 2
    for further mathematical functions, use Math.SOMETHING
    e.g.  Math.sqrt(196),  Math.sin(3),  Math.PI
    squaring/cubing/exponentiation(xy) do NOT have any specific operator, in particular NOT "^" (used for something else)
    instead use  Math.pow(x,y)  e.g.  Math.pow(3,2),  Math.pow(5,6)

    |String| operations

  • you can  material with  Strings using  +
    e.g. in  labclasses  project, in  LabClass.java, in method  printList():
            System.out.println("Lab class " + timeAndDay);
    when you do this, technically you get another result  String
    then you can concatenate further material with that  String
    e.g.  System.out.println("Instructor: " + instructor + "   room: " + room);
    e.g.  System.out.println("color is \"" + color + '"');
    e.g. to enable better viewing of code with some long output:
      System.out.println("Welcome to the Picture Drawing Program, which is a really nice demonstration of graphics and object-oriented programming.");
      System.out.println("Welcome to the Picture Drawing Program, "
            + "which is a really nice demonstration of graphics "
            + "and object-oriented programming.");
    

  •  .length()


  •  .indexOf(...)
    e.g.  color.indexOf('e')
    e.g.  color.indexOf('r')
    produces number of characters from  String's beginning to the target
    different from 'position' if counted characters starting at 1
    instead, here, consider counting characters in  String  starting at 0


  •  .substring(beginIndex, limitIndex)
    produces contents of  String  beginning at  beginIndex  and ending just before  limitIndex
    e.g. if  color  is  "green",
            color.substring(2,5)  would yield 
            color.substring(1,3)  would yield 




    if this way of doing output etc. is so old fashioned, do we need to learn any of it?
    well actually you use some of these operations to produce dialogs



    methods possibly returning values

    all the methods we've examined so far make objects do things
    e.g.  Circle.moveVertical(...),  Square.makeVisible()
    as they change objects' characteristics, such methods called 

    alternatively,  methods obtain information from objects

    such a method
    * needs to indicate what kind of thing it returns via return type
            String  or  int  or something, not just  void  used by mutators we've seen
    * returns a value via 
    e.g. add to  class Circle:

    then in  Picture.draw()  could do:
            System.out.println("initially, sun's color: " +  );

    further examples in  TicketMachine

    if, else

    arrange for program to do one thing or another depending on a condition
    syntax:
        if ( condition )
        {
            statements
        }
        else
        {
            statements
        }
    
    e.g.:
        if ( x < y )
        {
            System.out.println("The winner is " + x);
        }
        else
        {
            System.out.println("The winner is " + y);
        }
    
    syntax for condition:
    * compare values using "<", ">", "<=", ">=", "==", "!="
    * check boolean variable

    examples in programs we've been using


    (Copyright © 2009 by Hugh McGuire   — for thoughts about this, see   http://www.cis.gvsu.edu/~mcguire/teaching/copyright_thoughts.html .)