Showing Page:
1/5
Create a class that models a person that has a name and
money purse with some amount of cash. Note that this may
be the basis for Asn 7.
Create the UML (turn in as built).
Create the java class that implements the UML and create
a driver.
The object can start penniless or with an amount.
All attributes have setters and getters.
The object can accept a gift.
The object can pay a bill.
The object can answer how much it has.
All amounts will be displayed as dollars.
The class can answer the grand total of all transactions.
The driver will execute the following sequence:
Create a penniless object (Name Fred)
Create a rich object (Name Ethyl) with $10.50
Give Fred $5.50
Ask Fred and Ethyl how much each have and display the
amounts on the console.
Take $3.00 from Ethyl and give to Fred.
Create a new penniless object (Name Ricky) but give it
$2.00 of Ethyls money.
Display the amounts of all three objects.
Display grand sum of all transactions.
Solution
############### Person.java
######################3
Showing Page:
2/5
public class Person {
private String name;
private double amount;
private double giftted;
private double accepted;
public Person(String name){
this.name = name;
amount = 0;
}
public Person(String name, double amount){
this.name = name;
this.amount = amount;
}
public String getName() {
return name;
}
public double getAmount() {
return amount;
}
public void setName(String name) {
this.name = name;
}
Showing Page:
3/5
public void setAmount(double amount) {
this.amount = amount;
}
public void acceptGift(double gift){
accepted += gift;
amount += gift;
}
public double payGift(double a){
giftted += a;
amount += a;
return a;
}
public void displayAllTrans(){
System.out.println(\"Total Amount:
\"+String.format(\"%.2f\", amount));
System.out.println(\"Total Giftted Amount:
\"+String.format(\"%.2f\", giftted));
System.out.println(\"Total Accepted Amount:
\"+String.format(\"%.2f\", accepted));
}
}
Showing Page:
4/5
################### Driver CLass
###################
public class PersonDriver {
public static void main(String[] args) {
Person p1 = new Person(\"Fred\");
Person p2 = new Person(\"Ethyl\", 10.50);
p1.acceptGift(5.50);
System.out.println(\"Fred has: $\"+p1.getAmount());
System.out.println(\"Ethyl has: $\"+p2.getAmount());
p1.acceptGift(p2.payGift(3.0));
Person p3 = new Person(\"Ricky\");
p3.acceptGift(p2.payGift(2.0));
p1.displayAllTrans();
System.out.println();
p2.displayAllTrans();
System.out.println();
p3.displayAllTrans();
}
Showing Page:
5/5
}
########## Sample Output ################
Fred has: $5.5
Ethyl has: $10.5
Total Amount: 8.50
Total Giftted Amount: 0.00
Total Accepted Amount: 8.50
Total Amount: 15.50
Total Giftted Amount: 5.00
Total Accepted Amount: 0.00
Total Amount: 2.00
Total Giftted Amount: 0.00
Total Accepted Amount: 2.00

Unformatted Attachment Preview

Create a class that models a person that has a name and money purse with some amount of cash. Note that this may be the basis for Asn 7. Create the UML (turn in ‘as built’). Create the java class that implements the UML and create a driver. The object can start penniless or with an amount. All attributes have setters and getters. The object can accept a gift. The object can pay a bill. The object can answer how much it has. All amounts will be displayed as dollars. The class can answer the grand total of all transactions. The driver will execute the following sequence: Create a penniless object (Name Fred) Create a rich object (Name Ethyl) with $10.50 Give Fred $5.50 Ask Fred and Ethyl how much each have and display the amounts on the console. Take $3.00 from Ethyl and give to Fred. Create a new penniless object (Name Ricky) but give it $2.00 of Ethyl’s money. Display the amounts of all three objects. Display grand sum of all transactions. Solution ############### Person.java ######################3 public class Person { private String name; private double amount; private double giftted; private double accepted; public Person(String name){ this.name = name; amount = 0; } public Person(String name, double amount){ this.name = name; this.amount = amount; } public String getName() { return name; } public double getAmount() { return amount; } public void setName(String name) { this.name = name; } public void setAmount(double amount) { this.amount = amount; } public void acceptGift(double gift){ accepted += gift; amount += gift; } public double payGift(double a){ giftted += a; amount += a; return a; } public void displayAllTrans(){ System.out.println(\"Total Amount: \"+String.format(\"%.2f\", amount)); System.out.println(\"Total Giftted Amount: \"+String.format(\"%.2f\", giftted)); System.out.println(\"Total Accepted Amount: \"+String.format(\"%.2f\", accepted)); } } ################### Driver CLass ################### public class PersonDriver { public static void main(String[] args) { Person p1 = new Person(\"Fred\"); Person p2 = new Person(\"Ethyl\", 10.50); p1.acceptGift(5.50); System.out.println(\"Fred has: $\"+p1.getAmount()); System.out.println(\"Ethyl has: $\"+p2.getAmount()); p1.acceptGift(p2.payGift(3.0)); Person p3 = new Person(\"Ricky\"); p3.acceptGift(p2.payGift(2.0)); p1.displayAllTrans(); System.out.println(); p2.displayAllTrans(); System.out.println(); p3.displayAllTrans(); } } ########## Sample Output ################ Fred has: $5.5 Ethyl has: $10.5 Total Amount: 8.50 Total Giftted Amount: 0.00 Total Accepted Amount: 8.50 Total Amount: 15.50 Total Giftted Amount: 5.00 Total Accepted Amount: 0.00 Total Amount: 2.00 Total Giftted Amount: 0.00 Total Accepted Amount: 2.00 Name: Description: ...
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.
Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4