-3

please excuse my noob, but my current level of java knowledge is quite base. I really need help with an exercise, but it is a little beyond my very basic level of Java, (i.e. I'm only confident in declaring variables lol) But here is the question word for word:

Complete a main method that will accomplish the following:
you may assume that the class includes all appropriate set methods(also know as mutator methods) and get methods (also known as accessor methods). The output can be completed using the console output or GUI output.

  • create a blue kitchen table with 4 legs

  • create a brown dining table with 6 legs

  • change the color of the kitchen table to pink

  • print out the color of the dining table including text identifying which value you are printing and also the value itself.

  • print out the number of tables including text identifying which value you are print and also the value its self."

So for the last two I need a

System.out.print("the color of the dining table is " + 'diningtable color')

and a

System.out.print("number of tables: " + 'numberOftables');

what's the rest? I really would appreciate help and I know that I need to practice ;)

EDIT

here is what I have now, not sure if my print line is correct:

public static void main (String[] args){
    //table 1       

       table kitchenTable = new table();

       kitchenTable.setnumberOfLegs((int)4);
   kitchenTable.setcolor("blue");
    //table 2
    table DiningTable =new table();
    DiningTable.setnumberOfLegs((int)6);
    DiningTable.setcolor("brown");
 //change table 1 to pink
 kitchenTable.setcolor("pink");

 System.out.print("Dining Table Color is: " + kitchenTable.color());

  }
  • 9
    Welcome to stackoverflow. This is a free community where others helps altruistically. This community does not do homework from scratch, so in order to get help, please provide what have you tried so far. – RamonBoza Dec 11 '13 at 15:12
  • I'll help you with a starting point. Look up what "constructors" are, and "fields", and "getters and setters". :) Once you know what those are, this should be pretty straightforward. – asteri Dec 11 '13 at 15:13
  • See http://docs.oracle.com/javase/tutorial/index.html and http://docs.oracle.com/javase/6/docs/api/ – Reinstate Monica -- notmaynard Dec 11 '13 at 15:14
  • hmm well seeing as I really don't know what I'm doing yet, i have a prolly incorrectly written attempt: – user3091571 Dec 11 '13 at 15:15
  • 5
    @user3091571 That's fine. :) That's the whole point, actually! Post your broken code, and it gives us something to work with in order to help you. We will very rarely just provide code based on requirements, because then we're simply writing it for you, not answering a question or solving an issue. – asteri Dec 11 '13 at 15:18
  • along with the java language elements that you need to acquire the exercise is looking for you to do some basic data modelling . in other words from the real world create a data structure ( big hint : class ) that represent a real thing ( a table ) and allows you to manage various properties ( such as colour , number of legs .. etc ) – diarmuid Dec 11 '13 at 15:20
  • lol ok cant hit enter in comments sorry! – user3091571 Dec 11 '13 at 15:22
  • @user3091571 don't post important part of your question in comment, especially if it is code (1. not all read comments, 2. it is hard to read code in comments). Instead add it to your original question using [[edit]] option. – Pshemo Dec 11 '13 at 15:22
  • ok i edited the OP to reflect my attempts – user3091571 Dec 11 '13 at 15:30
  • @user3091571 minor note: if you have number literal such as 1 without additional type specification like `1L` (which would make 1 `long`) or `1f` (which would make 1 `float`) then this number is by default `int` so you don't need to cast it to it in your methods. Instead of `setnumberOfLegs((int)4)` just use `setnumberOfLegs(4)`. – Pshemo Dec 11 '13 at 15:36

1 Answers1

3

It looks like based on the sample code you've provided, you've got the right general idea with getters and setters. Here's what I'll do: I'll provide an example using something other than a table.

public class Lamp {

    private boolean on = false;

    public Lamp(boolean isOn) {
        on = isOn;
    }

    public boolean isOn() {
        return on;
    }

    public void setOn(boolean isOn) {
        on = isOn;
    }

}

So here I have a constructor for my Lamp, and a getter and a setter for its one property, boolean on. In order to create the Lamp with a starting state (or initialize it), I can simply call the constructor like the following:

Lamp myLamp = new Lamp(true);

Now I have a new Lamp object whose field on is set to true.

In order to manipulate the state of that variable, I can call the setter:

myLamp.setOn(false);

Now my lamp is off!

And to print out the state, I can try something like ...

System.out.println("Is the lamp on? " + myLamp.isOn());

Side comment: there's no need to cast an integer literal to an int, since it's already an int. (In other words, no need for (int)6, since 6 is already an int.)

asteri
  • 11,402
  • 13
  • 60
  • 84
  • I think i understand. is the code i wrote in the OP correct? – user3091571 Dec 11 '13 at 15:33
  • @user3091571 Well, I'd have to see your other classes, but no, the way you've written it won't even compile due to a spelling error. :) Also, there's no need to cast the integer literals to `int` (i.e., `(int)6` is redundant) – asteri Dec 11 '13 at 15:35
  • the question assumes the other classes, and setter and getter are already written, i just need to make a main method – user3091571 Dec 11 '13 at 15:38
  • in this case the print would be ("text" + kitchenTable.color); right? – user3091571 Dec 11 '13 at 15:44
  • @user3091571 Calling `kitchenTable.color` actually attempts to access the `color` field of the `kitchenTable` object directly. More than likely, that field is `private`, meaning your code won't compile. That's why we have getters. You should be able to do something like `kitchenTable.getColor()`. – asteri Dec 11 '13 at 16:09
  • ok! thank you very very much! i do believe i have it now! ill post a finished code in a bit. – user3091571 Dec 11 '13 at 16:13