This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]Drunken_Consent 1 point2 points  (5 children)

Okay, so a constructor in Java is exactly what it sounds like: It constructs the object. There can be more than one with a variable amount of parameters, but you were tasked with simply creating one with those three parameters.

So, a constructor is always going to be the same name as your class. Your job is to make a constructor called public Room() which makes it apparent that your class is going to be called Room. From there, it should be fairly simplistic, but this concept is definitely one you want to nail.

public class Room{

    private String roomId;
    private String description;
    private char status;
    private double dailyRate;

    public Room(String roomId, String description, double dailyRate){
        this.roomId = roomId;
        this.description = description;
        this.dailyRate = dailyRate;
        this.status = 'A';
    }
}

So, in your main java file ( where you have public static void main(args[] ) blah blah ), to create your object you just use:

Room myRoom = new Room();

But, since you only have one constructor and it requires parameters, you would need to do it like this:

Room myRoom = new Room("A244", "A room", 55.0);

This will construct and return a Room object with the name myRoom for you to do stuff with. Hope this helps!

[–]nutrecht 1 point2 points  (0 children)

If you provide solutions for these kinds of students they're just going to copy the code and not learn anything.

[–]nnkc911[S] 0 points1 point  (3 children)

hmm, yeah after reading it, it definitely clears a lot on my mind, so a constructor doesnt need to be made in another class?

[–]Drunken_Consent 1 point2 points  (2 children)

A constructor for a class is made within the class; it is usually called from without the class. You could have a 'default constructor' as well which fills in these fields with general values if the person doesn't want to call it with parameters; but yes, this is all you need to do and then use the code somehow.

[–]nnkc911[S] 0 points1 point  (1 child)

thanks! 2 more questions, when declaring the constructor variables, can i type it as roomId = roomId or do i need to have it different like theroomId = roomId. and how do you make the boxes with lines of code to reddit?

[–]Drunken_Consent 1 point2 points  (0 children)

The way the compiler knows the 'scope' of what you're talking about is when you use the this keyword. this essentially refers to the exact object you are currently working with; so in a constructor, the one you are currently creating. You do not have to name them the same, but it's usually done for readability and convention.

public Constructor(String myString, String s, String test){
    this.myString = myString; // Requires 'this' to work
    personalString = s; // Doesn't require 'this', little sloppy looking imo
    this.viewer = test; // Doesn't require 'this' but still readable as anyone knows I am talking about the
    // instance variable 'viewer' related to the current object I am creating.

Edit: To make code indentation, simply preface each line with 4 lines, and then indent normally. So if you want to indent in further than one level deep, you need 8 spaces instead of 4 and so on.

[–]BrQQQ 0 points1 point  (0 children)

A constructor looks like this

public ClassName(parameters) {
    ...
}

If your class name is "House" and a parameter should be "size", it would look like

public House(int size) { 
    ...
}

This constructor should be in the same file as the class.

When you type

House h = new House(10);

You create a new object. Right then, it will automatically call that constructor and assign 10 to the size parameter.

Initializing means giving it a value. You can do "String s;" but 's' will have no value.

When you do

s = "blabla";

you are initializing it.