Skip to content
Browse files

chapter 06 exercises

2-4, 9
  • Loading branch information...
1 parent 22e1142 commit 2438aa3afbe6b80612e64c2896ecfe7d861a9115 bcc32 committed Apr 10, 2012
Showing with 126 additions and 0 deletions.
  1. +53 −0 c6n3.java
  2. +20 −0 c6n9.java
  3. +53 −0 ch06.html
View
53 c6n3.java
@@ -0,0 +1,53 @@
+// Aaron Zeng 20120410
+// Chapter 06 Exercise 03
+
+public class c6n3
+{
+ public static void main( String[] args )
+ {
+ // Math.abs( x )
+ System.out.printf( "23.7 = %f\n", Math.abs( 23.7 ) );
+ System.out.printf( "0.0 = %f\n", Math.abs( 0.0 ) );
+ System.out.printf( "23.7 = %f\n", Math.abs( -23.7 ) );
+
+ // Math.ceil( x )
+ System.out.printf( "10.0 = %f\n", Math.ceil( 9.2 ) );
+ System.out.printf( "-9.0 = %f\n", Math.ceil( -9.8 ) );
+
+ // Math.cos( x )
+ System.out.printf( "1.0 = %f\n", Math.cos( 0.0 ) );
+
+ // Math.exp( x )
+ System.out.printf( "2.71828 = %f\n", Math.exp( 1.0 ) );
+ System.out.printf( "7.38906 = %f\n", Math.exp( 2.0 ) );
+
+ // Math.floor( x )
+ System.out.printf( "9.0 = %f\n", Math.floor( 9.2 ) );
+ System.out.printf( "-10.0 = %f\n", Math.floor( -9.8 ) );
+
+ // Math.log( x )
+ System.out.printf( "1.0 = %f\n", Math.log( Math.E ) );
+ System.out.printf( "2.0 = %f\n", Math.log( Math.E * Math.E ) );
+
+ // Math.max( x, y )
+ System.out.printf( "12.7 = %f\n", Math.max( 2.3, 12.7 ) );
+ System.out.printf( "-2.3 = %f\n", Math.max( -2.3, -12.7 ) );
+
+ // Math.min( x, y )
+ System.out.printf( "2.3 = %f\n", Math.min( 2.3, 12.7 ) );
+ System.out.printf( "-12.7 = %f\n", Math.min( -2.3, -12.7 ) );
+
+ // Math.pow( x, y );
+ System.out.printf( "128.0 = %f\n", Math.pow( 2.0, 7.0 ) );
+ System.out.printf( "3.0 = %f\n", Math.pow( 9.0, 0.5 ) );
+
+ // Math.sin( x )
+ System.out.printf( "0.0 = %f\n", Math.sin( 0.0 ) );
+
+ // Math.sqrt( x )
+ System.out.printf( "30.0 = %f\n", Math.sqrt( 900.0 ) );
+
+ // Math.tan( x )
+ System.out.printf( "0.0 = %f\n", Math.tan( 0.0 ) );
+ }
+}
View
20 c6n9.java
@@ -0,0 +1,20 @@
+// Aaron Zeng 20120410
+// Chapter 06 Exercise 09
+
+import java.util.*;
+import java.io.*;
+
+public class c6n9
+{
+ public static void main( String[] args )
+ {
+ Scanner input = new Scanner( System.in );
+ System.out.print( "Enter some numbers: " );
+ while ( input.hasNextDouble() )
+ {
+ double x = input.nextDouble();
+ System.out.printf( "Original number: %f\n", x );
+ System.out.printf( "Rounded: %d\n", (int)Math.floor( x + 0.5 ) );
+ }
+ }
+}
View
53 ch06.html
@@ -0,0 +1,53 @@
+<html>
+ <head>
+ <title>Chapter 06 Exercises</title>
+ <style type="text/css">
+ * {
+ font-family: "Ubuntu";
+ }
+ code {
+ font-family: "Ubuntu Mono";
+ }
+ ol.q1 {
+ list-style-type: decimal;
+ }
+ ol.q2 {
+ list-style-type: lower-latin;
+ }
+ </style>
+ </head>
+ <body>
+ <ol class="q1">
+ <li><ol class="q2">
+ <li>A method is invoked with a <u>method call</u>.
+ <li>A variable known only within the method in which it's declared is called a <u>local variable</u>.
+ <li>The <u>return</u> statement in a called method can be used to pass a value of an expression back to the calling method.
+ <li>The keyword <u>void</u> indicates that a method does not return a value.
+ <li>Data can be added or removed only from the <u>top</u> of a stack.
+ <li>Stacks are known as <u>last in, first out (LIFO)</u> data structures&emdash;the last item pushed (inserted) on the stack is the first item popped (removed) from the stack.
+ <li>The three ways to return control from a called method to a caller are <u><code>return;</code></u>, <u><code>return <i>expr</i>;</code></u>, and <u>reaching the end of the method</u>.
+ <li>An object of class <u><code>Random</code></u> produces random numbers.
+ <li>The program-execution stack contains the memory for local variables on each invocation of a method during a program's execution. These data, stored as a portion of the program-execution stack, is known as the <u>activation record</u>, or <u>stack frame</u> of the method call.
+ <li>If there are more method calls than can be stored on the program-execution stack, an error known as a <u>stack overflow</u> occurs.
+ <li>The <u>scope</u> of a declaration is the portion of a program that can refer to the entity in the declaration by name.
+ <li>It's possible to have several methods with the same name that each operate on different types or numbers of arguments. This feature is called method <u>overloading</u>.
+ <li>The program-execution stack is also referred to as the <u>method-call</u> stack.
+ </ol>
+ <li><ol class="q2">
+ <li>The variable <code>randomNumbers</code> is in scope throughout class <code>Craps</code>.
+ <li>The variable <code>die1</code> is in scope throughout method <code>rollDice()</code>.
+ <li>The method <code>rollDice()</code> is in scope throughout class <code>Craps</code>.
+ <li>The method <code>play()</code> is in scope throughout class <code>Craps</code>.
+ <li>The variable <code>sumOfDice</code> is in scope throughout method <code>play()</code>.
+ </ol>
+ </ol>
+ <ol class="q1" start=4>
+ <li><ol class="q2">
+ <li><code>double hypotenuse( double side1, double side2 )</code>
+ <li><code>int smallest( int x, int y, int z )</code>
+ <li><code>void instructions()</code>
+ <li><code>float intToFloat( int number )</code>
+ </ol>
+ </ol>
+ </body>
+</html>

0 comments on commit 2438aa3

Please sign in to comment.
Something went wrong with that request. Please try again.