Advertisement
Guest User

Alex's Assignments

a guest
Apr 7th, 2020
205
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.68 KB | None | 0 0
  1. //Stacks Assignment
  2.  
  3. import javax.swing.*;
  4.  
  5. public class ReverseWord
  6. {
  7.     public static void main(String[] args)
  8.     {
  9.         String s = JOptionPane.showInputDialog(null, "Please type a word");
  10.         wordReverse(s);
  11.     }
  12.     public static void wordReverse(String orgWord)
  13.     {
  14.         Stack wordStack = new Stack();
  15.         String finWord = null;
  16.         orgWord.toLowerCase();
  17.         char[] chArray = orgWord.toCharArray();
  18.        
  19.         for(int i = 0; i < orgWord.length(); i++)
  20.         {
  21.             wordStack.push(chArray[i]);
  22.         }
  23.         int x = 0;
  24.         while(!wordStack.isEmpty())
  25.         {
  26.             chArray[x++] = (char) wordStack.pop();
  27.         }
  28.         finWord = String.copyValueOf(chArray);
  29.         JOptionPane.showMessageDialog(null, finWord);
  30.     }
  31. }
  32.  
  33. //Queues assignment
  34.  
  35. import java.awt.*;
  36. import java.awt.geom.Ellipse2D;
  37. import javax.swing.*;
  38.  
  39. import java.util.Random;
  40.  
  41. public class RandomArt extends JPanel
  42. {
  43.     Queue randCol = new Queue();
  44.     Queue randString = new Queue();
  45.     Random rand = new Random();
  46.    
  47.     String[] shapes = {"circle", "square", "triangle", "rectangle"};
  48.     Color[] colors = {Color.black, Color.blue, Color.red, Color.magenta};
  49.    
  50.     public RandomArt()
  51.     {
  52.         for(int x = 0; x < 4; x++)
  53.         {
  54.             randString.enqueue(shapes[rand.nextInt(4)]);
  55.             randCol.enqueue(colors[rand.nextInt(4)]);
  56.         }
  57.     }
  58.     public void paintComponent(Graphics g)
  59.     {
  60.         Graphics2D gr2D = (Graphics2D)g;
  61.         Color shapeColor = null;
  62.         int x = 0;
  63.         while(x < 4)
  64.         {
  65. //          if(randCol.peek().equals(Color.black))
  66. //          {
  67. //              shapeColor = Color.black;
  68. //              randCol.dequeue();
  69. //          }
  70. //          else if(randCol.peek().equals(Color.blue))
  71. //          {
  72. //              shapeColor = Color.blue;
  73. //              randCol.dequeue();
  74. //          }
  75. //          else if(randCol.peek().equals(Color.red))
  76. //          {
  77. //              shapeColor = Color.red;
  78. //              randCol.dequeue();
  79. //          }
  80. //          else if(randCol.peek().equals(Color.magenta))
  81. //          {
  82. //              shapeColor = Color.magenta;
  83. //              randCol.dequeue();
  84. //          }
  85.             System.out.println(randString.peek());
  86.             if(((String)randString.peek()).equals("circle"))
  87.             {
  88.                 gr2D.setColor((Color)randCol.dequeue());
  89.                 int i = rand.nextInt(100);
  90.                 Ellipse2D.Float circle = new Ellipse2D.Float(rand.nextInt(500),rand.nextInt(500),i,i);
  91.                 gr2D.draw(circle);
  92.                 gr2D.fill(circle);
  93.                 randString.dequeue();
  94.             }
  95.             else if(((String)randString.peek()).equals("square"))
  96.             {
  97.                 gr2D.setColor((Color)randCol.dequeue());
  98.                 int i = rand.nextInt(100), a = rand.nextInt(500), b = rand.nextInt(500);
  99.                 gr2D.drawRect(a, b, i, i);
  100.                 gr2D.fillRect(a, b, i, i);
  101.                 randString.dequeue();
  102.             }
  103.             else if(((String)randString.peek()).equals("triangle"))
  104.             {
  105.                 gr2D.setColor((Color)randCol.dequeue());
  106.                 int[] a = {rand.nextInt(500),rand.nextInt(500),rand.nextInt(500)};
  107.                 int[] b = {rand.nextInt(500),rand.nextInt(500),rand.nextInt(500)};
  108.                 gr2D.drawPolygon(a, b, 3);
  109.                 gr2D.fillPolygon(a, b, 3);
  110.                 randString.dequeue();
  111.             }
  112.             else if(((String)randString.peek()).equals("rectangle"))
  113.             {
  114.                 gr2D.setColor((Color)randCol.dequeue());
  115.                 int i = rand.nextInt(100), j = rand.nextInt(100), a = rand.nextInt(500), b = rand.nextInt(500);
  116.                 gr2D.drawRect(a, b, i, j);
  117.                 gr2D.fillRect(a, b, i, j);
  118.                 randString.dequeue();
  119.             }
  120.             x++;
  121.         }
  122.     }
  123.     public static void main(String[] args)
  124.     {
  125.         JFrame f1 = new JFrame();
  126.         f1.add(new RandomArt());
  127.         f1.setSize(500,500);
  128.         f1.setLocationRelativeTo(null);
  129.         f1.setTitle("Got any grapes");
  130.         f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  131.         f1.setVisible(true);
  132.     }
  133. }
  134.  
  135.  
  136. //Linked Lists assignment
  137.  
  138. //SLIIndexedListCode main method:
  139.  
  140. public static void main(String[] args)
  141.     {
  142.         SLIndexedListCode list = new SLIndexedListCode();
  143.        
  144.         for(int x = 0; x < list.size(); x++)
  145.         {
  146.             System.out.println(list.get(x));
  147.         }
  148.         System.out.println("");
  149.         list.set(2, "yellow");
  150.        
  151.         for(int x = 0; x < list.size(); x++)
  152.         {
  153.             System.out.println(list.get(x).toString());
  154.         }
  155.         System.out.println("");
  156.         list.set(2, "blue");
  157.         for(int x = 0; x < list.size(); x++)
  158.         {
  159.             System.out.println(list.get(x).toString());
  160.         }
  161.         System.out.println("");
  162.         list.remove(3);
  163.         for(int x = 0; x < list.size(); x++)
  164.         {
  165.             System.out.println(list.get(x).toString());
  166.         }
  167.         System.out.println("");
  168.         System.out.println("Empty = " + list.isEmpty());
  169.        
  170.         list.set(4, "football");
  171.         JOptionPane.showMessageDialog(null, "Error: no node at position 5\nList size = " + list.size());
  172.     }
  173.  
  174.  
  175. //DLPositionalListCode main method:
  176.  
  177. public static void main(String[] args)
  178.     {
  179.         DLPositionalListCode list = new DLPositionalListCode();
  180.         System.out.println("Previous Element: " + list.previous());
  181.         System.out.println("Next Element: " + list.next());
  182.         System.out.println("List size: " + list.size());
  183.        
  184.         list.add("A");
  185.         list.add("B");
  186.         list.add("C");
  187.         list.add("D");
  188.         list.add("E");
  189.         list.add("F");
  190.         list.add("G");
  191.         list.add("H");
  192.         list.add("I");
  193.         list.add("J");
  194.         System.out.println("");
  195.         System.out.println(list);
  196.         System.out.println("");
  197.         System.out.println("Previous Element: " + list.previous());
  198.         System.out.println("Next Element: " + list.next());
  199.         System.out.println("List size: " + list.size());
  200.         System.out.println("");
  201.        
  202.         //move curpos to previous node and return the value
  203.         //Replaces what curPos is pointing to
  204.  
  205.         list.set(list.previous());
  206.        
  207.         list.set("$");
  208.         System.out.println(list);
  209.         int x = 0;
  210.         while(x < 5)
  211.         {
  212.             list.set(list.previous());
  213.             x++;
  214.         }
  215.         System.out.println("");
  216.         System.out.println("Current Value: " + list.curPos.value);
  217.         System.out.println("Current Index: " + list.currentIndex());
  218.         System.out.println("Previous Index: " + list.previousIndex());
  219.         System.out.println("Next Index: " + list.nextIndex());
  220.        
  221.         list.remove();
  222.        
  223.         System.out.println("");
  224.         System.out.println(list);
  225.         System.out.println("List size: " + list.size());
  226.         System.out.println("Current value: " + list.curPos.value);
  227.        
  228.         list.remove2();
  229.        
  230.         System.out.println("");
  231.         System.out.println(list);
  232.         System.out.println("List size: " + list.size());
  233.         System.out.println("Current value: " + list.curPos.value);
  234.        
  235.         list.curPos = list.head;
  236.        
  237.         list.curPos=list.head;
  238.         for(int i = 0; i <= list.size(); ++i)
  239.         {
  240.             list.set("*");
  241.             list.curPos = list.curPos.next;
  242.         }
  243.        
  244.         System.out.println("");
  245.         System.out.println(list);
  246.        
  247.         list.addAfter("Happy");
  248.         list.addAfter("Birthday");
  249.         list.addAfter("To");
  250.         list.addAfter("You");
  251.        
  252.         System.out.println("");
  253.         System.out.println(list);
  254.         System.out.println(list.toBackwardString());
  255.        
  256.         list.addAfter2("Happy");
  257.         list.addBefore("Birthday");
  258.         list.addBefore2("To");
  259.         list.addAfter("You");
  260.        
  261.         System.out.println("");
  262.         System.out.println(list);
  263.         System.out.println(list.toBackwardString());
  264.        
  265.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement