12-05-2011, 12:16 AM
|
|
|
|
تاريخ التسجيل: Jul 2009
الكلية: كلية الآداب والعلوم الانسانية
التخصص: علم نفس
نوع الدراسة: إنتساب
المستوى: متخرج
البلد:
جــــدة
الجنس: أنثى
المشاركات: 159
|
|
ممكن اعرف الخطأ في هذآ الكود *_^
السلام عليكم ورحمة الله وبركآته ^^
كيف حآل الجميــع ,, إن شاء الله كلكم تمـــآم
ممكن تسـآعدوني اعرف الخطأ في هذا الكود واكون ممنونه ..!!
Define a class named Money whose objects represent amounts of U.S. money. The class will
have two instance variables of type int for the dollars and cents in the amount of money.
Include a constructor with two parameters of type int for the dollars and cents, one with one
parameter of type int for an amount of dollars with zero cents, and a no-argument constructor.
Include the methods add for addition and minus for subtraction of amounts of money. These
methods should be a static methods and should each have two parameters of type Money and
returns a value of type Money. Include a reasonable set of accessors (getter) and mutator
(setter) methods as well as the methods equals and toString.
Add a second version of the methods for addition and subtraction. These methods should have
the same names as the static version but use a calling object and a single argument. For
example, this version of add method (for addition) has calling object and one argument. So
m1.add(m2) returns the result of adding the Money objects m1 and m2. Note that your class
should have all these methods; for example, there should be two methods named add.
Write a test program for your class.
Notes: You will need to implement the following methods:
int getCents()
int getDollars()
void setDollars(int dollars)
void setCents(int cents)
boolean equals(Money amount)
static Money add(Money amount1, Money amount2)
static Money minus(Money amount1, Money amount2)
Money add(Money m)
Money minus(Money m)
String toString()
كود PHP:
/ Class representing an amount of U.S. money */ class Money { / Dollars in this amount of money. If the instance represents a negative amount of money, dollars should be negative. */ private int dollars; / Cents in this amount of money. In this implementation, cents should always be non-negative. */ private int cents; // -------------------------------- // ----- ENTER YOUR CODE HERE ----- // -------------------------------- private int hundred; //Dollars and cents in this amount of money. private int aBuckFifty, aDollarFifty, aBuckHalf; public Money() //no argument constructor { } public Money(int dollars) //constructor for dollars and no cents { } public Money(int dollar, int cents) //two parameter constructor { } public int getDollars() { return dollars; } public void setDollars(int dollars) { this.dollars = dollars; } public int getCents() { return cents; } public void setCents(int cents) { this.cents=cents; } public boolean equals(Money obj) { return ((aBuckHalf == aDollarFifty) && (hundred == cents)); } public int Money add (Money m1, Money m2) { return m1 + m2; } public int Money minus (Money m1, Money m2) { return m1 - m2; } // -------------------------------- // --------- END USER CODE -------- // -------------------------------- / Returns a string representation of this object's amount */ public String toString() { // Note that we could use a DecimalFormat object here instead // of doing this test. String result; if (dollars < 0) result = "-$"; else result = "$"; if (cents < 10) result = result + Math.abs(dollars) + ".0" + cents; else result = result + Math.abs(dollars) + "." + cents; return result; } } / Test class for exercising the methods of the Money class */ public class MoneyDemo { public static void main(String[] args) { // Test the no-arg constructor, setter methods, and toString Money nickel = new Money(); nickel.setCents(5); System.out.println("A nickel: " + nickel); Money aBuckFifty = new Money(); aBuckFifty.setDollars(1); aBuckFifty.setCents(50); System.out.println("A buck fifty: " + aBuckFifty); // Test the one- and two-argument constructors Money cNote = new Money(100); System.out.println("A C-Note: " + cNote); Money aDollarFifty = new Money(1, 50); System.out.println("One dollar and fifty cents: " + aDollarFifty); // Test for equality System.out.println("C-Note equals a nickel? " + cNote.equals(nickel)); System.out.println("A buck fifty equals one dollar and fifty cents? " + aBuckFifty.equals(aDollarFifty)); // Test add and minus methods System.out.println(); System.out.println( cNote + " + " +aBuckFifty + " = " + Money.add(cNote, aBuckFifty)); System.out.println( cNote + " - " +aBuckFifty + " = " + Money.minus(cNote, aBuckFifty)); Money aDebt = Money.minus(nickel, cNote); System.out.println(nickel + " - " +cNote + " = " + aDebt); System.out.println( aDollarFifty + " - " + aBuckFifty + " = " + Money.minus(aDollarFifty, aBuckFifty)); System.out.println( aDollarFifty + " + " + aBuckFifty + " = " + Money.add(aDollarFifty, aBuckFifty)); System.out.println( aDebt + " + " + aBuckFifty + " = " + Money.add(aDebt, aBuckFifty)); } }
عارفه ان البرنآمج طويل وثقيل دمـــ ><"
بس بليــــــز محتاجه مسآعدتكم بأسرع وقت
وشكرآ
|