Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Stacks Assignment
- import javax.swing.*;
- public class ReverseWord
- {
- public static void main(String[] args)
- {
- String s = JOptionPane.showInputDialog(null, "Please type a word");
- wordReverse(s);
- }
- public static void wordReverse(String orgWord)
- {
- Stack wordStack = new Stack();
- String finWord = null;
- orgWord.toLowerCase();
- char[] chArray = orgWord.toCharArray();
- for(int i = 0; i < orgWord.length(); i++)
- {
- wordStack.push(chArray[i]);
- }
- int x = 0;
- while(!wordStack.isEmpty())
- {
- chArray[x++] = (char) wordStack.pop();
- }
- finWord = String.copyValueOf(chArray);
- JOptionPane.showMessageDialog(null, finWord);
- }
- }
- //Queues assignment
- import java.awt.*;
- import java.awt.geom.Ellipse2D;
- import javax.swing.*;
- import java.util.Random;
- public class RandomArt extends JPanel
- {
- Queue randCol = new Queue();
- Queue randString = new Queue();
- Random rand = new Random();
- String[] shapes = {"circle", "square", "triangle", "rectangle"};
- Color[] colors = {Color.black, Color.blue, Color.red, Color.magenta};
- public RandomArt()
- {
- for(int x = 0; x < 4; x++)
- {
- randString.enqueue(shapes[rand.nextInt(4)]);
- randCol.enqueue(colors[rand.nextInt(4)]);
- }
- }
- public void paintComponent(Graphics g)
- {
- Graphics2D gr2D = (Graphics2D)g;
- Color shapeColor = null;
- int x = 0;
- while(x < 4)
- {
- // if(randCol.peek().equals(Color.black))
- // {
- // shapeColor = Color.black;
- // randCol.dequeue();
- // }
- // else if(randCol.peek().equals(Color.blue))
- // {
- // shapeColor = Color.blue;
- // randCol.dequeue();
- // }
- // else if(randCol.peek().equals(Color.red))
- // {
- // shapeColor = Color.red;
- // randCol.dequeue();
- // }
- // else if(randCol.peek().equals(Color.magenta))
- // {
- // shapeColor = Color.magenta;
- // randCol.dequeue();
- // }
- System.out.println(randString.peek());
- if(((String)randString.peek()).equals("circle"))
- {
- gr2D.setColor((Color)randCol.dequeue());
- int i = rand.nextInt(100);
- Ellipse2D.Float circle = new Ellipse2D.Float(rand.nextInt(500),rand.nextInt(500),i,i);
- gr2D.draw(circle);
- gr2D.fill(circle);
- randString.dequeue();
- }
- else if(((String)randString.peek()).equals("square"))
- {
- gr2D.setColor((Color)randCol.dequeue());
- int i = rand.nextInt(100), a = rand.nextInt(500), b = rand.nextInt(500);
- gr2D.drawRect(a, b, i, i);
- gr2D.fillRect(a, b, i, i);
- randString.dequeue();
- }
- else if(((String)randString.peek()).equals("triangle"))
- {
- gr2D.setColor((Color)randCol.dequeue());
- int[] a = {rand.nextInt(500),rand.nextInt(500),rand.nextInt(500)};
- int[] b = {rand.nextInt(500),rand.nextInt(500),rand.nextInt(500)};
- gr2D.drawPolygon(a, b, 3);
- gr2D.fillPolygon(a, b, 3);
- randString.dequeue();
- }
- else if(((String)randString.peek()).equals("rectangle"))
- {
- gr2D.setColor((Color)randCol.dequeue());
- int i = rand.nextInt(100), j = rand.nextInt(100), a = rand.nextInt(500), b = rand.nextInt(500);
- gr2D.drawRect(a, b, i, j);
- gr2D.fillRect(a, b, i, j);
- randString.dequeue();
- }
- x++;
- }
- }
- public static void main(String[] args)
- {
- JFrame f1 = new JFrame();
- f1.add(new RandomArt());
- f1.setSize(500,500);
- f1.setLocationRelativeTo(null);
- f1.setTitle("Got any grapes");
- f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- f1.setVisible(true);
- }
- }
- //Linked Lists assignment
- //SLIIndexedListCode main method:
- public static void main(String[] args)
- {
- SLIndexedListCode list = new SLIndexedListCode();
- for(int x = 0; x < list.size(); x++)
- {
- System.out.println(list.get(x));
- }
- System.out.println("");
- list.set(2, "yellow");
- for(int x = 0; x < list.size(); x++)
- {
- System.out.println(list.get(x).toString());
- }
- System.out.println("");
- list.set(2, "blue");
- for(int x = 0; x < list.size(); x++)
- {
- System.out.println(list.get(x).toString());
- }
- System.out.println("");
- list.remove(3);
- for(int x = 0; x < list.size(); x++)
- {
- System.out.println(list.get(x).toString());
- }
- System.out.println("");
- System.out.println("Empty = " + list.isEmpty());
- list.set(4, "football");
- JOptionPane.showMessageDialog(null, "Error: no node at position 5\nList size = " + list.size());
- }
- //DLPositionalListCode main method:
- public static void main(String[] args)
- {
- DLPositionalListCode list = new DLPositionalListCode();
- System.out.println("Previous Element: " + list.previous());
- System.out.println("Next Element: " + list.next());
- System.out.println("List size: " + list.size());
- list.add("A");
- list.add("B");
- list.add("C");
- list.add("D");
- list.add("E");
- list.add("F");
- list.add("G");
- list.add("H");
- list.add("I");
- list.add("J");
- System.out.println("");
- System.out.println(list);
- System.out.println("");
- System.out.println("Previous Element: " + list.previous());
- System.out.println("Next Element: " + list.next());
- System.out.println("List size: " + list.size());
- System.out.println("");
- //move curpos to previous node and return the value
- //Replaces what curPos is pointing to
- list.set(list.previous());
- list.set("$");
- System.out.println(list);
- int x = 0;
- while(x < 5)
- {
- list.set(list.previous());
- x++;
- }
- System.out.println("");
- System.out.println("Current Value: " + list.curPos.value);
- System.out.println("Current Index: " + list.currentIndex());
- System.out.println("Previous Index: " + list.previousIndex());
- System.out.println("Next Index: " + list.nextIndex());
- list.remove();
- System.out.println("");
- System.out.println(list);
- System.out.println("List size: " + list.size());
- System.out.println("Current value: " + list.curPos.value);
- list.remove2();
- System.out.println("");
- System.out.println(list);
- System.out.println("List size: " + list.size());
- System.out.println("Current value: " + list.curPos.value);
- list.curPos = list.head;
- list.curPos=list.head;
- for(int i = 0; i <= list.size(); ++i)
- {
- list.set("*");
- list.curPos = list.curPos.next;
- }
- System.out.println("");
- System.out.println(list);
- list.addAfter("Happy");
- list.addAfter("Birthday");
- list.addAfter("To");
- list.addAfter("You");
- System.out.println("");
- System.out.println(list);
- System.out.println(list.toBackwardString());
- list.addAfter2("Happy");
- list.addBefore("Birthday");
- list.addBefore2("To");
- list.addAfter("You");
- System.out.println("");
- System.out.println(list);
- System.out.println(list.toBackwardString());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement