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...
    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) 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...
    Using Java, write the the following code. Only the bold needs to be answered Here is...
    Using Java, write the the following code. Only the bold needs to be answered Here is the driver: package assign public class A3Driver { public static void main(String[] args) { //Test GSUStudent object GSUStudentWorker gsw = new GSUStudentWorker("Bunny","Bugs", 2000, 9, 8, 9001234); gsw.setGPA(3.7); gsw.setHourlyRate(10.00); gsw.setTotalHours(30); System.out.println(gsw); GSUStudentWorker gsw2 = new GSUStudentWorker("Bunny","Betty", 1999, 9, 8, 9002413); gsw2.setGPA(4.0); gsw2.setHourlyRate(10.00); gsw2.setTotalHours(45); System.out.println(gsw2); //Test GSUStoreSupervisor GSUStoreSupervisor gss = new GSUStoreSupervisor("Duck","Daffy", 1980, 9, 8); gss.setMonthlyPay(4000); System.out.println(gss); //test GSUSuperStudent GSUSuperStudent gsuper = new GSUSuperStudent("Mouse","Minny", 1990, 9,...
    Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
    Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
    To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and...
    To write a Generic Collection class for Stack<E>, using the generic Node<E> from Lab 5, and test it using a stack of Car, Integer, and String Stack<E> For Programming Lab 6, you are to write your own Generic Collection for a Stack that will use your Node<E> from Lab 5 UML for Stack<E> Stack<E> - top : Node<E> - numElements : int + Stack( ) + push( element : E ) : void + pop( ) : E + size(...
    ADVERTISEMENT
    ADVERTISEMENT
    ADVERTISEMENT