0% found this document useful (0 votes)
8 views2 pages

Java Program to Find Greatest Number

The document contains a Java program that prompts the user to input three numbers and determines the greatest among them. The code utilizes the Scanner class for input and employs a conditional operator to find the maximum value. Two students, Arshan and Bhavesh Chauhan, have submitted identical code for the same task.

Uploaded by

donotknow369
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Java Program to Find Greatest Number

The document contains a Java program that prompts the user to input three numbers and determines the greatest among them. The code utilizes the Scanner class for input and employs a conditional operator to find the maximum value. Two students, Arshan and Bhavesh Chauhan, have submitted identical code for the same task.

Uploaded by

donotknow369
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Lab

Arshan
2300290110042 CSIT -
4A

1. WAP to insert 3 numbers from the keyboard and find a greater number among 3
numbers
Code:

import [Link];

public class GreatestOfThree {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");

int a = [Link]();

[Link]("Enter second number: ");

int b = [Link]();

[Link]("Enter third number: ");

int c = [Link]();

int greatest = (a > b && a > c) ? a : (b > c ? b : c);

[Link]("Greatest number is: " + greatest);

Output:
Enter first number: 12
Java Lab
Bhavesh Chauhan
2300290110064
CSIT - 4A

1. WAP to insert 3 numbers from the keyboard and find a greater number among 3
numbers
Code:

import [Link];

public class GreatestOfThree {

public static void main(String[] args) {

Scanner sc = new Scanner([Link]);

[Link]("Enter first number: ");

int a = [Link]();

[Link]("Enter second number: ");

int b = [Link]();

[Link]("Enter third number: ");

int c = [Link]();

int greatest = (a > b && a > c) ? a : (b > c ? b : c);

[Link]("Greatest number is: " + greatest);

Output:
Enter first number: 12

Common questions

Powered by AI

In Java, if integer overflow occurs, the number will wrap around to the negative range, resulting in incorrect output. This program is unlikely to encounter overflow given typical input scenarios. However, to theoretically handle potential overflow, the program could be refactored to use 'long' data types for input and comparisons, especially if anticipating numbers larger than 'Integer.MAX_VALUE'. This allows it to handle larger numbers without overflow.

To extend the program to handle an arbitrary number of inputs, the logic can be modified to use an array or a list to store user inputs. The program would iterate through the entire collection to compare and determine the greatest value. By leveraging Java's collection frameworks or simple iterative structures, it can efficiently scale its capability to find the maximum value for a larger set of inputs beyond the fixed three currently handled.

The Java program uses conditional operators to compare the three numbers. It utilizes a ternary operator to first check if the first number is greater than both the second and third numbers. If true, it assigns the first number to the variable 'greatest'. If false, it further checks if the second number is greater than the third number, assigning the second number if true, or the third number otherwise. Thus, the greatest number among the three inputs is determined and printed.

To enhance usability, the program could provide the user with clear prompts and validation messages. It could start by instructing the user on the allowed range and type of inputs before asking for input. Additionally, including input validation feedback, such as error messages when non-numeric values are entered, could help resolve user input errors effectively.

When collecting user inputs in Java programs, especially if these involve sensitive data, developers must ensure compliance with legal standards such as data protection laws (e.g., GDPR for EU or CCPA for California). Ethical considerations include ensuring privacy by not collecting more data than necessary, protecting data with appropriate security measures, and properly informing users about how their data will be used and stored. This transparency is essential to maintain user trust and comply with legal obligations.

The provided Java code does not handle the case where two or all input numbers are equal effectively. In such scenarios, it arbitrarily selects one of the equal numbers as the greatest. It does not notify the user that there are ties in value, which could be important depending on the use case.

The program could be modified to handle non-integer inputs by using 'double' or 'float' data types instead of 'int'. This adjustment involves changing the data type of variables 'a', 'b', and 'c' to 'double' and replacing 'sc.nextInt()' with 'sc.nextDouble()'. Additionally, to provide robustness against invalid inputs like non-numeric values, exception handling could be incorporated using try-catch blocks.

The ternary operator in Java offers a more concise and readable approach compared to traditional if-else statements by allowing a decision to be made in a single line. In this program, the ternary operation condenses the conditional logic into a straightforward statement that determines the greatest number. This reduces the amount of code and enhances readability by minimizing the cognitive load needed to follow the control flow, thus making it easier for developers to understand and maintain the code.

The 'Scanner' class in Java is used for parsing primitive types and strings using regular expressions. In the program, it is utilized to capture user input from the standard input stream (usually the keyboard). The 'Scanner' object created reads user-entered values and assigns them to variables in the program, allowing dynamic input during program execution.

The Java program can be adapted to handle inputs from other sources by modifying the 'Scanner' class's input stream. Instead of using 'System.in', which reads from the keyboard, the program can use 'Scanner' with file input streams, network streams, or strings by creating instances with 'File', 'InputStream', or 'String' arguments, respectively. This flexibility allows the program to read inputs from various alternative data sources.

You might also like