attach
Comp6010_Assignment02_s12021/.classpath Comp6010_Assignment02_s12021/.project Comp6010_Assignment02_s12021 org.eclipse.jdt.core.javabuilder org.eclipse.jdt.core.javanature Comp6010_Assignment02_s12021/.settings/org.eclipse.jdt.core.prefs eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled org.eclipse.jdt.core.compiler.codegen.targetPlatform=15 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=15 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=warning org.eclipse.jdt.core.compiler.release=enabled org.eclipse.jdt.core.compiler.source=15 Comp6010_Assignment02_s12021/bin/ComputingTasks.class Comp6010_Assignment02_s12021/bin/ComputingTasksTests.class Comp6010_Assignment02_s12021/src/ComputingTasks.java Comp6010_Assignment02_s12021/src/ComputingTasks.java /* * You cannot use ANY function that is not defined inside ComputingTasks class * You can reuse functions defined inside ComputingTasks class only */ public class ComputingTasks { /* * Look at the test cases for meaningful method names */ /** Question 01 * @param int[] data * @return true if all the elements in the array are odd, false otherwise * {1,3,5,7,9} --> true * {1,2,3,56,7} --> false * null --> false */ //function template provided as a template public static boolean oddNumbers(int[] data) { return false; } /** Question 02 * * @param n (n is non-negative) * @return the number of digits in n * You can assume n>0 * For example, * if n = 0, return 1 * if n = 22, return 2 * if n = 8866, return 4 * */ //add the function template /** Question 03 * @param int[] data * @return the index of the highest value (item) from the data array * {1,34,56,200,-1} --> 3 * {800,40,20,700} --> 0 * {-10,-2,-3,-4} --> 1 */ //add the function template /** Question 04 * @param int[] data * @return the sum of all positive integers in the data array * {1,2,100,-1,-8} -> 102 * {1,2,3,-9} --> 6 */ //add the function template /** Question 05 * @param int[] data1, int low, int high * @return an array that contains only the even numbers from the data array in the range [low, high], inclusive on both sides * {1,2,3,4,5,6,7} -> {2,4,6} * {3,5,7} --> {} * {2,4,6} --> {2,4,6} * {} -> {} */ //add the function template /** Question 06 * It is the first day of O-week and you have just moved into your new student * accommodations. You have decided to befriend all the other freshers (new * residents) on discord. Given the number of freshers (including yourself), how many friend requests * do you have to send/receive to befriend everyone. * * Assume that you do not already know anyone and that every resident has * discord. Write the function befriend(number of freshers). Assume that the * number of freshers is positive. */ //add the function template /** Question 07 * @param n * @return the highest prime number less than or equal to n. * for example, * hPrime(11) = 11 * hPrime(100) = 97 */ //add the function template /** Question 08 * You are holding a dinner and have invited n people. But with the COVID * restrictions, only 2 people are allowed to sit at the same table. Since * you don't want anyone to sit by themselves, you have decided to write the * function enoughTables(n) to determine if it will be possible to seat * everyone. Return true if everyone can sit with someone else, and false * otherwise. Remember that you will also need a seat. */ //add the function template /** Question 09 * The three friends Sam, Amy and Sara are planning to rent a new house. One of * their requirements is that none of them should risk hitting the ceiling. To * be able to satisfy this requirement, they want to know the maximum height * that the ceiling should be higher than. Help Sam, Amy and Sara to find this * height by writing the function maxHeight(Sam's height, Amy's Height, Sara's * Height) */ //add the function template /** Question 10 * Sam and Amy have been collecting stamps. Every time they meet their friend * Sara, she asks if they have collected at least x stamps. But as they are now * moving in together, Sam and Amy are afraid that they will have to answer the * question very often. Therefore, they have asked you to create a function that * can determine the answer to Sara’s question. * * The input for the function is collectedStamps(Sam's stamps, Amy's stamps, x). * For example, if Sam has collected 5 stamps and Amy has collected 6 stamps, * they have together collected 5+6=11 stamps. If Sara asks them if they have * collected at least 4 stamps, the answer would be true. */ //add the function template /** Question 11 * Depending on if it's a sales season or not a store changes its prices on * products. When they go into a sales season, every products price halves. But * if it's the end of the sales season the prices return to the original price. * * The variable "startSale" will be true if it's the beginning of a sale and * false if it's the end of a sale. Given this information and an array of all * the prices in the store, create a function with the input * updatePrice(startSale, current price of items) that automatically updates the * prices. */ //add the function template /** Question 12 * A group of friends are going hiking over the weekend. But when they meet up * to start the hike, they notice that their backpacks are unevenly packed. The * current weight of the backpacks is stored in an array. To make the hike as * simple as possible, determine the optimal weight so that every group members * backpack will be the same weight. * * The input for the function is backpackWeight(size of group, weights of * individual backpacks). The size of the group is non-negative. */ //add the function template /** Question 13 * This question assumes you have finished question befriend (number of * freshers). As you are befriending the freshers, the other freshers decided that they * also want to befriend every other fresher. Given the number of freshers, what * is the minimum number of friend requests needed to be sent so that every * fresher befriends every other fresher on discord. Assume that no two freshers * are already friends on discord. Write the function befriend2(number of new * residents). Assume that the number of freshers is nonnegative. * * For example: There are 4 new residents, Sam, Amy, Ida, Alex * * 1. Sam sends a friend request to Amy, Ida and Alex resulting is 3 requests * sent. * * 2. As Amy has already received a friend request from Sam, she will only send * an friend request to Ida and Alex resulting in `3+2=5` requests sent. * * 3. As Ida has already received a friend request from Sam and Amy, she will * only send a friend request to Alex resulting in `5+1=6` requests sent. * * 4. As Alex has already received a friend request from Sam, Amy and Ida, he * will not send any friend requests. The answer is therefore 6 friend requests * will be sent. */ //add the function template //(D/HD) /** Question 14 * * @param int[] data1, int[] data2 * @return a merged array that contains all the elements from data1 and data2 with no repetitions * {1,2,3,4} , {5,6,7,8} --> {1,2,3,4,5,6,7,8} * {1,2,2,3,4}, {5,6} --> {1,2,3,4,5,6} */ //add the function template /** Question 15 * * As the freshers are befriending each other, they decide that they also want * to befriend the returning students. If all of the returning students have * already befriended each other on discord, what is the minimum amount of * friend requests needed to be sent so that every resident will have befriended * every other resident. Assume that none of the new residents have befriended * any returners or freshers before. Write the function befriend3(number of * freshers, number of returners). * */ //add the function template } Comp6010_Assignment02_s12021/src/ComputingTasksTests.java Comp6010_Assignment02_s12021/src/ComputingTasksTests.java import static org.junit.jupiter.api.Assertions.*; import java.util.Arrays; import org.junit.jupiter.api.Test; class ComputingTasksTests { @Test public void testoddNumbers() { int[] a = {1,2,3,4,5}; assertFalse(ComputingTasks.oddNumbers(a)); int[] b = {1,3,5,7,9}; assertTrue(ComputingTasks.oddNumbers(b)); int[] c = {1,3,5,7,9,10,11}; assertFalse(ComputingTasks.oddNumbers(c)); int[] d = {2,4,5,7,9,10,11}; assertFalse(ComputingTasks.oddNumbers(d)); } @Test public void testnumDigits(int n) { assertEquals(2,ComputingTasks.numDigits(10)); assertEquals(1,ComputingTasks.numDigits(0)); assertEquals(4,ComputingTasks.numDigits(8000)); assertEquals(3,ComputingTasks.numDigits(655)); } @Test public void testHighestItemIdx() { int[] a = {1,34,56,200,-1}; assertEquals(3,ComputingTasks.highestItemIdx(a)); int[] b = {800,40,20,700}; assertEquals(0,ComputingTasks.highestItemIdx(b)); int[] c = {-10,-2,-3,-4}; assertEquals(1,ComputingTasks.highestItemIdx(c)); } @Test public void testsumPos() { int[] a = {1,2,100,-1,-8}; assertEquals(103,ComputingTasks.sumPos(a)); int[] b = {800,-40,20,-700}; assertEquals(820,ComputingTasks.sumPos(b)); int[] c = {-10,-2,-3,4}; assertEquals(4,ComputingTasks.sumPos(c)); int[] d = {1,0,-12,123}; assertEquals(124,ComputingTasks.sumPos(d)); int[] e = {0}; assertEquals(124,ComputingTasks.sumPos(e)); } @Test public void testEvenArray() { assertEquals("[2,4,6,8,12]",Arrays.toString(ComputingTasks.evenArray(new int[] {2,4,6,8,11,12}))); assertEquals("[]",Arrays.toString(ComputingTasks.evenArray(new int[] {1,3,5,7,11}))); assertEquals("[12]",Arrays.toString(ComputingTasks.evenArray(new int[] {1,3,5,7,12}))); assertEquals("[]",Arrays.toString(ComputingTasks.evenArray(new int[] {}))); assertEquals(null,ComputingTasks.evenArray(null)); } @Test public void testBefriend() { assertEquals(4, ComputingTasks.befriend(5)); assertEquals(1, ComputingTasks.befriend(2)); assertEquals(99, ComputingTasks.befriend(100)); } @Test public void testhPrime() { assertEquals(11,ComputingTasks.hPrime(11)); assertEquals(97,ComputingTasks.hPrime(100)); } @Test public void testEnoughTables() { assertTrue(ComputingTasks.enoughTables(1)); assertFalse(ComputingTasks.enoughTables(2)); assertTrue(ComputingTasks.enoughTables(3)); assertTrue(ComputingTasks.enoughTables(15)); assertFalse(ComputingTasks.enoughTables(300)); } @Test public void testmaxHeight() { assertEquals(210, ComputingTasks.maxHeight(150, 160, 210)); assertEquals(210, ComputingTasks.maxHeight(150, 210, 160)); assertEquals(210, ComputingTasks.maxHeight(210, 150, 160)); assertEquals(124, ComputingTasks.maxHeight(5, 124, 123)); assertEquals(124, ComputingTasks.maxHeight(5, 124, 124)); assertEquals(10, ComputingTasks.maxHeight(10, 10, 10)); } @Test public void testCollectedStamps() { assertTrue(ComputingTasks.collectedStamps(5, 6, 4)); assertTrue(ComputingTasks.collectedStamps(10, 20, 15)); assertTrue(ComputingTasks.collectedStamps(10, 70, 75)); assertTrue(ComputingTasks.collectedStamps(10, 10, 20)); assertFalse(ComputingTasks.collectedStamps(1, 1, 3)); assertFalse(ComputingTasks.collectedStamps(70, 70, 200)); } @Test public void testupdatePrice() { assertEquals("[1.0, 2.0, 3.0, 4.0]", Arrays.toString(ComputingTasks.updatePrice(true, new double[] { 2, 4, 6, 8 }))); assertEquals("[2.5, 10.0, 35.0, 45.0]", Arrays.toString(ComputingTasks.updatePrice(true, new double[] { 5, 20, 70, 90 }))); assertEquals("[4.0, 8.0, 12.0, 16.0]", Arrays.toString(ComputingTasks.updatePrice(false, new double[] { 2, 4, 6, 8 }))); assertEquals("[10.0, 40.0, 140.0, 180.0]", Arrays.toString(ComputingTasks.updatePrice(false, new double[] { 5, 20, 70, 90 }))); } @Test public void testBackpackWeight() { assertEquals(5, ComputingTasks.backpackWeight(4, new int[] { 2, 4, 6, 8 }), 0.01); assertEquals(25, ComputingTasks.backpackWeight(4, new int[] { 10, 20, 30, 40 }), 0.01); assertEquals(1, ComputingTasks.backpackWeight(2, new int[] { 1, 1 }), 0.01); assertEquals(44.2,ComputingTasks.backpackWeight(5, new int[] { 30, 10, 20, 90, 71 }), 0.01); } @Test public void testBefriend2() { assertEquals(6, ComputingTasks.befriend2(4)); assertEquals(10, ComputingTasks.befriend2(5)); assertEquals(31125, ComputingTasks.befriend2(250)); assertEquals(0, ComputingTasks.befriend2(1)); assertEquals(0, ComputingTasks.befriend2(0)); } @Test public void testmergeArray() { assertEquals("[1,2,3,4,5,6,7]",Arrays.toString(ComputingTasks.mergeArray(new int[] {1,2,3,4},new int[] {5,6,7}))); assertEquals("[2,3,5,6,7]",Arrays.toString(ComputingTasks.mergeArray(new int[] {2,3,4,3},new int[] {5,6,7,5}))); assertEquals(null,Arrays.toString(ComputingTasks.mergeArray(null,null))); assertEquals("[10,6,8]",Arrays.toString(ComputingTasks.mergeArray(new int[] {},new int[] {10,6,8}))); } @Test public void testBefriend3() { assertEquals(1, ComputingTasks.befriend3(1, 1)); assertEquals(3, ComputingTasks.befriend3(2, 1)); assertEquals(11, ComputingTasks.befriend3(2, 5)); assertEquals(10, ComputingTasks.befriend3(5, 0)); assertEquals(0, ComputingTasks.befriend3(0, 30)); assertEquals(93625, ComputingTasks.befriend3(250, 250)); } }