الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

كيفبه الاستداع فى main

بدأه دنية القوى في 15 مايو 2011 · 42 رد · 1,568 مشاهدة · في JavaSE
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

صباح الخير

أتمنى من أخوتى أن يسعدونى أنا عندى برنامج وأريد أن أقوم بعمليه الاستدع فى main هذا هوى البرنامج

ولكن يوجد فيه أخطأ أريد منكم الملاحظات وتوجيه

وشكرا.

import java.io.*;

public class Account

{

// Data members

public long user_ID; // attribute (data)

public int accountNumber; // attribute (data)

public int balance = 0; // attribute (data)

//Constructors

public Account() {

user_ID = 0;

accountNumber = 0;

balance = 0;

}

public Account( long i, int accountNumber, int balance) {

this.user_ID=i;

this.accountNumber = accountNumber;

this.balance = balance;

}

// Public functions

public int withdraw (int amount) // method

{

balance -= amount;

}

void deposit (int amount)

{

balance += amount;

}

int showBalance ()

{

System.out.println("Your balance is " + balance);

return (balance);

}

import java.util.Scanner;

public class AccountTest{

public static void main (String argv[]) {

Account B = new Account () ;

B. Account R( 0, 0, 0);

B. Account r(76523122, 12345, 25000);

B. withdraw r(6000);

B. deposit R(1000);

B. showBalance(R,r);

B. showBalance®;

B. showBalance®;

B. withdraw r(2000);

B. deposit r(2800);

B. showBalance®;

}// end main

}// end class

وهذا السؤال

Modify the withdraw method in the account class such that

• It withdraws money from the account only if the amount to be

withdrawn is less than the balance and less than 5000. If he

amount is greater than 5000, the function displays a message

stating that the maximum amount to be withdrawn is 5000. If

the amount is greater than the balance, it displays a message

saying amount is more than the balance. In both cases it

returns without changing the balance.

• If the amount is both more than 5000 and more than the

balance, it is enough to display a statement saying the

maximum amount to be withdrawn is 5000.

• If the user_ID is a zero, it must display a message stating that

this is an invalid account.

• Modify every function that accesses the account to check if the

user_ID is zero; it displays the same message as above and

returns right away. From now one, every access to an invalid

account should display such a message and does not change

anything in the account.

Create a subclass of Account called SavingAccount. The Saving

Account class should have an interestRate variable (double) (for

example 0.04 to represent 4% interest).

• The constructor of the Saving Account should use the

constructor of the Account class and sets the interest rate to

0%.

• Add a method called setInterest to set the interest rate to

the required value. This method should confirm setting the

interest rate by printing a statement like “the interest rate for

account xxxx is now y%”

• Add a method called addInterest that calculates the amount of

interest on the balance and add it to the balance (note that

the balance is an integer).

• Override the showBalance method to show both the balance

and the interest rate of the saving account.

Write a driver class called testAccount to do the following

• creates a saving account object with user_ID 0, account

number of 0, and a balance of 0.

• Create another saving account with user_ID=76523122,

account number of 12345 and balance of 25000

• withdraw 6000 Rials from the second account

• deposit 1000 Rials in the first account.

• Set the interest rate to both first and second account to 4%

• Add interest to the second account

• Show the balance of the second account.

• Withdraw 2000 SR from the second account

• Deposit 2800 SR to the second account

• Show the balance of the second account

#2

ضعي رسالة الخطأ

عفواً أحبتي انقطاعي بسبب الدراسة دعواتكم بالتوفيق..

{ لا ينال العلم مستحٍ ولا مستكبر }

#3

اولا الدالة public int withdraw(int amount) تحتاج الى جملة return

والدالة int showBalance() ينقصها قوس الاغلاق

في الملف الثاني قمتي بنفس الاخطاء التي قمتي بها في البرنامج السابق الذي ذركتيه في الموضوع السابق مثل

B. deposit R(1000);

يجب ان لا توجد مسافة بين اسم الداله واسم المتغير

لم افهم لماذا قمتي باستدعاء هذين السطرين

B.AccountR( 0, 0, 0);
B.Accountr(76523122, 12345, 25000);

لا يوجد دالة اسمها Accountr داخل الكلاس

لقد قمت بتصحيح بعض الاستدعاءات الخاطئة لديك في الكلاس

B.showBalance();
B.showBalance();
B.showBalance();
B.withdraw(2000);
B.deposit(2800);
B.showBalance();

قارنيها بالذي كان مكتوب عندك اول

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#4

الاته يوجد حسبين مختلفين

B. Account R( 0, 0, 0);

B. Account r(76523122, 12345, 25000);

الكى أفرق بين الحساب الاول والثانى وضعت R,r

عملات التعديلات بس مرضى يشتغل

#5

لازم تعملي الدالتين هذه داخل الكلاس اذا

الا اذا كان قصدك انهم constructors فهذا يعني انه لازم تعرفين two constructors

اذا كان constructor ماينفع تستدعي obj.constructor الا اذا انت بتستدعي حق سوبر كلاس

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#6

بس أبغى الحسبين المختالفين نرسالهم على نفس الداله الواحده فبتالى كيف تكون المندى

#7

بالنسبة لهذه الجزئية

اقتباس

Modify the withdraw method in the account class such that

• It withdraws money from the account only if the amount to be

withdrawn is less than the balance and less than 5000. If he

amount is greater than 5000, the function displays a message

stating that the maximum amount to be withdrawn is 5000. If

the amount is greater than the balance, it displays a message

saying amount is more than the balance. In both cases it

returns without changing the balance.

• If the amount is both more than 5000 and more than the

balance, it is enough to display a statement saying the

maximum amount to be withdrawn is 5000.

• If the user_ID is a zero, it must display a message stating that

this is an invalid account.

• Modify every function that accesses the account to check if the

user_ID is zero; it displays the same message as above and

returns right away. From now one, every access to an invalid

account should display such a message and does not change

anything in the account.

package account;

/**
 *
 * @author PSYCHO_ MANTIS
 */
public class Account {
// Data members

    public long user_ID; // attribute (data)
    public int accountNumber; // attribute (data)
    public int balance = 0; // attribute (data)
//Constructors

    public Account() {
        user_ID = 0;
        accountNumber = 0;
        balance = 0;
    }

    public Account(long i, int accountNumber, int balance) {
        this.user_ID = i;
        this.accountNumber = accountNumber;
        this.balance = balance;
    }
// Public functions

    public void withdraw(int amount) // method
    {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            if (amount > 5000) {
                System.out.println("You can not withdraw more than 5000");
            } else if (amount > balance) {
                System.out.println("You can not withdraw more than your available balance");
            } else {
                balance -= amount;
            }
        }
    }

    void deposit(int amount) {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            balance += amount;
        }
    }

    int showBalance() {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + balance);
            return (balance);
        }
        return 0 ;
    }
}

انظري لهذا الكود قبل ان نتقدم الجزء التالي من البرنامج

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#8

أنا مطلوب منى أسوى main فقط والدوالا الآخر صحيحه بس طريقه المندى عندى

#9

اعطيني بس فرصة اصحح لك الكود علشان بعدين الماين تتطبق معاك صح

تمام!

في الكلاس الاول الدوال خليها كلها public لانك عاملاها package access

الان هو بيطلب منك تعملي كلاس جديد

اقتباس

reate a subclass of Account called SavingAccount. The Saving

Account class should have an interestRate variable (double) (for

example 0.04 to represent 4% interest).

• The constructor of the Saving Account should use the

constructor of the Account class and sets the interest rate to

0%.

• Add a method called setInterest to set the interest rate to

the required value. This method should confirm setting the

interest rate by printing a statement like “the interest rate for

account xxxx is now y%”

• Add a method called addInterest that calculates the amount of

interest on the balance and add it to the balance (note that

the balance is an integer).

• Override the showBalance method to show both the balance

and the interest rate of the saving account.

public class SavingAccount extends Account {
    private double interestRate;

    public SavingAccount(long i, int accountNumber, int balance, double interestRate) {
        super(i,accountNumber,balance);
        this.interestRate = interestRate;
    }

   public void setInterest(double interest){
        this.interestRate = interest;
   }
   public void addInterest(){
        balance += (int)balance * interestRate;
   }
   @Override
   public int showBalance() {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + balance + " and the interest rate is: " + interestRate);

            return (balance);
        }
        return 0 ;
    }
}

بعد كده راح نكتب الماين زي ماقال هو : في الرد الجاي

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#10

الان تعملي الماين

package account;

public class AccountTest{

public static void main (String argv[]) {

SavingAccount s1 = new SavingAccount(0, 0, 0);

SavingAccount s2 = new SavingAccount(76523122, 76523122, 25000);
s2.withdraw(6000);
s1.deposit(1000);
s1.setInterest(.04);
s2.setInterest(.04);
s2.addInterest();
s2.showBalance();
s2.withdraw(2000);
s2.deposit(2800);
s2.showBalance();
}// end main

}// end class

لاحظي انه الحاسب الثاني بس علشان يظهر لك رسالة الخطأ كاختبار للكلاس مش اكثر ولا اقل

تصحيح في الكلاس saving account

package account;

/**
 *
 * @author PSYCHO_ MANTIS
 */
public class SavingAccount extends Account {
    private double interestRate;

    public SavingAccount(long i, int accountNumber, int balance) {
        super(i,accountNumber,balance);
        this.interestRate = 0;
    }

   public void setInterest(double interest){
        this.interestRate = interest;
   }
   public void addInterest(){
        balance += (int)balance * interestRate;
   }
   @Override
   public int showBalance() {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + balance + " and the interest rate is: " + interestRate);

            return (balance);
        }
        return 0 ;
    }
}

الــ constructor مفروض يخلي قيمة intersetRate 0 اتوماتيكي

بالنسبة للدالة showBalance انا اقترح عليكي تخليها ترجع void لانه لا ادري كيف يمكن تستفيدي من القيمة المرجعة

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#11

شكرا أخى على مقدمته من مساعده

أسال الله رب العرش القدير أن يوفقك دنيه وأخره

بس معليش أنا حولت أجرب فى البرنامج بس مو رضى يشتغل معى الن مادرى الماذه؟

#12

شكلك ما عملتي التعديلات اللي قلت لك عليها بس هذه هي كل الكلاسات

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package account;

/**
 *
 * @author PSYCHO_ MANTIS
 */
public class Account {
// Data members

    public long user_ID; // attribute (data)
    public int accountNumber; // attribute (data)
    public int balance = 0; // attribute (data)
//Constructors

    public Account() {
        user_ID = 0;
        accountNumber = 0;
        balance = 0;
    }

    public Account(long i, int accountNumber, int balance) {
        this.user_ID = i;
        this.accountNumber = accountNumber;
        this.balance = balance;
    }
// Public functions

    public void withdraw(int amount) // method
    {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            if (amount > 5000) {
                System.out.println("You can not withdraw more than 5000");
            } else if (amount > balance) {
                System.out.println("You can not withdraw more than your available balance");
            } else {
                balance -= amount;
            }
        }
    }

   public void deposit(int amount) {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            balance += amount;
        }
    }

    public int showBalance() {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + balance);
            return (balance);
        }
        return 0 ;
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package account;

/**
 *
 * @author PSYCHO_ MANTIS
 */
public class SavingAccount extends Account {
    private double interestRate;

    public SavingAccount(long i, int accountNumber, int balance) {
        super(i,accountNumber,balance);
        this.interestRate = 0;
    }

   public void setInterest(double interest){
        this.interestRate = interest;
   }
   public void addInterest(){
        balance += (int)balance * interestRate;
   }
   @Override
   public int showBalance() {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + balance + " and the interest rate is: " + interestRate);

            return (balance);
        }
        return 0 ;
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package account;

public class AccountTest{

public static void main (String argv[]) {

SavingAccount s1 = new SavingAccount(0, 0, 0);

SavingAccount s2 = new SavingAccount(76523122, 76523122, 25000);
s2.withdraw(6000);
s1.deposit(1000);
s1.setInterest(.04);
s2.setInterest(.04);
s2.addInterest();
s2.showBalance();
s2.withdraw(2000);
s2.deposit(2800);
s2.showBalance();
}// end main

}// end class

بالتأكيد راح يشتغل لاني شغلتهم عندي

بالتوفيق

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#13

السلام عليكم

مساء الخير

أنا عندى البرنامج صلاحات عليه تعديلات وهى أن أجعل الوبجكتprivate وكمان عمليت الاستدع ورجع وعمليت وميثد فى main المقرانه بين balance

بس جربت البرنامج فى أخطأ يقول القوس فى المثد الجديد العمله المقرنه بين balance

أرجو مساعده

وهذا البرنامج

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package account;

/**

*

* @author PSYCHO_ MANTIS

*/

public class Account {

// Data members

private long user_ID; // attribute (data)

private intaccountNumber; // attribute (data)

private int balance = 0; // attribute (data)

//Constructors

public Account() {

user_ID = 0;

accountNumber = 0;

balance = 0;

}

public Account(long i, intaccountNumber, int balance) {

this.user_ID = i;

this.accountNumber = accountNumber;

this.balance = balance;

}

// Public functions

public void withdraw(int amount) // method

{

if (user_ID == 0) {

System.out.println("This is an invalid account");

} else {

if (amount > 5000) {

System.out.println("You can not withdraw more than 5000");

} else if (amount > balance) {

System.out.println("You can not withdraw more than your available balance");

} else {

balance -= amount;

}

}

}

public void deposit(int amount) {

if (user_ID == 0) {

System.out.println("This is an invalid account");

} else {

balance += amount;

}

}

public intshowBalance() {

if (user_ID == 0) {

System.out.println("This is an invalid account");

} else {

System.out.println("Your balance is " + balance);

return (balance);

}

return 0 ;

}

public get user_ID ()

{ return user_ID;

}

public get accountNumber()

{return accountNumber;

}

public get balance()

{return balance;

}

}

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package account;

public class AccountTest{

public static void main (String argv[]) {

SavingAccount s1 = new SavingAccount(0, 0, 0);

SavingAccount s2 = new SavingAccount(76523122, 76523122, 25000);

s2.withdraw(6000);

s1.deposit(1000);

s1.setInterest(.04);

s2.setInterest(.04);

s2.addInterest();

s2.showBalance();

s2.withdraw(2000);

s2.deposit(2800);

s2.showBalance();

s1.user_ID();

s1.accountNumber();

s1.balance();

s2.user_ID();

s2.accountNumber();

s2.balance();

}// end main

public class Two balance (int s1,int s2)

{

if(s1==0)

System.out.println("the balance invald " );

else

System.out.println("the balance not invald " );

if(s2e==0)

System.out.println("the balance invald ");

else

System.out.println("the balance not invald " );

if(s1>s1)

System.out.println("the balance s1>s2 " );

else

System.out.println("the balance s2>s1 " );

}

}// end class

/*

* To change this template, choose Tools | Templates

* and open the template in the editor.

*/

package account;

/**

*

* @author PSYCHO_ MANTIS

*/

public class SavingAccount extends Account {

private double interestRate;

public SavingAccount(long i, int accountNumber, int balance) {

super(i,accountNumber,balance);

this.interestRate = 0;

}

public void setInterest(double interest){

this.interestRate = interest;

}

public void addInterest(){

balance += (int)balance * interestRate;

}

@Override

public int showBalance() {

if (user_ID == 0) {

System.out.println("This is an invalid account");

} else {

System.out.println("Your balance is " + balance + " and the interest rate is: " + interestRate);

return (balance);

}

return 0 ;

}

}

#14

نسختي الثلاث الملفات اللي حطيتهم لكي اخر مرة؟

اذا انسخ رسالة الخطأ كما هي و اكتبيها في الرد

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#15

الخطأ هى Erro4 والموشر على قوس الميثد الجديد

#16

اطبعي لي الخطأ من داخل النت بينز يعني اعملي له كوبي بايست

شوفي الصورة عندك البرنامج شغال عندي ، فأنا اشتي اعرف ايش اللي هو خطأك عندك بالضبط

زي ما قلت لكي اعملي copy & paste ولو ممكن صورة يكون احسن

post-244925-093260700 1305558917_thumb.j

المرفقات
Untitled-1.jpg

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#17

أنا سويت على البر نامج تعديلات

private long user_ID; // attribute (data)
    private int accountNumber; // attribute (data)
   private int balance = 0; // attribute (data)

public get user_ID ()
   	{ return user_ID;

   	}

   	public get accountNumber()
   	{return accountNumber;
   	}

   		public get balance()
   	{return balance;


s1.user_ID();
s1.accountNumber();
s1.balance();
s2.user_ID();
s2.accountNumber();
s2.balance();
public class Two balance (int s1,int s2)
{
if(s1==0)
System.out.println("the balance invald  " );
else
System.out.println("the balance not invald " );
	if(s2e==0)
		System.out.println("the balance invald ");
		else
	System.out.println("the balance not invald " );
	if(s1>s1)
	System.out.println("the balance s1>s2 " );
	else
		System.out.println("the balance s2>s1 " );

بالون الا أخضر عمليه أستدع

بالون الا أزرق عبره عن ميثد تقران كم المبلغ الموجود فى الحساب الاول والثانى

#18
public class Two balance (int s1,int s2)

ما ينفعش تعملي فراغ بين الكلمتين حق اسم الكلاس

public class Two_balance (int s1,int s2)

عدليه لهذا الشكل

الحاجة الثانيه : هل انت مسويه الكلاس داخل الداله main?

اذا كان نعم فا ما ينفعش تسوي كلاس داخل دالة

ولو تشتي تسوي كلاس public لازم يكون اسم الكلاس نفس اسم الملف : قلت هذا من قبل

لما تكتبي داخل الIDE اضغطي بالماوس يمين واختاري format علشان النص يكون مرتب و مقرؤ

تم تعديل هذه المشاركة بواسطة mental-driller في 16 مايو 2011 في 19:07

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#19

سويت التعديلات بس خرج هذا فى شاشه run

You can not withdraw more than 5000
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - balance has private access in account.Account
This is an invalid account
        at account.SavingAccount.addInterest(SavingAccount.java:24)
        at account.AccountTest.main(AccountTest.java:19)
Java Result: 1
BUILD SUCCESSFUL (total time: 3 seconds)
#20

الان انت خليتي balance private يعني في الدالة

public void addInterest(){
        balance += (int)balance * interestRate;
   }

الاستدعاء هذا خاطئ ، لازم تستدعيها باستخدام دوال setBalance و getBalance

يعني بيصير الكود

public void addInterest(){

 setBalance((int) getBalance() * interestRate + getBalance()); 
 }

طبعا لازم تكوني سويتي الدوال حق set, get

تم تعديل هذه المشاركة بواسطة mental-driller في 16 مايو 2011 في 19:25

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#21

حولات أسوى set, get بس ماشىء البرنامج ياليت توضح

#22

خلاص انا اقول لك

حطي الكود حق كل ملف لحالة

في

علشان يبان مرتب وانا راح انسخهم واقول لك

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#23
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package account;

/**
 *
 * @author PSYCHO_ MANTIS
 */
public class SavingAccount extends Account {
    private double interestRate;

    public SavingAccount(long i, int accountNumber, int balance) {
        super(i,accountNumber,balance);
        this.interestRate = 0;
    }

   public void setInterest(double interest){
        this.interestRate = interest;
   }
   public void addInterest(){
        balance += (int)balance * interestRate;
   }
   @Override
   public int showBalance() {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + balance + " and the interest rate is: " + interestRate);

            return (balance);
        }
        return 0 ;
    }
}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package account;

public class AccountTest{

public static void main (String argv[]) {

SavingAccount s1 = new SavingAccount(0, 0, 0);

SavingAccount s2 = new SavingAccount(76523122, 76523122, 25000);
s2.withdraw(6000);
s1.deposit(1000);
s1.setInterest(.04);
s2.setInterest(.04);
s2.addInterest();
s2.showBalance();
s2.withdraw(2000);
s2.deposit(2800);
s2.showBalance();
s1.user_ID();
s1.accountNumber();
s1.balance();
s2.user_ID();
s2.accountNumber();
s2.balance();
}// end main

public class Two_ balance (int s1,int s2)
{ 
if(s1==0)
System.out.println("the balance invald  " );
else
System.out.println("the balance not invald " );	
	if(s2==0)
		System.out.println("the balance invald ");
		else
	System.out.println("the balance not invald " );
	if(s1>s2)	
	System.out.println("the balance s1>s2 " );
	else
		System.out.println("the balance s2>s1 " );		

	}


}// end class

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package account;

/**
 *
 * @author PSYCHO_ MANTIS
 */
public class Account {
// Data members

    private long user_ID; // attribute (data)
    private int accountNumber; // attribute (data)
   private int balance = 0; // attribute (data)
//Constructors

    public Account() {
        user_ID = 0;
        accountNumber = 0;
        balance = 0;
    }

    public Account(long i, int accountNumber, int balance) {
        this.user_ID = i;
        this.accountNumber = accountNumber;
        this.balance = balance;

    }
// Public functions

    public void withdraw(int amount) // method
    {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            if (amount > 5000) {
                System.out.println("You can not withdraw more than 5000");
            } else if (amount > balance) {
                System.out.println("You can not withdraw more than your available balance");
            } else {
                balance -= amount;


            }
        }
    }

   public void deposit(int amount) {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            balance += amount;

        }
    }

    public int showBalance() {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + balance);
            return (balance);
        }
        return 0 ;
    }
   public get user_ID ()
   	{ return user_ID;

   	}

   	public get accountNumber()
   	{return accountNumber;
   	}

   		public get balance()
   	{return balance;
   	}
                public void addInterest(){    setBalance((int) getBalance() * interestRate + getBalance());  }
}
#24

اولا اريد ان اعرف هل هذه داله ام كلاس؟

public class Two_ balance (int s1,int s2)
{
if(s1==0)
System.out.println("the balance invald  " );
else
System.out.println("the balance not invald " );
        if(s2==0)
                System.out.println("the balance invald ");
                else
        System.out.println("the balance not invald " );
        if(s1>s2)
        System.out.println("the balance s1>s2 " );
        else
                System.out.println("the balance s2>s1 " );

        }

الاستدعاء بهذه الصورة خاطئ لان الكلاس لا يأخذ متغيرات

لقد قمت باصلاح الكود

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package account;

public class AccountTest {

    public static void main(String argv[]) {

        SavingAccount s1 = new SavingAccount(0, 0, 0);

        SavingAccount s2 = new SavingAccount(76523122, 76523122, 25000);
        s2.withdraw(6000);
        s1.deposit(1000);
        s1.setInterest(.04);
        s2.setInterest(.04);
        s2.addInterest();
        s2.showBalance();
        s2.withdraw(2000);
        s2.deposit(2800);
        s2.showBalance();

    }// end main
}// end class

package account;
/**
 *
 * @author PSYCHO_ MANTIS
 */
public class Account {
// Data members

    private long user_ID; // attribute (data)
    private int accountNumber; // attribute (data)
    private int balance = 0; // attribute (data)
//Constructors

    public Account() {
        user_ID = 0;
        accountNumber = 0;
        balance = 0;
    }

    public Account(long i, int accountNumber, int balance) {
        this.user_ID = i;
        this.accountNumber = accountNumber;
        this.balance = balance;

    }
// Public functions

    public void withdraw(int amount) // method
    {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            if (amount > 5000) {
                System.out.println("You can not withdraw more than 5000");
            } else if (amount > balance) {
                System.out.println("You can not withdraw more than your available balance");
            } else {
                balance -= amount;


            }
        }
    }

    public void deposit(int amount) {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            balance += amount;

        }
    }

    public int showBalance() {
        if (user_ID == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + balance);
            return (balance);
        }
        return 0;
    }

    public long getUser_ID() {
        return user_ID;

    }

    public int getAccountNumber() {
        return accountNumber;
    }

    public int getBalance() {
        return balance;
    }
    public void setBalance(int i){
       balance = i ;
    }

}

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package account;

/**
 *
 * @author PSYCHO_ MANTIS
 */
public class SavingAccount extends Account {
    private double interestRate;

    public SavingAccount(long i, int accountNumber, int balance) {
        super(i,accountNumber,balance);
        this.interestRate = 0;
    }

   public void setInterest(double interest){
        this.interestRate = interest;
   }
   public void addInterest(){
       setBalance((int) (getBalance() * interestRate )+ getBalance());

   }
   @Override
   public int showBalance() {
        if (getUser_ID() == 0) {
            System.out.println("This is an invalid account");
        } else {
            System.out.println("Your balance is " + getBalance() + " and the interest rate is: " + interestRate);

            return (getBalance());
        }
        return 0 ;
    }
}

هذا البرنامج بعد التعديل بالنسبة للكود المطروح بالاعلي قول لي ماذا تريدين ان تفعلي بالضبط

No intellectual battle was ever won through retreat
You do not watch Gintama? Dude, you are missing a lot!


صورةmsrgb1485.gif ocajavase7programmerclr.gif

#25

الكود الاول عبره عن داله تقوم بالمقارنه بين Balance الحساب الآول والحساب الآخر

وش الفرق بين class وبين الدله ماتدى نفس القرض؟

مواضيع مشابهة