cambeme
Giới tính : Cung : Con giáp : Bài gửi : 174 Điểm : 5272 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 04 – Simple Loop 29/3/2011, 18:56 | |
| | 29/3/2011, 18:56 Assignment 04 – Simple Loop | | | Bài 1: - Code:
-
/* * @author Trinh Thai Anh * T094054 */ public class cAss04Pro01_SimplePower_T094054 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // use for to print out <i> <i*i> <i*i*i> for (int i = 0, j = i * i, k = i * i * i; i < 31; i++, j = i * i, k = i * i * i) { System.out.print(i + "\t" + j);// print out <i> and <i*i> System.out.println("\t" + k);// print out <i*i*i> }// end for }// end main function }// end class Bài 2: - Code:
-
/* * @author Trinh Thai Anh * T094054 */ public class cAss04Pro02_SimpleInvestment_T094054 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // print out the header System.out .println("Invest: 1000\tInterest rate: 14,4%\tUnit: 1000 VND"); System.out.println("\tMonth\tIncome\tProfit"); // use for to calculate income and profit for (int i = 1; i <= 12; i++) { double income = 1000; double profit; for (int j = 1; j <= i; j++) income = income * 1.012; profit = income - 1000; // print out income and profit System.out.println("\t" + i + "\t" + (int) income + "\t" + (int) profit); }// end for System.out.println("For more information, pls. contact with T094054."); }// end main function }// end class Bài 3: - Code:
-
/* * @author Trinh Thai Anh * T094054 */ public class cAss04Pro03_VluKfc_T094054 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // print out lines 01 and 02 System.out.println("COMBO 01"); System.out.println("STT" + "\t" + "So Luong" + "\t" + "Don Gia" + "\t" + "Thanh Tien"); // use for to calculate if customers purchase 1-15 servings for (int i = 1; i <= 15; i++) { // print out lines 03 - 17 System.out.println(i + "\t" + i + "\t" + "\t" + 41500 + "\t" + i * 41500); }// end for // use for to calculate if customers purchase 16-40 servings for (int j = 16; j <= 40; j++) { // print out lines 18 – 42 System.out.println(j + "\t" + j + "\t" + "\t" + 39500 + "\t" + j * 39500); }// end for // use for to calculate if customers purchase more than 41 servings int k = 41; // print out line 43 System.out.println(k + "\t" + ">40" + "\t" + "\t" + 35000); }// end main function }// end class Bài 4: - Code:
-
/* * @author Trinh Thai Anh * T094054 */ public class cAss04Pro04_TaxiPrice_T094054 { /** * @param args */ @SuppressWarnings("unused") public static void main(String[] args) { // TODO Auto-generated method stub // declare variable int i, j, k; // print out the header System.out.println("VLU-Taxi:Vios Prices"); System.out.println("\t" + "Km" + "\t" + "Total" + "\t" + "Amount"); // use for to calculate Taxi price from 1km to 20km for (int i1 = 1; i1 <= 20; i1++) { // print out line 03 - 22 System.out.println("\t" + i1 + "\t" + 13500 * i1 + "\t" + "VND"); }// end for // use for to calculate Taxi price from 21km to 50km for (int j1 = 21; j1 <= 50; j1++) { // print out line 23 - 52 System.out.println("\t" + j1 + "\t" + (20 * 13500 + (j1 - 20) * 12500) + "\t" + "VND"); }// end for // use for to calculate Taxi price from 51km to 80km for (int k1 = 51; k1 <= 80; k1++) { // print out line 53 - 82 System.out.println("\t" + k1 + "\t" + (645000 + (k1 - 50) * 11000) + "\t" + "VND"); }// end for // print out the contact contents System.out.println("\t" + "From 81 km, the price: 10000vnd / 1km."); System.out.println("Call (083)VLUTAXI for more information."); }// end main function }// end class Bài 5: - Code:
-
import javax.swing.JFrame; import java.awt.*;
/* * @author Trinh Thai Anh * T094054 */ @SuppressWarnings("serial") public class cAss04Pro05_ClosedGrid_T094054 extends JFrame { // constructor public cAss04Pro05_ClosedGrid_T094054() { // set size width=400, height= 300 setSize(400, 300); // set background color setBackground(Color.white); // set title setTitle("Closed Grid"); }
// paint function public void paint(Graphics g) { // set grids color g.setColor(Color.GRAY); for (int i = 40; i <= 240; i += 20) { g.drawLine(40, i, 340, i);// use for to draw vertical lines }// end for for (int j = 40; j <= 340; j += 20) { g.drawLine(j, 40, j, 240);// use for to draw horizontal lines }// end for }// end paint
/** * @paramargs */ public static void main(String[] args) { // TODOAuto-generated method stub // declare the window cAss04Pro05_ClosedGrid_T094054 mainWindow = new cAss04Pro05_ClosedGrid_T094054(); // set status as closing mainWindow.setDefaultCloseOperation(EXIT_ON_CLOSE); // show the window mainWindow.setVisible(true); }// end main function }// end class Bài 6: - Code:
-
import javax.swing.JFrame; import java.awt.*;
/* * @author Trinh Thai Anh * T094054 */ @SuppressWarnings("serial") public class cAss04Pro06_OpenedGrid_T094054 extends JFrame { // constructor public cAss04Pro06_OpenedGrid_T094054() { // set size width=400, height= 300 setSize(400, 300); // set background color setBackground(Color.white); // set title setTitle("Opened Grid"); }
// paint function public void paint(Graphics g) { // set grids color g.setColor(Color.BLUE); for (int i = 60; i <= 220; i += 20) { g.drawLine(40, i, 340, i);// use for to draw vertical lines }// end for for (int j = 60; j <= 320; j += 20) { g.drawLine(j, 40, j, 240);// use for to draw horizontal lines }// end for }// end paint
/** * @paramargs */ public static void main(String[] args) { // TODOAuto-generated method stub // declare the window cAss04Pro06_OpenedGrid_T094054 mainWindow = new cAss04Pro06_OpenedGrid_T094054(); // set status as closing mainWindow.setDefaultCloseOperation(EXIT_ON_CLOSE); // show the window mainWindow.setVisible(true); }// end main function }// end class Bài 7: - Code:
-
/* * @author Trinh Thai Anh * T094054 */ public class cAss04Pro07_PowerOfTwo_T094054 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // print out the header System.out.println("Power of two"); System.out.println("\t" + "#no" + "\t" + "n" + "\t" + "2^n"); // declare variable double s = 0.5; // use for to calculate exponentiation for (int i = 0; i <= 20; i++) { s = s * 2; // print out line 03 - 23 System.out.println("\t" + (i + 1) + "\t" + i + "\t" + (int) s); }// end for // print out the last line System.out.print("Example of LOOP!"); }// end main function }// end class Bài 8: - Code:
-
/* * @author Trinh Thai Anh * T094054 */ public class cAss04Pro08_Rectangle_T094054 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub // print line i (i=1, .. ,10) for (int i = 0; i < 10; i++) // use for to print out the "*" in 1 line { for (int j = 1; j <= 20; j++) System.out.print("*"); System.out.println(""); }// end for }// end main function }// end class Bài 9: - Code:
-
/* * @author Trinh Thai Anh * T094054 */ public class cAss04Pro09_TwinAngle_T094054 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub for (int i = 1; i <= 20; i++) {// print line i (i=1, .. ,10) for (int j = 1; j <= i; j++) {// use for to print out the "*" in // left side System.out.print("*"); }// end for for (int k = 40 - 2 * i; k > 0; k--) {// use for to print out the // spaces in 1 line System.out.print(" "); }// end for for (int j = 1; j <= i; j++) {// use for to print out the "*" in // right side System.out.print("*"); }// end for System.out.println(""); }// end for }// end main function }// end class
Bài 10: - Code:
-
import java.util.Scanner;
/* * @author Trinh Thai Anh * T094054 */ public class cAss04Pro10_AllAreMine_T094054 { /** * @param args */ public static void main(String[] args) { Scanner in = new Scanner(System.in); // TODO Auto-generated method stub // declare variable int n = 0; System.out.print("Nhap n: ");// input n n = in.nextInt(); System.out.print("Cac so tu 1 den " + n + ": "); // use for to calculate numbers from 1 to n for (int i = 1; i <= n; i++) { System.out.print(i + "-");// print out the result }// end for }// end main function }// end class
| | | | | |
|