Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Homework8 {
- public static void main(String[] args) {
- Node rootNode = new Node(new Double(2), null);
- problem0(rootNode);
- problem1(rootNode);
- }
- /*
- * Problem 0 - 2-99 in 1.0 increments
- * Problem 1 - Insert Double objects for 5.01 through 5.99 in 0.01 increments to the linked list.
- */
- public static Node problem0(Node rootNode) {//method for your "problem0"
- Node last = rootNode; //setting "next" local variable to "rootnode"
- double data = rootNode.getdata(); //setting "data" variable to whatever the initial data-value of "rootnode" is
- while (data < 100){ //will loop until the data in the latest node is 99
- Node mostRecent = new Node(data, null);//makes new nodes and puts data in them.
- last.setnext(mostRecent); //makes the node "last" point to the mostRecent we just created... "before the dot" points to "in the parentheses"
- last = mostRecent; //sets last node reference to the new "last node"
- data++;//want to make this increase the data by 1
- }
- return rootNode;
- }
- public static void problem1(Node rootNode){
- double data = rootNode.getdata();
- Node integers = problem0(rootNode);
- Node mostRecent = rootNode;
- while (integers.getnext()!= null){
- //System.out.println(integers.getdata());
- if (data == 5){//the code reaches this point, because if i ask it to print just this loop, it will
- //however, if i have it try to print the "integers" data, it infinitely loops/prints 2
- /// You never set your "mostRecent" node or your "integers" node to anything different, so
- ///your code never approaches a terminating condition. When you use while loops, be sure
- ///that your code inside the while loop eventually reaches your terminating condition.
- for (double minidata = 5.01; minidata <= 6.00; minidata+=0.01){
- Node miniNode = new Node(minidata, null);
- //System.out.println(miniNode.getdata());
- mostRecent.setnext(miniNode);
- mostRecent = miniNode;
- }
- break; //if i set a break here... it only prints 4 2's.... but they're still all 2's
- //which means that it's not actually cycling through problem0...?
- }
- data++;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement