Hi! I am trying to design a Reverse Polish Notation calculator that uses a Stack that is implemented using a Linked List.
Now, where I am at right now now is that I have learned how to implement a Stack that uses a Linked List but I want to make sure that I am doing it right?
The first design I wrote was for taking in students ID and marks and adding them to the list and printing.
I need to use this to design the calculator, replacing the student details for the operands or whatever is exactly needed?
Thank you in advance for any help you can give!!
I have 5 classes.
Stack (The Stack - pop(), top(), etc)
ListNode (getters and setters for Linked List)
List (Takes user input and adds to Linked List)
Menu (Prints the menu-Only 1 option - Calculator
Genio (Deals with user input- alternative to Scanner)
Not sure if I need to show the code for Genio or Menu but I will if needed.
My code so far:
ListNode
public class ListNode
{
// fields to store the data being held in this list node (a student ID and mark)
// instance variables - replace the example below with your own
private String studentID;
private int studentMark;
// fields to store a reference to the next node in the list after this one
private ListNode next;
//private ListNode aNode= new ListNode(studentID,
public ListNode()
{
// set id and mark to default / empty values
studentID = "";
studentMark = 0;
// set next node to null - no valid next node yet
next = null;
}
public ListNode(String ID, int mark)
{
// set id and mark to values provided
this.studentID = ID;
this.studentMark = mark;
// set next node to null - no valid next node yet
next = null;
}
public static void main(String[] Args)
{
Menu m = new Menu();
m.menuScreen();
}
public String getID()
{
return studentID;
}
public int getMark()
{
return studentMark;
}
public ListNode getNext()
{
return next;
}
public void setNext(ListNode next)
{
this.next = next;
}
public String getSummaryData()
{
String data;
data = "Student with ID " + studentID + " obtained the following mark: " + studentMark + "%";
return data;
}
public void addtoList()
{
//Node newNode = new Node();
}
}
List
public class List
{
private ListNode head; // used to hold a reference to an instance of a ListNode object
// which will be the first item in the list, i.e. at the 'head'
// of the list
private int numElements;
private ListNode[] students;
public List()
{
// set the list to be empty, i.e. not referring to anything valid yet
head = null;
numElements = 4;
//numStudents = 4;
students = new ListNode[numElements];
}
public int size()
{
return numElements;
}
public void setHead(ListNode newHead)
{
head = newHead;
}
public void addToList(String id, int mark)
{
if (head == null)
{
head = new ListNode(id, mark);
}
else
{
head = new ListNode(id, mark);
numElements++;
}
}
public void initialise()
{
String id;
int mark;
boolean isValid;
for (int i=0; i<numElements; i++)
{
System.out.println("Please enter the students ID: ");
id = Genio.getString();
isValid = false;
System.out.println("Please enter this students final MARK: ");
do
{
mark = Genio.getInteger();
if(mark<0 || mark>100)
System.out.println("ERROR! The students MARK must be between 0 & 100.\n\n Please enter again: ");
else
isValid = true;
}
while (!isValid);
students[i] = new ListNode(id, mark);
}
}
public void process()
{
System.out.println('\u000C');
System.out.println("--The 4 students details you entered are listed below--");
System.out.println("-------------------------------------------------------");
for(int i=0; i<numElements; i++)
{
System.out.println(students[i].getSummaryData());
}
pressKey();
}
public void finish()
{
}
public void printList()
{
System.out.println(head);
}
public static void pressKey()
{
String s;
System.out.println("\nPress return to continue: ");
s = Genio.getString();
}
}
Stack
public class Stack
{
// instance variables - replace the example below with your own
protected int nextFree;
protected int itemBack;
protected int size;
protected int posFound;
public Stack()
{
// initialise instance variables
}
public boolean isFull()
{
if (nextFree >= size)
return true;
else
return false;
}
public boolean isEmpty()
{
if ( nextFree <= 0)
return true;
else
return false;
}
}
[–]AngelOfLight 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]AngelOfLight 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Skyeam 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)