Question

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 hard to follow should be written. You can look at the Java programs in the text book to see how comments are added to programs.
Program Description

Assignment #2 will be the construction of a program that reads in an unspecified number of integers from standard input, performs some calculations on the input numbers, and outputs the results of those calculations to standard output. The numbers could be delimited by any kind of whitespace, i.e. tabs, spaces, and lines (Note that if you use the Scanner class, you do not have to worry about these delimiters. They will be taken care of). 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 integer is 0
The count of odd integers in the sequence is 0
The largest even integer in the sequence is 0
The sum of positive integers is 0

This means that using all numbers your program reads (including the last number 0), you need to compute the minimum, the number of odd integers (i.e. "num%2 == 1"), the largest even integer, and sum of all positive integers (are greater than 0) in the sequence.

Note that the above is an output for the first test case. For other test cases, you will have different numbers.

For this assignment, you're required to write Assignment2.java from scratch, no skeleton of code is provided. Do not prompt to query for the numbers. The number 0 is included in the sequence of integers and should be included in all of your calculations.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

package HomeworkLib;

import java.util.Arrays;
import java.util.Scanner;

public class Q1 {

   public static void main(String[] args) {
       // TODO Auto-generated method stub
       //if array size exceeds by 40 enter the desired amount in place of 40 in the following line
       int[] arr = new int[40];
        Scanner sc = new Scanner(System.in);
        int j = 0;
        int count = 0;
        int x = 1;


        //taking input and stopping when user enters 0
        while (x != 0) {
           x = sc.nextInt();
            arr[j] = x;
            j++;
            count++;
        }
      
      
        //printing the array
        for (int i = 0; i < count; i++) {
            System.out.println("Element at index " + i + " : "+ arr[i]);
        }

        //count of odd numbers
        int odd = 0;
        int even = 0;
        for (int k = 0; k < count; k++) {
           if (arr[k]%2 != 0) {
               odd++;            
           }
           else {
               continue;
           }
        }
      
        //greatest even number
        for (int i = 0; i < count; i++) {
           if (arr[i]%2 == 0) {
               if (even<arr[i]) {
               even = arr[i];
               System.out.println("even"+even);
               }      
           }
              
        }
      
        //sum of positive integers
        int total = 0;
        for (int i = 0; i < count; i++) {
           if (arr[i] > 0) {
               total += arr[i];
               System.out.println("total "+total);
              
           }
        }
      //minimum integer
        Arrays.sort(arr);
        int min = arr[0];
        System.out.println("The minimum integer is " + min);
        System.out.println("The count of odd integer in the sequence is " + odd);
        System.out.println("The largest even integer in the sequence is " + even);
        System.out.println("The sum of positive integers is " + total);
   }

}

Add a comment
Know the answer?
Add Answer to:
What this Assignment Is About: Review on Java I topics, such as primitive data types, basic...
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
  • 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...

  • 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...

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is...

    DESCRIPTION: The range of integers that can be represented in Java using a primitive data type is only from-263 to 263-1. What if we need to manipulate integer values beyond this range? In this assignment you will write a HugeInteger class which is able to represent arbitrar- ily large integer numbers. This class must implement arithmetic operations on integers such as addition, subtraction, multiplication, division and comparison. You have to implement this class without using Java predefined classes, unless specified...

  • Please help with my java program

    I'm in my first java class at school and really need some help. I've looked online for different programs for the same assignment I'm doing now, and none of them seemto work. Here is the assignment:Write a program that reads integers, finds the largest of them, and counts its occurrences. Assume that the input ends with number 0. Suppose that you entered 3 5 2 55 5 0; the program finds that the largest is 5 and the occurrence count...

  • This Java program reads an integer from a keyboard and prints it out with other comments....

    This Java program reads an integer from a keyboard and prints it out with other comments. Modify the comments at the top of the program to reflect your personal information. Submit Assignment1.java for Assignment #1 using Gradescope->Assignemnt1 on canvas.asu.edu site. You will see that the program has a problem with our submission. Your program is tested with 4 testcases (4 sets of input and output files). In order to pass all test cases, your program needs to produce the same...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • In this lab, you complete a partially written Java program that includes two methods that require...

    In this lab, you complete a partially written Java program that includes two methods that require a single parameter. The program continuously prompts the user for an integer until the user enters 0. The program then passes the value to a method that computes the sum of all the whole numbers from 1 up to and including the entered number. Next, the program passes the value to another method that computes the product of all the whole numbers up to...

  • Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design...

    Count Occurrences in Seven Integers Using Java Single Dimension Arrays In this assignment, you will design and code a Java console application that reads in seven integer values and prints out the number of occurrences of each value. The application uses the Java single dimension array construct to implement its functionality. Your program output should look like the sample output provided in the "Count Occurrences in Seven Integers Using Java Single Dimension Arrays Instructions" course file resource. Full instructions for...

  • C++ assignment Write a program that reads a sequence of integers and prints the following: 1....

    C++ assignment Write a program that reads a sequence of integers and prints the following: 1. The number of odd and even numbers of inputs Use a sentinel value to signal the end of inputs. If the sentinel value is the first you enter, give a message “NO input is entered!”. Input Validation: Do not accept a negative number.

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
ADVERTISEMENT