Solved

array example containing 9s

Posted on 2014-10-29
186 Views
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
Question by:gudii9
• 5
• 4
• 3
• +2

LVL 16

Assisted Solution

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;
}
``````

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;

}
``````

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;

}
``````
0

LVL 7

Author Comment

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;
}
}
``````

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 16

Expert Comment

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

LVL 16

Expert Comment

ID: 40413644
``````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;
}
}
``````

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

LVL 7

Author Comment

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;
}
}
``````

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.

0

LVL 16

Expert Comment

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

LVL 31

Accepted Solution

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 16

Expert Comment

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

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

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)

0

LVL 84

Assisted Solution

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

LVL 31

Expert Comment

ID: 40419357
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

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;
}
}
``````

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 31

Expert Comment

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

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to thâ€¦
Whether youâ€™re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor anâ€¦