代写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.
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:
- Input list1_size
- Int[] list1 = generateList(list1_size) // (1-20 inclusive)
- Sort list1
- printList(list1)
- Input list2_size
- Int[] list1 = generateList(list2_size) // (1-20 inclusive)
- Sort list2
- printList(list2)
- Int[] result = merge(list1, list2)
- 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.
public Money add(Money m)
{
}
—|—
For example:
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.
public Money subtract(Money m)
{
}
—|—
For example:
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:
- 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).
- Print the list (10 per line)
- Calculate and print the sum.
STAGE 3 | Submission
Submit Hard copy and upload Canvas