Advertisement
Guest User

Untitled

a guest
Apr 8th, 2020
360
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.64 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. /*Sophie and Sally are learning about money.  Every once in a while, their parents will give them penny or shilling coins (no pounds at their age!).  They each keep their money in a special purse, and their parents may ask them how many pence or shillings coins they have.  (Note we're not interested in the monetary values, just the number of coins.  Thus someone might have 25 shilling coins or 20 penny coins.)  The interaction between the girls' parents and Sophie and Sally is similar to the following:
  4.  
  5.  
  6. Enter 1 for Sophie, 2 for Sally, or 0 to exit: 1
  7. Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 1
  8. Enter the pence to give: 3
  9.  
  10. Enter 1 for Sophie, 2 for Sally, or 0 to exit: 2
  11. Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 2
  12. Enter the shillings to give: 2
  13.  
  14. Enter 1 for Sophie, 2 for Sally, or 0 to exit: 2
  15. Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 2
  16. Enter the shillings to give: 1
  17.  
  18. Enter 1 for Sophie, 2 for Sally, or 0 to exit: 1
  19. Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 3
  20. The purse has 3 pence, 0 shillings
  21.  
  22. Enter 1 for Sophie, 2 for Sally, or 0 to exit: 2
  23. Enter 1 to give pence, 2 to give shillings, 3 to query her purse: 3
  24. The purse has 0 pence, 3 shillings
  25.  
  26. Enter 1 for Sophie, 2 for Sally, or 0 to exit: 0
  27.  
  28. To get started, consider the following questions:
  29.  
  30. What are the major nouns of the scenario?  Sophie, Sally, parents, pence, shilling, coin, purse
  31. What are the relationships between these nouns?
  32. Sophie and Sally each have purses.  Having a Daughter or Child object might be possible, but in this scenario the only thing interesting about Sophie and Sally is that they each have a purse.  We can treat them as names of different purse objects (i.e., they are variables of type Purse).
  33. the parents are really just a euphemism for the user of the program.  We have to interact with the user, but don't need a specific class.
  34. pence and shillings are coins, but they also just count something--the int data type will represent them just fine
  35. a purse contains pence and shillings, and we also need to add pence and shillings to a purse, and fetch its number of pence and shilling coins.  This looks like an object.
  36. Write a program to implement this scenario.  Use two classes.  One class should contain a main method and manage the dialog with the user.  The other class should represent a Purse.  The design of the Purse class is up to you.  Make sure that your instance variables are private, follow the naming conventions, etc.
  37.  
  38. Grading Elements
  39.  
  40. Program has Purse class with no main method and that maintains the number of penny or shilling coins
  41. Program has tester class with a main method that contains all interaction with the user and manipulates Purse objects for sophie and sally
  42. The tester class does not keep track of any coin counts except by using Purse objects
  43. The program should support the sample user interaction*/
  44.  
  45. public class Tester {
  46.     private static String start = "Enter 1 for Sophie, 2 for Sally, or 0 to exit: ";
  47.     private static String options = "Enter 1 to give pence, 2 to give shillings, 3 to query her purse:";
  48.     private static int optionSelected;
  49.     private static int daughter;
  50.     public static void main(String[] args) {
  51.         Scanner input = new Scanner(System.in);
  52.  
  53.         Purse sally = new Purse();
  54.         Purse sophie = new Purse();
  55.         Purse currentPurse = null;
  56.         System.out.println(start);
  57.         daughter = Integer.parseInt(input.nextLine());
  58.         while (daughter!=0) {
  59.            
  60.         if (daughter == 1) {
  61.             currentPurse = sophie;
  62.         } else if (daughter == 2) {
  63.             currentPurse = sally;
  64.         }
  65.         System.out.println(options);
  66.         optionSelected = Integer.parseInt(input.nextLine());
  67.         if (optionSelected==1) {
  68.             System.out.println("Enter the shillings to give: ");
  69.             currentPurse.setShillings(Integer.parseInt(input.nextLine()));
  70.         }
  71.         if(optionSelected==2) {
  72.             System.out.println("Enter the pence to give: ");
  73.             currentPurse.setPence(Integer.parseInt(input.nextLine()));
  74.         }
  75.         if(optionSelected==3) {
  76.             System.out.println("The purse has "+ currentPurse.getPence()+ " pence, "+ currentPurse.getShillings()+ " shillings. ");
  77.         }
  78.         System.out.println("\n" +start);
  79.         daughter = Integer.parseInt(input.nextLine());
  80.                
  81.         }
  82.     }
  83. }
  84. //////////////////////////////////////
  85. public class Purse {
  86. private int pence;
  87. private int shillings;
  88.  
  89.  Purse(){
  90.      setPence(0);
  91.      setShillings(0);
  92.      
  93.  }
  94.  
  95. public int getPence() {
  96.     return pence;
  97. }
  98.  
  99. public void setPence(int pence) {
  100.     this.pence += pence;
  101. }
  102.  
  103. public int getShillings() {
  104.     return shillings;
  105. }
  106.  
  107. public void setShillings(int shillings) {
  108.     this.shillings += shillings;
  109. }
  110.  
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement