Reading assignment for Lab 03: Chapter 5

Expressions:

 2

 "abc"

 3 + 2

 3 < 2

Every expression has a value. 

What is the value of 3 < 2 (and/or its type)?

What is a type? 

3 < 2 is a boolean. Its value is false. 

A type is a set of values with some operations defined on it. 

Examples of types:

  int

  double 

  boolean

What is the set boolean and what operations do we have defined on it? 

boolean is this set: {true, false}

The operations are: 
  
  && this is the binary operation "and" (conjunction)

  || this is "or" also a binary operation (disjunction) 

  ! the unary operation "not" (negation)

Truth tables for these operations 

  p     q     p && q    p || q     !p 
 ---------------------------------------
 true  true   true      true      false 
 true  false  false     true      
 false true   false     true      true 
 false false  false     false 

These expressions are equivalent (x is an integer):

  x * 0      0

  x * 1      x

  x - x      0

I call the equivalent expressions on the right: simplifications. 

Please simplify the following expression(s) where p is a boolean variable: 
  
  p == true    is the same as   p 

  p        p == true        p
 ---------------------------------
 true      true            true 
 false     false           false 

  p && !p   ... simplifies to false

  p     !p      p && (!p)    false 
 ----------------------------------
 true   false   false        false 
 false  true    false        false 


  p || ! p


  p == false can be simplified to !p


  p       p == false     !p 
-------------------------------
 true     false          false   
 false    true           true 


Assume that you have n and m two integers. 

Can you calculate the biggest of the two? 

import java.util.*; 

public class One {
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);  
    System.out.print("First number: "); 
    int n = in.nextInt(); 
    System.out.print("Second number: "); 
    int m = in.nextInt(); 
    if (n < m) {
      System.out.println( m );  
    } else {
      System.out.println( n );  
    }
    System.out.println( Math.max( n, m ) ); 
    System.out.println( (n < m) ? m : n ); 
    // do it again using only + - / Math.abs(...) and the digit 2

    System.out.println( (n + m) / 2 + Math.abs(n - m) / 2 ); 

    System.out.println( (n + m + Math.abs(n - m)) / 2 ); 
    
  }
}

Next we run some programs in DrJava and from the command line in Windows:

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\WINDOWS\system32>cd
C:\WINDOWS\system32

C:\WINDOWS\system32>cd \

C:\>cd Users

C:\Users>cd dgerman

C:\Users\dgerman>cd Desktop

C:\Users\dgerman\Desktop>PATH="C:\Program Files\Java\jdk1.8.0_102\bin";%PATH%

C:\Users\dgerman\Desktop>javac
Usage: javac <options> <source files>
where possible options include:
  -g                         Generate all debugging info
  -g:none                    Generate no debugging info
  -g:{lines,vars,source}     Generate only some debugging info
  -nowarn                    Generate no warnings
  -verbose                   Output messages about what the compiler is doing
  -deprecation               Output source locations where deprecated APIs are used
  -classpath <path>          Specify where to find user class files and annotation processors
  -cp <path>                 Specify where to find user class files and annotation processors
  -sourcepath <path>         Specify where to find input source files
  -bootclasspath <path>      Override location of bootstrap class files
  -extdirs <dirs>            Override location of installed extensions
  -endorseddirs <dirs>       Override location of endorsed standards path
  -proc:{none,only}          Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...] Names of the annotation processors to run; bypasses default discovery process
  -processorpath <path>      Specify where to find annotation processors
  -parameters                Generate metadata for reflection on method parameters
  -d <directory>             Specify where to place generated class files
  -s <directory>             Specify where to place generated source files
  -h <directory>             Specify where to place generated native header files
  -implicit:{none,class}     Specify whether or not to generate class files for implicitly referenced files
  -encoding <encoding>       Specify character encoding used by source files
  -source <release>          Provide source compatibility with specified release
  -target <release>          Generate class files for specific VM version
  -profile <profile>         Check that API used is available in the specified profile
  -version                   Version information
  -help                      Print a synopsis of standard options
  -Akey[=value]              Options to pass to annotation processors
  -X                         Print a synopsis of nonstandard options
  -J<flag>                   Pass <flag> directly to the runtime system
  -Werror                    Terminate compilation if warnings occur
  @<filename>                Read options and filenames from file


C:\Users\dgerman\Desktop>

import javax.swing.JFrame; 

public class One {
  public static void main(String[] args) {
    JFrame james; 
    james = new JFrame(); 
    james.setVisible(true); 
    james.setSize(200, 500); 
  } 
}

Let me end with this example.

public class Fraction {
  int num, den; 
  public Fraction(int num, int b) {
    this.num = num;
    den = b;
  }
  public void talk() {
    System.out.println("I am a Fraction with ID: " + this + " with value " + num + "/" + den); 
  }
}

Here's how this runs in DrJava:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> Fraction a = new Fraction(2, 3)
> Fraction b = new Fraction(2, 3)
> a
Fraction@4ade1e49
> b
Fraction@9f02e8e
> a.talk()
I am a Fraction with ID: Fraction@4ade1e49 with value 2/3
> b.talk()
I am a Fraction with ID: Fraction@9f02e8e with value 2/3
> a.num = 6
6
> a.den = 21
21
> a.talk()
I am a Fraction with ID: Fraction@4ade1e49 with value 6/21