Concept explainers
Fix the code below so that there is a function that calculate the height of the triangle.
package recursion;
import javax.swing.*;
import java.awt.*;
/**
* Draw a Sierpinski Triangle of a given order on a JPanel.
*
*
*/
public class SierpinskiPanel extends JPanel {
private static final int WIDTH = 810;
private static final int HEIGHT = 830;
private int order;
/**
* Construct a new SierpinskiPanel.
*/
public SierpinskiPanel(int order) {
this.order = order;
this.setMinimumSize(new Dimension(WIDTH, HEIGHT));
this.setMaximumSize(new Dimension(WIDTH, HEIGHT));
this.setPreferredSize(new Dimension(WIDTH, HEIGHT));
}
public static double height(double size) {
double h = (size * Math.sqrt(3)) / 2.0;
return h;
}
/**
* Draw an inverted triangle at the specified location on this JPanel.
*
* @param x the x coordinate of the upper left corner of the triangle
* @param y the y coordinate of the upper left corner of the triangle
* @param size the width and height of the triangle
* @param g the graphics object on which to draw the triangle
*/
private void drawTriangle(int x, int y, int size, Graphics g) {
Polygon triangle = new Polygon();
double h = height(size);
triangle.addPoint(x, y);
triangle.addPoint(x - size /2, (int) (y + h));
triangle.addPoint(x + size / 2, (int) (y + h));
g.fillPolygon(triangle);
}
/**
* Draw a Sierpinski triangle of the specified order at the specified location
* on this JPanel.
*
* @param order the order of the Sierpinski triangle to draw.
* @param x the x coordinate of the upper left corner of the central order 1 triangle
* @param y the y coordinate of the upper left corner of the central order 1 triangle
* @param size the width and height of the triangle
* @param g the graphics object on which to draw the triangle
*/
private void drawSierpinski(int order, int x, int y, int size, Graphics g) {
// fill in this method.
/*
* You'll want to make use of the drawTriangle method to do the
* actual drawing of the triangles.
*/
drawTriangle(x, y, size, g);
if (order == 1) {
} else {
drawSierpinski(order - 1, x + size / 4, y - size / 2, size / 2, g);
drawSierpinski(order - 1, x - size / 4, y + size / 2, size / 2, g);
drawSierpinski(order - 1, x + 3 * size / 4, y + size / 2, size / 2, g);
}
}
/**
* Override the paintComponent method so that each time the JPanel is painted on
* the screen we can draw the Sierpinski Triangle on the JPanel.
*
* @param g the graphics object on which to draw the triangle.
*/
public void paintComponent(Graphics g) {
g.setColor(Color.BLACK);
drawSierpinski(order, 205, 425, 400, g);
}
/*
* NOTE: Some code adopted/adapted from an earlier COMP132 lab created by
* Tim Wahls who in turn borrowed some of the code from Robert Sedgewick.
*/
}
Step by stepSolved in 2 steps with 1 images
- Interpreter.java is missing these methods in the code so make sure to add them: -print, printf: Exist, marked as variadic, call Java functions -getline and next: Exist and call SplitAndAssign -gsub, match, sub, index, length, split, substr, tolower, toupper: Exist, call Java functions, correct return Below is interpreter.java import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class Interpreter { private HashMap<String, InterpreterDataType> globalVariables; private HashMap<String, FunctionDefinitionNode> functions; private class LineManager { private List<String> lines; private int currentLineIndex; public LineManager(List<String> inputLines) { this.lines = inputLines; this.currentLineIndex = 0; } public boolean splitAndAssign() { if (currentLineIndex < lines.size()) { String currentLine = lines.get(currentLineIndex);…arrow_forwardThis class contains a number of syntax and semantic errors.Your job is to find them all. public Class DebugMe{ public static void main(String args[]){ printSums(args); compareBoxes(); } //This function is designed to print the sums of all numbers between 1 and the //first number entered as an argument to DebugMe //For example, if you enter: DebugMe 3 //You should get: // The sum of the first 1 numbers is 1. // The sum of the first 2 numbers is 3. // The sum of the first 3 numbers is 6. public static void printSums(String[] args){ { int count; count = Integer.parseInt(args); for (i = 1 ; i <= count ; i++); { int sum = 0; int i = 0; sum += i - 1; System.out.println("The sum of the first " + i + " numbers is " + sum + "."); } } //This function demonstrates the use of the Box class //DO NOT change anything in this function //use it to…arrow_forwardwrite a code in java (using recursion)arrow_forward
- PLZ help with the folllowig IN JAVA TRUE OR FALSE When a recursive call is encountered, computation is temporarily suspended; all of the information needed to continue the computation is saved and the recursive call is evaluated. 2.Can you have a static method in a nonstatic inner class?arrow_forwardSolve in javaFXarrow_forwardNeed python help. For problems 1 and 2, add your code to the file Lab2.java. Add your tests in the main function of Lab2.java. Do not use static variables in class Lab2 to implement recursive methods. Problem 1: Implement a recursive method min that accepts an array and returns the minimum element in the array. The recursive step should divide the array into two halves and find the minimum in each half. The program should run in O(n) time and consume O(logn) memory. Demonstrate the output of min on the array int [] a = { 2, 3, 5, 7, 11, 13, 17, 19, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 23, 29, 31, 37, 41, 43 } Problem 2 You have been offered a job that pays as follows: On the first day, you are paid 1 cent, on the second day, 2 cents, on the third day, 4 cents and so on. In other words, your pay doubles every day. Write a recursive method computePay that for a given day number computes the pay in cents. Assume that you accumulate all the money that you are paid. Write a…arrow_forward
- in java Revers an Array of n integers Reversing an Array1. Describe the definition of recursive function.oBase case(s)oRecursive case(s)2. Write the code.arrow_forwardPlease help me fix the errors in the java program belowarrow_forwardLab Goal : This lab was designed to teach you more about recursion. Lab Description : Take a string and remove all occurrences of the word chicken and count how many chickens were removed. Keep in mind that removing a chicken might show a previously hidden chicken. You may find substring and indexOf useful. achickchickenen - removing the 1st chicken would leave achicken behindachicken - removing the 2nd chicken would leave a behindSample Data : itatfunitatchickenfunchchickchickenenickenchickchickfunchickenbouncetheballchickenSample Output : 01302arrow_forward
- Code 16-1 /** This class has a recursive method. */ public class EndlessRecursion { public static void message() { System.out.println("This is a recursive method."); message(); } } Code 16.2 /** This class has a recursive method message, which displays a message n times. */ public class Recursive { public static void messge (int n) { if (n>0) { System.out.println (" This is a recursive method."); message(n-1); } } } Task #1 Tracing Recursive Methods 1. Copy the file Recursion.java (see Code Listing 16.1) from the Student Files or as directed by your instructor. 2. Run the program to confirm that the generated answer is correct. Modify the factorial method in the following ways: a. Add these lines above the first if statement: int temp; System.out.println("Method call -- " + "calculating " + "Factorial of: " + n); Copyright © 2019 Pearson Education, Inc., Hoboken NJ b. Remove this line in the recursive section at the end of the method: return…arrow_forwardThis is a debugging question - The files provided in the code editor to the right contain syntax and/or logic errors. In each case, determine and fix the problem, remove all syntax and coding errors, and run the program to ensure it works properly. Code - // This class determines the logarithm of a number public class DebugTwelve1 { public static void main(String[] args) throws ArithmeticException { double num = -8.8, result. try { if(num <= 0) throw(new ArithmeticException()); result = Math.log(num); System.out.println("Result is " + result); } catch() { System.out.println("Can't take logarithm for value of zero or lower"); } } }arrow_forwardUse Javaarrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education