1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
irina1246 [14]
3 years ago
11

Write a Java application with a main() to find the two largest distinct integers entered on the command line by the user. We are

just creating a simple stand-alone application. All your code can be in main().

Computers and Technology
1 answer:
Setler [38]3 years ago
7 0

Answer:

import java.util.Scanner; // Scanner class is to use input output functions

public class TwoDistinctLargest //class to find two distinct largest nos

{

public static void main(String[] args) { //start of main() function

Scanner input = new Scanner(System.in); // creates Scanner type object

       int integers = 0, largest1 = 0, largest2 = 0; // declare variables

/* integers holds the input integer values, largest1 has 1st largest number and largest 2 contains second distinct largest number, i is used as the count variable to move through the input integers */

       int i = 0;  

//prompts user to enter integers and enter 0 to stop

       System.out.println("Enter the integers (press 0 to stop): ");

       integers = input.nextInt(); //reads and scans input integers

       while(integers != 0) {

/* the loop keeps taking integer values from user until the value of integers is equal to 0, this means until the user enters 0 */

//checks if the value of integer is greater than first largest computed number

           if(integers > largest1) {

//if the value in largest1 is greater than that of largest2

               if(largest1 > largest2) {

                   largest2 = largest1; } // then value of largest1 is set to largest2                

               largest1 = integers; // largest1 now holds the value of integers //which means largest1 holds the first distinct largest integer

           } else { //checks if the value of integers is greater than largest2

               if(integers > largest2) {

//if the value of integers is not equal to that of largest1

                   if(integers!=largest1)  {

// set the value of second distinct largest number to largest2

                   largest2 = integers;}              }            }

                     i++; //keeps moving to the next input integer

           integers = input.nextInt();     //keeps taking input integers    }      

//prints the two distinct largest integers

       System.out.println("\n The 1st largest distinct integer is " + largest1);

       System.out.println("\n The 2nd largest distinct integer is " + largest2); } }

Explanation:

Lets suppose the user inputs the following integers

1, 6, 5, 6, 0

Initial values are:

integers = 0

largest1 = 0

largest2 = 0

i = 0

When the user enters 1, then value of integers = 1

while loop checks if integers!=0. As integers =1 so the program enters the body of while loop. The first IF condition is checked if(integers > largest1)

As largest1 = 0 and integers =1 so this condition evaluates to true as 1 > 0

So the statements in the body of this IF condition are executed. This if conditions contains another if statement if(largest1 > largest2) which checks if the value largest1 is greater than that of largest2. Its false because largest1 =0 and largest2 =0 so largest1 = largest2, so the statement of this if condition body will not execute and program moves to this                 largest1 = integers; statement which sets the value of integers i.e. 1 to largest1. So the value of largest1 = 1

Now the i is incremented by 1 and it points at second value of integers= 6.

while loop checks if integers!=0. As integers =6 so the program enters the body of while loop. The first IF condition is checked if(integers > largest1)

As largest1 = 1 and integers =6 so this condition evaluates to true as 6>1

So the statements in the body of this IF condition are executed. This if conditions contains another if statement if(largest1 > largest2) which checks if the value largest1 is greater than that of largest2.

Its true because largest1 =1 and largest2 =0 so largest1 > largest2, so the next statement is executed largest2 = largest1; So now the value of largest2=1.

Now program moves to this largest1 = integers; statement which sets the value of integers i.e. 6 to largest1. So the value of largest1 = 6

Now the i is incremented by 1 and it points at second value of integers= 5.

while loop condition is again true. IF condition is checked if(integers > largest1)

As largest1 = 6 and integers =5 so this condition evaluates to false as 5<6. So this IF part will not execute and program control moves to the else part.

Else part has an if condition if(integers > largest2) which evaluates to true because integers = 5 and largest2 = 1 so the program moves to next if statement inside the previous if statement of else part i.e. if(integers!=largest1). This if statement is the main statement which will help in finding two distinct largest numbers in case the same largest value is input more than once. It checks If the value of integers is not equal to largest1 value. As integers = 5 and  largest1 = 6 so this if condition is true. If this condition is not used in this program then largest2 will not be assigned distinct largest values but assigned 6 for both largest1 and largest2 as 6 is input twice. Next this if statement  largest2 = integers assigns value of integers to largest2 which is the second largest distinct integer.

Now the i is incremented by 1 integers value become 0.

while loop checks if integers!=0. As integers =0 so the while loop breaks. The last two print statements which produce following output.

The 1st largest distinct integer is: 6

The 2nd largest distinct integer is: 5

You might be interested in
What is the proper order for the fetch-execute cycle?
vesna_86 [32]

Control Unit – controls all parts of the computer system. It manages the four basic operations of the Fetch Execute Cycle as follows:

Fetch – gets the next program command from the computer’s memory

Decode – deciphers what the program is telling the computer to do

Execute – carries out the requested action

Store – saves the results to a Register or Memory

Arithmetic Logic Unit (ALU) – performs arithmetic and logical operations

Register – saves the most frequently used instructions and data

5 0
3 years ago
Ginny downloads and modifies an open source software program, then uploads the program with a different name. Which type of soft
abruzzese [7]

Answer:

The correct answer to the following question will be "GNU General Public License".

Explanation:

GNU General Public License:

  • This is a commonly used free and license open source that ensures the right to operate, test, distribute and change the program for end-users.
  • To prevent the GNU program from becoming proprietary, Richard Stallman developed the GPL. It is a basic use of his idea of the "copyleft."

If an individual alters and downloads a free software program, the program will then be uploaded with another name, then they would have to use this type of software license.

3 0
3 years ago
Give two examples of html presentation
svp [43]

Answer:

Size, colors, borders, margins, etc. can be taken as the example of HTML presentation.

5 0
3 years ago
A computer is completely compromised when it wont turn on or operate. What is the best way to fix
miskamm [114]

There really is no getting it back, if it is from a virus, then you need to buy a new computer. If it is something internal, depending on what type of computer you have it might just be best to buy a new one. Hope this helps :-)

8 0
3 years ago
Read 2 more answers
You receive a suspicious text message from an unknown sender asking you to click on a link to access your current credit score.
Lyrx [107]

Answer: Phishing

Explanation: A cybercrime in which, a target or targets are contacted by email, telephone or text message by someone acting or posing as a legitimate institution to lure these targets or a target into providing sensitive data such as personally identifiable information, banking and credit card details, and passwords is referred to as Phishing.

The CyberCriminals use these information to access important accounts, steal identities and even money.

Beside email and website phishing, there are phishing methods like 'vishing' (voice phishing), 'smishing' (SMS Phishing).

SOME FEATURES OF PHISHING EMAILS

The Emails are usually Too Good To Be True with a sense of Urgency, telling you to click on the link fast because the offers are limited.

They may tell you that your account will be suspended unless you update your personal details immediately and with attachments you are supposed to click on and from an unusual sender.

Whether it looks like it's from someone you know or don't know if anything seems out of the ordinary, unexpected, out of character or just suspicious please don't click on it the link. It might just be phishing.

7 0
3 years ago
Other questions:
  • Survey Q. Non-scoring: What role is played in the team? (1 correct answer)
    14·1 answer
  • What binary number is the output of adding 1001011 and 100000?
    13·1 answer
  • Which of these converts analog audio signals into digital codes in a computer?
    5·2 answers
  • Assume a PHP document named hello.php has been saved in a folder named carla inside the htdocs folder on your computer. Which is
    11·1 answer
  • Betty removed a web page from her website. Some users were browsing on her website. One of them clicked on a particular link and
    15·1 answer
  • Ryan needs to see the space available to insert content on a slide in his presentation. Which feature of a presentation program
    10·1 answer
  • A trust domain is defined as Select one: a. The agreed upon, trusted third party b. A scenario where one user needs to validate
    5·2 answers
  • Does using interior lighting help improve a drivers visibility at night
    11·1 answer
  • In a food chain are living creatures that eat organisms from a diferente population?
    13·2 answers
  • In C++ please (read the image below for instructions)
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!