Question

In: Computer Science

Write a test program that prompts the user to enter a sequence of numbers ending with...

  • Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes this method to return the largest number in the input.
  • Use inheritance and polymorphism approach.
  • in java please

Solutions

Expert Solution

This is the simplest way to write this program.

import java.util.Scanner;
import java.util.ArrayList;

public class first{

   public static void main(String[] args) {
       //to read input
       Scanner input = new Scanner(System.in);

       // Create an ArrayList
       ArrayList<Integer> list = new ArrayList<Integer>();

  
       System.out.print("Enter a sequence ending wih 0: ");
       Integer number = input.nextInt();
       while (number.intValue() != 0) {
           list.add(number);
           number = input.nextInt();
       }

       // Display the largest number in the input
       System.out.println("The largest number in the input is " + max(list));
   }

   //Return the maximum value in an ArrayList of integers
   public static Integer max(ArrayList<Integer> list) {
       if (list.size() == 0)
           return null;

       Integer max = list.get(0);
       for (int i = 0; i < list.size(); i++) {
           if (list.get(i) > max)
               max = list.get(i);
       }
       return max;
   }
}


Related Solutions

write a c++ program that prompts a user to enter 10 numbers. this program should read...
write a c++ program that prompts a user to enter 10 numbers. this program should read the numbers into an array and find the smallest number in the list, the largest numbers in the list the sum of the two numbers and the average of the 10 numbers PS use file I/o and input error checking methods
JAVA Write a test program that prompts the user to enter a series of integers and...
JAVA Write a test program that prompts the user to enter a series of integers and displays whether the series contains runLength consecutive same-valued elements. Your program’s main() must prompt the user to enter the input size - i.e., the number of values in the series, the number of consecutive same-valued elements to match, and the sequence of integer values to check. The return value indicates whether at least one run of runLength elements exists in values. Implement the test...
You will write a program that prompts the user to enter a 7-digit phone numbers, and...
You will write a program that prompts the user to enter a 7-digit phone numbers, and finds the 3- and 4-letter words that map to the phone number, according to the restrictions outlined earlier. A sample run: unixlab% java MapNumbers Enter name of dictionary file: words10683 Enter a test word (3 letters): cat Test word maps to 228 Enter telephone number (7 digits, no 0's or 1's, negative to quit): 2282273 Options for first 3 digits: act cat bat Options...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program...
USE PYTHON. Write a program that prompts the user to enter 5 test scores. The program should display a letter grade for each score and the average test score. Hint: Declare local variables under main() program Prompts the user to enter 5 test scores Define a function to calculate the average score: this should accept 5 test scores as argument and return the avg Define a function to determine the letter grade: this should accept a test score as argument...
JAVA Write a test program that prompts the user to enter a list and displays whether...
JAVA Write a test program that prompts the user to enter a list and displays whether the list is sorted or not. Here is a sample run. Note that the program first prompts the user to enter the size of the list. Using Array My output should look like this Enter the size of the list: 8 Enter the contents of the list: 10 1 5 16 61 9 11 1 The list has 8 integers 10 1 5 16...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute...
JAVA 5- Write a program that prompts the user to enter two (2) numbers and compute the sum of the numbers between these two numbers (including the numbers entered). If the user Enters 2 and 6. The program will calculate the sum of the numbers: 2+3+4+5+6 and will display the result. Enter First number> 2 Enter second number> 6 The sum is 20
Write a C program that prompts the user to enter three sets of five double numbers...
Write a C program that prompts the user to enter three sets of five double numbers each. (You may assume the user responds correctly and doesn’t enter non-numeric data.) The program should accomplish all of the following: a. Store the information in a 3×5 array. b. Compute the average of each set of five values. c. Compute the average of all the values. d. Determine the largest value of the 15 values. e. Report the results. Each major task should...
Write a program that prompts the user to enter three sets of five double numbers each....
Write a program that prompts the user to enter three sets of five double numbers each. (You may assume the user responds correctly and doesn’t enter non-numeric data.) The program should accomplish all of the following: a. Store the information in a 3×5 array. b. Compute the average of each set of five values. c. Compute the average of all the values. d. Determine the largest value of the 15 values. e. Report the results. Each major task should be...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT