Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. public class Homework8 {
  2.  
  3.     public static void main(String[] args) {
  4.         Node rootNode = new Node(new Double(2), null);
  5.         problem0(rootNode);
  6.         problem1(rootNode);
  7.     }
  8.        
  9.     /*
  10.      * Problem 0 - 2-99 in 1.0 increments
  11.      * Problem 1 - Insert Double objects for 5.01 through 5.99 in 0.01 increments to the linked list.
  12.      */
  13.     public static Node problem0(Node rootNode) {//method for your "problem0"
  14.         Node last = rootNode; //setting "next" local variable to "rootnode"
  15.         double data = rootNode.getdata(); //setting "data" variable to whatever the initial data-value of "rootnode" is
  16.        
  17.         while (data < 100){ //will loop until the data in the latest node is 99
  18.             Node mostRecent = new Node(data, null);//makes new nodes and puts data in them.
  19.             last.setnext(mostRecent); //makes the node "last" point to the mostRecent we just created... "before the dot" points to "in the parentheses"
  20.             last = mostRecent; //sets last node reference to the new "last node"
  21.             data++;//want to make this increase the data by 1
  22.         }
  23.         return rootNode;
  24.     }
  25.     public static void problem1(Node rootNode){
  26.         double data = rootNode.getdata();
  27.         Node integers = problem0(rootNode);
  28.         Node mostRecent = rootNode;
  29.         while (integers.getnext()!= null){
  30.             //System.out.println(integers.getdata());
  31.             if (data == 5){//the code reaches this point, because if i ask it to print just this loop, it will
  32.                 //however, if i have it try to print the "integers" data, it infinitely loops/prints 2
  33.  
  34.                 /// You never set your "mostRecent" node or your "integers" node to anything different, so
  35.                 ///your code never approaches a terminating condition. When you use while loops, be sure
  36.                 ///that your code inside the while loop eventually reaches your terminating condition.
  37.  
  38.                 for (double minidata = 5.01; minidata <= 6.00; minidata+=0.01){
  39.                     Node miniNode = new Node(minidata, null);
  40.                     //System.out.println(miniNode.getdata());
  41.                     mostRecent.setnext(miniNode);
  42.                     mostRecent = miniNode;
  43.                 }
  44.                 break; //if i set a break here... it only prints 4 2's.... but they're still all 2's
  45.                 //which means that it's not actually cycling through problem0...?
  46.             }
  47.         data++;
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement