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 2 of 2

Thread: Wierd problem

  1. #1
    Junior Member
    Join Date
    Sep 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Wierd problem

    Hi! Im all new to this forum!

    I have an assignment where Im working with linking classes together.

    After that I'm supposed to build a method that lower a cricle when price lowered and make it go up when the price is raised.

    I wrote everything and it works fine when lowering the price, but when raising the price I can se my circle for a millisec in a new place, and then it appears in the same place as before when price is lowered.
    Method that runs to create the problem is "setPris".

    I can't figure out whats the problem Ive tried everything....Our techer gave us a hint with using a temporary variable to hold the oject itself, but I think ive done it the correct way..

    Please help, would mean a lot

    Picture Class
    /**
     * This class represents a simple picture. You can draw the picture using
     * the draw method. But wait, there's more: being an electronic picture, it
     * can be changed. You can set it to black-and-white display and back to
     * colors (only after it's been drawn, of course).
     *
     * This class was written as an early example for teaching Java with BlueJ.
     * 
     * @author  Michael Kolling and David J. Barnes
     * @version 2008.03.30
     */
    public class Picture
    {
        private Circle mPriceChange;
        /**
         * Constructor for objects of class Picture
         */
        public Picture()
        {
     
            // nothing to do... instance variables are automatically set to null
        }
     
        /**
         * Draw this picture.
         */
        public void draw()
        {
            mPriceChange = new Circle();
            mPriceChange.makeVisible();
     
        }
        /**
         * Move Circle down 
         */
        public void nerPris()
        {
            mPriceChange.moveVertical(200);
     
        }
        /**
        * Moves Circle upp
        */
        public void uppPris()
        {
            mPriceChange.moveVertical(-200);
     
        }
    }

    Goods Class

    /**
     * Beskrivning av klassen Goods
     * 
     * @author Maritha Dahlin
     * @version 2010-08-01
     */
    public class Goods
    {
    private String mBeskrivning;
    private int mAntal;
    private int mPris;
    private int mLagervarde;
    private String mNyavaror;
    private int mKostnad;
    private Picture priceshift;
     
    /**
    * Konstruktor för att tilldela initialvärden vid skapande av en ny instans av klassen.
    * @param String beskrivning
    * @param int pris
    * @param int antal
    */
    public Goods(String beskrivning, int pris, int antal)
    {
    mBeskrivning = beskrivning;
    mAntal = antal;
    mPris = pris;
    }
     
    /**
    * Returnerar lagrat värde i variabeln mBeskrivning
    * @return     lagrat värde i mBeskrivning
    */ 
    public String getBeskrivning()
    {
    return mBeskrivning;
    }
    /**
    * Returnerar lagrat värde i variabeln mPris
    * @return     lagrat värde i mPris
    */ 
    public int getPris()
    {
    return mPris;
    }
    /**
    * Returnerar lagrat värde i variabeln mAntal
    * @return     lagrat värde i mAntal
    */ 
    public int getAntal()
    {
    return mAntal;
    }
    /**
    * Tilldelar värdet för variabeln mBeskrivning med det inkommande värdet i inparametern beskrivning
    * @param String beskrivning - ny beskrivning
    */ 
    public void setBeskrivning(String beskrivning)
    {
    mBeskrivning = beskrivning;
    }
    /**
    * Tilldelar värdet för variabeln mAntal med det inkommande värdet i inparametern antal
    * @param int antal - nytt antal
    */ 
            public void setAntal(int antal)
    {
            mAntal = antal;
    }
    /**
    * Tilldelar värdet för variabeln mPris med det inkommande värdet i inparametern pris
    * @param int pris - nytt pris
    */ 
    public void setPris(int pris)
    {
    priceshift = new Picture();
     
    if(mPris > pris)
    {
    priceshift.draw();
    priceshift.nerPris();
    mPris = pris;
    }
    else
    {
    priceshift.draw();
    priceshift.uppPris();
    mPris = pris;
    }
     
    }
    /**
    * Kontrollerar om varan finns i lager, skriver ut text på skärmen beroende på värde i mAntal.
    */ 
     
    public void isInStorage()
    {
    if(mAntal > 1){
    System.out.println("Varan " +mBeskrivning + " finns i lager!");      
    }
    else {
    System.out.println("Varan " +mBeskrivning + " finns INTE i lager!");
    }
    }
    /**
     * 
     */
    public void countaLagerVarde()
    {
    mLagervarde = (mAntal * mPris);
    System.out.println("Lagervärdet för " + mAntal + " st " + mBeskrivning + " á-pris " + mPris + " är " + mLagervarde + " kronor.");
    if(mAntal > 3){
     
    }
    else{
    orderGoods();
    System.out.println(mNyavaror);
    }
    }
    public String orderGoods()
    {
    mNyavaror = "Varning! Dags att beställa nya varor!";
    return mNyavaror;
    }
    public void kopGoods(int antal)
    {
    mKostnad = (antal * mPris);
    mAntal = (mAntal - antal);
     
    System.out.println("Du har köpt " + antal + " st " + mBeskrivning + " á-pris " + mPris +". Kostnaden blir " + mKostnad 
    + ". Det är " + mAntal + " st kvar i lager.");
    if(mAntal > 3){
     
    }
    else{
    orderGoods();
    System.out.println(mNyavaror);
    }
     
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Wierd problem

    I can se my circle for a millisec in a new place, and then it appears in the same place as before when price is lowered
    What variable controls where the circle is shown? Where is the value of the variable changed?
    Try debugging your code by adding print outs to show the value of the variable every time it is changed and every time it is used to draw the circle.
    From the output you should be able to see where the code is changing the value incorrectly.