Question

In: Computer Science

TwoNumbersAddTo 1. write a class called TwoNumbersAddTo with a main method 2. the program asks for...

TwoNumbersAddTo 1. write a class called TwoNumbersAddTo with a main method 2. the program asks for a number (lets call it "goal"). 3. then the program reads at most 20 integers (i.e. any number of integers from 0 to 20) 4. the program stops reading when user enters 0 or user has entered 20 numbers. 5. program prints all the pairs that add up to goal. 6. Notice that if goal is 5, for example, 1 + 4 and 4 + 1 are the same pair. Your program should report only one. 7. The pairs should be printed such that the first number is in the same order in which it was entered. For example: % java TwoNumbersAddTo enter goal: 5 enter at most 20 integers, end with 0: 1 2 3 4 5 0 1 + 4 = 5 2 + 3 = 5 % java TwoNumbersAddTo enter goal: 5 enter at most 20 integers, end with 0: 4 3 2 1 0 4 + 1 = 5 3 + 2 = 5

Solutions

Expert Solution

import java.util.Scanner;

public class TwoNumbersAddTo {
   public static void main(String[] args) {
       int arr[] = new int[20];
       int goal;
       int len = 0;
       Scanner sc = new Scanner(System.in);
       //reading goal from user
       System.out.println("Enter goal: ");
       goal = sc.nextInt();
       int n = 0;
       //reading numbers from user
       System.out.println("enter at most 20 integers, end with 0:");
       for (int i = 0; i < 20; i++) {
           n = sc.nextInt();
           arr[len++] = n;
           if (n == 0) {
               break;
           }
       }
       //finding some of the numbers
       for (int i = 0; i < len; i++) {
           for (int j = i + 1; j < len; j++) {
               //check if sum is =goal
               if (arr[i] + arr[j] == goal) {
                   System.out.println(arr[i] + " + " + arr[j] + " = " + (arr[i] + arr[j]));
               }
           }
       }
   }
}

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me


Related Solutions

Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for...
Write a class called WhereIsMyNumber. WhereIsMyNumber must have a main method. Your program must ask for a number (it must work if user enters numbers with decimal point of not). Then your program must print number is negative -- if humber is negative number in [0,10) -- if it is between 0 and 10, not including the 10 number in [10,100) -- if it is between 10 and 100, not including the 100 number in [100,1000) -- if it is...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for...
Write a class called CheckUserName. CheckUserName must have a main method. Your program must ask for a user name. If the name is on the list below (Liam, for example) greet the user saying: welcome back: Liam If the user is an admin (like Benkamin, for example) print: welcome back: Benjamin you have admin privileges Your program must accept upper case or lower case: emacs% java CheckUserName enter a user name: Liam welcome back: Liam emacs% java CheckUserName enter a...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
1. create a class called ArrayStack that is a generic class. Create a main program to...
1. create a class called ArrayStack that is a generic class. Create a main program to read in one input file and print out the file in reverse order by pushing each item on the stack and popping each item off to print it. The two input files are: tinyTale.txt and numbers.txt. Rules: You cannot inherit the StackofStrings class. 2. Using your new ArrayStack, create a new class called RArrayStack. To do this, you need a) remove the capacity parameter...
In Java Create a class called "TestZoo" that holds your main method. Write the code in...
In Java Create a class called "TestZoo" that holds your main method. Write the code in main to create a number of instances of the objects. Create a number of animals and assign a cage and a diet to each. Use a string to specify the diet. Create a zoo object and populate it with your animals. Declare the Animal object in zoo as Animal[] animal = new Animal[3] and add the animals into this array. Note that this zoo...
Define Loan Class – Add to your project. And write program in main method to test...
Define Loan Class – Add to your project. And write program in main method to test it. Note: Assume year is number of years, rate is the annual interest rate and P is principle is loan amount, then the total payment is Total payment = P *(1+ rate/12)^ year*12; Monthly Payment = TotalPayment/(year*12); java
In a file called LengthSum.java, write a program that: - Asks the user to enter a...
In a file called LengthSum.java, write a program that: - Asks the user to enter a string. - Asks the user to enter a second string. - Prints out the length of the first string, the length of the second string, and the sum of the two lengths, using EXACTLY the same format as shown below. For example: if the user enters strings "UT" and "Arlington", your program output should look EXACTLY like this: Please enter a string: UT Please...
Write a program that contains a main method and another method named userName. The main method...
Write a program that contains a main method and another method named userName. The main method should prompt the user to enter a full name. The userName method takes the full name as an argument and prints the name in reverse order and returns the number of characters in the name. See Sample Output (input shown in blue). Sample Output Please enter your FULL name Billy Joe McCallister Here is the name Billy Joe McCallister in reverse: retsillaCcM eoJ ylliB...
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT