1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
importjava.text.NumberFormat; import static java.lang.System.out; classGoodAccount { String lastName; int id; double balance; double getInterest(double rate) { double interest; out.print("Adding "); out.print(rate); out.println(" percent…"); interest = balance * (rate / 100.0); return interest; } void display() { NumberFormat currency = NumberFormat.getCurrencyInstance(); out.print("The account with last name "); out.print(lastName); out.print(" and ID number "); out.print(id); out.print(" has balance "); out.println(currency.format(balance)); } } |
و این کد متد بالا را فراخوانی می کند
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
importjava.util.Random; importjava.text.NumberFormat; classProcessGoodAccounts { public static void main(String args[]) { Random myRandom = new Random(); NumberFormat currency = NumberFormat.getCurrencyInstance(); GoodAccountanAccount; doubleinterestRate; doubleyearlyInterest; for (int i = 0; i < 3; i++) { anAccount = new GoodAccount(); anAccount.lastName = "" + (char) (myRandom.nextInt(26) + 'A') + (char) (myRandom.nextInt(26) + 'a') + (char) (myRandom.nextInt(26) + 'a'); anAccount.id = myRandom.nextInt(10000); anAccount.balance = myRandom.nextInt(10000); anAccount.display(); interestRate = myRandom.nextInt(5); yearlyInterest = anAccount.getInterest(interestRate); System.out.print("This year's interest is "); System.out.println(currency.format(yearlyInterest)); System.out.println(); } } } |
مقداد علی بخشی هستم. موسیقی دان، برنامه نویس، متخصص هوش مصنوعی، علم داده، متخصص بلاکچین و توسعه دهنده ربات های هوشمند.
دانش آموخته مقطع ارشد و دکتری دانشکده فنی دانشگاه تهران هستم. با سابقه تدریس درس برنامه نویسی در دانشگاه (پردیس بین الملل کیش دانشگاه تهران)