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]
2 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]2 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
A ____ is a device that interconnects two or more workstations in a star-wired bus local area network and immediately retransmit
prisoha [69]

Answer:

A HUB

Explanation:

A hub can described as an hardware or network device use for multi purpose functions like connecting series of internet devices together there by making it possible for all devices to work or function together on a single network.

Hubs are classified into active hub, passive hub, and intelligent hub.

ACTIVE HUB: This type of hub boost the signals but can not perform calculations.

PASSIVE HUB: this type only send the signals but can not boost a signal.

INTELLIGENT HUB: it brings about the boosting of signals and also bring about packets to be analyzed.

7 0
3 years ago
Necesito 7 innovaciones tecnológicas de la informática para 2021
Sonja [21]

‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍  ‍

3 0
2 years ago
Why are there more producers then consumers?​
Ivanshal [37]

Answer:

because producer can make their own food

6 0
3 years ago
Read 2 more answers
How does a cloud-first strategy differ from other approaches to cloud?
Irina18 [472]

Cloud-first strategy differ from other approaches as It keeps all the services performed by legacy systems while moving to the Cloud in a staggered approach.

<h3>What is a cloud first strategy?</h3>

The change of cloud computing has brought about  “cloud-first” strategy.

This  is known to be a way to computing that tells that a firm should look first to cloud solutions when creating new processes or taking in old processes before taking in non-cloud-based solutions.

Note that Cloud-first strategy differ from other approaches as It keeps all the services performed by legacy systems while moving to the Cloud in a staggered approach.

See options below

it enables an organization to completely move to the cloud without infrastructure or support requirement

it keeps all the services performed by legacy systems while moving to the cloud in a staggered approach.

it partners technology with multiple other disciplines for comprehensive business transformation.

it uses artificial intelligence to automate all business processes and completely eliminate human error.

Learn more about cloud from

brainly.com/question/19057393

#SPJ1

6 0
2 years ago
Which is the best response to receiving a threating text from a classmate
Yuri [45]

Answer:

send a threatening text in reply, then block the phone number block the phone number, then contact his phone service provider immediately delete the text, then report the incident to authorities ignore the message, then get a new cell phone number.

Explanation:

5 0
2 years ago
Read 2 more answers
Other questions:
  • in what way do rules and laws created to address public problems affect individuals groups and business
    13·1 answer
  • The _______ has shortcuts to commonly used commands
    8·2 answers
  • What is the boundary folding method?
    5·1 answer
  • Electricity is moved from place to place a long __________​
    14·2 answers
  • An open intersection is one that
    9·2 answers
  • Trong ửod muốn trình bày dạng cột càn thực hiện lệnh nào
    13·1 answer
  • If you give an actual answer I'll give brainliest​
    13·2 answers
  • What is this error SyntaxError: Unexpected token '??='
    12·1 answer
  • Authenticating information can be something a user knows, like a password; something a user has, like a smart card; or something
    5·1 answer
  • Question 2 of 25
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!