Question

In: Computer Science

Java Write a program that reads 4 integer numbers from the user combine it into a...

Java

Write a program that reads 4 integer numbers from the user combine it into a single number. You need to ask the user to first specify how to combine the numbers (forward or backward). Note that for the backward option you need to check that the last digits should be 0, also for the forward option you need to check that the fist digit is not 0. Use for loops and switch for the menu.                       4. Write an application that prompts the user to enter an integer limit and then print the numbers from 1 to this limit in the following format. The number read should not be greater than 10 and should not be negative.        Enter a number: 3 Enter a number: 6 Enter a number: 2 Enter a number: 0 1.Forward 2.Backward Choose an option: 1 The number is 3620 Enter a number: 3 Enter a number: 6 Enter a number: 2 Enter a number: 0 3.Forward 4.Backward Choose an option: 2 Error!

Solutions

Expert Solution

Here is the completed code for the two programs you asked. Named it Program1.java and Program2.java. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// Program1.java

import java.util.Scanner;

public class Program1 {

      public static void main(String[] args) {

            // scanner to read user input

            Scanner scanner = new Scanner(System.in);

            // cretaing an array of 4 numbers, reading 4 numbers from user

            int numbers[] = new int[4];

            for (int i = 0; i < numbers.length; i++) {

                  System.out.print("Enter a number: ");

                  numbers[i] = scanner.nextInt();

            }

            // displaying choice (to select if user likes to make numbers by forward

            // or backward arrangement)

            System.out.println("1. Forward");

            System.out.println("2. Backward");

            System.out.print("Choose an option: ");

            // getting choice, switching it

            int option = scanner.nextInt();

            switch (option) {

            case 1:

                  if (numbers[0] == 0) {

                        // first digit is 0

                        System.out.println("Error!");

                  } else {

                        // creating the number by arranging numbers forward, and

                        // displaying it

                        int number = numbers[0] * 1000 + numbers[1] * 100 + numbers[2]

                                    * 10 + numbers[3];

                        System.out.println("The number is " + number);

                  }

                  break;

            case 2:

                  if (numbers[3] == 0) {

                        //last digit cant be 0

                        System.out.println("Error!");

                  } else {

                        // creating the number by arranging numbers backward, and

                        // displaying it

                        int number = numbers[3] * 1000 + numbers[2] * 100 + numbers[1]

                                    * 10 + numbers[0];

                        System.out.println("The number is " + number);

                  }

                  break;

            default:

                  System.out.println("Invalid choice!");

            }

      }

}

// Program2.java

import java.util.Scanner;

public class Program2 {

      public static void main(String[] args) {

            // scanner to read user input

            Scanner scanner = new Scanner(System.in);

            // initializing a number to -1

            int n = -1;

            // looping as long as number is not within 1-10

            while (n <= 0 || n > 10) {

                  System.out.print("Enter the upper limit (1-10): ");

                  // reading n

                  n = scanner.nextInt();

                  // validating

                  if (n <= 0 || n > 10) {

                        // invalid

                        System.out.println("Invalid, try again!");

                  }

            }

            // looping from 1 to n, printing each number on separate lines

            for (int i = 1; i <= n; i++) {

                  System.out.println(i);

            }

      }

}


Related Solutions

Using Java Prime numbers. Write a program that prompts the user for an integer and then...
Using Java Prime numbers. Write a program that prompts the user for an integer and then prints out all prime numbers up to that integer. For example, when the user enters 20, the program should print 2 3 5 7 11 13 17 19 Recall that a number is a prime number if it is not divisible by any number except 1 and itself. Use a class PrimeGenerator with methods nextPrime and isPrime. Supply a class PrimePrinter whose main method...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values,...
Write a Java program (name it LargestOccurenceCount) that reads from the user positive non-zero integer values, finds the largest value, and counts it occurrences. Assume that the input ends with number 0 (as sentinel value to stop the sentinel loop). The program should ignore any negative input and should continue to run. Hint: to remember/save entered (good) values, you can concatenate them into a string (separated by spaces) that you can output later on. Sample runs showing input prompts and...
in.java Write a program that reads an integer from the user and prints a rectangle of...
in.java Write a program that reads an integer from the user and prints a rectangle of starts of width 5 3 and height N. Sample run 1: Enter N: 5 *** *** *** *** *** Bye Sample run 2: Enter N: 8 *** *** *** *** *** *** *** *** Bye Sample run 3: Enter N: 2 *** *** Bye Sample run 4: Enter N: -2 Bye
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input. Do the following: Output the sum of all even numbers Output the sum of all odd numbers Output the count of all even numbers Output the count of all odd numbers You must use loops and numbers to do this. You must not use any arrays or vectors for this program.
Write a C++ program that reads numbers from the user until the user enters a Sentinel....
Write a C++ program that reads numbers from the user until the user enters a Sentinel. Use a Sentinel of -999. Ignore all negative numbers from the user input (other than the sentinel). Do the following: 1. Output the sum of all even numbers 2. Output the sum of all odd numbers 3. Output the count of all even numbers 4. Output the count of all odd numbers You must use alternation ('if' statements), loops and simple calculations to do...
C++ program that reads a positive integer number from a user and displays all binary numbers...
C++ program that reads a positive integer number from a user and displays all binary numbers from 0 to the number. For the program, you can assume that the input number is a number between 0 and 100. Sample Run 1: Assume that the user typed the following number. 5 This is the correct output of your program. 000 001 010 011 100 101 Sample Run 2: Assume that the user typed the following number. 0 This is the correct...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and...
1. Write a Java program that prompts the user to enter three integer numbers. Calculate and print the average of the numbers. 2. Write a Java program that uses a for loop to print the odd numbers from 1 to 20. Print one number per line in the command line window. 3. Write a program which asks the user to input the size of potatoe fries she would like to purchase, and based on the size, it will tell her...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers...
Write a Java program (name it InputSum) that prompts the user to enter positive integer numbers using a sentinel while loop. The program should accept integer inputs until the user enters the value -1 (negative one is the sentinel value that stops the while loop). After the user enters -1, the program should display the entered numbers followed by their sum as shown below. Notice that -1 is not part of the output. The program should ignore any other negative...
Write a C++ or Java program that reads an input graph data from a user. Then,...
Write a C++ or Java program that reads an input graph data from a user. Then, it should present a path for the travelling salesman problem (TSP). In the assignment, you can assume that the maximum number ofvertices in the input graph is less than or equal to 20. Input format: This is a sample input from a user. 4 12 0 1 2 0 3 7 0 2 5 1 0 2 1 2 8 1 3 3 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT