Monthly Archives: October 2023

How to Access 1 digit at at time of an int in Java

How to access each digit in an int in Java

 

Array Fun 3 [Resizing] Version 2

Version 1

ArrayFun3 - Please call your class that , exactly. Yes, you will lose a point for not following this simple specification.

  • use meaningful variable names
  • use  camelCase for variable and method names
  • use  i  as the variable name for for-i loops . Remember – programming is generally done in teams so your code should be  as expressive as possible. Use conventions to convey meaning, not cause confusion.
  • do not use i as the variable name in for-each  loops. Again, this would be misleading and imply you’re using a different type of loop.
  • do not import any external libraries . All solutions can be done by making use of variables and arrays and some basic math ; nothing more is needed.

 

int[]  removeZeroes(int [] nums)

Description: This method returns a version of nums  in which all occurrences of 0 , zero, are  removed

Method Call return value/output
noZeroes(  { 3 , 4, 0, 1} ) { 3, 4, 1}
noZeroes(  { 0, 5 , 0, 0, 9, 0, 1 , 11} ) { 5,9,1,11}

String[]  removeNulls(String[] strs)

Description: This method returns a version of  strsin which all null values are removed. Note: removeNullsv2

 


int[] digitsToArray(int n)

@precondition n > 9

Description: This method takes each digit in the integer n  and stores it in an array in reverse order, as shown below. First, make sure you understand the code here. , which btw, testers had to figure out , on the fly, in a previous AP A test.

digitstoarray


int[] removeLeadingZeroes(int[] nums)

@precondition nums.length >=2 

Description: This method returns an array in which all elements that meet the 2 criteria below are removed.

  • the element has a value of 0, zero
  • the element’s index is less than the index of the first element whose value is not zero.

 

remove_leading_zeros


 int[]  doubleUp( int  [] nums, int val)

@precondition nums.length >=2 

Description: This method returns a version of nums –with all occurrences of val   duplicated.

Method Call return value/output
doubleUp(  { 3 , 4, 0, 1}  , 4  ) { 3, 4, 0, 1}
doubleUp(  { 1, 3 , 1, 5 , 3, 3} ,  3 )    {1 , 3,  , 1, 5 , 3, 3, 3, }

 

 

 

Similar problems on codingbat

Pre4