Can you please write a JavaFX program following ALL of the guidlines below? Prob
ID: 665937 • Letter: C
Question
Can you please write a JavaFX program following ALL of the guidlines below?
Problem Description: In this JavaFX program you are going to create a simple interface that demonstrates the use of a stack graphically. The user will be able to push and pop values from a stack, and the UI will show the contents of the stack as it is being changed. The application serves as a simple tutorial on the use of stacks.
Objectives To use a Generic Stack to create an application that functions as a simple tutorial to the use of a stack (push and pop).
Language features used Generics, Stack class Problem Description In this project you are going to create a simple interface that demonstrates the use of a stack graphically. The user will be able to push and pop values from a stack, and the UI will show the contents of the stack as it is being changed. The application serves as a simple tutorial on the use of stacks.
Implementation requirements
(1) The first thing you must do is add a generic stack class to your application. This class will serve as the model (data store) for this project.
(2) Create an appropriate UI to serve as your demo of how stacks work. Include a push and pop button to the UI. When the push is clicked, a value should be added to the stack and when the pop button is clicked, the top value should be removed. You do not have to remove the control, just its value. Make sure you use a context-aware design in order to guide the user's interaction. This means that upon start-up or when the stack is empty, the user should not be allowed to pop, and when the stack is full they should not be allowed to push. You decide how many cells you want to have in your stack (4-6) should sufce, since this is only a tutorial. The top of the stack should be made clear, either by using a label or color or both.
(3) The push/pop operations should update the model (Generic Stack) and then the UI should be updated by displaying the contents of the Stack. It is tempting to simply update the UI and bypass the Stack altogether, so don't do it.
Explanation / Answer
Below is the program.
/*-------------------------------- Stack.java --------------------------------------/
public class Stack
{
private List<string> data
private ObservableList items;
private int index = 0;
public Stack()
{
this.data = new ArrayList<string>();
this.data.add("One");
this.data.add("Two;
this.data.add("Three");
this.data.add("Four");
this.data.add("Five");
this.data.add("Six");
this.items = FXCollections.observableArrayList();
}
public List<string> getItems(){
return this.items;
}
public void push(){
if(this.index < this.data.size())
{
this.items.add(this.data.get(this.index));
this.index++;
}
Collections.reverse(this.items);
}
public void pop(){
if(index > 0){
this.items.remove(index);
}
}
}
//------------------------------ main.java-------------------------------
//--------------------------------------
public class StackFx extends Application
{
private Stack stack = new Stack();
private ListView<string> list = new ListView();
@override
public void start(Stage primryStage){
list.setItems(stack.getItems());
Button pushBtn = new Button("Push")
pushBtn .setOnAction(e->btnPush());
Button popBtn = new Button("Pop")
popBtn .setOnAction(e->btnPop());
StackPane pane = new StackPane();
pane.getChildren().add(this.list);
pane.getChildren().add(pushBtn);
pane.getChildren().add(popBtn);
Scene scene = new Scene(pane, 200, 200);
primaryStage.setScene(scene);
primaryStage.setTitle("Stacker");
primaryStage.Show();
}
public void btnPush(){
this.stack.push();
this.list.getSelectionModel().set(0);
}
public void btnPop(){
this.stack.popup();
}
public static void main(String[] args){
launch(args);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.