Question
I know how to do number one just not number 2. Please answer number 2 only
Write a java program that will print out following pattern. Use nested for loop. 2. Add a method to the above program that generates takes two numbers as parameters and prints out all the numbers from number 1 to number 2 separated by using a while loop. (Do not print the last ,). For example: Calling method1(9,51) should print: 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 Calling method1(5,1) should print: 5, 4, 3, 2,1
0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class HourGlass {
  public static void method1(int num1, int num2){
    if(num1>num2) {
      for(int i = num1;i>num2;i--){
        System.out.print(i+",");
      }
      System.out.println(num2);
    }
    else {
      for (int i = num1; i < num2; i++) {
        System.out.print(i + ",");
      }
      System.out.println(num2);
    }
  }
  public static void main(String[] args) {
    int size = 3;
    int n = 2 * size - 1;
    for (int x = 0; x < size; x++) {
      for (int y = 0; y < x; y++) {
        System.out.print(" ");
      }
      for (int i = 0; i < (n - x - x); i++) {
        System.out.print("*");
      }
      System.out.println();

    }

    int t = size - 2;
    while (t >= 0) {
      for (int x = 0; x < t; x++) {
        System.out.print(" ");
      }
      for (int y = 0; y < (n - t - t); y++) {
        System.out.print("*");
      }
      System.out.println();
      t -= 1;
    }

    System.out.println("\n");
    method1(9,51);
    method1(5,1);

  }
}

9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29, 30,31,32,33,34,35,36,37,38,39,40,41, 42,43,44,45,46,47,48,49,50,51 5,4,3,2,1 Process finished with exit code 0

\color{blue}Please\; up\;vote\;the \;solution \;if \;it \;helped.\;Thanks!

Add a comment
Know the answer?
Add Answer to:
I know how to do number one just not number 2. Please answer number 2 only...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a python nested for loop that prints out the following pattern 100 99 98 97...

    Write a python nested for loop that prints out the following pattern 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85 84 83 82 81 80 79 78 77 76 75 74 73 72 71 70 69 68 67 66 65 64 63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 47 46 45 44 43 42 41 40 39 38 37 36 35 34 33...

  • For this assignment, you will apply what you learned in analyzing for, while, and do-while loops...

    For this assignment, you will apply what you learned in analyzing for, while, and do-while loops by writing these statements yourself. The Java™ program you write should do the following: Display a pyramid of asterisks onscreen (i.e., a nested for loop) Display the integers 10 to 1 in decreasing order, one number per line (i.e., a while/do-whlie loop) Add 7 until the sum becomes greater than 157, at which point the program should display both the sum and the number...

  • PLEASE DO BOTH OF THESE IN PYTHON!!! AND MAKE SURE THAT I CAN COPY THE CODE....

    PLEASE DO BOTH OF THESE IN PYTHON!!! AND MAKE SURE THAT I CAN COPY THE CODE. 1.) Exercise #1: Design and implement a program (name it PrintSum) that prompts the user for an integer values between 1 and 100. The program then uses a while loop to determine and print out the sum of all values between 1 and the entered value. The program rejects invalid input values with proper message such “Invalid Input. Try again.” Document your code and...

  • 1) Translate the following equation into a Python assignment statement 2) Write Python code that prints...

    1) Translate the following equation into a Python assignment statement 2) Write Python code that prints PLUS, MINUS, O ZERO, depending on the value stored in a variable named N. 3) What is printed by: 3 - 1 while 5: while 10 Print ) Page 1 of 9 4) Write a Python while loop that reads in integers until the user enters a negative number, then prints the sum of the numbers. 1-2 of 9 4) Write a Python while...

  • JAVA only Please do and post Code with Screenshot. Write a program that reads a paragraph...

    JAVA only Please do and post Code with Screenshot. Write a program that reads a paragraph from user and extract and print out any telephone number in the text. The telephone numbers can have the following formats. For this assignment you need to create objects from class Pattern and Matcher. (xxx) xxx-xxxx xxx-xxx-xxxx

  • Follow exactly all the instructions provided below: Do not deviate from the instructions provided below or...

    Follow exactly all the instructions provided below: Do not deviate from the instructions provided below or add any unnecessary code to program. Pay close attention to the directions! Make absolutely certain that you are doing exactly what the assignment asks you to do without any more code required. Keep the program's code as simple as possible and begineer friendly level with no advanced level Java concepts/skills. Write a program that asks the user for a number. The program should check...

  • For JAVA Please post code in bold. Thank you. Print numbers 0, 1, 2, ..., userNum...

    For JAVA Please post code in bold. Thank you. Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints: 0 1 2 3 Challenge 4.6.1: Nested loops ndent...

  • Python problem: How do you include the number that you are trying to process if it's...

    Python problem: How do you include the number that you are trying to process if it's the sentinel?  This is my solution won't include -999 in the process, but I am not sure how do this... # write a program that prints out the average of # the negative numbers in the list that appear before a -999 is detected. def main(): MyList = [ 23, -45, 6, -23, -9, 77, 54, -54, 21, -2, 8, -3, -23, 45, 93, -43,...

  • Hello Guys. I need help with this its in java In this project you will implement...

    Hello Guys. I need help with this its in java In this project you will implement a Java program that will print several shapes and patterns according to uses input. This program will allow the use to select the type (say, rectangle, triangle, or diamond), the size and the fill character for a shape. All operations will be performed based on the user input which will respond to a dynamic menu that will be presented. Specifically, the menu will guide...

  • I want to know how to do this assignment!!! Help me please~ The first part of...

    I want to know how to do this assignment!!! Help me please~ The first part of your program should do the following: • Ask the user type of test they took. o (ACT or SAT) • If they said ACT then ask them their score o If their ACT score was between 0 and 7 say "Needs Work" o If their ACT score was between 10 and 20 say "Acceptable" o If they ACT score was above 20 say "Above...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT