Question

Write a program FindDuplicate.java that reads n integer arguments from the command line into an integer...

Write a program FindDuplicate.java that reads n integer arguments from the command line into an integer array of length n, where each value is between is 1 and n, and displays true if there are any duplicate values, false otherwise. CODE IN JAVA and NO IMPORT STATEMENTS CAN BE USED

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//FindDuplicate.java
public class FindDuplicate {
    public static void main(String[] args) {
        if(args.length>0){
            int sum = 0, n = args.length;

            for(int i = 0;i<n;i++){
                sum += Integer.parseInt(args[i]);
            }

            int actualSum = (n*(n+1))/2;
            if(actualSum==sum){
                System.out.println(false);
            }
            else{
                System.out.println(true);
            }
        }
        else{
            System.out.println("Error: PLease pass command line arguments");
        }
    }
}
Add a comment
Know the answer?
Add Answer to:
Write a program FindDuplicate.java that reads n integer arguments from the command line into an integer...
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
  • 1) Write a C program that displays all the command line arguments that appear on the...

    1) Write a C program that displays all the command line arguments that appear on the command line when the program is invoked. Use the file name cl.c for your c program. Test your program with cl hello goodbye and cl 1 2 3 4 5 6 7 8 and cl 2) Write a C program which displays the sum of the command line arguments. Hint: use sscanf to convert the decimal arguments (which are strings) to binary numbers that...

  • Write a full program that will accept any number of command line arguments and print them...

    Write a full program that will accept any number of command line arguments and print them to the screen. Suppose your program is called Print.java. When the command java Print hello goodbye you is run, the output will be hello goodbye you Write a full program that will accept exactly  three command line arguments that can be interpreted as integers, and will add them up. If there aren't exactly 3 command line arguments, print an error message and terminate the program....

  • Your task is to write a C++ program that consumes integer values as command line arguments...

    Your task is to write a C++ program that consumes integer values as command line arguments and returns the arithmetic mean of these values. To increase the flexibility of the program, there should be no set number of arguments. To overcome this, we will require the argument argv[1] to be the number of integers the user enters. For example, if the user wants to calculate the arithmetic mean of 4 numbers, they would pass in 4 as the first argument...

  • Write a program Minesweeper.java that takes 3 command-line arguments M, N, and p and produces an...

    Write a program Minesweeper.java that takes 3 command-line arguments M, N, and p and produces an M-by-N boolean array where each entry is occupied with probability p. In the minesweeper game, occupied cells represent bombs and empty cells represent safe cells. Print out the array using an asterisk for bombs and a period for safe cells. Then, replace each safe square with the number of neighboring bombs (above, below, left, right, or diagonal) and print out the solution. Try to...

  • After reading pages 330 - 336, write a program that takes two command line arguments which...

    After reading pages 330 - 336, write a program that takes two command line arguments which are file names. The program should read the first file line by line and write each line, in reverse order, into the second file. The program should include a "usage" method that displays the correct command line syntax if two file names are not provided. Example: if my input file says Hello, World! then my output file will contain !dlroW ,olleH Hints: Use CaesarCipher...

  • Write a Java program that reads words from a text file and displays all the non-duplicate...

    Write a Java program that reads words from a text file and displays all the non-duplicate words in ascending order. The text file is passed as a command-line argument. Command line argument: test2001.txt Correct output: Words in ascending order... 1mango Salami apple banana boat zebra

  • In C, Write a program that can accept an arbitrary number of command line arguments, e.g....

    In C, Write a program that can accept an arbitrary number of command line arguments, e.g. program must do the following: program needs to split each of the arguments into two halves based on the length of a string. Specifically, the first half will be [0, n/2) and the second half will be [n/2] where n is the length of the string and n > 1. For instances, hello is split into two halves: he and llo. Similarly, world! is...

  • Task 1 : Write a Java program that prompts for and reads 10 integers from the...

    Task 1 : Write a Java program that prompts for and reads 10 integers from the user into an integer array of size 10, it then: - calculates the sum and average of positive numbers of the array and displays them. [Note: consider 0 as positive] Note: The program must display the message: "There are no positive values" if the array does not contain any positive value.

  • PYTHON (Triangle Inequality) Write a program triangle.py that takes three integers as command-line arguments and writes...

    PYTHON (Triangle Inequality) Write a program triangle.py that takes three integers as command-line arguments and writes True if each one of them is less than or equal to the sum of the other two and False otherwise. Note: this computation tests whether the three numbers could be the lengths of the sides of some triangle.

  • Write a program that first reads an integer for the array size, then reads double values...

    Write a program that first reads an integer for the array size, then reads double values into the array, and displays the number of values greater than the average. Enter array size: 6 Enter a number: 1.1 Enter a number: 2.2 Enter a number: 3.4 Enter a number: 3.9 Enter a number: 5.5 Enter a number: 8.75 The number of values greater than the average is 2

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