Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class NumberGroup {
- private int[] nums;
- public NumberGroup(int[] nums){
- this.nums=nums;
- }
- public int[] getNums() {
- return nums;
- }
- public void setNums(int[] nums) {
- this.nums = nums;
- }
- /**
- * print all elements in nums array in the following format:
- * [element1, element2, element3, element4, . . . last element]
- * */
- public void printNums(){
- //write a loop that will access each item in the list
- //each iteration of the loop should print one item in the list
- System.out.print("["+nums[0]);
- for (int i=1; i<nums.length;i++){
- System.out.print(", "+nums[i]);
- }
- System.out.println("]");
- }
- /**
- * find and return the index of the target number in nums array
- * If target is not found, return -1
- * @param -1 < target < nums.length
- * @return index of the target element or -1 if target not found in nums
- * */
- public int findTargetNumber(int target){
- int count=0;
- int found=-1;
- while(found<0&&count<nums.length){
- if(nums[count]==target){
- found=count;
- }
- }
- return found;
- }
- /**
- * @return the count of even numbers stored in nums
- * */
- public int countEven(){
- //make a counter
- int count=0;
- //traversal loop from 0 to length-1
- for(int i=0; i<nums.length; i++){
- if(nums[i]%2==0){
- count++;
- }
- }
- return count;
- }
- @Override
- public String toString() {
- return "NumberGroup{" +
- "nums=" + Arrays.toString(nums) +
- '}';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement