-5

Write an OfficeChair class.

  • have two instance variables
  • have one static int that is used to keep track of the number of OfficeChairs
  • have a default constructor
  • have an overloaded constructor – with both data members as input
  • both constructors need to add to the static int

Write two lines of code that show how to make an instance of the OfficeChair – one using the default constructor and one using the overloaded constructor.

You do not need to write any getters/setters.

I need know how would you write two lines of code to make an instance.

public class OfficeChair
{
    public string function chairSwivel;
    private int chairSoftness;
    static int chairCount = 0;
}   
  OfficeChair()
        {
        chairSwivel = “Yes”;
        chairSoftness = “Not Given”;
        chairCount = “Not Given”;
        }   
     public OfficeChair( string Str, int num1, static int num2)
     {
     chairSwivel = str
     chairSoftness = num1
     chairCount = num2
     }
}
public Class officeChairs {
    public static void main(String args[])
    {
        Chair officeChair = new OfficeChair(Yes, Very Soft, 8)
idk
  • 13
  • 1
  • 6
  • You should instantiate one chair with the *default* constructor, and one instance with the *overloaded* constructor. You've only done the latter on your bottom line. – aioobe Aug 15 '14 at 11:55
  • oh I okay so that is an instance thanks so how would I add – idk Aug 15 '14 at 11:55
  • 2
    I down-voted you, and it's because you asked your question without showing your attempt to create the 2nd instance and without clarifying your misunderstandings. Without showing your attempt, you cheat yourself out of a valuable experience, and you also prevent us from seeing what you could be doing wrong. I'm sure that your questions will get better in the future, but please keep this in mind for your future questions. – Hovercraft Full Of Eels Aug 15 '14 at 11:58
  • My attempt is the entire code I wrote ....... as u know coding is very sensitive I'm new to java just try to learn from helpful people – idk Aug 15 '14 at 12:00
  • Nothing to do with your question, but according to your requirements you are supposed to `increment` your counter in both constructors. Your not supposed to set the number directly. Also your code won't work like this. `chairCount` is an `Integer(int)`. You can't assign the `String` _Not Given_ to it because it's the wrong type. – Sebastian_H Aug 15 '14 at 12:00
  • Thank think I fix the issue you were talk about int chairCount; static int count = 0 chairCount = count + 1; – idk Aug 15 '14 at 12:09

2 Answers2

3

Oh, dear goodness. There are so many things wrong with the code. Well,

public class OfficeChair
{
    // `string` is not a type in Java
    // `function` is a made up keyword
    // chairSwivel should probably be a `boolean` typed variable
    public string function chairSwivel;
    private int chairSoftness;
    static int chairCount = 0;
} // Class definition ends

  // This is outside the class, see above }
  OfficeChair()
        {
        // Wrong quotes (for other literals as well)
        chairSwivel = “Yes”;
        // Type error - a string cannot be assigned to an integer variable.
        chairSoftness = “Not Given”;
        // Type error as per above, also fails to meet requirements
        chairCount = “Not Given”;            
        }
     // Please choose meaningful parameter/variable names
     public OfficeChair( string Str, int num1, static int num2)
     {
     // Missing semicolons
     chairSwivel = str
     chairSoftness = num1
     // Fails to meet requirements
     chairCount = num2
     }
} // Bogus, see first class-ending }

// Class should be `class` - all keywords in Java are lowercase.
// officeChairs is also mis-cased as class names should all be CamelCased.
// Only one class with a given name (or parent type if nested) can exist.
public Class officeChairs {
    public static void main(String args[])
    {
        // There is no Chair type defined
        // There is no Yes identifier in scope, string literals need quotes.
        // Very Soft (or even the less-incorrect "Very Soft") is still not
        // assignable to an integer. Missing semicolon.
        Chair officeChair = new OfficeChair(Yes, Very Soft, 8)

And after all that (which might not be all of it), using new creates a new instance:

The new operator instantiates a class by allocating memory for a new object and returning a reference to that memory. The new operator also invokes the object constructor.

user2864740
  • 60,010
  • 15
  • 145
  • 220
1

That will be

OfficeChair officeChair1=new OfficeChair();

and

OfficeChair officeChair2=new OfficeChair("yes","verySoft",8);

What is an instance?

Community
  • 1
  • 1
Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115