Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- import java.util.Stack;
- /**
- * PROBLEM *
- Write a program to insert 5 float values in a dynamic list.
- Now take user input for a variable named “option”.
- If the option is 1 then insert another value.
- If option is 2 delete the top value from the list.
- If the option is 3 just output the top value.
- Apply stack list to implement the above-mentioned solution and use proper stack methods.
- **/
- public class Problem1 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Stack<Float> stack = new Stack<Float>();
- // Inserting 5 values in a dynamic list (Stack)
- System.out.print("Enter 5 float values: ");
- for (int i = 0; i < 5; i++) {
- stack.push(scanner.nextFloat());
- }
- System.out.println("Dynamic list: " + stack + "\n\n");
- // Taking user input in varriable "option"
- System.out.print("Enter your option (1/2/3): ");
- int option = scanner.nextInt();
- switch (option) {
- case 1:
- System.out.print("User option is 1. So enter a value to insert into the list: ");
- stack.push(scanner.nextFloat());
- System.out.println("\n" +"Dynamic list: " + stack + "\n\n");
- break;
- case 2:
- System.out.println("User option is 2. So deleting top value from the list... ");
- stack.pop();
- System.out.println("\n" + "Dynamic list: " + stack + "\n\n");
- break;
- case 3:
- System.out.print("User option is 2. So top value is : " + stack.peek());
- break;
- }
- scanner.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement