 Java Programming: Strings, Odd & Even Numbers, Absolute Values

Subject Computer Science Java Programming

Question

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 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");
}
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Java Programming Assignment \$30.00
Java Programming
Variables
ToString Method
Equals Method
Computer Science
Java Problems With Stack \$40.00
Java
Programming
Coding
Computer Science
Stack
Generic Types
Push
Pop
Peek
Size
Functions
Strings
Integers
Summation
Negative Values
Input
Output
Student Average Test Scores & Grades Using Java NetBeans \$20.00
Programming
Java
Computer Science
Average Test Scores
Students
NetBeans
Coding
Data Set
Methods
Statements
Variables
Parking Problem in Java \$30.00
Java
Programming
Codes
Classes
Computer Science
Parking
OOP
Car Model
Owner Name
Strings
Integers
Statements
Input
Output
Constructor
Public Methods
Functions
From BS Tree to AVL Tree in Java \$20.00
Java
Programming
Codes
Algorithms
Binary Search Tree
AVL Tree
Search Operation
Logarithmic Time
Rebalancing
Loops
Statements
Variables
Successors
Ancestors
Parents
Nodes
Edges
Boolean Values
Data Structures
Live Chats