Results 1 to 6 of 6

Thread: Method help

  1. #1
    Army is offline Senior Member
    Join Date
    Jan 2012
    Posts
    147
    Rep Power
    0

    Default Method help

    Does this method do what the question asks? The reason why I can't run it is because I don't understand my own method to be honest. I don't know what Vic one and Vic two do. Lol.

    Exercise 3.33** Write a method public void shiftOne (Vic one, Vic two) for a subclass of Vic: At each position where the executor has an empty slot and either of the two Vic parameters has a filled slot in the orresponding position, shift the CD to the executor's slot. When a choice is possible, take a CD from the first parameter's slot.

    Java Code:
    public void shiftOne(Vic one, Vic two){
                String thisSpot = this.getPosition();
                String oneSpot = one.getPosition();
                String twoSpot = two.getPosition();
               
                while (seesSlot() && one.seesSlot() && two.seesSlot()){
                    moveOn();
                    one.moveOn();
                    two.moveOn();
                    if(!seesCD() && one.seesCD()){
                        String objPosition = getPosition();
                        one.takeCD();
                        while (! objPosition.equals(thisSpot)){
                            backUp();
                        }
                        one.putCD();
                    }
                    if(!seesCD() && two.seesCD()){
                        String objPosition = getPosition();
                        two.takeCD();
                        while (! objPosition.equals(thisSpot)){
                            backUp();
                        }
                        two.putCD();
                    }
                    
                }
            }

  2. #2
    nelin15 is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Method help

    hey i edited your code so it could be compiled:)
    public class ShiftOne extends Vic.

    {public void ShiftOne (Vic one, Vic two)

    {String thisSpot = this.getPosition();
    String oneSpot = one.getPosition();
    String twoSpot = two.getPosition();

    while (seesSlot() && one.seesSlot() && two.seesSlot()){
    moveOn();
    one.moveOn();
    two.moveOn();
    if(!seesCD() && one.seesCD()){
    one.takeCD();
    while (! this.getPosition().equals(thisSpot))
    backUp();
    }
    one.putCD();
    }
    if(!seesCD() && two.seesCD())
    two.takeCD();
    while (! two.getPosition().equals(thisSpot))
    backUp();

    two.putCD();
    }
    }

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: Method help

    Really? What is public void ShiftOne(Vic one, Vic two)? Where is the Vic super class?

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    nelin15 is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Method help

    It is a method, public void ShiftOne (Vic one, Vic two) is the name. You need the public void in order for it create a method. I used ShiftOne but it could be anything. The ( Vic one, Vic two) is needed because there are two parameters in this method. The Vic super class is below.
    Hope i helped- Ne

    public class Vic
    {
    private static Stack theStack = new Stack(); // where spare CDs are kept
    private ArrayList itsSequence; // its slots
    private int itsPos; // 0,1,2,...; 0 is at front
    private int itsID; // 1 for first Vic, 2 for...


    /* QUERY METHODS */

    /** Return the current position as a String value. */

    public String getPosition()
    { return itsID + ", " + itsPos;
    } //======================


    /** Tell whether there is a slot at its position. */

    public boolean seesSlot()
    { return itsPos < itsSequence.size();
    } //======================


    /** Tell whether there is a CD in its current slot. */

    public boolean seesCD()
    { if (! seesSlot())
    fail ("Can't see a CD where there is no slot!");
    return itsSequence.get (itsPos) != null;
    } //======================


    /** Return the CD that is in its current slot. */

    public String getCD()
    { if (! seesSlot())
    fail ("There is no slot to get a CD from!");
    return (String) itsSequence.get (itsPos);
    } //======================


    /** Tell whether the stack has any CDs available. */

    public static boolean stackHasCD()
    { return ! theStack.isEmpty();
    } //======================


    /* ACTION METHODS */

    /** Move forward to the next slot in the sequence. */

    public void moveOn()
    { if (! seesSlot())
    fail ("Already at the end of the sequence!");
    itsPos++;
    trace ("moveOn to slot " + (itsPos + 1));
    } //======================


    /** Back up to the previous slot in the sequence. */

    public void backUp()
    { if (itsPos == 0)
    fail ("Already at the front of the sequence!");
    itsPos--;
    trace ("backUp to slot " + (itsPos + 1));
    } //======================


    /** Move a CD from the stack to the current slot. */

    public void putCD()
    { if (! seesCD() && stackHasCD())
    itsSequence.set (itsPos, theStack.pop());
    trace ("putCD at slot " + (itsPos + 1));
    } //======================


    /** Move a CD from the current slot to the stack. */

    public void takeCD()
    { if (seesCD())
    { theStack.push (itsSequence.get (itsPos));
    itsSequence.set (itsPos, null);
    }
    trace ("takeCD at slot " + (itsPos + 1));
    } //======================


    /** Terminate the program with an appropriate message. */

    private void fail (String cause)
    { JOptionPane.showMessageDialog (null, "STOPPING: " + cause
    + " (Vic #)" + itsID + ", position =" + itsPos);
    System.exit (0);
    } //======================


    /** Two convenience methods */

    public void shiftFromSlotToStack()
    { takeCD();
    } //======================

    public void shiftFromStackToSlot()
    { putCD();
    } //======================


    /* METHODS THAT USE THE FRAME */

    private static String vicSay = "Programmable CD Organizer "
    + " mfd by Jones & Co.";
    private static final VicFrame theFrame = new VicFrame();
    //////////////////////////////////


    public static void say (String message)
    { vicSay = message;
    theFrame.repaint();
    } //======================


    /** Print a trace of the Vic's action. */

    private void trace (String message)
    { System.out.println (message + " for Vic #" + itsID);
    theFrame.repaint();
    pause (500); // half-a-second between actions
    } //======================


    /** Pause for the specified number of milliseconds. */

    private static void pause (int milliseconds)
    { try
    { Thread.sleep (milliseconds);
    }
    catch (InterruptedException e)
    { // never happens
    }
    } //======================


    /* THE INITIALIZER AND CONSTRUCTOR METHODS */

    private static final int MAXSLOTS = 8;
    private static final int MINSLOTS = 3;
    private static final int MAXVICS = 4;
    private static final Random random = new Random();
    private static int theMaxVics = random.nextInt (MAXVICS) + 1;
    private static ArrayList[] theSeq = new ArrayList[theMaxVics];
    private static int theNumVics = 0;
    private static final Vic[] theVics = {null, null, null, null};
    //////////////////////////////////


    /** Initialize individual sequences and stacks. An initializer method
    is used because these have to exist before any Vics are created. */

    static
    { for (int k = 0; k < theMaxVics; k++)
    { theSeq[k] = new ArrayList();
    int numSlots = random.nextInt (MAXSLOTS - MINSLOTS + 1)
    + MINSLOTS;
    for (int i = 0; i < numSlots; i++)
    { String it = random.nextInt (2) == 0 ? null
    : "" + (char) (i + 'a') + (k + 1);
    theSeq[k].add (it);
    }
    }

    // start with up to 2 CDs on the stack
    if (random.nextInt (3) > 0) // 2 times out of 3
    { theStack.push ("GarthB");
    if (random.nextInt (2) == 0) // 1 time out of 3
    theStack.push ("LyleL");
    }
    } //======================


    /** Construct a new Vic. */

    public Vic()
    { super();
    itsSequence = theNumVics < theMaxVics
    ? theSeq[theNumVics] : new ArrayList();
    itsPos = 0;
    itsID = theNumVics + 1;
    theVics[theNumVics] = this;
    theNumVics++;
    trace ("construction");
    } //======================


    /** Replace random arrangement by user-specified arrangement. */

    public static void reset (String[] args)
    { if (args.length > 0 && theNumVics == 0)
    { theMaxVics = Math.min (args.length, MAXVICS);
    theSeq = new ArrayList[theMaxVics];
    for (int k = 0; k < theMaxVics; k++)
    { theSeq[k] = new ArrayList();
    int longest = Math.min (args[k].length(), MAXSLOTS);
    for (int i = 0; i < longest; i++)
    { String it = args[k].charAt (i) == '0' ? null
    : "" + (char)(i + 'a') + (k + 1);
    theSeq[k].add (it);
    }
    }
    }
    } //======================


    // THE NESTED FRAME CLASS

    static class VicFrame extends JFrame
    { private final int SLOT = 75; // between CD slots
    private final int EDGE = 10; // leave free at left side
    private final int WIDTH = (MAXSLOTS + 2) * SLOT + 2 * EDGE;
    private final int DIST = 60; // between CD sequences
    private final int SAY = 45; // depth of say's output
    private final int TOPSEQ = SAY + DIST; // depth of first seq


    public VicFrame()
    { addWindowListener (new Closer());
    setSize (WIDTH, TOPSEQ + MAXVICS * DIST + 2 * EDGE);
    setBackground (new Color (255, 252, 224)); // a nice cream
    setVisible (true); // make it visible to user
    } //======================


    /** Same as for an applet; called by repaint. */

    public void paint (Graphics page)
    { // PRINT THE vicSay MESSAGE AT THE TOP
    page.setColor (getBackground());
    page.fillRect (EDGE, EDGE, WIDTH - 2 * EDGE,
    TOPSEQ + MAXVICS * DIST);
    page.setColor (Color.white);
    page.fillRect (20, SAY - 20, WIDTH - 40, 20);
    page.setColor (new Color (0, 96, 0)); // a light green
    page.drawString (vicSay, 25, SAY - 5); // message

    // DRAW UP TO FOUR Vic SEQUENCES AND THE STACK
    for (int k = 0; k < theMaxVics; k++)
    drawSequence (page, k, TOPSEQ + k * DIST);
    page.setColor (Color.red);
    int y = TOPSEQ + MAXVICS * DIST;
    page.drawString ("stack", EDGE, y);
    page.fillRect (EDGE, y - 25, 40, 5); // dividing line
    for (int k = 0; k < theStack.size(); k++)
    page.drawString ((String) theStack.get (k),
    EDGE, y - 30 - k * 20);
    } //======================


    /** Called by VicFrame's paint method. */

    private void drawSequence (Graphics page, int index, int y)
    { page.setColor (Color.red);
    if (theVics[index] != null)
    drawMacMan (page, theVics[index].itsPos, y - 15);
    page.setColor (Color.blue);
    drawAllCDs (page, y, theSeq[index]);
    } //======================


    private void drawAllCDs (Graphics page, int y,
    ArrayList slots)
    { int atEnd = slots.size();
    for (int n = 0; n < atEnd; n++)
    { String it = (String) slots.get (n);
    page.drawString (it == null ? "---" : it,
    (n + 1) * SLOT + EDGE, y);
    }
    page.drawString ("END", (atEnd + 1) * SLOT + EDGE, y);
    } //======================


    private void drawMacMan (Graphics page, int pos, int y)
    { // <x, y> is the lower-left corner of the stick figure
    int x = pos * SLOT + EDGE + 78;
    page.setColor (Color.black);
    page.drawLine (x, y, x + 6, y - 6); // leg
    page.drawLine (x + 6, y - 6, x + 12, y); // leg
    page.drawLine (x + 6, y - 6, x + 6, y - 18); // body
    page.drawLine (x, y - 14, x + 12, y - 14); // arms
    page.drawOval (x + 1, y - 28, 10, 10); // head
    page.drawLine (x + 4, y - 25, x + 5, y - 25); // eye
    page.drawLine (x + 7, y - 25, x + 8, y - 25); // eye
    page.drawLine (x + 3, y - 22, x + 9, y - 22); // mouth
    } //======================
    } // end of VicFrame class


    private static class Closer extends java.awt.event.WindowAdapter
    {
    public void windowClosing (java.awt.event.WindowEvent e)
    { System.exit (0);
    } //======================
    }
    }

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,514
    Rep Power
    5

    Default Re: Method help

    I know its a method. My question was rhetorical. But you named it wrong. It should be shiftOne according to the OP (note the lower case 's').

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    nelin15 is offline Member
    Join Date
    Dec 2013
    Posts
    3
    Rep Power
    0

    Default Re: Method help

    Because I am an expert at knowing which questions are rhetorical questions. I appreciate you correcting me, oops.

Similar Threads

  1. Replies: 2
    Last Post: 03-23-2012, 04:53 AM
  2. Replies: 1
    Last Post: 10-27-2011, 07:16 AM
  3. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  4. ArrayLists compareTo method, equals method
    By random0munky in forum New To Java
    Replies: 2
    Last Post: 10-26-2009, 07:20 PM
  5. Replies: 29
    Last Post: 09-25-2008, 07:55 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •