Java代写:COMP110 Merge and Money

代写Java基础作业,练习基本的面向对象的编程方法。

Merge Two Sorted Lists

STAGE 1 | (Merge two sorted lists)

Write the following method that merges two sorted lists into a new sorted list.

1
public static int[] merge(int[] list1, int[] list2)

Implement the method in a way that takes at most list1.length + list2.length comparisons. Write a test program that doe the following:

  1. Input list1_size
  2. Int[] list1 = generateList(list1_size) // (1-20 inclusive)
  3. Sort list1
  4. printList(list1)
  5. Input list2_size
  6. Int[] list1 = generateList(list2_size) // (1-20 inclusive)
  7. Sort list2
  8. printList(list2)
  9. Int[] result = merge(list1, list2)
  10. printList(result)

Example

List 1:

1 5 8 10 18

List 2:

2 4 5 6 9 15 17 20

Result:

1 2 4 5 5 6 8 9 10 15 17 18 20

STAGE 2 | Submission

Submit: CANVAS

Classes and Objects

STAGE 1 | (Money class)

Design a class named Money. The class contains:

  • The data fields dollars and cents that represent a money.
  • A no-arg constructor that creates a Money object of $0.00
  • A constructor that constructs a Money object with specified dollars and cents.
  • Two get methods for the data fields dollars and cents.
  • Two set methods for the data fields dollars and cents.
  • A method named add(Money m) that adds object m to this object.
1
2
3
4
public Money add(Money m)
{


}

For example:

1
2
3
Money m1 = new Money(5,45);
Money m2 = new Money(10,95);
Money result = m1.add(m2); // result should be $16.40

  • A method named subtract(Money m) that subtracts object m from this object. If object m is greater than this object, then return null.
1
2
3
4
public Money subtract(Money m)
{


}

For example:

1
2
3
Money m1 = new Money(10,45);
Money m2 = new Money(5,95);
Money result = m1.subtract(m2); // result should be $4.50

  • A method named toString() that returns a string in $*.** format
    • Use String.format() method

STAGE 2 | (Driver program)

Write a driver program called TestMoney to do the following:

  1. Generate a list of fifteen random Money objects (Dollar amount in the range of 1 to 30, inclusive. Cent amount in the range of 0 to 99, inclusive).
  2. Print the list (10 per line)
  3. Calculate and print the sum.

STAGE 3 | Submission

Submit Hard copy and upload Canvas