****************0.1******************************** ****************************************************** //// Practice evaluating an expression. class Main { public static void main(String[] args) { double result; result= // WRITE ANY EXPRESSION BELOW // 1+772.3/(333.0*2.0/7.0) // WRITE AN EXPRESSION ABOVE, THEN CLICK THE RUN BUTTON // ; System.out.print( "The result is: " ); System.out.println( result ); } } ****************1.5******************************** ****************************************************** class Main { public static void main(String[] args) { double result; result= (9.5*4.5-2.5*3)/(45.5-3.5); System.out.print( "The result is: " ); System.out.println( result ); } } ****************1.7******************************** ****************************************************** //Exercise 1.7 Compute PI //Zhilin Kuang class Main { public static void main(String[] args) { double result1; result1= 4.0*(1.0-1.0/3.0+1.0/5.0-1.0/7.0+1.0/9.0-1.0/11.0) ; System.out.print( "The result1 is: " ); System.out.println( result1 ); double result2; result2= 4.0*(1.0-(1.0/3.0)+(1.0/5.0)-(1.0/7.0)+(1.0/9.0)-(1.0/11.0)+(1.0/13.0)) ; System.out.print( "The result2 is: " ); System.out.println( result2 ); } } ****************2.1******************************** ****************************************************** //// Exercise 2.1: Convert Centigrade to Fahrenheit. //// Zhilin Kuang import java.util.Scanner; // Scanner class reads input. class Main { public static void main(String[] args) { double tempC, tempF; // 0. SETUP: Create Scanner object for console input. Scanner input = new Scanner ( System.in ); // 1. INPUT: Prompt and read C temperature. System.out.print ( "Enter a dergee in Celsius:" ); tempC = input.nextDouble () ; // 2. PROCESS: Convert to F. tempF = tempC * ( 9.0 / 5.0 ) + 32.0 ; // 3. OUTPUT: Display the result. System.out.println ( tempC + " Celcius " + " is " + tempF + "Fahrenheit." ); } } ****************2.6******************************** ****************************************************** //// Exercise 2.6: Sum the digits of a number. //// Zhilin import java.util.Scanner; class Main { //// public static void main(String[] args) { Scanner input = new Scanner ( System.in ); System.out.print ( "Please enter an integer between 0 and 1000: "); int n = input.nextInt (); int answer = (n / 1000) + (n % 10) + ((n % 100)/10) + ((n % 1000)/100) ; System.out.println ( "The sum of all its digits is: " + answer + "."); } } ****************2.7******************************** ****************************************************** //// Exercise 2.7: Number of years and days, //// Zhilin Kuang import java.util.Scanner; class Main { public static void main (String [] args){ Scanner input = new Scanner ( System.in ); System.out.print ("Enter the number of minutes you wish to convert: "); int sec = input.nextInt (); int year = sec/ ( 365 * 1440); int day = (sec % ( 365 * 1440))/1440; System.out.println ( sec + " seconds is approximately " + year + " years and " + day + " days." ); } } ****************2.15******************************** ****************************************************** //// Exercise 2.15: Distance between two points (x1,y1) and (x2,y2) //// Zhilin Kuang import java.util.Scanner; class Main { public static void main (String [] args){ //declare doubles. double x1, x2, y1, y2, d; //input numbers. Scanner input = new Scanner ( System.in ); System.out.print ("Enter x1 and y1: "); x1 = input.nextDouble (); y1 = input.nextDouble (); System.out.print ("Enter x2 and y2: "); x2 = input.nextDouble (); y2 = input.nextDouble (); //calculate distance. d= Math.sqrt (Math.pow((x2-x1),2)+Math.pow((y2-y1),2)); //output result. System.out.println ( "The distance between the two points is " + d ); } } ****************2.17******************************** ****************************************************** //// Exercise 2.17: Wind chill. //// Zhilin Kuang import java.util.Scanner; class Main { public static void main ( String [] args) { double Ta, W ; Scanner input = new Scanner (System.in); System.out.print ( "Enter the temperature in Fahrenheit" + " between -58F and 41F:"); Ta = input.nextDouble (); if ( Ta < -58.0 || Ta > 41 ) { System.out.println( " SORRY! Temperature is out" + " of range. "); System.out.print ( "Enter the temperature in Fahrenheit" + " between -58F and 41F:"); Ta = input.nextDouble (); } else { System.out.print ( "Enter the wind speed:"); W = input.nextDouble (); double t = 35.74 + 0.6215 * Ta - 35.75 * Math.pow ( W,0.16) + 0.4275 * Ta * Math.pow( W,0.16); double roundOff = (double) Math.round(t*100)/100; System.out.println(roundOff); System.out.print ("The wind chill index is " + roundOff + "."); } } } ****************2.19******************************** ****************************************************** //// Exercise 2.19: Area of a triangle (given 3 sides). //// Zhilin Kuang import java.util.Scanner; class Main { public static void main (String [] args){ double x1, y1, x2, y2, x3, y3, s1, s2, s3, s, area; System.out.print ( "Enter x1:"); Scanner input = new Scanner ( System.in ); x1 = input.nextDouble (); System.out.print ( "Enter y1:"); y1 = input.nextDouble (); System.out.print ( "Enter x2:"); x2 = input.nextDouble (); System.out.print ( "Enter y2:"); y2 = input.nextDouble (); System.out.print ( "Enter x3:"); x3 = input.nextDouble (); System.out.print ( "Enter y3:"); y3 = input.nextDouble (); s1 = Math.sqrt((( Math.pow ((x1-x2), 2)) + Math.pow ((y1-y2), 2))); s2 = Math.sqrt((( Math.pow ((x3-x2), 2)) + Math.pow ((y3-y2), 2))); s3 = Math.sqrt((( Math.pow ((x1-x3), 2)) + Math.pow ((y1-y3), 2))); s = (s1+s2+s3)/2; area = Math.sqrt (s*(s-s1)*(s-s2)*(s-s3)); double roundOff = (double) Math.round(area*100)/100 ; System.out.println ( "The area of this triangle is: " + roundOff + "."); } } ****************2.23******************************** ****************************************************** //// Exercise 2.23: Compute the cost of driving. //// Zhilin Kuang import java.util.Scanner; class Main { public static void main ( String [] args) { double miles, price, mpg, cost; Scanner input = new Scanner (System.in); System.out.print ( "Enter the driving distance in miles: "); miles = input.nextDouble(); System.out.print ( "Enter miles per gallon: "); mpg = input.nextDouble (); System.out.print ( "Enter price per gallon: "); price = input.nextDouble(); cost = (miles/mpg)*price; System.out.println ( "The cost of driving is: $" + cost); } } ****************P01******************************** ****************************************************** //// Project #1: Compute the amount due for purchases (after tax). //// Zhilin Kuang import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); double amount; double taxRate= 0.08625; int pen, pap, era; System.out.print ("Please enter the amount of pencils you need: "); pen = input.nextInt (); System.out.print ("Please enter the amount of sheets of paper you need: "); pap = input.nextInt (); System.out.print ("Please enter the amount of erasers you need: "); era = input.nextInt (); amount = (pen*0.15 + pap * 0.01 + era * 0.5) * (1 + taxRate) ; double roundOff = (double) Math.round(amount *100)/100 ; System.out.println ("The total amount due: $" + roundOff + "."); } } ****************Profit or loss.******************************** ****************************************************** //// Profit or loss. //// B.A.Martin import java.util.Scanner; class Main { public static void main(String[] args) { // 0. SETUP: Declare variables for input & output. Also Scanner. double revenues, expenses; double profit; Scanner read; read= new Scanner( System.in ); // 1. Get the inputs: revenu & expenses. System.out.print( "How much money did you spend? $"); expenses= read.nextDouble(); System.out.print( "How much money did you collect? $"); revenues= read.nextDouble(); // 2. Compute the profit (or loss, if negative). profit= revenues - expenses; // 3. Display the results: profit (or loss) System.out.println( "Your profit was: $" + profit ); if (profit < 0) { System.out.println( " SORRY! That was a LOSS, not a profit." ); } } } ****************Kinematics example******************************** ****************************************************** //// Kinematics example: compute the height of a building. //// B.A.Martin '6918 class Main { public static void main(String[] args) { final double g= 10; // Acceleration of gravity (approx 10 meters per second-squared) double vInitial, vFinal; double time; double height; // Unknown // Problem 1: A rock is dropped from a building; it hits the ground after ten seconds. // How high was the building? time= 5; vInitial= 0; // The rock was at rest when dropped. // The final velocity, after 10 seconds, is: vFinal= vInitial + g * time; // Now, use the formula: vFinal-squared - vInitial-squared = 2 g d // where d is the distance (and g is acceleration of gravity). // Therefore, d= (vFinal-squared - vInitial-squared) / (2 a) height= ( Math.pow(vFinal,2) - Math.pow(vInitial,2) ) / (2*g); System.out.println( "The building is " + height + " meters high." ); // Convert meters to feet. double hf; // Height, in feet. hf= height / 0.0254 / 12; hf= hf - hf%.01; System.out.println( "The building is " + hf + " feet high." ); } } ****************p03******************************** ****************************************************** //// Project 3: Compute the mean, median, mode, and variance of three integer values. //// Zhilin Kuang import java.util.Scanner; class Main { public static void main(String[] args) { // 1. The three input values. int a, b, c, median, mode; double mean; double variance, sigma, sd; Scanner input = new Scanner (System.in); System.out.print ("Enter 3 integers: ") ; a = input.nextInt() ; b = input.nextInt (); c = input.nextInt (); // 2. Calculation. //find mean. mean = (a + b + c)/3; //find mode. if ( a == b ) { mode = a; System.out.println( "The mode is: " + mode ); } else if ( a == c ) { mode = a; System.out.println( "The mode is: " + mode ); } else if ( b == c ) { mode = b; System.out.println( "The mode is: " + mode ); } else { System.out.println( "There is no mode." ); } //find median. if ( a>b ) { if ( b>c ){ median = b; } else if ( c>a){ median = a; } else { median = c; } } else { if ( b