How do you do these in Java:

  (a) calculate the square root of 2

  (b) determine the value of pi

  (c) calculate 2.71828 to the power of 3.141592

  (d) calculate the logarithm in base n of m

  If n is 10 and m is 34875634785 what does the result mean? 

Here are the answers:

Welcome to DrJava.  Working directory is C:\Users\dgerman
> 1 + 2
3
> Math.sqrt(2)
1.4142135623730951


How else can I answer these questions? 

-bash-4.2$ cd c212-workspace/
-bash-4.2$ mkdir 08282017
-bash-4.2$ cd 08282017
-bash-4.2$ pwd
/u/dgerman/c212-workspace/08282017
-bash-4.2$ nano -w Egret.java
-bash-4.2$ javac Egret.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.2$ ls -l
total 8
-rw-r--r-- 1 dgerman faculty 776 Aug 28 14:55 Egret.class
-rw-r--r-- 1 dgerman faculty 591 Aug 28 14:55 Egret.java
-bash-4.2$ java Egret
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-=[ egret ]=-  12/96
          _,
     -==<' `\
         ) /
        / (_.
       | ,-,`\
        \\  \ \
         `\, \ \
          ||\ \`|,
   jgs   _|| `=`-'
        ~~`~`

-bash-4.2$ nano -w One.java
-bash-4.2$ javac One.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.2$ ls -ld *
-rw-r--r-- 1 dgerman faculty 776 Aug 28 14:55 Egret.class
-rw-r--r-- 1 dgerman faculty 591 Aug 28 14:55 Egret.java
-rw-r--r-- 1 dgerman faculty 251 Aug 28 14:56 One.class
-rw-r--r-- 1 dgerman faculty  70 Aug 28 14:56 One.java
-bash-4.2$ java One
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.2$ nano -w One.java
-bash-4.2$ nano -w One.java
-bash-4.2$ nano -w One.java
-bash-4.2$ javac One.java
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
-bash-4.2$ java One
Picked up _JAVA_OPTIONS: -Xms512m -Xmx512m
8.0
-bash-4.2$ cat One.java
public class One {
  public static void main(String[] args) {
    System.out.println( Math.pow( 2, 3 ) ); // 8
  }
}
-bash-4.2$ cat Egret.java
public class Egret {
  public static void main(String[] args) {
    System.out.println("-=[ egret ]=-  12/96          ");
    System.out.println("          _,");
    System.out.println("     -==<' `\\");
    System.out.println("         ) /");
    System.out.println("        / (_.");
    System.out.println("       | ,-,`\\");
    System.out.println("        \\\\  \\ \\");
    System.out.println("         `\\, \\ \\");
    System.out.println("          ||\\ \\`|,");
    System.out.println("   jgs   _|| `=`-'");
    System.out.println("        ~~`~`");
    System.out.println("");
  }
}
-bash-4.2$

Finally I can do this in DrJava. 

Welcome to DrJava.  Working directory is C:\Users\dgerman
> Math.pow(2.71828, 3.141592)
23.14062860742646
> Math.PI
3.141592653589793
> Math.E
2.718281828459045
> Math.pow(Math.E, Math.PI)
23.140692632779263


Here's the last one in DrJava:

public class One {
  public static void main(String[] args) {
    double n = 10, m = 348756834785L;
    System.out.println( Math.log( m ) / Math.log( n )); // about 11 
  }
}

Here's how it runs:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
11.542522727479716


Now let's discuss objects. 

So what objects have you been exposed to in java so far? 

We have seen and worked with objects of these types: 

    String   (it's a class)

    BigDecimal 

    Scanner 

    JFrame 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> import javax.swing.JFrame;
> JFrame a;
> a = new JFrame()
javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
> a
javax.swing.JFrame[frame0,0,0,0x0,invalid,hidden,layout=java.awt.BorderLayout,title=,resizable,normal,defaultCloseOperation=HIDE_ON_CLOSE,rootPane=javax.swing.JRootPane[,0,0,0x0,invalid,layout=javax.swing.JRootPane$RootLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=16777673,maximumSize=,minimumSize=,preferredSize=],rootPaneCheckingEnabled=true]
> a.setVisible(true)
> a.setSize(300, 400)


What about Strings? 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> String a;
> a = "whatever";
> a.length()
8
> a.charAt(3)
't'
> a.substring(4)
"ever"
> a.substring(1, 4)
"hat"
> a = new String("whatever");
> a
"whatever"


The fundamental unit of compilation in Java is the package. 

A package is just a folder. The current folder is the default package.

Every class is in some package. 

What package is Scanner in? 

Answer: java.util

What package is BigDecimal in? 

Answer: java.math

What package is JFrame in? 

Answer: javax.swing

Is Math a class? What package is it in. 

Answer: yes, java.lang (the only package whose classes are imported by default)

What package is String in? 

Answer: java.lang

Why do I never use the new operator with class Math?

What am I doing when I write Math.PI or Math.sqrt(...)? 

https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html

public class One {
  public static void main(String[] args) {
    javax.swing.JFrame a = new javax.swing.JFrame(); 
    a.setVisible(true); 
    a.setSize(400, 700); 
  }
}

Now I start a series of experiments as follows:

public class One {
  public static void main(String[] args) {
    System.out.println( "How are you?" ); 
  }
}

--

public class One {
  public static void main(String[] args) {
    System.out.println( System.out ); 
  }
}

This prints: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
java.io.PrintStream@7a646f39


---

System is a class it belongs to java.lang as well. 

public class One {
  public static void main(String[] args) {
    System.out.println( "Ha ha." ); 
  }
}

public class System {
  static PrintWriter out = new PrintWriter(); 
  public static void main(String[] args) {
    java.lang.System.out.println( "Not sure this will work..." );  
  }
}

public class PrintWriter {
  public void println(String something) {
    java.lang.System.out.println( "I don't want to print: " + something ); 
  }
}

This compiles and runs as follows:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
I don't want to print: Ha ha.


--