Solved

array example containing 9s

Posted on 2014-10-29
14
207 Views
Last Modified: 2014-11-03
Hi ,

I am trying below array example containing 9s
http://codingbat.com/prob/p184031


I wrote as below but could not proceed further. Please advise on how to proceed and complete this.

public class Test22 {

      /**
       * @param args
       */
      public static void main(String[] args) {
            // TODO Auto-generated method stub

      }
      public int arrayCount9(int[] nums) {
              int[] arrayCount9= {1, 2, 9};
             
              System.out.println("9s are"+arrayCount9.
             
             
            }
}
0
Comment
Question by:gudii9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
  • +2
14 Comments
 
LVL 17

Assisted Solution

by:krakatoa
krakatoa earned 167 total points
ID: 40411881
public int arrayCount9(int[] nums) {
  int numnines=0;
  for(int n=0;n<nums.length;n++){if(nums[n]==9){numnines++;}}
  return numnines;
}

Open in new window


or this . . .

public int arrayCount9(int[] nums) {
  int numnines=0;
  int ninetally=0;
  for(int n=0;n<nums.length;n++){if (!(nums[n]==9)){nums[n]=0;}}

  for(int n =0;n<nums.length;n++){ninetally=ninetally+nums[n];}
  
  return ninetally/9;
  
}

Open in new window


although this would be preferable . . .

public int arrayCount9(int[] nums) {

  int ninetally=0;
  for(int n=0;n<nums.length;n++){ninetally= nums[n]==9?ninetally+9:ninetally ;}
  return ninetally/9;
  
}

Open in new window

0
 
LVL 7

Author Comment

by:gudii9
ID: 40413206
public class Test22 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		arrayCount9;//how to call array from main method??

	}
	public int arrayCount9(int[] nums) {
		  int[] arrayCount9= {1, 2, 9};
		int numNine=0;
		  for(int i=0;i<=arrayCount9.length;i++){
			  if(arrayCount9[i]==9){
				  
				  numNine=numNine+1;
			  }
		  System.out.println("9s are"+numNine);
		  }
		  
		  return numNine;
		}
}

Open in new window


I wonder how to call this arrayCount9 method from main method.

your first solution is easy to understand to me. One challenge i face while working on different projects is i need to understand different styles of code written by different developers at different past years. That lead me to learn java completely(including different styles of writing code ) even though i like particular simple way of writing code irrespective of my preference. Please advise
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40413235
how to call this arrayCount9 method from main method

without sounding indelicate, I thought that it was already crystal clear how you call a method from within a class or instance?? We have covered the point countless times in other questions.

You build your class, and, if it is for example for test purposes - which it mainly is at this stage for you - then you may as well make methods static, so that you can call them from the static main() method, and save yourself the trouble of having to instantiate an instance of the class, just to try things out.

So what you have above is effectively, that. Now all you need to do is to add the static access qualifier to the method signature, and you can call it from main(). When you call it, you pass the same arguments of course - which in this case is the int array.

As to the other part of your comment above, about styles, I would say that that is a weak approach. You should rather concentrate on understanding the mechanics of the language, and not mistaking how different mechanics are used by different programmers, and conclude that one is a better style than the other. At the moment, I'd say you need to show yourself that you completely understand the for next loop, the while loop, the for-each loop and the ternary operator, and the conditions under which these various elements operate - i.e. begin, end, and produce a result.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 17

Expert Comment

by:krakatoa
ID: 40413644
Now to talk about your code . . .
public class Test22 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

               static int[] nums = {1,2,9};
		// TODO Auto-generated method stub
		//arrayCount9;//how to call array from main method??//like this : arrayCount9(nums);

	}
	public static int arrayCount9(int[] nums) {//declare as static so your main() can call it.
		 // int[] arrayCount9= {1, 2, 9};//[b]Why[/b] are you doing this?? You've already been passed the array - "nums".
		int numNine=0;
		  //for(int i=0;i<=arrayCount9.length;i++){// NO . . do:i<nums.length
			  if(arrayCount9[i]==9){//No . . . use: nums[i]==9
				  
				  numNine=numNine+1;
			  }
		 // System.out.println("9s are"+numNine);// don't need this
		  }
		  
		  return numNine;
		}
}

Open in new window


So now make the corrections marked, and try to run again.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40413665
public class Test22 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		arrayCount9(null);//how to call array from main method??

	}
	public static int arrayCount9(int[] nums) {
		  int[] arrayCount9= {1, 2, 9};
		int numNine=0;
		  for(int i=0;i<=arrayCount9.length;i++){
			  if(arrayCount9[i]==9){
				  
				  numNine=numNine+1;
			  }
		  System.out.println("9s are"+numNine);
		  }
		  
		  return numNine;
		}
}

Open in new window


i know calling a method uisng the object instance without static before method or keeping the static before method and call without object instance.

Where i am struck now is i need to pass {1, 2, 9} from the main method not declaring in arrayCount9.


Please advise
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40413678
So look at lines 8 and 10 of my last comment.
0
 
LVL 32

Accepted Solution

by:
awking00 earned 167 total points
ID: 40414136
You've already seen how a for-each loop works, so let's use it.

public static void main(String[] args) {
            int[] nums= {1, 9, 9, 3, 9};
            System.out.println(arrayCount9(nums));

}
      public static int arrayCount9(int[] nums) {
         int count = 0;
         for (int num : nums) {
            if (num == 9) {
              count++;
            }
        }
            return count;
      }
0
 
LVL 17

Expert Comment

by:krakatoa
ID: 40414652
I'm counting myself out of this one, as there doesn't seem to me to be anything more to add to any of it that hasn't been said countless times already, and it's starting to fold back in on itself, in the way that has happened several times before.
0
 
LVL 14

Expert Comment

by:Geisrud
ID: 40415627
Is there a problem with simply iterating through a given array and incrementing an integer if a value equals 9?

edit: reading back through other comments, I guess that is what krakatoa did in the very first comment.  Seems the OP is making this harder than it is.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40417488
package eePackage;

public class Test21 {

      public static void main(String[] args) {
        int[] nums= {1, 9, 9, 3, 9};
        System.out.println(arrayCount9(nums));

}
  public static int arrayCount9(int[] nums) {
     int count = 0;
     for (int num : nums) {
        if (num == 9) {
          count++;
        }
    }
        return count;
  }
}

in the above solution i see for each loop. Can i simply use traditional for loop instead like below
for(initialization; condition increment)

Please advise
0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 166 total points
ID: 40417513
Yes, you can.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40419357
>>Can i simply use traditional for loop instead like below<<
I don't see any loop "like below" but, if you mean can I replace "for (int num : nums)" and "if (num == 9)" with
"for (int i = 0; i < nums.length; i++)" and "if( nums[ i] == 9)" then, as ozo stated, of course you can.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40419697
package eePackage;

public class Test23 {

      public static void main(String[] args) {
        int[] nums= {1, 9, 9, 3, 9,8,9};
        System.out.println(arrayCount9(nums));

}
  public static int arrayCount9(int[] nums) {
     int count = 0;
    // for (int num : nums) {
    	 //"for (int i = 0; i < nums.length; i++)" and "if( nums[ i] == 9)" 
    	 // if (num == 9) {
     for (int i = 0; i < nums.length; i++){  
         if (nums[i] == 9) {
          count++;
        }
    }
        return count;
  }
}

Open in new window


I see it is working as above. I personally feel comfortable, easy to understand as above with for loop and if condition rather than foreach loop as commented above.
0
 
LVL 32

Expert Comment

by:awking00
ID: 40419712
>> I personally feel comfortable, easy to understand as above with for loop and if condition rather than foreach loop as commented above. << Not surprising since a loop using a counter, condition and increments is usually the one you learn first and if it works don't fix it.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show, step by step, how to integrate R code into a R Sweave document
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

691 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question