Results 1 to 8 of 8
- 03-08-2012, 02:54 AM #1
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
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:
Here is my outputJava 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()); } } }
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?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 |
- 03-08-2012, 10:48 PM #2
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.
- 03-08-2012, 11:19 PM #3
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.
- 03-09-2012, 10:08 AM #4
Re: Node, Stacks and Queues: Why am I getting a bizarre output?
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.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 03-09-2012, 04:32 PM #5
Re: Node, Stacks and Queues: Why am I getting a bizarre output?
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?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.
- 03-09-2012, 04:47 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Node, Stacks and Queues: Why am I getting a bizarre output?
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 03-09-2012, 04:49 PM #7
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.
- 03-21-2012, 06:18 AM #8
Member
- Join Date
- Oct 2010
- Posts
- 38
- Rep Power
- 0
Similar Threads
-
Bizarre screen distortion
By morello in forum New To JavaReplies: 1Last Post: 12-21-2011, 03:28 PM -
Most BIZARRE problem ever ( Need Advance People )
By trupsterful in forum Advanced JavaReplies: 11Last Post: 06-12-2011, 04:17 AM -
the most Bizarre problem EVER.
By trupsterful in forum Java AppletsReplies: 3Last Post: 05-19-2011, 05:49 PM -
bizarre auto expansion
By fishtoprecords in forum Suggestions & FeedbackReplies: 2Last Post: 07-31-2008, 10:52 PM -
I want to be able to do this with stacks and queues as well as with vectors
By carl in forum New To JavaReplies: 1Last Post: 08-07-2007, 07:05 AM


4Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks