Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 2.
- public class Main
- {
- int num = 5;
- writeNums(num);
- }
- public static void writeNums(int num)
- {
- int num2 = 1;
- writeNums(num, num2);
- }
- public static void writeNums(int num, int num2)
- {
- if(num == num2)
- {
- System.out.println(num);
- }
- else
- {
- System.out.print(num2 + ", ");
- writeNums(num2 + 1);
- }
- }
- 4.
- public static int mystery(int n) {
- if (n < 10) {
- return (10 * n) + n;
- } else {
- int a = mystery(n / 10);
- int b = mystery(n % 10);
- return (100 * a) + b;
- }
- }
- 6.
- import java.util.*;
- public class writeSquares
- {
- public static writeSquares(int n)
- {
- int t=n;
- if(n<=0)
- return;
- if(n%2==1)
- {
- System.out.println(n*n);
- writeSquares(--n);
- }
- else
- {
- writeSquares(--n);
- System.out.println(t*t);
- }
- }
- }
- 8.
- import java.util.*;
- import java.io.*;
- public class exercises
- {
- public static void main(String [] args)
- {
- Scanner console = new Scanner(System.in);
- int n = console.nextInt();
- System.out.println(multiplyEvens(n));
- }
- private static int multiplyEvens(int n) {
- if (n < 1) throw new IllegalArgumentException("Value less than 1 not supported");
- else if (n == 1) return 2;
- else return multiplyEvens(n - 1) * (n * 2);
- }
- }
- 10.
- package suarez.com;
- import java.util.*;
- public class digitMatch
- {
- public static void main(String [] args)
- {
- Scanner console = new Scanner(System.in);
- System.out.println("gimme an integer sunny boi(non-negative integer)");
- int x = console.nextInt();
- System.out.println("gimme another integer sunny boi(non-negative integer");
- int y = console.nextInt();
- int count = 0;
- int z = DigitMatch(x, y, count);
- System.out.println("You have" + z + "Digits Matching between your two inputs(");
- }
- public int DigitMatch(int x, int y)
- {
- return DigitMatch( x,y, 0);
- }
- private static int DigitMatch(int x, int y, int count)
- {
- if(x < 0 || y < 0)
- {
- throw new IllegalArgumentException("THATS A FREAKIN NEGATIVE NUMBER");
- }
- if(x == 0 || y == 0)
- {
- return count;
- }
- else
- {
- if(x % 10 == y % 10)
- {
- count++;
- }
- return DigitMatch(x/10, y/10, count);
- }
- }
- }
- 12.
- public static boolean isReverse(String word1, String word2) {
- if (word1 == null || word2 == null) {
- return false;
- }
- if (word1.length() == 1 && word2.length() == 1) {
- //Used equals just for fast compare
- return word1.equals(word2);
- } else if (word1.length() == word2.length()) {
- if (word1.charAt(0) == word2.charAt(word2.length() - 1)) {
- String firstWord = word1.substring(1, word1.length());
- String secondWord = word2.substring(0, word2.length() - 1);
- System.out.printf("Trimmed %s, %s to %s, %s\n", word1, word2, firstWord, secondWord);
- return isReverse(firstWord, secondWord);
- } else {
- //Characters didn't matched
- return false;
- }
- } else {
- //Lenght doesn't match
- return false;
- }
- }
- 14.
- public static int evenDigits(int n) {
- int rev = 0;
- int digitCount = 0;
- // handle negative arguments
- if (n < 0) return -evenDigits(-n);
- // Extract the even digits to variable rev
- while (n != 0) {
- if (n % 2 == 0) {
- rev = rev * 10 + n % 10;
- digitCount += 1;
- }
- n /= 10;
- }
- // The digits were extracted in reverse order; reverse them again
- while (digitCount > 0) {
- n = n * 10 + rev % 10;
- rev /= 10;
- digitCount -= 1;
- }
- // The result is in n
- return n;
- }
- 16.
- import java.awt.*;
- import java.awt.event.*;
- import javax.swing.*;
- public class SierpinskiCarpet extends JPanel implements ActionListener {
- public static void main(String[] args) {
- JFrame window = new JFrame("Sierpinski Carpet");
- window.setContentPane( new SierpinskiCarpet() );
- window.pack();
- window.setResizable(false);
- window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
- int x = Math.max(10, (screensize.width - window.getWidth()) / 2);
- int y = Math.max(10, (screensize.height - window.getHeight()) / 2);
- window.setLocation(x,y);
- window.setVisible(true);
- }
- private int level;
- private JRadioButton[] levelButton;
- private JCheckBox[][] regionCheckbox;
- private Display display;
- private class Display extends JPanel {
- Display() {
- setBackground(Color.WHITE);
- setPreferredSize(new Dimension(729,729));
- }
- protected void paintComponent(Graphics g) {
- super.paintComponent(g);
- drawCarpet(g,level,0,0,729);
- }
- }
- public SierpinskiCarpet() {
- setLayout(new BorderLayout(3,3));
- setBackground(Color.GRAY);
- setBorder(BorderFactory.createLineBorder(Color.GRAY,3));
- display = new Display();
- add(display,BorderLayout.CENTER);
- Box controls = Box.createVerticalBox();
- controls.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
- controls.setBackground(Color.WHITE);
- controls.setOpaque(true);
- add(controls, BorderLayout.EAST);
- controls.add(new JLabel("Controls"));
- controls.add(Box.createVerticalStrut(30));
- levelButton = new JRadioButton[7];
- ButtonGroup grp = new ButtonGroup();
- for (int i = 0; i < 7; i++) {
- levelButton[i] = new JRadioButton("Recursion Level " + i);
- grp.add(levelButton[i]);
- levelButton[i].addActionListener(this);
- controls.add(levelButton[i]);
- }
- level = 4;
- levelButton[4].setSelected(true);
- regionCheckbox = new JCheckBox[3][3];
- JPanel checks = new JPanel();
- checks.setLayout(new GridLayout(3,3,5,5));
- for (int r = 0; r < 3; r++) {
- for (int c = 0; c < 3; c++) {
- regionCheckbox[r][c] = new JCheckBox();
- checks.add(regionCheckbox[r][c]);
- regionCheckbox[r][c].addActionListener(this);
- regionCheckbox[r][c].setSelected(true);
- }
- }
- checks.setMaximumSize(new Dimension(90,90));
- checks.setAlignmentX(LEFT_ALIGNMENT);
- checks.setBackground(Color.WHITE);
- regionCheckbox[1][1].setSelected(false);
- controls.add(Box.createVerticalStrut(30));
- controls.add(new JLabel("Regions To Include:"));
- controls.add(Box.createVerticalStrut(15));
- controls.add(checks);
- }
- private void drawCarpet(Graphics g, int level, int x, int y, int size) {
- g.fillRect(x,y,size,size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment