Permalink
Browse files

chapter 06 exercises cont.

8, 10
  • Loading branch information...
1 parent 2438aa3 commit 5b7ebc8aae5a03739195f01dc48143f3fc8fbbde bcc32 committed with bcc32 Apr 11, 2012
Showing with 69 additions and 0 deletions.
  1. +46 −0 c6n10.java
  2. +23 −0 c6n8.java
View
@@ -0,0 +1,46 @@
+// Aaron Zeng 20120411
+// Chapter 06 Exercise 11
+
+import java.util.*;
+import java.io.*;
+
+public class c6n10
+{
+ 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.println( "Rounded to nearest integer: " +
+ roundToInteger( x ) );
+ System.out.println( "Rounded to nearest tenth: " +
+ roundToTenths( x ) );
+ System.out.println( "Rounded to nearest hundredth: " +
+ roundToHundredths( x ) );
+ System.out.println( "Rounded to nearest thousandth: " +
+ roundToThousandths( x ) );
+ }
+ }
+
+ private static double roundToInteger( double number )
+ {
+ return Math.floor( number );
+ }
+
+ private static double roundToTenths( double number )
+ {
+ return Math.floor( number * 10 + 0.5 ) / 10;
+ }
+
+ private static double roundToHundredths( double number )
+ {
+ return Math.floor( number * 100 + 0.5 ) / 100;
+ }
+
+ private static double roundToThousandths( double number )
+ {
+ return Math.floor( number * 1000 + 0.5 ) / 1000;
+ }
+}
View
@@ -0,0 +1,23 @@
+// Aaron Zeng 20120411
+// Chapter 06 Exercise 08
+
+/*
+double calculateCharges( int hours ):
+ if hours <= 3:
+ return 2.00
+ else:
+ charge = 2 + Math.floor( hours - 3 ) * 0.50
+ if charge > 10.00:
+ charge = 10.00
+ return charge
+
+totalCharge = 0.00
+
+prompt: enter hours (-1 to quit)
+accept hours
+while hours != -1:
+ charge = calculateCharges( hours )
+ display charge
+ totalCharge += charge
+ display totalCharge
+ */

0 comments on commit 5b7ebc8

Please sign in to comment.