Open ErSKS opened 5 years ago
package beanarray;
/**
@College: Khwopa College of Engineering */ public class BeanArray {
public static void main(String[] args) { System.out.println("Bean Array-- if it contains 9 then it should contain 13 and if it contains 7 then it doesn not contain 16"); System.out.println("{9,1,7,13} 1:" + isBean(new int[]{9, 1, 7, 13, 16})); System.out.println("{9,1,7} 0:" + isBean(new int[]{9, 1, 7})); System.out.println("{9,1,7,13,7} 1:" + isBean(new int[]{9, 1, 7, 13, 7})); System.out.println("{} 1:" + isBean(new int[]{})); }
private static int isBean(int[] i) { int nine = 0, thirteen = 0, seven = 0, sixteen = 0, result = 0; for (int j = 0; j < i.length; j++) { if (i[j] == 9) { nine = 1; } else if (i[j] == 13) { thirteen = 1; } else if (i[j] == 7) { seven = 1; } else if (i[j] == 16) { thirteen = 1; } } if ((nine == 1 && thirteen == 1) || (nine == 0 && thirteen == 0)) { result = 1; } if (seven == 1 && sixteen == 1) { result = 0; } return result; } }
CoreTask#1: BeanArray - An array is defined to be a Bean array if it meets the following conditions a. If it contains a 9 then it also contains a 13. b. If it contains a 7 then it does not contain a 16. So {1, 2, 3, 9, 6, 13} and {3, 4, 6, 7, 13, 15}, {1, 2, 3, 4, 10, 11, 12} and {3, 6, 9, 5, 7, 13, 6, 17} are Bean arrays. The following arrays are not Bean arrays: a. { 9, 6, 18} (contains a 9 but no 13) b. {4, 7, 16} (contains both a 7 and a 16) Write a function named isBean that returns 1 if its array argument is a Bean array, otherwise it returns 0. Function signature is int isBean (int[ ] a)