Question

Ive got this started but im really struggling its a c# program, I have to modify...

Ive got this started but im really struggling its a c# program, I have to modify the class  Purse  to implement the interface ICloneable . create a main program that demonstrates that the Purse method Clone works.

using System;
using System.Collections;

namespace TestingProject
{
   /// <summary>
   /// A coin with a monetary value.
   /// </summary>
   public class Coin   {

       ///   Constructs a coin.
       ///   @param aValue the monetary value of the coin
       ///   @param aName the name of the coin
       public Coin(double aValue, String aName)
       {
           value = aValue;
           name = aName;
       }

       /// Gets the coin value.
       /// @return the value
       public double GetValue()   
       {
           return value;
       }

       ///   Gets the coin name.
       /// @return the name
       public String GetName()
       {
           return name;
       }
      
       public override bool Equals(Object otherObject)
       {
           Coin other = (Coin)otherObject;
           return name==other.name
               && value == other.value;
       }

       // C# requirement:
       // since we override Equals, MUST also override GetHashCode ( !! )
       public override int GetHashCode()
       {
           return base.GetHashCode ();
       }


      

       private double value;
       private string name;
   }
}

using System;
using System.Collections;

namespace TestingProject
{
   /// <summary>
   /// A purse holds a collection of coins.
   /// </summary>
   public class Purse : ICloneable
   {
       /// Constructs an empty purse.
       public Purse()
       {
           coins = new ArrayList();
           Purse MyPurse = (Purse)MemberwiseClone();
       }

       /// Add a coin to the purse.
       /// @param aCoin the coin to add
       public void Add(Coin aCoin)
       {
           coins.Add(aCoin);
       }

       /// Get the total value of the coins in the purse.
       /// @return the sum of all coin values
       public double GetTotal()
       {
          
           double total = 0;
           for (int i = 0; i < coins.Count; i++)
           {
               Coin aCoin = (Coin)coins[i];
               total = total + aCoin.GetValue();
           }
           return total;
       }

       public Object Clone()
       {
           return MemberwiseClone();
       }
      

       private ArrayList coins;
   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

TestingProject. Purse pursel = new TestingProject. Purse(); purse1.Add(new Testing Project.Coin(1, penny)); purse1.Add(new

using System;
using System.Collections;

namespace TestingProject
{
   /// <summary>
   /// A coin with a monetary value.
   /// </summary>
   public class Coin {

       ///   Constructs a coin.
       ///   @param aValue the monetary value of the coin
       ///   @param aName the name of the coin
       public Coin(double aValue, String aName)
       {
           value = aValue;
           name = aName;
       }

       /// Gets the coin value.
       /// @return the value
       public double GetValue()   
       {
           return value;
       }

       ///   Gets the coin name.
       /// @return the name
       public String GetName()
       {
           return name;
       }
      
       public override bool Equals(Object otherObject)
       {
           Coin other = (Coin)otherObject;
           return name==other.name
               && value == other.value;
       }

       // C# requirement:
       // since we override Equals, MUST also override GetHashCode ( !! )
       public override int GetHashCode()
       {
           return base.GetHashCode ();
       }

       private double value;
       private string name;
   }

   /// <summary>
   /// A purse holds a collection of coins.
   /// </summary>
   public class Purse : ICloneable
   {
       /// Constructs an empty purse.
       public Purse()
       {
           coins = new ArrayList();
       }

       /// Add a coin to the purse.
       /// @param aCoin the coin to add
       public void Add(Coin aCoin)
       {
           coins.Add(aCoin);
       }

       /// Get the total value of the coins in the purse.
       /// @return the sum of all coin values
       public double GetTotal()
       {
           double total = 0;
           for (int i = 0; i < coins.Count; i++)
           {
               Coin aCoin = (Coin)coins[i];
               total = total + aCoin.GetValue();
           }
           return total;
       }

       public Object Clone()
       {
           Purse p = new Purse();
           p.coins = (ArrayList)coins.Clone();
           return p;
       }


       private ArrayList coins;
   }

}

class MainClass {
  public static void Main (string[] args) {
    TestingProject.Purse purse1 = new TestingProject.Purse();
    purse1.Add(new TestingProject.Coin(1, "penny"));
    purse1.Add(new TestingProject.Coin(5, "nickel"));
    purse1.Add(new TestingProject.Coin(10, "dime"));

    Console.WriteLine(purse1.GetTotal());

    // Get purse2 by cloning
    TestingProject.Purse purse2 = purse1.Clone() as TestingProject.Purse;
    Console.WriteLine(purse2.GetTotal());

    // Now add something on purse1, which should not change purse 2.
    purse1.Add(new TestingProject.Coin(25, "quarter"));
    
    Console.WriteLine();
    Console.WriteLine(purse1.GetTotal());
    Console.WriteLine(purse2.GetTotal());
  }
}

**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.

Add a comment
Know the answer?
Add Answer to:
Ive got this started but im really struggling its a c# program, I have to modify...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Money Lab using arraylists in Java Using the Coin class below create a program with the...

    Money Lab using arraylists in Java Using the Coin class below create a program with the following requirements Create an arraylist that holds the money you have in your wallet. You will add a variety of coins(coin class) and bills (ones, fives, tens *also using the coin class) to your wallet arraylist. Program must include a loop that asks you to purchase items using the coins in your wallet. When purchasing items, the program will remove coins from the arraylist...

  • How to solve and code the following requirements (below) using the JAVA program? 1. Modify the...

    How to solve and code the following requirements (below) using the JAVA program? 1. Modify the FoodProduct Class to implement Edible, add the @Overrride before any methods that were there (get/set methods) that are also in the Edible interface. 2. Modify the CleaningProduct Class to implement Chemical, add the @Overrride before any methods that were there (get/set methods) that are also in the Chemical interface. 3. Create main class to read products from a file, instantiate them, load them into...

  • Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a...

    Modify the library program as follows: Create a method in the LibraryMaterial class that accepts a string s as a parameter and returns true if the title of the string is equal to s. Create the same method for your library material copies. Note that it will need to be abstract in the LibraryMaterialCopy class MY CODE **************************************************************** LibraryCard: import java.util.List; import java.util.ArrayList; import java.time.LocalDate; import    java.time.temporal.ChronoUnit; public class LibraryCard {    private String id;    private String cardholderName;   ...

  • This is a simple C++ class using inheritance, I have trouble getting the program to work....

    This is a simple C++ class using inheritance, I have trouble getting the program to work. I would also like to add ENUM function to the class TeachingAssistant(Derived class) which is a subclass of Student(Derived Class) which is also a subclass of CourseMember(Base Class). The TeachingAssistant class uses an enum (a user-defined data type) to keep track of the specific role the TA has: enum ta_role {LAB_ASSISTANT, LECTURE_ASSISTANT, BOTH}; You may assume for initialization purposes that the default role is...

  • I have currently a functional Java progam with a gui. Its a simple table of contacts...

    I have currently a functional Java progam with a gui. Its a simple table of contacts with 3 buttons: add, remove, and edit. Right now the buttons are in the program but they do not work yet. I need the buttons to actually be able to add, remove, or edit things on the table. Thanks so much. Here is the working code so far: //PersonTableModel.java import java.util.List; import javax.swing.table.AbstractTableModel; public class PersonTableModel extends AbstractTableModel {     private static final int...

  • Hi, I am having trouble solving the constructor, it requires to ArrayList marks to be a...

    Hi, I am having trouble solving the constructor, it requires to ArrayList marks to be a deep copy of the instance variable. I have also provided the J-Unit test I have been provided. import java.util.*; public class GradebookEntry { private String name; private int id; private ArrayList<Double> marks; /** * DO NOT MODIFY */ public String toString() { String result = name+" (ID "+id+")\t"; for(int i=0; i < marks.size(); i++) { /** * display every mark rounded off to two...

  • Project 2 Tasks: Create a Coin.java class that includes the following:             Takes in a coin...

    Project 2 Tasks: Create a Coin.java class that includes the following:             Takes in a coin name as part of the constructor and stores it in a private string             Has a method that returns the coins name             Has an abstract getvalue method Create four children classes of Coin.java with the names Penny, Nickle, Dime, and Quarter that includes the following:             A constructor that passes the coins name to the parent             A private variable that defines the...

  • Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java...

    Language is Java, any help is appreciated. Thank you! WHERE TO START FROM: import java.util.ArrayList; //PartTest.java //package simple; public class PartTest { public static void main(String[] args) { ArrayList<ExpendablePart> system=new ArrayList<ExpendablePart>();   // creating Part Object Part part1 = new ExpendablePart("Last, First"); part1.setNumber("AX-34R"); part1.setNcage("MN34R"); part1.setNiin("ABCD-RF-WDE-KLJM"); // printing information System.out.println(part1.toString());       //Create a part2 object of class Part Part part2=new ConsumablePart("Widget, purple"); part2.setNumber("12345"); part2.setNcage("OU812"); part2.setNiin("1234-12-123-1234");    // printing information System.out.println(part2.toString());    //checking equality of two Part class objects if(part1.equals(part2)) System.out.println("part1 and...

  • Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a...

    Lab Assignment : In this lab, you are going to implement QueueADT interface that defines a queue. Then you are going to use the queue to store animals. 1. Write a LinkedQueue class that implements the QueueADT.java interface using links. 2. Textbook implementation uses a count variable to keep track of the elements in the queue. Don't use variable count in your implementation (points will be deducted if you use instance variable count). You may use a local integer variable...

  • UPDATE THE UNIVERSE PROGRAM: C. Research the size of each CelestialObject and its distance from the...

    UPDATE THE UNIVERSE PROGRAM: C. Research the size of each CelestialObject and its distance from the sun (for planets) and the distance of the moons from the planets D. Calculate the distance between any two CelestialObject (e.g. between 2 planets and between two moons) COMPLETE STEPS C AND D and THE MODIFICATION BELOW. SUBMIT THE UPDATES! 1. GALAXY CLASS //**MODIFICATION NEEDED** //Use the DBPM format to complete the class. //Review the SolarSystem class for the Star attribute as an example...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT