Question

Hello, I need help writing the two methods to print a triangle shape in java. An...

Hello, I need help writing the two methods to print a triangle shape in java. An example of the shape is as follows:

9 8 7 6 5 4 3 
  8 7 6 5 4 3 
    7 6 5 4 3 
      6 5 4 3 
        5 4 3 
          4 3 
            3 
  One method should be defined as "public static void printPattern(int num1, int num2, Boolean ascending)". This method will print a upper-triangle matrix-like layout filled will a sequence of integers between num1 and num2 inclusive. Each subsequent row will contain the postfix of the previous row. For example for num1=3 and num2=9, the pattern should look like the follows. If the ascending is true, the numbers should printed in the ascending order.Another method should be defined as "public static void printPattern(Scanner scnr)". This method will prompt the user for entering thenum1, num2, and ascending, and call the other method to print the pattern. Again, this method must validate the user's input. Notice that this method have the same name as the previous method but has a different set of parameters.

Thank you! I am having so much difficulty!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

class Triangle{ //driver class named Triangle
   //printPattern function takes three arguments
   public static void printPattern(int num1,int num2,Boolean ascending){
       int spaces = 0; //spaces of type integer
       if(ascending==false){ //if ascending is false then prints the numbers in lower triangle form
           //loops from num2 to num1 and prints the numbers using inner loop
           for(int i=num2;i>=num1;i--){
               for(int k=0;k<spaces;k++)
                   System.out.print(" ");
               for(int j=i;j>=num1;j--){
                   System.out.printf("%4d",j);
               }
               spaces += 2; //increases the spaces by 2
               System.out.println();
           }
       }else{
           spaces = (num2-num1)*2; //here the number of spaces is evaluated from this expression
           //logic same as above but loops from num1 to num2
           for(int i=num1;i<=num2;i++){
               for(int k=0;k<spaces;k++)
                   System.out.print(" ");
               for(int j=num1;j<=i;j++){
                   System.out.printf("%4d",j);
               }
               spaces -= 2; //decreases spaces by 2.
               System.out.println();
           }
       }
   }
   //main mehtod
   public static void main(String[] args) {
       printPattern(3,9,false); //calls the printPattern method by passing ascending parameter value as false
       printPattern(1,10,true); //calls the printPattern method by passing ascending parameter value as true
   }
}

//output screenshots

//any query, post in the comment section

Add a comment
Know the answer?
Add Answer to:
Hello, I need help writing the two methods to print a triangle shape in java. An...
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
  • 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...

  • How can i print a diamond implementing these methods public static void printNChars(int n, char c))....

    How can i print a diamond implementing these methods public static void printNChars(int n, char c)). This method will print n times the character c in a row, public static void printDiamond(int size, char edgeChar, char fillChar). This method will call printNChars() method to print the shape. It will use the edgeChar to print the sides and the fillChar to fill the interior of the shape. The shape will have a height and a width of the given size. public...

  • JAVA I need a switch so that users can input one selection then another. These selections...

    JAVA I need a switch so that users can input one selection then another. These selections are for a simple calculator that uses random numbers 1. + 2. - 3. * 4./ 5. RNG. This has to be simple this is a beginners class. Here is what I have import java.util.Scanner; import java.util.Random; import java.util.*; public class You_Michael02Basic_Calculator {    public static void main(String[] args) {        // Generate random numbers        int num1,num2;        num1 =(int)(Math.random()*100+1);...

  • Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right...

    Hi i need heeeeelllllp on this assignment i need to print the numbers diagonal top right corner to the bottom left corner and i dont know how to do it please help me thank you dd another method to the bottom of the "TwoDimArraysMethods.java" file called "printDiagonalRL()"                                         public static void printDiagonalRL(int[][] matrix) 4. Call this method in the main file ("TwoDimArraysAsParam.java") passing it "board." e.g. TwoDimArraysMethods.printDiagonal(board); 5. Your new method should print any numbers along the diagonal from...

  • Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the...

    Java 1. Write a getCount method in the IntArrayWorker class that returns the count of the number of times a passed integer value is found in the matrix. There is already a method to test this in IntArrayWorkerTester. Just uncomment the method testGetCount() and the call to it in the main method of IntArrayWorkerTester. 2. Write a getLargest method in the IntArrayWorker class that returns the largest value in the matrix. There is already a method to test this in...

  • I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need...

    I must implement a class to calculate n-th row of Pascal's Triangle (in Java). I need to create a static method that takes an argument "n" and returns the n'th line of pascal's triangle. the main method, which will call the class, is already done and the class is set up. Please build this without modifying the main method and without modifying exactly what the static method returns. public class Main { public static void main(String[] args) { int n...

  • please help me I need two methods that add and subtract two very large numbers in...

    please help me I need two methods that add and subtract two very large numbers in java created in a class called number and it runs on the demo below class below. Please don't use parseInt or BigIntger, they are not allowed. Thank you so much public class demoClass { public static void main(String[] args) { number num1; //number is class that we need to create to the add and subtract methods num1 = new number("11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"); number num2; //number is...

  • Write a recursive method in java to find GCD of two integers using Euclid's method. Integers...

    Write a recursive method in java to find GCD of two integers using Euclid's method. Integers can be positive or negative. public class Recursion {                 public static void main(String[] args) {                                 Recursion r = new Recursion();                                 System.out.println(“The GCD of 24 and 54 is “+r.findGCD(24,54)); //6                 }                 public int findGCD(int num1, int num2){                                 return -1;                 } }

  • Hi I need help with a java program that I need to create a Airline Reservation...

    Hi I need help with a java program that I need to create a Airline Reservation System I already finish it but it doesnt work can someone please help me I would be delighted it doesnt show the available seats when running the program and I need it to run until someone says no for booking a seat and if they want to cancel a seat it should ask the user to cancel a seat or continue booking also it...

  • What I need: Create a new program named Reverse4 and declare four integer variables with the...

    What I need: Create a new program named Reverse4 and declare four integer variables with the values 23, 45, 55, and 67. Add a method named Reverse that reverses the positions of four integer variables. The Reverse method should accept the variables as references. Write a Main() method that displays the four variables both before and after reversing them to ensure the method works correctly. What I have: using System; class Reverse4 { public static void reverse(ref int num1, ref...

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
ADVERTISEMENT