Question

Arizona State University - CSE205 Assignment #9 Due Date Friday, March April 3rd, 5:30pm Important: This...

Arizona State University - CSE205

Assignment #9

Due Date Friday, March April 3rd, 5:30pm

Important: This is an individual assignment. Please do not collaborate.

No late assignment will be accepted.

Make sure that you write every line of your code. Using code written by someone else will be considered a violation of the academic integrity and will result in a report to the Dean's office.

It must be submitted on-line (Course website).

Go to "GradeScope" tab on Canvas -> CSE205 -> Assignment9, and upload your program file.

Minimal Submitted Files

You are required, but not limited, to turn in the following source file:


Assignment9.java file (you need to create this file from scratch)

Requirements to get full credits in Documentation

1.       The assignment number, your name, StudentID, Lecture day/time, and a class description need to be included at the top of each file/class.

2.      A description of each method is also needed.

3.       Some additional comments inside of methods (especially for a "main" method) to explain code that are hard to follow should be written.

You are not allowed to use the Scanner class in this assignment and any assignment after this one. You will need to use InputStreamReader and BufferedReader (they are in java.io package) to process input and also take care of IOException.

Click on the following link to view sample code of using InputStreamReader and BuferedReader classes.

InputStreamReaderDemo.java

New Skills to be Applied

In addition to what has been covered in previous assignments, the use of the following items, discussed in class, will probably be needed:

Recursion
One-dimensional arrays

Program Description

Assignment #9 will be the construction of a program that reads in a sequence of integers from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers.

Then compute the minimum number, count odd integers, compute the sum of numbers that are larger than the first number in the array, and compute the largest even integer in the sequence using recursion. Thus, you will create recursive methods findMin, countOddNumbers, computeLargestEven, and sumOfNumbersLargerThanFirst in Assignment9 class and they will be called by a main method.

Specifically, the following recursive methods must be implemented (These methods should not contain any loop):

    public static int findMin(int[] numbers, int startIndex, int endIndex)

    public static int countOddNumbers(int[] elements, int startIndex, int endIndex)

    public static int computeLargestEven(int[] elements, int startIndex, int endIndex)

    public static int sumOfNumbersLargerThanFirst(int[] elements, int startIndex, int endIndex, int firstNumber)

If these methods are implemented using a Loop or any Static Variable, points will be deducted (from the test cases) even if your program passes test cases. DO NOT use any Static Variables.

The program should output the results of those calculations to standard output. Your program will continue to read in numbers until the number 0 is entered. At this point, the calculations will be outputted in the following format:

The minimum number is 0

The count of odd integers in the sequence is 0

The largest even integer in the sequence is 0

The sum of numbers larger than the first is 0

Note that the result values will be different depending on test cases (not always 0).

Do not output a prompt to query for the numbers. The number 0 is included in the sequence of numbers and should be included in all of your calculations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Assignment9
{
public static void main(String[] args) throws IOException
{
//Enter data using BufferReader
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int arr[]=new int[100];
int i=0,num;
while(true)
{
System.out.print("Enter an integer or 0 to stop: ");
num=Integer.parseInt(reader.readLine());
if(num==0)
break;
arr[i]=num;
i++;
}
  
System.out.println("\nThe minimum number is "+findMin(arr, 0, i--));
System.out.println("The count of odd integers in the sequence is "+countOddNumbers(arr, 0, i--));
System.out.println("The largest even integer in the sequence is "+computeLargestEven(arr,0,i--));
System.out.println("The sum of numbers larger than the first is "+sumOfNumbersLargerThanFirst(arr,0,i--,arr[0]));
}
  
public static int findMin(int[] numbers, int startIndex, int endIndex)
{
if(startIndex == endIndex) return numbers[0];
int min = findMin(numbers, startIndex+1, endIndex);
return (numbers[startIndex] <= min) ? numbers[startIndex] : min;
}
public static int countOddNumbers(int[] elements, int startIndex, int endIndex)
{
if (startIndex==endIndex)
{
if(elements[startIndex]%2!=0) {
return 1;
} else {
return 0;
}
}
else {
if(elements[startIndex]%2!=0) {
return countOddNumbers(elements,startIndex+1, endIndex)+1;
}
else{
return countOddNumbers(elements,startIndex+1, endIndex);
}
}
}
public static int computeLargestEven(int[] elements, int startIndex, int endIndex)
{
if(startIndex == endIndex) return elements[0];
int min = computeLargestEven(elements, startIndex+1, endIndex);
  
if(elements[startIndex]%2==0)
{
return (elements[startIndex] >= min) ? elements[startIndex] : min;
}
return computeLargestEven(elements, startIndex+1, endIndex);
}
public static int sumOfNumbersLargerThanFirst(int[] elements, int startIndex, int endIndex, int firstNumber)
{   
if (endIndex == startIndex)
{
if (elements[startIndex] > firstNumber)
{
int sum = elements[startIndex];
return sum;
}
else
{
int sum = 0;
return sum;
}
   }
else
{
int sum = sumOfNumbersLargerThanFirst(elements, startIndex+1, endIndex,firstNumber);      
if (elements[startIndex] >firstNumber)
{
return sum += elements[startIndex];
}
else
{
return sum;
}
}
}

}

output

0 Recursive Array methods - NetBeans IDE 8.0.1 File Edit View Navigate Source Refactor Run Debug Profile Team Tools Window He

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
Arizona State University - CSE205 Assignment #9 Due Date Friday, March April 3rd, 5:30pm Important: This...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Assignment #9 will be the construction of a program that reads in a sequence of integers...

    Assignment #9 will be the construction of a program that reads in a sequence of integers from standard input until 0 is read, and store them in an array (including 0). This is done using iteration (choose one of for, while, or do while loop). You may assume that there will not be more than 100 numbers. Then compute the minimum number, compute the largest number among the numbers that are divisible by 2, count even numbers, and compute the...

  • What this Assignment Is About: Review on Java I topics, such as primitive data types, basic...

    What this Assignment Is About: Review on Java I topics, such as primitive data types, basic I/O, conditional and logical expressions, etc. Review on Java loops. Documentation Requirements to get full credits in Documentation The assignment number, your name, StudentID, Lecture number(time), and a class description need to be included at the top of each file/class. A description of each method is also needed. Some additional comments inside of methods (especially for a "main" method) to explain code that are...

  • A test harness program for testing sorting methods is provided with the rest of the textbook...

    A test harness program for testing sorting methods is provided with the rest of the textbook program files. It is the file Sorts.java in the ch11 package. The program includes a swap method that is used by all the sorting methods to swap array elements. Describe an approach to modifying the program so that after calling a sorting method the program prints out the number of swaps needed by the sorting method. Implement your approach. Test your new program by...

  • Lab #7 CSE110 - Arizona State University Topics • Basic arrays Coding Guidelines • Give identifiers...

    Lab #7 CSE110 - Arizona State University Topics • Basic arrays Coding Guidelines • Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc). • Keep identifiers to a reasonably short length. • Use upper case for constants. Use title case (first letter is upper case) for classes. Use lower case with uppercase word separators for all other identifiers (variables, methods, objects). • Use tabs or spaces to indent code within blocks (code surrounded by braces)....

  • Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What...

    Please complete the following programming with clear explanations. Thanks! Homework 1 – Programming with Java: What This Assignment Is About? Classes (methods and attributes) • Objects Arrays of Primitive Values Arrays of Objects Recursion for and if Statements Selection Sort    Use the following Guidelines: Give identifiers semantic meaning and make them easy to read (examples numStudents, grossPay, etc.) Use upper case for constants. • Use title case (first letter is upper case) for classes. Use lower case with uppercase...

  • Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations...

    Programming Assignment #7 (Recursion) This assignment is to write some methods that perform simple array operations recursively. Specifically, you will write the bodies for the recursive methods of the ArrayRecursion class, available on the class web page. No credit will be given if any changes are made to ArrayRecursion.java, other than completing the method bodies Note that the public methods of ArrayRecursion – contains(), getIndexOfSmallest(), and sort() – cannot be recursive because they have no parameters. Each of these methods...

  • The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13,...

    The Fibonacci sequence is the sequence of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, … The next number is found by adding up the two numbers before it. For example, the 2 is found by adding the two numbers before it (1+1). The 3 is found by adding the two numbers before it (1+2). The 5 is found by adding the two numbers before it (2+3), and so on! Each number in the sequence is called...

  • Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Wr...

    Trying to practice this assignment Argument list: the *yahoonews.txt Data file: yahoonews.txt Write a program named WordCount.java, in this program, implement two static methods as specified below: public static int countWord(Sting word, String str) this method counts the number of occurrence of the word in the String (str) public static int countWord(String word, File file) This method counts the number of occurrence of the word in the file. Ignore case in the word. Possible punctuation and symbals in the file...

  • Write a program that instantiates an array of integers named scores. Let the size of the...

    Write a program that instantiates an array of integers named scores. Let the size of the array be 10. The program then first invokes randomFill to fill the scores array with random numbers in the range of 0 -100. Once the array is filled with data, methods below are called such that the output resembles the expected output given. The program keeps prompting the user to see if they want to evaluate a new set of random data. Find below...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT