Question

In: Computer Science

Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may...

  1. Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may write this in jGrasp
    1. Create a Stack of String called, myStacks
  1. Read input from keyboard, 10 names and then add to myStacks
  1. As you remove each name out, you will print the name in uppercase along with a number of characters the name has in parenthesis. (one name per line).  
    e.g.     Kennedy (7)
  1. Using existing Stack Java Collection Framework, write Java Code segment to do the following.   You may write this in jGrasp
    1. Create a Stack of Integer called, myScores
  1. Read input from keyboard, you will ask the user to enter 5 integer-scores and then add to myScores
  1. In the end, you will remove each score and print on the screen, TAB separated. Then print sum and average of all 5 numbers.

ENHANCE this part by allowing the user to enter as many scores as they want, until they enter -1 to stop.

  1. Show the effect of the following stack operations (step-by-step), assuming you begin with an empty stack.

push   2

push   3

push   1

pop

pop

push   5

push   4

pop

push   6

push   9

push   7

pop

push   1

pop

pop

Note that because we are typing information so use left side line to be the bottom of your stack


    Show stack (left side is bottom of the stack)

    Show what is written by the following segment of code (i.e. output on the screen), given that item1, item2, and item3 are int variables, and stack is an object that fits our abstract description of a stack.  
    (Assume that you can store and retrieve variables of type int on stack.)

    int item1 = 4;

    int item2 = 2;

    int item3 = -3;

    stack.push(item2);

    stack.push(item1);

    stack.push(item1 + item3);

    item2 = stack.peek();

    stack.push (item3 - item3);

    stack.push(item2);

    item1 = stack.peek();

    stack.push(item1 + item3);

    stack.push(3);

    item1 = stack.pop();

    System.out.println(item1 + " " + item2 + " " + item3);

    while (!stack.empty())

    {

    item1 = stack.pop();

    System.out.println(item1);

    }

    Output on the screen:

    Solutions

    Expert Solution

    Answer for part 1

    import java.util.*; 
    import java.io.*; 
      
    public class Main { 
        public static void main(String args[]) 
        { 
            Stack<String> myStacks = new Stack<String>(); 
            Scanner sc = new Scanner(System.in);
            for(int i=0;i<2;i++){
                System.out.print("Enter Name:");
                String name = sc.nextLine();
                myStacks.push(name); 
            }
            
            System.out.println("\nThe names and their lengths are: ");
            while(!myStacks.empty()){
                String name = myStacks.pop().toString();
                System.out.println("Name:"+name+ " Length:"+name.length());
            }
        } 
    }

    Answer for part 2

    import java.util.*; 
    import java.io.*; 
      
    public class Main { 
        public static void main(String args[]) 
        { 
            Stack<Integer> myScores = new Stack<Integer>(); 
            Scanner sc = new Scanner(System.in);
            System.out.println("\nEnter 5 integers: ");
            for(int i=0;i<5;i++){
                System.out.print("Enter integer:");
                int num = sc.nextInt();
                myScores.push(num); 
            }
            
            System.out.println("\nThe scores are: ");
            int sum = 0;
            while(!myScores.empty()){
                int thisscore = myScores.pop();
                System.out.print(thisscore+"\t");
                sum = sum + thisscore;
            }
            System.out.println("\nSum of all:"+sum);
            System.out.println("Average:"+(sum/5));
        } 
    }

    If you have any doubt get back through comments


    Related Solutions

    Using the provided Java program below, complete the code to do the following. You may need...
    Using the provided Java program below, complete the code to do the following. You may need to create other data items than what is listed for this assignment. The changes to the methods are listed in the comments above the method names. TODO comments exist. Apply any TODO tasks and remove the TODO comments when complete. Modify the method findMyCurrency() to do the following:    a. Use a do-while loop b. Prompt for a currency to find. c. Inside this...
    Java Code using Stack Write a program that opens a text file and reads its contents...
    Java Code using Stack Write a program that opens a text file and reads its contents into a stack of characters, it should read character by character (including space/line change) and push into stack one by one. The program should then pop the characters from the stack and save them in a second text file. The order of the characters saved in the second file should be the reverse of their order in the first file. Ex input file: Good...
    In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...
    In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries. Assume your data structure only allows Strings. Implement the following operations for the data structure: Queue: enqueue, dequeue, create, isEmpty (10 points) Stack: push, pop, create, isEmpty (10 points) Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.) Upload a zip file with one implementation for...
    JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
    JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
    Write a java code segment to declare an array of size 10 of type String and...
    Write a java code segment to declare an array of size 10 of type String and read data for them from a file, prompt user for the file name. Data is stored in a file with 1 string per line.
    How do you implement stack by using linked list? No code just explain it.
    How do you implement stack by using linked list? No code just explain it.
    JAVA: Use existing Java Stack class to solve the "Balancing Symbols" problem. The symbols are (),...
    JAVA: Use existing Java Stack class to solve the "Balancing Symbols" problem. The symbols are (), [], and {}, and each opening symbol must have a corresponding closing symbol as well as in correct order. Ignore operands and arithmetic operators since they are not relevant to our problem. You can assume each token is separated by spaces. For example: { ( a + b ) * c1 } – valid { ( a + b ) * c1 ] –...
    (JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
    (JAVA) Why do we need a dynamic stack? How do you implement a dynamic stack array?
    Write assembly code for the following machine code. Assume that the segment is placed starting at...
    Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual memory addresses represented by such labels. 0010 1010 0000 1000 0000 0000 0000 1010 0001 0001 0000 0000 0000 0000 0000 0010 0000 0010 0001 0001 1000 0000 0010 0000 0000 1000 0000 0000 0100 1110 0010 0101 0000 0010 0001 0010 1000 0000 0010 0000
    a. Write machine code for the following assembly code. Assume that the segment is placed starting...
    a. Write machine code for the following assembly code. Assume that the segment is placed starting at location 80000. Use decimal numbers to represent each instruction. loop:         beq $s3, $s1, endwhile                  add $t0, $s3, $s4                  lw $t1, 0($t0)                  add $s0, $s0, $t1                  addi $s3, $s3, 4                  j loop endwhile: b. Write assembly code for the following machine code. Assume that the segment is placed starting at location 80000. Create labels for jump and branch instructions. Indicate the actual...
    ADVERTISEMENT
    ADVERTISEMENT
    ADVERTISEMENT