Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Need help please!

  1. #1
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Need help please!

    Hello All! I am working on the following assignment: An attempt to moveOn or backUp or to evaluate seesCD when it is illegal causes an abrupt System.exit(0) without explanation. The user would appreciate a tracing output in such cases. Revise these three methods to call a private method that explains the problem (with showMessageDialog) and then terminates.


    I wrote the following code:

    import java.util.*;
    import java.awt.*;
    import javax.swing.*;

    public class Vic extends Object
    {
    private static final String NONE = "0";
    /////////////////////////////////
    private String itsSequence = "";
    private int itsPos = 1;
    private final int itsID; // assigned by the constructor
    private void trace (String action)

    {

    System.out.println ("Vic# " + itsID + ": " + action

    + itsPos + "; sequence= " + itsSequence);

    } //======================



    public void backUp()

    {

    if (itsPos == 1)

    error("Could not backUp");

    itsPos--;

    trace ("backUp to slot ");

    } //======================



    public void moveOn()

    {

    if ( ! seesSlot())

    error("Could not moveOn");

    itsPos++;

    trace ("moveOn to slot ");

    } //======================



    public boolean seesSlot()

    {

    return itsPos < itsSequence.length();

    } //======================



    public boolean seesCD()

    {

    if ( ! seesSlot())

    error("Can't see CD, there is no slot");

    String s = itsSequence.substring (itsPos, itsPos + 1);

    return ! s.equals (NONE);

    } //======================



    private void error(String message)

    {

    JOptionPane.showMessageDialog(null, "ERROR: " + message);

    System.exit(0);

    }
    }


    When I compile I receive the following error message:

    "variable itsID might not have been initialized".
    and this line is highlighted."public class Vic extends Object"

    Any help would be greatly appreciated! Thank you!

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Need help please!

    When posting code, *please* use the highlight tags to preserve formatting.

    As for your error, when do you initialize the itsID variable?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Feb 2013
    Posts
    30
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help please!

    Thank you for the info! I played around with my code and I figured out my problem. Thanks!

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Need help please!

    Quote Originally Posted by Techstudent View Post
    Thank you for the info! I played around with my code and I figured out my problem. Thanks!
    Please try to remember to mark your threads as "Solved" when your questions have been answered.
    Thread marked Solved.