Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write your code for Java 7. IT MUST RUN IN ORDER FOR YOUR SUBMISSION TO GET RATE

ID: 3549800 • Letter: W

Question

Write your code for Java 7. IT MUST RUN IN ORDER FOR YOUR SUBMISSION TO GET RATED! I will take it and put it in NetBeans and if there are compile errors I won't give you the points until it's fixed.


http://www.mathjax.org/help/configuration

For each of the following three problems, write a Java program in a file named "Problemx.java", where "x" is the problem number (1,2, or 3 for this week). Place the file in the subdirectory "Week2" of your csll4 directory on afs. You can use the Scanner class to read input from the keyboard. Follow the input/output and naming specifications precisely. In particular, for your final submission your program should not print prompts for input or add text such as "The answer is:". Write a recursive method that takes a string of digits as argument and returns the corresponding integer value. Use it to write a program that reads in a string of digits from the keyboard and prints out the numerical value (the input and output should read the same, but you need to convert it in between). Write a recursive method that takes an integer array as argument and returns an integer array that contains the same numbers as the input array, but re-ordered so that all even integers come before any odd integers. Use your method in a program that reads in a line of integers from the keyboard and prints out the same integers, even numbers first. For example, if the input is 7 22 13 4 18 21 6 8 then a correct output is 22 4 18 6 8 7 13 21 Another correct output is 4 18 22 8 6 13 7 21 For the previous problem, what are the minimum and maximum number of recursive calls your method mate for an input of n numbers? Design your method to minimize these numbers. Place your answer in comments in Problem2.java, or in a separate Problems file (text, pdf, word,...).

Explanation / Answer

Problem1.java:


import java.util.Scanner;


public class Problem1 {

public static int number=0;


public static int stringToNumber(String num, int index, int place){

int digit = Integer.parseInt(Character.toString(num.charAt(index)));

number += digit *place;

if (index == 0) {

return number;

}

return stringToNumber(num, --index, place * 10); //recursive call

}


public static void main(String[] args) {

  Scanner inp = new Scanner(System.in);

System.out.print("Enter a String of number digits: ");

String num = inp.nextLine();

System.out.println(stringToNumber(num, num.length()-1, 1));

}

}


Problem2.java:


import java.util.Scanner;


public class Problem2{

public static int oddReplaceIndex=-1, temp;


public static void arrange(int arr[], int pos, int size)

{

if(pos==size)

return;


if(arr[pos]%2==0){

temp=arr[oddReplaceIndex+1]; //swapping the odd and even nos

arr[oddReplaceIndex+1]=arr[pos];

arr[pos]=temp;

oddReplaceIndex++; //the next position where swapping will be done

}


arrange(arr, ++pos, size); //recursive call

}

public static void main(String[] args) {

Scanner inp = new Scanner(System.in);

int size;

System.out.println("Enter the size of the arry: ");

size=inp.nextInt();

int[] arr=new int[size];

System.out.println("Enter the elements of the arry: ");

for(int i=0;i<size;i++)

  arr[i]=inp.nextInt();

arrange(arr,1,size);

System.out.println("Now, the required array is ");

for(int i=0;i<size; i++)

System.out.print(arr[i]+" ");

}

}



Problem 3:

size-1 recursive calls will happen in every case as once the array has to be traversed, even the last element can be an odd number. So, O(n) is the time complexity