****************Q1P1******************************** ****************************************************** /*Write a program to compute the total amount due, including sales tax. Ask the user how many of each item are needed: Pencils are fifteen cents ($0.15) each. Paper is once cent ($0.01) per sheet. An eraser costs fifty cents ($0.50). The sales tax rate is of 8.625% (0.08625). Extra credit for rounding off the total to the nearest cent ($0.01).*/ //// PURPOSE: Calculate mean average of 5 numbers. //// AUTHOR: Zhilin Kuang import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); double a, b, c, d, e, avr; System.out.print ( "Enter your first number: "); a = input.nextDouble (); System.out.print ( "Enter your second number: "); b = input.nextDouble (); System.out.print ( "Enter your third number: "); c = input.nextDouble (); System.out.print ( "Enter your fourth number: "); d = input.nextDouble (); System.out.print ( "Enter your fifth number: "); e = input.nextDouble (); avr = (a+b+c+d+e)/5; System.out.println ( "The mean average of the five numbers you entered is: " + avr); } } ****************Q1P2******************************** ****************************************************** /*q1b: Write another java program (to do IPO): Compute the volume of a cylinder, given its radius and height. Input the radius and height. Compute the volume. Output the result q1b: = exercise 2.2 on page 69. Write a java program that computes the volume of a cylinder. The formula for volume of a cylinder is: volume = area * height The formula for the area of the circular base is area = PI * Math.pow( radius, 2 )*/ //// QUIZ 1, PROBLEM 2 Compute the volume of a cylinder //// Zhilin Kuang import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); double area, height, radius, volume; System.out.print("Enter the radius and height of a cylinder: "); radius = input.nextDouble(); height = input.nextDouble(); area = 3.1415926 * Math.pow (radius, 2); volume = area * height; System.out.println ("The volume of the cylinder is: " + volume); } } ****************Q1P3******************************** ****************************************************** //// r1c: REDO QUIZ #1 PROBLEM 3: roots of a quadratic. //// AUTHOR: Zhilin Kuang import java.util.Scanner; class Main { public static void main(String[] args) { Scanner input = new Scanner (System.in); double a, b, c, root, root2; // Input System.out.print ( "For a function f(x)=ax^2+bx+c, please enter coefficient a:"); a = input.nextDouble (); System.out.print ( "Enter coefficient b:"); b = input.nextDouble (); System.out.print ("Enter coefficient c:"); c = input.nextDouble (); // Process root = (-b + Math.sqrt (Math.pow(b,2)-4*(a*c)))/(2*a); root2 = (-b - Math.sqrt (Math.pow(b,2)-4*(a*c)))/(2*a); //Output System.out.print ( "The roots for this function are: " + root + " and " + root2); } } ****************Quiz 2******************************** ****************************************************** //// REDO #2 (November 2016) //// CST 112 [aft]: Zhilin Kuang import java.util.Scanner; class Main { static String lastName, firstName, winner, pw; static int areaCode, zipCode, n5, count=0, countl, countf, fac, n1; static double answer1, answer2, answer3, answer4, answer5, sd; static Scanner r= new Scanner( System.in ); //// MAIN PROGRAM //// public static void main(String[] args) { lastName= "Kuang"; firstName= "Zhilin"; // Title // say( "\nCST 112 [aft] Quiz # 2: " + lastName +" "+firstName ); answer1=answer2=answer3=answer4=answer5=countl=countf=0; // TASK 1. Count the number of consonants in each String (firstName and lastName), // then print a message saying which one (if any) has more of them. // count consonants in last name say("\nTask 1"); for (int i = 0; i < lastName.length(); i++) { if (isConsonant(lastName.charAt(i))) countl++; } // count consonants in first name for (int i = 0; i < firstName.length(); i++) { if (isConsonant(firstName.charAt(i))) countf++; } // compare numbers of consonants if (countf>countl){ winner=firstName; } else { winner=lastName; } // output result say ("There are " + countl + " consonants in your last name and " + countf+ " consonants in your first name."); say ( winner + " has more consonants."); // TASK 2. Check whether area code has exactly three digits; print error message if not. // Check whether area code has exactly three digits; print error message if not. say("\nTask 2"); while (countDigits (areaCode)!=3){ areaCode= ask( "What is your area code? "); if (countDigits(areaCode)!=3) { say("Invalid area code! "); } } while (countDigits (zipCode)!=5){ zipCode= ask( "What is your zip code? "); if (countDigits (zipCode)!=5){ say("Invalid zip code! "); } } // TASK 3. Check to see if each number (zipCode and areaCode) is divisible by // any of the first six primes (2, 3, 5, 7, 11, 13). say("\nTask 3"); for (int i=0; i<=13; i++) { if (isPrime(i) && (isMult( zipCode, i)){ say ("Zip code is divisible by", i); } } for (int i=0; i<=13; i++) { if (isPrime(i) && (isMult( areaCode, i)){ say ("Area code is divisible by", i); } } // TASK 4. Calculate and print the factorials of the numbers one to twelve. say("\nTask 4"); for (int i=1; i<=12; i++){ fac= factorial (i); System.out.println("The factorial of "+i+ " is "+fac+ " "); } // TASK 5. Prompt the user to enter a password; check if it follows these rues:. // -- between five and fifteen characters long. // -- exactly three digit characters. // -- more consonats than vowels. // -- at least one character that is not a letter or number. say("\nTask 5"); // Prompt to enter password pw= askString ("Enter your password:"); if (isValidPassword(pw)) { System.out.println("Valid password"); } else { System.out.println("Invalid password"); } // OPTIONAL: Instead of writing code here (in main), // OPTIONAL: write a separate method for each of the above // OPTIONAL: and call it with appropriate arguments. // Now, calculate the five answers, as described in the web page for Quiz #2. // // answer 1 int answer1=0; while (Math.pow(answer1,2) <=zipCode){ answer1++; } // answer 2 answer2 = sumFactorial(zipCode); // answer 3 answer3 = Math.toDegrees(Math.acos(3.0/5.0)); //answer 4 answer4 = Math.sqrt(Math.pow(zipCode,2) + Math.pow(areaCode,2)); // answer5 say ("\n"); do { n5=ask ( "Enter a non-zero number:" ); answer5+= Math.pow(n5, 2); count++; } while (n5!=0); // Standard deviation sd= Math.sqrt(answer5/(count-1)); // Show the answers say( "\nANSWERS:" ); say( "Answer #1 is: ", answer1 ); say( "Answer #2 is: ", answer2 ); say( "Answer #3 is: ", answer3 ); say( "Answer #4 is: ", answer4 ); say( "Answer #5 is: ", answer5 ); say( "Standard deviation of the numbers entered is", sd ); }// END of main()// //methods public static int factorial (int n){ int result=1; if (n==0){ return result; } else { for (int i=1; i<=n; i++){ result= result*i; } return result; } } public static int sumDigits( int n) { int result=0, remainder=0; for ( int i = n; i>0; i=i/10 ) remainder += i % 10 ; result += remainder; return result; } public static boolean isMult( int number, int factor ) { boolean result; if (number % factor == 0) { result=true; } else{ result=false; } return result; } public static boolean isVowel (char c){ if (Character.isLetter (c)) { switch (Character.toLowerCase(c)) { case 'a': case 'e': case 'i': case 'o': case 'u': return true; default: return false; } } } public static boolean isConsonant (char c){ if (Character.isLetter (c) && !isVowel (c)){ return true; } else { return false; } } public static int countDigits( int n) { int count=0; for ( int i = n; i>0; i=i/10 ) count++; return count; } public static boolean isValidPassword(String s) { // Check length if (s.length() < 5 && s.length() > 15 ) return false; // at least one character that is not a letter or a digit for (int i = 0; i < s.length(); i++) { if (!Character.isLetter(s.charAt(i)) && !Character.isDigit(s.charAt(i))) return true; else { return false; } } // Count exactly three digit of characters int count = 0; for (int i = 0; i < s.length(); i++) { if (Character.isLetter(s.charAt(i))) count++; } if (count == 3) return true; else return false; // check if more consonats than vowels /*for (int i = 0; i < s.length(); i++) { if (Character.isLetter(s.charAt(i)) && isConsonant(s.charAt(i))){ count++; } } return false;*/ } public static int sumFactorial( int n) { int result=0, remainder, fact=0; for ( int i = n; i>0; i=i/10 ) fact += factorial(i % 10 ); result += fact; return result; } public static boolean isPrime ( int a ) { boolean result; int f=a, n=0; while ( f>1) { f--; if (a%f == 0) { n+=f; } } if (n>1||a==0) { result=false; } else{ result=true; } return result; } //++++++ PLEASE DO NOT ADD ANY NEW CODE BELOW THIS LINE. ++++++++// //// I/O METHODS //// // Display a String (and/or a value) on a line. public static void say( String s ) { System.out.println( s ); } public static void say( String s, double x ) { System.out.println( s +": "+ x ); } public static void say( double x ) { System.out.println( x ); } // Display prompt (w/o NL); prompt and return input. public static void prompt( String s ) { System.out.print( s ); } public static int ask( String s ) { prompt( s ); return r.nextInt(); } public static String askString( String s ) { prompt( s ); return r.next(); } }//END OF class Main// ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ****************************************************** ****************SEP 12******************************** ******************************************************