#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0

    Exclamation I need help with this question for my assignment


    Question:
    The back button of a typical web browser works by keeping track of all websites visited and reproducing the websites in a first-in-last-out fashion when clicked. Write an application called WebBackButtonApp that will emulate a simple back button functionality of a web browser. The program will do the following:
    a. Prompt the user to enter the number of websites visited. The value will range from 1 (inclusive) to 10 (inclusive).
    b. Prompt the user to enter the URL of each website visited. For each URL entered, push the URL into a stack.
    c. Prompt the user to enter the number of times the back button is clicked. The value will range from 1 (inclusive) to 10 (inclusive).
    d. Emulate the back button by displaying the corresponding URL for each click.
     When there are no more items left in the stack, display the URL as Empty. This scenario will happen if the number of websites visited is less than the number of times the back button is clicked.
     You can refer to the samples below for the expected outputs.


    My Answer:
    Main class
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package webbackbuttonapp;
    import java.util.Scanner;
    /**
    *
    * @author Andrea
    */
    public class WebBackButtonApp {

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc = new Scanner(System.in);

    //Create a Stack object
    Stack myStack = new Stack(10);

    //a. Prompt the user to enter the number of websites visited.
    //The value will range from 1 (inclusive) to 10 (inclusive).
    System.out.print("Number of websites visited (from 1 to 10): ");
    int web = sc.nextInt(); sc.nextLine();

    //b. Prompt the user to enter the URL of each website visited.
    // For each URL entered, push it into a Stack
    String url = new String();
    for (int i=0; i< web; i++)
    {
    System.out.print("Enter the URL: ");
    url = sc.nextLine();
    myStack.push(url);

    }

    //Prompt the user to enter the number of times the back button is clicked.
    // The value will range from 1(inclusive) to 10(inclusive)
    //Emulate the back button by displaying the corresponding URL for each click.
    //When there are no more items left in the stack, display the URL as Empty.
    //This scenario will happen if the number of websites visited is less than
    //the number of times the back button is clicked.



    System.out.print("Enter the number of times the back button is clicked: ");
    int click = sc.nextInt();sc.nextLine();
    for(int i=0; i< web; i++)
    {
    if(click==1)
    {
    myStack.pop();
    System.out.println("Websites Visited: " + myStack.peek());
    }

    if(click==2)
    {
    myStack.pop();
    myStack.pop();
    System.out.println("Website Visited: " );
    }


    }












    }
    }


    Stack class
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */

    package webbackbuttonapp;

    /**
    *
    * @author sllau
    */
    public class Stack {

    private int top; //index
    private Object[] items; //use array to store data in stack
    private int count;

    public Stack(int s)
    {
    items = new Object[s]; //create stack using fixed size array
    top = -1;
    count = 0;
    }


    public boolean push(Object x)
    {
    // Is the stack full?
    if (count == items.length)
    {
    return false;
    }
    else
    {
    top++; //move pointer up by 1
    items[top] = x; //store item into array location
    count++;
    return true;
    }
    }

    public Object pop()
    {
    Object result = null;
    if (count == 0) // If the stack is empty
    {
    return null;
    }
    else
    {

    result = items[top]; // remove the top element from the stack
    top--; //move pointer down by 1
    count--;
    return result; //return value of the item popped
    }
    }

    public Object peek()
    {
    if (count == 0) // If the stack is empty
    {
    return null;
    }
    else
    {
    // only return a copy of the top item
    return items[top];
    }
    }

    public boolean isEmpty()
    {
    if (count == 0) // If the stack is empty
    {
    return true; //confirmed empty
    }
    else
    {
    return false; //stack not empty
    }
    }
    public int size()
    {
    return count;
    }



    }


    I cannot seem to figure out how to continue the steps since the answers must be in this form:
    Number of websites visited: 4
    Enter the URL of #1 website:
    Enter the URL of #2 website:
    Enter the URL of #3 website:
    Enter the URL of #4 website:
    Number of times back button is clicked: 3
    #1 click:
    #2 click:
    #3 click:
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.


    What problems are you having? Please post some specific questions about your problems.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Post


    Code:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    package webbackbuttonapp;
    import java.util.Scanner;
    /**
    *
    * @author Andrea
    */
    public class WebBackButtonApp {
    
    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc = new Scanner(System.in);
    
    //Create a Stack object
    Stack myStack = new Stack(10);
    
    //a. Prompt the user to enter the number of websites visited. 
    //The value will range from 1 (inclusive) to 10 (inclusive).
    System.out.print("Number of websites visited (from 1 to 10): ");
    int web = sc.nextInt(); sc.nextLine();
    
    //b. Prompt the user to enter the URL of each website visited.
    // For each URL entered, push it into a Stack
    String url = new String();
    for (int i=0; i< web; i++)
    {
    System.out.print("Enter the URL: ");
    url = sc.nextLine();
    myStack.push(url);
    
    }
    
    //Prompt the user to enter the number of times the back button is clicked.
    // The value will range from 1(inclusive) to 10(inclusive)
    //Emulate the back button by displaying the corresponding URL for each click. 
    //When there are no more items left in the stack, display the URL as Empty. 
    //This scenario will happen if the number of websites visited is less than 
    //the number of times the back button is clicked.
    
    
    
    System.out.print("Enter the number of times the back button is clicked: ");
    int click = sc.nextInt();sc.nextLine();
    for(int i=0; i< web; i++) 
    {
    if(click==1)
    {
    myStack.pop();
    System.out.println("Websites Visited: " + myStack.peek());
    }
    
    if(click==2)
    {
    myStack.pop();
    myStack.pop();
    System.out.println("Website Visited: " );
    }
    
    
    }
    
    
    
    
    
    
    
    
    
    
    
    
    }
    }

    Stack class
    Code:
    /*
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    */
    
    package webbackbuttonapp;
    
    /**
    *
    * @author sllau
    */
    public class Stack {
    
    private int top;	 //index
    private Object[] items; //use array to store data in stack
    private int count;
    
    public Stack(int s)
    {
    items = new Object[s]; //create stack using fixed size array
    top = -1;
    count = 0;
    }
    
    
    public boolean push(Object x)
    {
    // Is the stack full?
    if (count == items.length)
    {
    return false;
    }
    else
    {
    top++; //move pointer up by 1
    items[top] = x; //store item into array location
    count++;
    return true;
    }
    }
    
    public Object pop()
    {
    Object result = null;
    if (count == 0) // If the stack is empty
    {
    return null;
    }
    else
    {
    
    result = items[top]; // remove the top element from the stack
    top--; //move pointer down by 1
    count--;
    return result; //return value of the item popped
    }
    }
    
    public Object peek()
    {
    if (count == 0) // If the stack is empty
    {
    return null;
    }
    else
    {
    // only return a copy of the top item
    return items[top];
    }
    }
    
    public boolean isEmpty()
    {
    if (count == 0) // If the stack is empty
    {
    return true; //confirmed empty
    }
    else
    {
    return false; //stack not empty
    }
    }
    public int size()
    {
    return count; 
    }
    
    
    
    }

    Im using my other acc because i cannot rembr previous pw.
    I dont know why cant i display the right output for the last part. which is Emulate the back button by displaying the corresponding URL for each click.
     When there are no more items left in the stack, display the URL as Empty. This scenario will happen if the number of websites visited is less than the number of times the back button is clicked.
     You can refer to the samples below for the expected outputs.
  6. #4
  7. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,699
    Rep Power
    347
    why cant i display the right output
    Please post the program's output and add some comments saying what the problem is.


    The code has lost all its formatting. Nested statements need to be indented to show the logic. Please edit the post and fix the formatting for the code.

IMN logo majestic logo threadwatch logo seochat tools logo