Programming problems 1. Write a Java program that will ask the user to enter thr
ID: 3802475 • Letter: P
Question
Programming problems 1. Write a Java program that will ask the user to enter three integer numbers and find the greatest or the smallest of these numbers. The program will then ask the user to enter the choice: 1 (for greatest) and 2 (for smallest). Based on the entered choice, the program displays the greatest or the smallest of the three numbers.
Sample output is as follows:
First number: 24
Second number: 10
Third number: 55
Enter 1 (for greatest) and 2 (for smallest): 1
The greatest of these three numbers is 55
2. Write a Java program that will sort first, middle and last names of the user. The program will ask user to enter the first, middle and the last names. It will also ask sort order e.g. you will enter ‘1’ for ascending alphabetical order and ‘2’ for descending alphabetical order. The program should display the three names arranged in the specified sort order and also print “- OK” (if the total length of the names is less than 20 characters, including the space character) or “- Too long” (otherwise) in front of the name.
For example, if the user enters the names as:
First name: Pradeep
Middle name: Kumar
Last name: Atrey Sort order: 1
The program should display the following:
Atrey Kumar Pradeep - OK
Explanation / Answer
Programming problems 1. Write a Java program that will ask the user to enter three integer numbers and find the greatest or the smallest of these numbers. The program will then ask the user to enter the choice: 1 (for greatest) and 2 (for smallest). Based on the entered choice, the program displays the greatest or the smallest of the three numbers.
Sample output is as follows:
First number: 24
Second number: 10
Third number: 55
Enter 1 (for greatest) and 2 (for smallest): 1
The greatest of these three numbers is 55
import java.io.*;
public class Program {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("First Number:");
int x = Integer.parseInt(br.readLine());
System.out.println("Second Number:");
int y = Integer.parseInt(br.readLine());
System.out.println("Third Number:");
int z = Integer.parseInt(br.readLine());
System.out.println("Enter 1 (for greatest) and 2 (for smallest):");
int choice = Integer.parseInt(br.readLine());
if(choice == 1){
if ( x > y && x > z )
System.out.println("The greatest of these three numbers is "+x);
else if ( y > x && y > z )
System.out.println("The greatest of these three numbers is "+y);
else
System.out.println("The greatest of these three numbers is "+z);
}
else{
if ( x < y && x < z )
System.out.println("The smallest of these three numbers is "+x);
else if ( y < x && y < z )
System.out.println("The smallest of these three numbers is "+y);
else
System.out.println("The smallest of these three numbers is "+z);
}
}
}
2. Write a Java program that will sort first, middle and last names of the user. The program will ask user to enter the first, middle and the last names. It will also ask sort order e.g. you will enter ‘1’ for ascending alphabetical order and ‘2’ for descending alphabetical order. The program should display the three names arranged in the specified sort order and also print “- OK” (if the total length of the names is less than 20 characters, including the space character) or “- Too long” (otherwise) in front of the name.
For example, if the user enters the names as:
First name: Pradeep
Middle name: Kumar
Last name: Atrey Sort order: 1
The program should display the following:
Atrey Kumar Pradeep - OK
import java.io.*;
public class StringComparision {
public static void main(String[] args)throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("First Name:");
String firstName = br.readLine();
System.out.println("Middle Name:");
String middleName = br.readLine();
System.out.println("Last Name:");
String lastName = br.readLine();
String temp;
System.out.println("Sort Order:");
int choice = Integer.parseInt(br.readLine());
String fullName[] = {firstName,middleName, lastName} ;
if((firstName+" "+middleName+" "+lastName).length() < 20){
switch (choice){
case 1:{
for ( int i = 0; i < fullName.length - 1; i++ )
{
for ( int j = i + 1; j < fullName.length; j++ )
{
if ( fullName [ i ].compareToIgnoreCase( fullName [ j ] ) > 0 )
{
temp = fullName [ i ];
fullName [ i ] = fullName [ j ];
fullName [ j ] = temp;
}
}
}
break;
}
case 2:{
for ( int i = 0; i < fullName.length - 1; i++ )
{
for ( int j = i + 1; j < fullName.length; j++ )
{
if ( fullName [ i ].compareToIgnoreCase( fullName [ j ] ) < 0 )
{
temp = fullName [ i ];
fullName [ i ] = fullName [ j ];
fullName [ j ] = temp;
}
}
}
break;
}
default:
System.out.println("Enter a valid choice.");
}
System.out.println(fullName[0]+" "+fullName[1]+" "+fullName[2]+"-OK");
}
else
System.out.println(fullName[0]+" "+fullName[1]+" "+fullName[2] + "-Too Long" );
}
}
HOPE THIS HELPS
FEEL FREE TO COMMENT
THANKS FOR USING CHEGG
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.