QuestionQuestion

Question 1. Write a program to read in two strings and print out the difference (if there is any)
between two string, and which string should come first in lexicography order.
Example:
Enter string one: Small
Enter string two: Big
“Small” and “Big” are different by 17 units. “Big” should come
before “Small” in lexicography order.

Question 2. Write a program to prompt the user for an integer 0≤N≤1000 and list all the odd number from 1 to N. Use a while() loop to check for an appropriate input. List the numbers in multiple lines where each line contain only 10 odd numbers, each number is printed using printf() function with 4 units in width.
Example:
Enter the number N = -5
Error: 0 <= N <= 1000. Reenter.
Enter the number N = 1001
Error: 0 <= N <= 1000. Reenter.
Enter the number N = 30
1 3 5 7 9 11 13 15 17 19
21 23 25 27 29

Question 3. Write a program to prompt the user for an integer -100≤N≤100 and print out the sum of all even numbers from 0 to N inclusively (if N is positive) or from N to 0 (if N is negative). Use a while() loop to check for an appropriate input. Use printf() function to print out the result with 16 units in width and with a comma “,” to separate between thousands. If the sum is negative, print out the result within “()”.
Example:
Enter the number N = -106
Error: -100 <= N <= 100. Reenter.
Enter the number N = 101
Error: -100 <= N <= 100. Reenter.
Enter the number N = 10
Sum of even number from 0 to 10 is: 30
Enter the number N = 50
Sum of even number from 0 to 10 is: 650

Question 4. Write a program to prompt the user for a positive integer N≤10. Read exactly N numbers (can be fractional) from the users and return the number with maximum absolute value among N numbers you just read. Use a while() loop to check for an appropriate input.
Example:
Enter the number N = -10
Error: 0 < N <= 10. Reenter.
Enter the number N = 15
Error: 0 < N <= 10. Reenter.
Enter the number N = 5
Enter 5 numbers each separated by a space: 10.56 3.79 2.11 4.90 -
10000
The number with maximum absolute value is: -10000.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

1.

import java.util.Scanner;


public class StringCompare {

public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
String one, two;

System.out.print("Enter string one: ");
one = input.next();
System.out.print("Enter string two: ");
two = input.next();

int diff = one.compareTo(two);

System.out.println("\"" + one + "\" and \"" + two + "\" are different by " + (int)Math.abs(diff) + " units. ");
if(diff < 0)
System.out.println("\"" + one + "\" should come before \"" + two + "\" in lexicography order");
else if(diff > 0)
System.out.println("\"" + two + "\" should come before \"" + one + "\" in lexicography order");
else
System.out.println("\"" + two + "\" is equal to \"" + one + "\" in lexicography order");
}
}...
$25.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats