精英团队10年代写经验
专业CS编程代写服务

Data Structures Programming

For this assignment, you need to install and run Eclipse on your machine and import the project. Please generate an API document (see Appendix E) and then take a look at that document as well as the source code to familiarize yourself with this assignment. This assignment provides you with a set of incomplete classes. To see these classes, choose the src source folder and packages such as csi213.projects.computers and csi213.projects.util. You will need to update the classes in these packages. Your code will be graded by running unit tests (see UnitTests.java in the csi213.projects.computers and csi213.projects.util packages) and then examining the changes you made in the source code. The unit tests use JUnit (http://junit.org). Refer to Appendix D for details of running these tests. Note that passing unit tests does not necessarily guarantee that your implementation is correct and efficient. Please make sure that your code would not cause problems even in situations not covered by the unit tests.

In this assignment, Part 3 focuses on performing certain array operations using either iteration or recursion. Since the ability to develop iterative and/or recursive solutions to computational problems is very crucial, pay a particular attention to Part 3. Problems 3a-3c require a basic understanding and application of iteration and recursion, so do your best to solve these problems. Problem 3d is more challenging/interesting and many different solutions to this problem would be feasible. Try to develop as elegant/succinct/brilliant a solution as possible by yourself. In case you cannot solve Problem 3d, it will have a limited impact since only 5 points are allocated to this problem.

In Part 3, Problems 3a-3c are designed to provide data for ABET (Accreditation Board for Engineering and Technology) assessement regarding the following performance indicator:
SO6. Apply computer science theory and software development fundamentals to produce computing- based solutions.

PI-6.1. Identifies the appropriate use of computer science theories in solving a computing-based problem.

Please understand that your solutions to Problems 3a-3c will play a critical role in ABET assessment and will be graded according to the following levels of performance:

• UNSATISFACTORY (0-20): The code from the student does not compile, or at most one method is correctly implemented.

1

  • DEVELOPING (21-35): The code from the student compiles and at most two methods are correctly implemented.
  • SATISFACTORY (36-40): The code from the student compiles and the three methods are almost correctly implemented.
  • EXEMPLARY (41-45): The code from the student compiles and the three methods are correctly implemented.

    If you have questions, please contact the TA(s) or the instructor. The remainder of this docu-

ment describes the classes that you need to complete.

Part 1. Completing Computer.java (30 points)

Problem 1a. (5 points) In Computer.java, update the constructor Computer(String serialNumber, double price) so that it can create a new Computer instance while setting the member variables (i.e., serialNumber and price) of that instance.

Problem 1b. (5 points) In Computer.java, add an appropriate method so that the following code in Computer#main(String[]) will output “serial number: s1, price: 500.0”:

Computer c = new Computer(“s1” , 500); System.out.println(c);

Note: in Java, when a string representation of an object is needed (as in System.out.println(c)), a method is automatically called on that object. This method is one of the several methods (includ- ing wait(), hashCode(), and others) that can be invoked on any Java object. Your implementation of this method is a case of method overriding in Java. Please understand that this method needs to return a string representation in the exact format mentioned above. Otherwise, your code will not be able to pass the unit test named test1ab. For example, test1ab fails if your implemen- taton returns “[serial number: s1, price: 500.0]” while test1ab expects “serial number: s1, price: 500.0” due to the mismatch of these two string representations (i.e., because of the two unnecessary characters [ and ]).1

Problem 1c. (5 points) In Computer.java, update the getSerialNumber() method so that, given a Computer instance, this method will return the serial number of that instance. If this method is implemented correctly, the following code in Computer#main(String[]) (if executed after the code in Problem 1b) will output “s1”:

System.out.println(c.getSerialNumber()); Your code needs to pass the unit test named test1c.

Problem 1d. (5 points) In Computer.java, update the setPrice(double price) method so that, given a Computer instance, this method will set the price of that instance to the specified argu- ment price given to the method. If this method is implemented correctly, the following code in Computer#main(String[]) (if executed after the code in Problem 1c) will output “serial number: s1, price: 600.0”:

c.setPrice(600); System.out.println(c);

1In test1ab(), see assertEquals(“serial number: s1, price: 500.0”, “” + new Computer(“s1”, 500.0));. The assertEquals(String, String) method throws a ComparisonFailure exception if the two String arguments are not equal.

2

Also, your code will pass the unit test named test1d.
Problem 1e. (5 points) In Computer.java, add an appropriate method so that the following code

in Computer#main(String[]) (if executed after the code in Problem 1d):

System.out.println(c.equals(new Computer(“s1”, 500))); System.out.println(c.equals(new Computer(new String(“s1”), 500))); System.out.println(c.equals(new Computer(“s2”, 500))); System.out.println(c.equals(“s1”));

will output:

true
true
false
false

In the above example, variable c refers to a Computer instance whose serialNumber and price are s1 and 600.0, respectively. Given an object o, c.equals(o) must return true if (i) o refers to a Computer instance and (ii) the Computer instances that c and o refer to have the same serialNumber (please ignore their price values). Please use, when comparing java String objects, the equals(Object) method, not the == operator.2 Also, ensure that your code passes the unit test named test1e.

Problem 1f. (5 points) The provided Computer.java file does not support serialization. Change Computer.java so that it supports serialization and thus the following code outputs “serial number: s1, price: 600.0” without throwing a NotSerializableException:

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(“test.dat”)); out . writeObject ( c ) ;
out . close ( ) ;
ObjectInputStream in = new ObjectInputStream(new FileInputStream(“test.dat”)); System . out . println ( in . readObject ( ) ) ;

in . close ( ) ;
Your code needs to pass the unit test named test1f.

Part 2. Completing Laptop.java (15 points)

Problem 2a. (5 points) In Laptop.java, make Laptop a sub-class of Computer (i.e., inherit mem- ber variables and methods from the Computer class). Also, update the constructor Laptop(String serialNumber, double price, double screenSize) so that it can create a new Laptop instance while setting the member variables (i.e., serialNumber, price, and screenSize) of that instance.

Problem 2b. (5 points) In Laptop.java, add an appropriate method so that the following code in Laptop#main(String[]) will output “serial number: l1, price: 1000.0, screen size: 13.1”:

Laptop l = new Laptop(“l1” , 1000.0 , 13.1); System.out.println(l);

2To understand the point clearly, try System.out.println(“1” == new String(“1”));. You will be able to see that the return value of the == operator is false since “1” and the new String obtained from new String(“1”) are different objects at different locations in memory.

3

Your code needs to pass the unit test named test2ab.

Problem 2c. (5 points) In Laptop.java, the purpose of the count() method is to return the number of Laptop instances that have been constructed since the Java program started. Modify Laptop.java so that the count() method will always return the exact number of Laptop in- stances. The following code in Laptop#main(String[]) needs to output “number of Laptops: 2” (if executed after the code in Problem 2b):

new Laptop(“l2” , 400.0 , 12);
System.out.println(“number of Laptops: ” + Laptop.count());

Also, ensure that your code passes the unit test named test2c.

Part 3. Completing IntArrays.java (50 points)

Problem 3a. (20 points) In IntArrays.java, implement the sum(int[] a) method. Given an int array a, this method must return an int value which is the sum of the elements (i.e., int values) of a. For example, if a is set to {1, 2, 3}, then sum(a) must return 6 since the sum of 1, 2, and 3 is 6. Please implement this sum(int[] a) method using only one for loop. If sum(int[] a) is correctly implemented, then the following code in IntArrays#main(String[]):

System.out.println(sum(new int[] { 1, 2, 3 })); System.out.println(sum(new int[] { 1, 2, 3, 4 })); System.out.println(sum(new int[] { 1, 2, 3, 4, 5 }));

will output:

6 10 15

Your code also needs to pass the unit test named test3a in UnitTests.java of the csi213.projects.util package.

Problem 3b. (15 points) In IntArrays.java, implement the sumR(int[] a, int startIndex) method. Given an int array a, this method must return an int value which is the sum of the elements a[startIndex], a[startIndex + 1], ···, a[a.length – 1] (i.e., the sum of all int values within the range that begins with startIndex in array a). Please implement this sumR(int[] a, int startIndex) method using recursion understanding that (i) the return value of sumR(a, a.length – 1) is a[a.length – 1] (i.e., the last element in array a) and (ii) if startIndex < a.length – 1, then the return value of sumR(a, startIndex) is the sum of a[startIndex] and sumR(a, startIndex + 1). Once you complete the sumR(int[] a, int startIndex) method, make sure that sumR(new int[] { 1, 2, 3 }, 2) returns 3 (i.e., the element at index 2 in the array) and sumR(new int[] { 1, 2, 3 }, 1) returns 5 (i.e., the sum of 2 and 3, which are the elements at indices 1 and 2). Furthermore, the following code in IntArrays#main(String[]):

System.out.println(sumR(new int[] { 1, 2, 3 }, 0)); System.out.println(sumR(new int[] { 1, 2, 3, 4 }, 0)); System.out.println(sumR(new int[] { 1, 2, 3, 4, 5 }, 0));

will output:

6 10 15

4

Please ensure that your code passes the unit test named test3b.

Problem 3c. (10 points) In IntArrays.java, implement the minSum2(int[] a) method. Given an int array a, this method must return an int value which is the minimum possible sum of two numbers from a. For example, minSum2(new int[] { 1, 2, 3 }) must return 3 since (i) there are 3 possible cases of adding two numbers (1 + 2 = 3, 1 + 3 = 4, and 2 + 3 = 5) and (ii) 3 is the minimum of these three possible sums. Please implement this minSum2(int[] a) method using two for loops. If this method is correctly implemented, then the following code in IntArrays#main(String[]):

System.out.println(minSum2(new int[] { 1, 2, 3 })); System.out.println(minSum2(new int[] { 5, 2, 3, 4 })); System.out.println(minSum2(new int[] { 6, 7, 3, 4, 5 }));

will output:

3 5 7

Your code will also pass the unit test named test3c.

Problem 3d. (5 points) In IntArrays.java, implement the minSum(int[] a, int k) method. Given an int array a, this method must return an int value which is the minimum possible sum of k numbers from a. For example, minSum(new int[] { 1, 2, 3 }, 2) must return 3 since (i) k = 2, so all cases of adding two numbers from a are considered, (ii) there are 3 possible cases of adding twonumbers(1 + 2 = 3,1 + 3 = 4,and2 + 3 = 5),and(iii)3istheminimumofthesethree possible sums. Furthermore, minSum(new int[] { 1, 2, 3 }, 3) must return 6 since there is only one case of adding three numbers (1 + 2 + 3 = 6). In the case of minSum(new int[] { 1, 2, 3 }, 1), the return value is 3 because 3 is the smallest among 1, 2, and 3. If you would like, feel free to implement the minSum(int[] a, int k) method while creating an additional method and making minSum(int[] a, int k) call that method.

If you complete this method correctly, then the following code in IntArrays#main(String[]):

System.out.println(minSum(new int[] { 1, 2, 3, 4, 5, 6 }, 1)); System.out.println(minSum(new int[] { 1, 2, 3, 4, 5, 6 }, 2)); System.out.println(minSum(new int[] { 1, 2, 3, 4, 5, 0 }, 2)); System.out.println(minSum(new int[] { 6, 5, 4, 3, 2 }, 2)); System.out.println(minSum(new int[] { 1, 2, 3, 4, 5, 6 }, 3)); System.out.println(minSum(new int[] { 1, 2, 3, 4, 5, 6 }, 4));

will output:

1 3 1 5 6 10

In addition, your code will pass the unit test named test3d.

赞(0)

精英团队10年代写经验,专业CS编程作业代写服务

微信: cscodinghelp
邮箱: info@cscoding.net

联系我们交易流程