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 9222229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:47:03+00:00 2026-06-18T03:47:03+00:00

I have two classes: Apartments and Building. The Apartment class: public class Apartment {

  • 0

I have two classes: Apartments and Building. The Apartment class:

public class Apartment {
    private String familyName;
    private int rent;
    private boolean isRented;
}

The Building class has an Apartment array, and a numOfApartments variable which indicates how many apartments the building has, and also a FINAL variable who indicates the max number for apartments in one building.

private Apartment[] apartments;
private int numOfApartments;
private final int MAX_APARTMENTS=20;

public Building() {
    this.apartments=new Apartment[MAX_APARTMENTS];
    this.numOfApartments=0;
}

Now I want to make a method which raises the rent for an apartment by an amount which is taken as parameter:

public void raiseRent(int amount) {
    for (int i=0; i<numOfApartments; i++) {
        apartments[i].setRent(apartments[i].getRent() + amount);
    }
}

the addAppartment method:

public boolean addApartment(Apartment a){ //adds an appartment to the building if
                                           //there's enough space in the array.
        if (this.numOfApartments<this.MAX_APARTMENTS)
        {
            this.apartments[this.numOfApartments]=new Apartment(a);
            this.numOfApartments++;
            return true;
        }
        return false;
    }

The apartment class string ToString method:

public String toString()
{
    if (this.isRented) //if the apartment is rented, prints:
    {
        return "Apartment rent: " + this.rent + ". rented to " + this.familyName + " family.";
    }
    else
        return "Apartment rent: " + this.rent  + ". not rented yet.";
}

The main is:

String familyName; int rent;
System.out.println("enter family name and rent by this order");
familyName=scan.next();
rent=scan.nextInt();

Apartment a=new Apartment(familyName,rent);  //build new apartment object.
Building b=new Building();
System.out.println(b.addApartment(a));
b.raiseRent(200); //initiate the raiseRent method.

Everything here works, but when I write this:

 System.out.println(a);

The rent is shown as the old rent, not the updated one.
When I write this:

System.out.println(b.apartments[0]);

It works.

Why when I write System.out.println(a); it doesn’t work?

Thank you for the answer.

  • 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-06-18T03:47:06+00:00Added an answer on June 18, 2026 at 3:47 am

    In your

    public boolean addApartment(Apartment a) method:

    this.apartments[this.numOfApartments]=new Apartment(a);
    

    Creates a new apartment which appears to be constructed from an existing apartment. This is a copy.

    After you raise the rent, the

    Apartment a=new Apartment(familyName,rent); //build new apartment object.

    That you passed in to

    b.addApartment(a)

    Will not have an updated rent. This is because Java is pass-by-value. It’s a “new” int in the new Apartment your Building Apartment[] collection contains, even if you set it from an existing one (this.rent = other.rent)

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

Sidebar

Related Questions

I have two classes: Public Class Subscribing Private _subscribingObjects As IList(Of String) Public Sub
I have two classes public class PrepaidPackage { private String name; private String serviceClassID;
I have two classes: class ClassA { public: ClassB *classB; int i = 100;
I have two classes: class OrdenO { public int id_orden_O {get;set;} public int Ei_O
I have two classes, ThreadItem and Enquiry. public class ThreadItem { [Key] public int
I have two classes: testClass and castClass : class testClass { public: int field1;
I have two classes: public class Reference { public virtual string Id { get;
I have two classes class a { public: a(int i); }; class b {
I have two classes: namespace Something { [Serializable] public class Spec { public string
I have two classes public class A { public int BaseA {get;set;} } public

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.