cambeme
Giới tính : Cung : Con giáp : Bài gửi : 174 Điểm : 5469 Thanks : 0 Sinh nhật : 05/12/1991 Ngày gia nhập : 25/03/2011 Tuổi : 32 Đến từ : Thành phố Hồ Chí minh Nghề nghiệp/ước mơ : IT Tâm trạng : Hưng phắn Châm ngôn sống : Không có gì quý hơn hột vịt thịt kho!
Cao nhân tắt thở vô phương trị!
| Tiêu đề: Assignment 08: Methods 15/5/2011, 23:48 | |
| | 15/5/2011, 23:48 Assignment 08: Methods | | | Bài 1: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro01_PrimeNumber_T094054 { /** * Check prime number (progamming by Anh Trinh) * * @param nInput * : int number that >0 * @return int. Return if it's prime number or normal number. If input data * is not valid (<=0), INVALID INPUT will be return. */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in);// The reader // use for to make the calculation loop 2 times for (int nLoop = 0; nLoop < 2; nLoop++) { // declare the variable int nInput, nK = 0; // input a number from keyboard System.out.print("Nhap so n: "); nInput = in.nextInt(); // use if when input <=0 if (nInput <= 0) { System.out.println("INVALID INPUT"); return; }// end if // use if to calculate if input =2 if (nInput == 2) { System.out.println(nInput + " is PRIME NUMBER."); return; }// end if // use for to calculate if input >2 for (int nCounter = 2; nCounter <= nInput / 2; ++nCounter) if (nInput % nCounter == 0) { nK = 1; break; }// end if // print out the result when nK=0 if (nK == 0) { System.out.println(nInput + " is PRIME NUMBER."); }// end if else { System.out.println(nInput + " is NORMAL NUMBER."); }// end else }// end for }// end main function }// end class Bài 2: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro02_SimpleFunction_T094054 { /** * Calculate simple function (progamming by Anh Trinh) * * @param dInput * : double number * @return double. Return the result of the function */ public static void main(String[] args) { Scanner in = new Scanner(System.in);// The reader // TODO Auto-generated method stub // Declare the variable double dInput, dResult; // use for to make the calculation loop 3 times for (int nLoop = 0; nLoop <= 2; nLoop++) { // input a number from keyboard System.out.print("Nhap gia tri x = "); dInput = in.nextDouble(); // calculate the result of function dResult = dInput * dInput * dInput + 3 * dInput * dInput + dInput + 3; // print out the result System.out.println("Ket qua cua phuong trinh: " + "f(" + (dInput) + ") = " + dResult); }// end for }// end main function }// end class Bài 3: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro03_DistanceXY_T094054 { /** * Calculate distance of two point (progamming by Anh Trinh) * * @param dInput * : double number * @return double. Return the result of kind of triangles */ public static void main(String[] args) { // TODO Auto-generated method stub // Declare Scanner Scanner in = new Scanner(System.in); // Declare the variable double[] dX = new double[3]; double[] dY = new double[3]; // use for to input from keyboard for (int i = 0; i < 3; i++) { System.out.println("Toa do dinh thu " + (i + 1) + " cua tam giac:"); System.out.print("x" + (i + 1) + "="); dX[i] = in.nextDouble(); System.out.print("y" + (i + 1) + "="); dY[i] = in.nextDouble(); }// end for // Declare the variable and calculate distance between X and Y base // on the function getDistanceXY double dAB = getDistanceXY_T094054(dX[0], dY[0], dX[1], dY[1]); double dBC = getDistanceXY_T094054(dX[1], dY[1], dX[2], dY[2]); double dAC = getDistanceXY_T094054(dX[0], dY[0], dX[2], dY[2]); // use if to prove that ABC is an equilateral triangle if (dAB == dBC && dBC == dAC) { System.out.println("ABC is equilateral triangle"); System.exit(1); }// end if // use if to prove that ABC is not a triangle if (dAB >= (dAC + dBC) || dBC >= (dAB + dBC) || dAC >= (dAB + dBC)) { System.out .println("ABC is not triangle (ABC khong phai la tam giac)"); } else { // use if to prove that ABC is a rectangled isosceles triangle // or a rectangled triangle base on Square function if (Square_T094054(dX, dY)) { if (dAB == dAC || dAC == dBC || dBC == dAB) { System.out .println("ABC is rectangled isosceles triangle (ABC la tam giac vuong can)"); }// end if else { System.out .println("ABC is rectangled triangle (ABC la tam giac vuong)"); }// end else }// end if else { // use if to prove that ABC is an isosceles triangle or a normal // triangle if (dAB == dAC || dAC == dBC || dBC == dAB) { System.out .println("ABC is isosceles triangle (ABC la tam giac can)"); }// end if else { System.out .println("ABC is normal triangle (ABC la tam giac thuong)"); }// end else }// end else }// end else }// end main function
public static double getDistanceXY_T094054(double iX1, double iY1, double iX2, double iY2) { return Math.sqrt((iX1 - iX2) * (iX1 - iX2) + (iY1 - iY2) * (iY1 - iY2)); }// get distance between X and Y // test if the triangle have angle square or not
public static boolean Square_T094054(double[] aX, double[] aY) { // declare the variable double dA, dB, dC; if ((aX[0] - aX[1]) == 0) { dB = (aY[1] - aY[2]) / (aX[1] - aX[2]); dC = (aY[2] - aY[0]) / (aX[2] - aX[0]); if (dB * dC == -1 || dB == 0 || dC == 0) { return true; }// end if else { return false; }// end else }// end if if ((aX[1] - aX[2]) == 0) { dA = (aY[0] - aY[1]) / (aX[0] - aX[1]); dC = (aY[2] - aY[0]) / (aX[2] - aX[0]); if (dA == 0 || dC == 0 || dA * dC == -1) { return true; }// end if else { return false; }// end else }// end if if ((aX[2] - aX[0]) == 0) { dA = (aY[0] - aY[1]) / (aX[0] - aX[1]); dB = (aY[1] - aY[2]) / (aX[1] - aX[2]); if (dA == 0 || dB == 0 || dA * dB == -1) { return true; }// end if else { return false; }// end else }// end if dA = (aY[0] - aY[1]) / (aX[0] - aX[1]); dB = (aY[1] - aY[2]) / (aX[1] - aX[2]); dC = (aY[2] - aY[0]) / (aX[2] - aX[0]); return (dA * dB == -1 || dA * dC == -1 || dB * dC == -1) ? true : false; }// end test function }// end class Bài 4: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro04_Midpoint_T094054 { /** * Calculate midpoint (progamming by Anh Trinh) * * @param dInput * : double number * @return double. Return the midpoint of two points */ public static void main(String[] args) { Scanner in = new Scanner(System.in);// The reader // TODO Auto-generated method stub // Declare the variable double dX1, dY1, dX2, dY2, dXm, dYm; // input x1 from keyboard System.out.print(" Nhap gia tri x1 = "); dX1 = in.nextDouble(); // input y1 from keyboard System.out.print(" Nhap gia tri y1 = "); dY1 = in.nextDouble(); // input x2 from keyboard System.out.print(" Nhap gia tri x2 = "); dX2 = in.nextDouble(); // input y2 from keyboard System.out.print(" Nhap gia tri y2 = "); dY2 = in.nextDouble(); // calculate dXm and dYm dXm = (dX1 + dX2) / 2; dYm = (dY1 + dY2) / 2; // print out the result System.out.print("Khoang cach giua hai diem co toa do (" + dX1 + "," + dY1 + ") va (" + dX2 + "," + dY2 + ") la (" + dXm + "," + dYm + ")"); }// end main function }// end class Bài 5: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro05_AverageOfNumber_T094054 { /** * Calculate the average of numbers (progamming by Anh Trinh) * * @param nInput * : int number, adArrValue: double number * @return double. Return the average of numbers */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner keyboard = new Scanner(System.in);// the scanner // declare variable int nInput; // input n from keyboard System.out.print("So phan tu cua mang: n="); nInput = keyboard.nextInt(); // declare variable double[] adArrValue = new double[nInput]; // use for to input A[n]from keyboard for (int i = 0; i < nInput; i++) { // input A[n] from keyboard System.out.print("Nhap A[" + (i + 1) + "] :"); adArrValue[i] = keyboard.nextDouble(); }// end for // declare variable double result = 0.0; // use for to calculate average then print out the result for (int j = 0; j < adArrValue.length; j++) { result = result + adArrValue[j]; }// end for System.out.println("Trung binh cong = " + result / adArrValue.length); }// end main function }// end class Bài 6: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro06_ProperCase_T094054 { /** * Convert string to Proper Case (progamming by Anh Trinh) * * @param sInput * : string * @return string. Convert sInput to Proper Case */ static String toProperCase_T094054(String stringToConvert) { // create a string buffer as we cannot change a string StringBuffer sBuffer = new StringBuffer(stringToConvert); // every time we come across a new word set the first letter to upper // case boolean bSpace = true;// set it initially to true so that we set the // first letter for (int i = 0; i < sBuffer.length(); i++) { if (sBuffer.charAt(i) == ' ') { bSpace = true; }// end if else { // must be a letter so check to see if the last item was // a space to set to upper case if (bSpace) { sBuffer.setCharAt(i, Character.toUpperCase(sBuffer.charAt(i))); bSpace = false; }// end if else { // must be a letter so push to lower sBuffer.setCharAt(i, Character.toLowerCase(sBuffer.charAt(i))); }// end else }// end else }// end for return (sBuffer.toString()); }// end string function
@SuppressWarnings("unused") public static void main(String[] args) { Scanner in = new Scanner(System.in);// the scanner // Declare the variable String sInput; int nCounter = 0; // input the string from keyboard System.out.print("Nhap chuoi:"); sInput = in.nextLine(); System.out.println(cAss08Pro06_ProperCase_T094054 .toProperCase_T094054(sInput)); }// end main function }// end class Bài 7: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro07_BinaryConverter_T094054 { /** * Convert Binary to Octal and Hexa decimal (progamming by Anh Trinh) * * @param sBinary * : string * @return string. Convert sInput to Octal and Hexa decimal */ public static void main(String[] args) { Scanner scan = new Scanner(System.in);// the scanner // input the string from keyboard System.out.print("Enter a binary number: "); String sBinary = scan.next(); boolean isBinary = true; // convert the binary string into a char array and check for each char // whether it is zero or one char[] bits = sBinary.toCharArray(); for (int j = 0; j < bits.length; j++) { if ((bits[j] != '0') && (bits[j] != '1')) { isBinary = false; break; }// end if }// end for if (!isBinary) {// not binary System.out.println("Invalid Input."); }// end if else {// binary int sConvert = Integer.parseInt(sBinary, 2); // print out the result System.out.println("Octal: " + Integer.toString(sConvert, 8)); System.out .println("Hexa decimal: " + Integer.toHexString(sConvert)); }// end else }// end main function }// end class Bài 8: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro08_SimpleInvestment_T094054 { /** * Calculate the payment (progamming by Anh Trinh) * * @param dAmountOfMoney * : double, nNumberOfMonth: int, dInterestRate: double * @return double. Calculate payment */ public static void main(String[] args) { Scanner in = new Scanner(System.in);// The reader // TODO Auto-generated method stub // Declare the variable int nNumberOfMonth; double dAmountOfMoney, dInterestRate, nPayment = 0; // input amount of money from keyboard System.out.print(" Nhap so tien gui = "); dAmountOfMoney = in.nextDouble(); // input number of month from keyboard System.out.print(" Nhap so thang gui = "); nNumberOfMonth = in.nextInt(); // input interest rate from keyboard System.out.print(" Nhap lai suat(%/nam) = "); dInterestRate = in.nextDouble(); // calculate nPayment = dAmountOfMoney * Math.pow((1 + ((double) dInterestRate / 1200)), nNumberOfMonth); // print out payment System.out.println("Tien lai: " + nPayment); }// end main function }// end class Bài 9: - Code:
-
import java.util.Scanner;
/* * @author Anh Trinh * T094054 */ public class cAss08Pro09_Determinant2x2_T094054 { /** * Calculate the determinant 2x2 (progamming by Anh Trinh) * * @param double: da1, db1, dc1, da2, db2, dc2, dx, dy * @return double. Calculate roots x and y of the set of equations */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner in = new Scanner(System.in);// the scanner // declare the variable double da1, db1, dc1, da2, db2, dc2, dx, dy; // input a1 from keyboard System.out.print("Nhap so a1:"); da1 = in.nextDouble(); // input b1 from keyboard System.out.print("Nhap so b1:"); db1 = in.nextDouble(); // input c1 from keyboard System.out.print("Nhap so c1:"); dc1 = in.nextDouble(); // input a2 from keyboard System.out.print("Nhap so a2:"); da2 = in.nextDouble(); // input b2 from keyboard System.out.print("Nhap so b2:"); db2 = in.nextDouble(); // input c2 from keyboard System.out.print("Nhap so c2:"); dc2 = in.nextDouble(); // convert from + to - dc1 = -dc1; dc2 = -dc2; // calculate x and y base on getDeterminant2x2 function dx = getDeterminant2x2_T094054(dc1, db1, dc2, db2) / getDeterminant2x2_T094054(da1, db1, da2, db2); dy = getDeterminant2x2_T094054(da1, dc1, da2, dc2) / getDeterminant2x2_T094054(da1, db1, da2, db2); // print out the result System.out.println("Nghiem cua he phuong trinh " + da1 + "*x + " + db1 + "*y + " + -dc1 + "= 0 va " + da2 + "*x + " + db2 + "*y + " + -dc2 + "= 0 la:"); System.out.println("x = " + dx); System.out.print("y = " + dy); }// end main function
public static double getDeterminant2x2_T094054(double da, double db, double dc, double dd) { double dA = (da * dd) - (db * dc); // return result return dA; }// end getDeterminant2x2 function }// end class
| | | | | |
|