Results 1 to 8 of 8
Like Tree4Likes
  • 1 Post By quad64bit
  • 1 Post By Norm
  • 1 Post By DarrylBurke
  • 1 Post By JosAH

Thread: Node, Stacks and Queues: Why am I getting a bizarre output?

  1. #1
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Cool Node, Stacks and Queues: Why am I getting a bizarre output?

    The assignment asked me to write 4 classes: Car, Node, Lstack, and Lqueue

    1) create an instance of the Lstack and place 7 values into it using a while loop that goes while the size is < 7, allowing the user to enter a String value for the license plate, and placing the character 'X' as the car's action code. Display it with labels.

    2) create an instance of the Lqueue and place 5 values into it using a while loop that goes while the size is < 5, allowing the user to enter a String value for the license plate, and placing the character 'Y' as the car's action code. Display it with labels.

    3) call the reverseQueue method on the queue and display the resulting contents.

    4) call the reverseStack method on the stack and display the resulting contents.

    Here's my code:
    Java Code:
    package assignment5;
    import java.util.*;
    import java.lang.String;
    
    public class Assignment5 {
    	public static void main(String[] args) {
    		Scanner input = new Scanner(System.in);
    //create instance objects of class Lstack
    		System.out.println("Lstack ");
    		Lstack myLstack = new Lstack();	
    		int i = 0;
    		while (i < 7) {
    			System.out.print("Enter a license plate number: ");
    			String licenseP = input.next();
    			Car myCar = new Car(licenseP, 'X');
    			myLstack.push(myCar);  //place into Lstack
    			i++;					
    		}
    		System.out.println();
    //create instance objects of class Lqueue		
    		System.out.println("Lqueue ");
    		Lqueue myLqueue = new Lqueue();
    		int j = 0;
    		while (j < 5) {
    			System.out.print("Enter a license plate number: ");
    			String licenseP = input.next();
    			Car myCar = new Car(licenseP, 'Y');
    			myLqueue.insert(myCar);  //place into Lqueue
    			j++;			
    		}
    //display stuff
    		myLstack.display();
    		myLqueue.display();
    		//reverse
    		myLqueue.reverseQueue(myLqueue);
    		myLqueue.display();
    		myLstack.reverseStack(myLstack);
    		myLstack.display();			
    	}
    }
    
    class Car {
    //define variables
    	String licensePlate;
    	char actionCode;
    //constructor with specific license plate and action code
    	Car(String newLicense, char newAction) {
    		licensePlate = newLicense;
    		actionCode = newAction;
    	}
    //getter and setter methods
    	public String getLicensePlate() {
    		return licensePlate;
    	}
    	public char getActionCode() {
    		return actionCode;
    	}
    	public void setLicensePlate(String newLicense) {
    		licensePlate = newLicense;
    	}
    	public void setActionCode(char newAction) {
    		actionCode = newAction;
    	}	
    }
    
    class Node {
    //define variables
    	Car data;
    	Node next;
    //constructor with specific size
    	Node(Car newData, Node newNext) {
    		data = newData;
    		next = newNext;
    	}
    }
    
    class Lstack {
    //define variables
    	private Node top;
    	private int numelements;
    //constructor with specific size
    	Lstack() {
    		top = null;
    		numelements = 0;
    	}		
    //Lstack Methods
    	public void push(Car x) {  //push a car on the stack
    		top = new Node(x, top);
    		numelements++;
    	}
    	public Car pop() {  //pop a car off the stack
    		Car temp = top.data;
    		top = top.next;
    		numelements--;
    		return temp;
    	}
    	public boolean isEmpty() {  //checks if empty
    		return (top == null);
    	}
    	public int size() {  //returns number of elements in the stack
    		return numelements;
    	}
    	public void display() {  //displays stack contents
    		Node curr = top;
    		System.out.print("TOP- ");
    		while(curr != null) {
    			System.out.print(curr.data + " | ");
    			curr = curr.next;
    		}
    		System.out.println();
    	}
    	public void reverseStack(Lstack myS) {  //reverses stack's contents
    		Lqueue q = new Lqueue();
    		while(! myS.isEmpty()) {
    			q.insert(myS.pop());
    		}
    		while(!q.isEmpty()) {
    			myS.push(q.remove());
    		}	
    	}
    }
    class Lqueue {
    //define variables
    	private int numElements;
    	private Node front;
    	private Node rear;
    //constructor with specific size
     	Lqueue() {
    		front = null;
    		rear = null;
    	}
    //Lqueue Methods
    	public void insert(Car x) {  //add a car to rear of the queue
    		if(front == null) {
    			front = new Node(x, null);
    			rear = front;
    		} else {
    			rear.next = new Node(x, null);
    			rear = rear.next;
    		}
    	}
    	public Car remove() {  //remove a car from the front of the queue
    		Car temp = null;
    		front = front.next;
    		return temp;
    	}
    	public boolean isEmpty() {  //checks if empty
    		return (front == null);
    	}
    	public int size() {  //returns number of elements in the queue
    		return numElements;
    	}
    	public void display() {  //diplays contents of the queue
    		Node curr = front;
    		System.out.print("FRONT- ");
    		while(curr != null) {
    			System.out.print(curr.data + " | ");
    			curr = curr.next;
    		}
    		System.out.println();	
    	}
    	public void reverseQueue(Lqueue myQ) {  //reverses the contents of the queue
    		Lstack s = new Lstack();
    		while(! myQ.isEmpty()) {
    			s.push(myQ.remove());
    		}
    		while(!s.isEmpty()) {
    			myQ.insert(s.pop());
    		}
    	}
    }
    Here is my output
    Lstack
    Enter a license plate number: 123456
    Enter a license plate number: 123455
    Enter a license plate number: 235657
    Enter a license plate number: 468445
    Enter a license plate number: 579456
    Enter a license plate number: 986463
    Enter a license plate number: 986346

    Lqueue
    Enter a license plate number: 567445
    Enter a license plate number: 358026
    Enter a license plate number: 468073
    Enter a license plate number: 568033
    Enter a license plate number: 369870
    TOP- assignment5.Car@1a758cb | assignment5.Car@1b67f74 | assignment5.Car@69b332 | assignment5.Car@173a10f | assignment5.Car@530daa | assignment5.Car@a62fc3 | assignment5.Car@89ae9e |
    FRONT- assignment5.Car@1270b73 | assignment5.Car@60aeb0 | assignment5.Car@16caf43 | assignment5.Car@66848c | assignment5.Car@8813f2 |
    FRONT- null | null | null | null | null |
    TOP- null | null | null | null | null | null | null |
    Why am I getting an unusual output? Why am I getting assignment5.Car@1a758cb?? And why is null appearing in the last two lines of my output?

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Node, Stacks and Queues: Why am I getting a bizarre output?

    "assignment5.Car@1b67f74" and the like are memory addresses. This happens when you try to print a java object (rather than a primitive) that has no defined toString() method.
    son012189 likes this.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,423
    Rep Power
    25

    Default Re: Node, Stacks and Queues: Why am I getting a bizarre output?

    If you want to print the value of a Car variable, add a toString() method to it that returns a String containing what you would like to see.
    son012189 likes this.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,242
    Rep Power
    19

    Default Re: Node, Stacks and Queues: Why am I getting a bizarre output?

    Quote Originally Posted by quad64bit View Post
    "assignment5.Car@1b67f74" and the like are memory addresses. This happens when you try to print a java object (rather than a primitive) that has no defined toString() method.
    Misleading.

    1) It's not documented to have any relation to the memory address. That's the typical return value from the toString() method inherited from java.lang.Object.

    2) Every class has a 'defined' toString() method; either overridden in the class or inherited from a superclass. A class that needs to provide a human-readable description of itself should override the method accordingly.

    db
    son012189 likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Node, Stacks and Queues: Why am I getting a bizarre output?

    It's not documented to have any relation to the memory address. That's the typical return value from the toString() method inherited from java.lang.Object.
    I thought it was the address in virtual memory of the object, i.e. the memory address as it pertains the the memory space allocated by the JVM. I realize it isn't a machine memory address in physical memory, but is "1b67f74" not the hexadecimal address of the space reserved inside the JVM's memory space? A 'virtual' memory address if you will?

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default Re: Node, Stacks and Queues: Why am I getting a bizarre output?

    Quote Originally Posted by quad64bit View Post
    I thought it was the address in virtual memory of the object, i.e. the memory address as it pertains the the memory space allocated by the JVM. I realize it isn't a machine memory address in physical memory, but is "1b67f74" not the hexadecimal address of the space reserved inside the JVM's memory space? A 'virtual' memory address if you will?
    That is only true for the Sun/Oracle implementation of the JVM; straight from the API docs:

    Quote Originally Posted by API toString()
    The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.
    Quote Originally Posted by API hashCode()
    As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the JavaTM programming language.)
    kind regards,

    Jos
    son012189 likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Node, Stacks and Queues: Why am I getting a bizarre output?

    Great information! Thank you :)

    TIL the default toString returns a hash value of the object in question, not necessarily any useful memory address information.

  8. #8
    son012189 is offline Member
    Join Date
    Oct 2010
    Posts
    38
    Rep Power
    0

    Default Re: Node, Stacks and Queues: Why am I getting a bizarre output?

    Thanks guys. Sorry about the late response. I completely forgot I posted this. I modified my code and got it to work properly.

Similar Threads

  1. Bizarre screen distortion
    By morello in forum New To Java
    Replies: 1
    Last Post: 12-21-2011, 03:28 PM
  2. Most BIZARRE problem ever ( Need Advance People )
    By trupsterful in forum Advanced Java
    Replies: 11
    Last Post: 06-12-2011, 04:17 AM
  3. the most Bizarre problem EVER.
    By trupsterful in forum Java Applets
    Replies: 3
    Last Post: 05-19-2011, 05:49 PM
  4. bizarre auto expansion
    By fishtoprecords in forum Suggestions & Feedback
    Replies: 2
    Last Post: 07-31-2008, 10:52 PM
  5. Replies: 1
    Last Post: 08-07-2007, 07:05 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
  •