Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7696785
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T21:51:39+00:00 2026-05-31T21:51:39+00:00

I have a problem and I am stack. If anyone have a spare moment

  • 0

I have a problem and I am stack. If anyone have a spare moment it would be great. I am trying to pass values from other methods getters which are in other class but it is not passing the values of the variables. For example in my class Chair in the method toString I am expecting to print the values that I have assigned in the Class FurnitureItem. Chair is a subclass of FurnitureItem. I am setting the values with setter methods and when i try to retrieve them it is printing the old values.
All this is happening when the button addChair is pressed. The class with the main method is RunFurniture and the Panel and the GUI of the program is in PanelFurniture. When the program is runned and the button addChair is pressed we enter and idNum which is int type of wood which is char w or o quantity which is int and we select armrest or not boolean. The program is compiling and running just is not doing what I am expecting it to do. I am using eclipse. Here is the code.

/** This is the driver class of the program.
 * Here is the main method with the JFrame.
 * class name RunFurniture.class
 * @author Kiril Anastasov
 * @date 18/03/2012
 */

import java.awt.*;
import javax.swing.*;
public class RunFurniture 
{

    /**
     * @param args
     */
    public static void main(String[] args) 
    {

        JFrame application = new JFrame();
        PanelFurniture panel = new PanelFurniture();
        application.add(panel);
        application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        application.setSize(650,650);
        application.setLocationByPlatform(true);
        application.setVisible(true);

    }

}

Here is the GUI of the program.

/** Here is the GUI of the program.
 * class name PanelFurniture.class
 * @author Kiril Anastasov
 * @date 18/03/2012
 */

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class PanelFurniture extends JPanel implements ActionListener
{
    //FurnitureItem my = new FurnitureItem("");  the class is abstract so you cannot make an object from it.

    JButton center, east;
    JButton[] commandButtons = {
                                 new JButton(" Add Chair"),
                                 new JButton(" Add Table"),
                                 new JButton(" Add Desk "),
                                 new JButton(" Clear All   "),
                                 new JButton("Total Price   "),
                                 new JButton("   Save       "),
                                 new JButton("   Load       "),
                                 new JButton("Summary ")
                                                        };
    JLabel  desks;
    JLabel[] chairsAndTables  = {
                                    new JLabel("Possition 1"),
                                    new JLabel("Possition 2"),
                                    new JLabel("Possition 3"),
                                    new JLabel("Possition 4"),
                                    new JLabel("Possition 5"),
                                    new JLabel("Possition 6")};

    protected ImageIcon[] image = { new ImageIcon("Pictures/Chair1.png"),
                                    new ImageIcon("Pictures/Chair2.png"),
                                    new ImageIcon("Pictures/Desk1.png"),
                                    new ImageIcon("Pictures/Desk2.png"),
                                    new ImageIcon("Pictures/Desk3.png"),
                                    new ImageIcon("Pictures/Desk4.png"),
                                    new ImageIcon("Pictures/Table1.png"),
                                    new ImageIcon("Pictures/Table2.png")};

    JPanel centerPanel, westPanel, eastPanel, leftPanel, rightPanel;

    PanelFurniture()
    {

        this.setLayout(new BorderLayout());


         westPanel = new JPanel();
         westPanel.setLayout(new BoxLayout(westPanel, BoxLayout.PAGE_AXIS));

        for(int i=0; i<commandButtons.length; i++)      
        {
            westPanel.add(commandButtons[i]);
            commandButtons[i].addActionListener(this);

        }

        this.add(westPanel, BorderLayout.WEST);

        // split the panel by 2     
        centerPanel = new JPanel(new GridLayout(1,2));

        // split the left half for the chairs and the tables
        leftPanel = new JPanel(new GridLayout(3,2));

        for(int j=0; j<chairsAndTables.length; j++)
        {       
            leftPanel.add(chairsAndTables[j]);
        }
        centerPanel.add(leftPanel);

        //split the right part of the middle for the desks
        rightPanel = new JPanel(new GridLayout(3,1));

        desks = new JLabel(image[2]);
        rightPanel.add(desks);
        desks = new JLabel("2");
        rightPanel.add(desks);
        desks = new JLabel("3");
        rightPanel.add(desks);
        centerPanel.add(rightPanel);



        this.add(centerPanel, BorderLayout.CENTER);


    }

    public void actionPerformed(ActionEvent ae)
    {
        if(ae.getSource() == commandButtons[0])
        {
            Chair myChair  = new Chair(); // create an object of type Chair
            myChair.setIdNum(0); // ask the user for the Id of the chair
            System.out.println(myChair.getIdNum()); // print the id of the chair

            myChair.setTypeOfWood('w');
            System.out.println(myChair.getTypeOfWood());

            myChair.setQuantity(0);
            System.out.println(myChair.getQuantity());

            myChair.setArmRest(false);
            System.out.println(myChair.getArmRest());

            System.out.println(myChair.toString());

            if(myChair.getArmRest() == true)
            {
                chairsAndTables[0].setIcon(image[1]);

            }
            else
            {
                chairsAndTables[0].setIcon(image[0]);   
            }
        }

        if(ae.getSource() == commandButtons[1])
        {
            Table myTable = new Table();

            myTable.setIdNum(0);
            System.out.println(myTable.getIdNum());
            myTable.setTypeOfWood('o');
            System.out.println(myTable.getTypeOfWood());
            myTable.setQuantity(2);
            System.out.println(myTable.getQuantity());
            System.out.println(myTable.toString());
            System.out.println(myTable.getIdNum());

        }       
    }
}

This is the class FurnitureItem which is abstract and superclass for Chair

import java.io.Serializable;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;

    import java.util.Scanner;
    /** Here is the super class for Chair, Desk, Table
     * class name FurnitureItem.class
     * I have inlcuded the methods setTypeOfWood setIdNum and getIdNum.
     * @author Kiril Anastasov
     * @date 18/03/2012
     */

    abstract class FurnitureItem  implements Serializable
    {
        private int idNum;
        private char typeOfWood;
        protected double itemPrice;
        private int quantity;

        protected ImageIcon[] image; /*= { new ImageIcon("Pictures/Chair1.png"),
                                        new ImageIcon("Pictures/Chair2.png"),
                                        new ImageIcon("Pictures/Desk1.png"),
                                        new ImageIcon("Pictures/Desk2.png"),
                                        new ImageIcon("Pictures/Desk3.png"),
                                        new ImageIcon("Pictures/Desk4.png"),
                                        new ImageIcon("Pictures/Table1.png"),
                                        new ImageIcon("Pictures/Table2.png")};
                                        */

        //default constructor
        public FurnitureItem()
        {

            idNum = 0;
            typeOfWood = 'o' ; //oak or walnut
            itemPrice = 0;
            quantity = 0;
            image = null;

        }

        //parameterized constructor
        public FurnitureItem(int id, char tw, int qty, ImageIcon[] img)
        {
            idNum = id;
            typeOfWood = tw;
            itemPrice = 0;
            quantity = qty;
            image = img;
        }

        //mutator method for the id num
        public void setIdNum(int id)
        {
            String prompt = "Please enter furniture's id number: \n" +
                            "furniture's id must be numbers only";  

            String tablesIdNumber = JOptionPane.showInputDialog(null, prompt);
            Scanner input = new Scanner(tablesIdNumber);
            id = input.nextInt();
            idNum = id;     

        }

        //accesor method for the id
        public int getIdNum()
        {
            return idNum;

        }

        public double getItemPrice()
        {   
            return itemPrice;
        }

        public double getTotalPrice()
        {
            return itemPrice * 0.03; // incomplete
        }

        //mutator method for type of wood
        public void setTypeOfWood(char tw)
        {
            String prompt = "Please enter type of wood: \n" +
                    "type of wood can be oak or walnut";
            boolean validFlag;
            do
            {                   
                    String chairsTypeOfWood = JOptionPane.showInputDialog(null, prompt);
                    prompt = "Invalid data, please re-enter type of wood o for oak and w for walnut";
                    Scanner input = new Scanner(chairsTypeOfWood);
                    validFlag = true;
                    tw = input.nextLine().charAt(0);
                    if(tw != 'o' && tw != 'w')
                    {
                        validFlag = false;
                    }

            }while(!validFlag);
            typeOfWood = tw;

        }

        //accesor method for type of wood
        public char getTypeOfWood()
        {
            return typeOfWood;
        }

        //mutator method for quantity
        public void setQuantity(int qty)
        {

            String prompt = "Please enter quantity. \n" +
                    "How many furnitures would you like";
            boolean validFlag;
            do
            {


                String chairsQuantity = JOptionPane.showInputDialog(null, prompt);
                prompt = "Invalid data, the quantity of the chairs must be a possitive number, please re-enter";
                Scanner input = new Scanner(chairsQuantity);
                validFlag = true;
                qty = input.nextInt();
                if(qty < 0)
                {
                    validFlag = false;
                }


            }while(!validFlag);
            quantity = qty;
        }

        // accesor method for quantity
        public int getQuantity()
        {
            return quantity;
        }



        public ImageIcon[] getImage()
        {
            return image;
        }

        public String toString()
        {
            return "The id is: " + idNum + " and the qunatity is: " + quantity +
                    "the type of wood is:" + typeOfWood +" and the price of the item is: " + itemPrice;
        }

        public  int calcUnits() // make it abstract
        {
            return 5;
        }
    }

And this is the class Chair which is subClass of the FurnitureItem.

import javax.swing.ImageIcon;
import java.util.Scanner;
import javax.swing.JOptionPane;

/** Here is the sub class for FurnitureItem
 * class name Chair.class
 * @author Kiril Anastasov
 * @date 18/03/2012
 */
public class Chair extends FurnitureItem
{
//  private int idNum;
//  private char typeOfWood;
//  private int quantity;
    private boolean armRest;
//  Chair useChair = new Chair();


    //default constructor
    public Chair()
    {
        armRest = false;
        image = null;
        itemPrice = 0.0;
//      idNum = 0;
//      typeOfWood = 0;
//      quantity = 0;
    }

    //parameterized constructor
    public Chair(int id, char tw, int qty, ImageIcon[] img, boolean a)
    {

//      idNum = id;
//      typeOfWood = tw;    
//      quantity = qty;
        image = null;
        itemPrice = 0.0;        
        armRest = a;     

    }


    public void setArmRest(boolean a)
    {
        String[] withArmRest = {"Yes please", "No thank you"};
        String prompt = "Would you like an armrest for the chair";
        int option = JOptionPane.showOptionDialog(null, 
                                                  prompt,
                                                  "ArmRest",
                                                  JOptionPane.YES_NO_OPTION,
                                                  JOptionPane.QUESTION_MESSAGE,
                                                  null,
                                                  withArmRest,
                                                  withArmRest[0]);

        if(option == 0)
        {
            a = true;
        }
        else
        {
            a = false;
        }

        armRest = a;
        //System.out.println(useChair.toString());
    }
    public boolean getArmRest()
    {
        return armRest;
    }

    public String toString()
    {
        Chair myChair = new Chair();
        return "The id is: " + myChair.getIdNum() + " and the price of the item is: " + myChair.getTotalPrice() +
                " Is there an armrest ? ==> " + getArmRest() ;
    }
    public int calsUnits()
    {
        return getQuantity();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T21:51:40+00:00Added an answer on May 31, 2026 at 9:51 pm

    Looks liket the code is the problem

    public String toString() 
        { 
            Chair myChair = new Chair(); 
            return "The id is: " + myChair.getIdNum() + " and the price of the item is: " + myChair.getTotalPrice() + 
                    " Is there an armrest ? ==> " + getArmRest() ; 
        } 
    

    You create a new instance and print values of the new instance
    Instead of myChair.getIdNum() use this.getIdNum()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know if the problems that have been affecting Stack Overflow with regards
Ok so I have a recursion problem thats causing a stack overflow error at
I have Bitnami Rails stack installed on my Mac. To better explain my problem
I have another problem regarding Git. This time I thoroughly searched Google and Stack
I am working on a problem and got stuck at a wall I have
I'm new to wicket and stuck with the following problem: I have a table
I've been stuck on a little unix command line problem. I have a website
Hope to get solution to this problem. I have been stuck on it since
I have problem in some JavaScript that I am writing where the Switch statement
I have problem with return statment >.< I want to store all magazine names

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.