Results 1 to 3 of 3

Thread: Random code

  1. #1
    Samar is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Default Random code

    class Node
    {
    public long dData; // data item
    public Node next;

    public Node (long d)
    { dData = d; }

    public void displayLink()
    { System.out.print(dData + " "); }

    }
    class AddList
    {
    private Node first;
    private Node last;

    public AddList()
    {
    first = null;
    last = null;
    }
    public boolean isEmpty()
    { return first==null; }

    public void insertFirst(long dd) {
    Node newLink = new Node(dd);
    if( isEmpty() )
    last = newLink;
    else
    newLink.next = first;
    first = newLink;
    }
    public void insertLast(long dd)
    {
    Node newLink = new Node(dd);
    if( isEmpty() )
    first = newLink;
    else last.next = newLink;
    last = newLink;
    }
    public long deleteFirst()
    {
    long temp = first.dData;
    if(first.next == null)

    last = null;
    first = first.next;
    return temp;
    }
    public void displayList() {
    System.out.print("List (first-->last): ");
    Node current = first; // start at beginning
    while(current != null){ // until end of list, {
    current.displayLink(); // print data
    current = current.next; // move to next link

    System.out.println("");
    }
    }
    }


    public class LinkListN {


    public static void main(String[] args) {

    AddList theList = new AddList();
    theList.insertFirst(22);
    theList.insertFirst(44);
    theList.insertFirst(66);
    theList.insertLast(11); // insert at rear
    theList.insertLast(33);
    theList.insertLast(55);
    theList.displayList(); // display the list
    theList.deleteFirst(); // delete first two items
    theList.deleteFirst();
    theList.displayList(); // display again


    }

    }

  2. #2
    Samar is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    class StackN
    {
    private int maxSize; // size of stack array
    private long[] stackArray;
    private int top; // top of stack
    //--------------------------------------------------------------
    public StackN(int s) // constructor
    {
    maxSize = s; // set array size
    stackArray = new long[maxSize]; // create array
    top = -1; // no items yet
    }
    //--------------------------------------------------------------
    public void push(long j) // put item on top of stack
    {
    stackArray[++top] = j;
    // increment top, insert item

    }

    //--------------------------------------------------------------
    public long pop() // take item from top of stack
    { return stackArray[top--]; // access item, decrement top

    }
    //--------------------------------------------------------------
    public boolean isEmpty() // true if stack is empty
    { return (top == -1); }
    //--------------------------------------------------------------
    public boolean isFull() // true if stack is full
    { return (top == maxSize-1); }
    //--------------------------------------------------------------
    } // end class StackX
    ////////////////////////////////////////////////////////////////
    class StackApp1
    {
    public static void main(String[] args) {
    StackN theStack = new StackN(10); // make new stack
    theStack.push(50); // push items onto stack
    theStack.push(40);
    theStack.push(60);
    theStack.push(80);
    theStack.pop();
    while( !theStack.isEmpty() ) // until it's empty,
    { // delete item from stack
    long value = theStack.pop();
    System.out.println(value); // display it
    System.out.print(" ");
    } // end while

    System.out.println(" ");
    } // end main()
    } //

  3. #3
    Samar is offline Member
    Join Date
    Jan 2017
    Posts
    3
    Rep Power
    0

    Default Re: Is it worth to learn Swing?

    import java.io.*;
    public class Fibonacci {

    public static void main(String[] args) {
    for(int j = 1 ; j<13 ; j++)
    System.out.println(fib(j));
    }

    public static int fib (int n) {
    if (n <= 1) return 1;
    else
    return (fib(n-1) + fib(n-2)) ;
    }
    }

Similar Threads

  1. Distance Formula and Random numbers code
    By granslime in forum New To Java
    Replies: 10
    Last Post: 09-26-2013, 07:27 PM
  2. How do I create a code that generates random text?
    By Maya Berkowitz in forum New To Java
    Replies: 7
    Last Post: 02-08-2012, 12:01 PM
  3. Replies: 0
    Last Post: 01-23-2012, 10:12 AM
  4. Help needed debugging Random Color code for applet
    By rjavaprodev in forum Java Applets
    Replies: 2
    Last Post: 11-19-2011, 08:35 AM
  5. Generate a random code 4 letters long
    By bl00dr3d in forum New To Java
    Replies: 9
    Last Post: 04-06-2009, 05:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •