Question

In: Computer Science

In class need to define two fields: 1. Integer part of the number - ulong; &...

In class need to define two fields:

1. Integer part of the number - ulong; & fractional part of the number - ushort in C#

Using C#, create the following methods:

1. Method for adding two prices (parameters for function is 2 numbers, integer(main currency) and fractional parts(Fractional currency))

2. Method for adding two prices(parameter is object of same type as your class)

3. Method for adding two prices(parameter is string)

Solutions

Expert Solution

Code:

using System;

class Amount
{
    private ulong integerPart;
    private ushort fractionPart;

    public Amount(ulong integerPart, ushort fractionPart)
    {
        this.integerPart = (ulong) integerPart + (ulong) fractionPart / 100;
        this.fractionPart = (ushort) (fractionPart % 100);
    }

    public void AddPrices(ulong integerPart, ushort fractionPart)
    {
        this.integerPart += (ulong) integerPart + (ulong) ((this.fractionPart + fractionPart) / 100);
        this.fractionPart = (ushort) ((this.fractionPart + fractionPart) % 100);
    }

    public void AddPrices(Amount c)
    {
        AddPrices(c.integerPart, c.fractionPart);
    }

    public void AddPrices(string s)
    {
        string[] money = s.Split('.');
        ulong integerPart = Convert.ToUInt64(money[0]);
        ushort fractionPart = Convert.ToUInt16(money[1]);

        AddPrices(new Amount(integerPart, fractionPart));
    }

    public override string ToString()
    {
        return $"Amount: ${this.integerPart}.{this.fractionPart}";
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Console.WriteLine("Hello World!");
        Amount a = new Amount(20, 98);
        Amount b = new Amount(23, 88);


        Console.WriteLine(a.ToString());
        a.AddPrices(20, 50);
        Console.WriteLine(a.ToString());
        a.AddPrices(b);
        Console.WriteLine(a.ToString());
        a.AddPrices("10.87");
        Console.WriteLine(a.ToString());

    }
}


Output:

Code screenshots:


Related Solutions

Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a...
Create a class named Salesperson. Data fields for Salesperson include an integer ID number and a doubleannual sales amount. Methods include a constructor that requires values for both data fields, as well as get and set methods for each of the data fields. Write an application named DemoSalesperson that declares an array of 10 Salesperson objects. Set each ID number to 9999 and each sales value to zero. Display the 10 Salesperson objects. public class DemoSalesperson { public static void...
Question 1 - Create a class named Student that has fields for an ID number, number...
Question 1 - Create a class named Student that has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. A Student also has a field for grade point average. Include a method to compute the grade point...
java Define the Circle2D class that contains: • Two double data fields named x and y...
java Define the Circle2D class that contains: • Two double data fields named x and y that specify the center of the circle with get methods. A data field radius with a get method. • A data field radius with a get method. • A no-arg constructor that creates a default circle with (0, 0) for (x, y) and 1 for radius. • A constructor that creates a circle with the specified x, y, and radius. • A method getArea()...
Define the circle2d class that contains: Two double data fields named x and y that specify...
Define the circle2d class that contains: Two double data fields named x and y that specify the center of the circle with getter methods A data field radius with a getter method A no arg constructor that creates a default circle with 0,0 for x,y and 1 for the radius A constructor that creates a circle with the specified x,y of the the circle A method getArea() that returns the area of the circle A method Contains(Double x, Double y)...
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well...
INVENTORY CLASS You need to create an Inventory class containing the private data fields, as well as the methods for the Inventory class (object). Be sure your Inventory class defines the private data fields, at least one constructor, accessor and mutator methods, method overloading (to handle the data coming into the Inventory class as either a String and/or int/float), as well as all of the methods (methods to calculate) to manipulate the Inventory class (object). The data fields in the...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class...
Define a class called Counter whose internal "count" variable is a basic integer counter. This class track that count from its instantiation in the constructor (can be set to any positive integer, or 0). The count should never be allowed to be negative. Include methods that will set the counter to 0, increase the count by 1, and decrease the count by 1. Include an accessor method that returns the current count value (getCount()). There should be no input /...
Create a class named Student. Student has fields for an ID number, number of credit hours...
Create a class named Student. Student has fields for an ID number, number of credit hours earned, and number of points earned. (For example, many schools compute grade point averages based on a scale of 4, so a three-credit-hour class in which a student earns an A is worth 12 points.) Include methods to assign values to all fields. Student also has a field for grade point average. Include a method to compute the grade point average field by dividing...
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and...
JAVAFX LAB 2 1. The Soda class has two fields: a String for the name and a double for the price. 2. The Soda class has two constructors. The first is a parameterized constructor that takes a String and a double to be assigned to the fields of the class. The second is a copy constructor that takes a Soda object and assigns the name and price of that object to the newly constructed Soda object. 3. The Soda class...
In Chapter 8, you created a Salesperson class with fields for an ID number and sales...
In Chapter 8, you created a Salesperson class with fields for an ID number and sales values. Now, create an application that allows a user to enter values for an array of seven Salesperson objects. Offer the user the choice of displaying the objects in order by either (I)D number or (S)ales value. ----------------------- Salesperson.java public class Salesperson {    private int id;    private double sales;    Salesperson(int idNum, double amt)    {       id = idNum;       sales = amt;    }    public int getId()...
In java, create a class named Contacts that has fields for a person’s name, phone number...
In java, create a class named Contacts that has fields for a person’s name, phone number and email address. The class should have a no-arg constructor and a constructor that takes in all fields, appropriate setter and getter methods. Then write a program that creates at least five Contact objects and stores them in an ArrayList. In the program create a method, that will display each object in the ArrayList. Call the method to demonstrate that it works. Include javadoc...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT