Open Side Menu Go to the Top
Register
Urgent (easy) Java help needed Urgent (easy) Java help needed

01-10-2015 , 01:39 PM
Hi,

I'm studying for an exam, and I'm having some problems with the last question.

I'm in quite desperate need for help. This isn't an assignment, rather it's for me to study (and try tro remember).

The question is as follows:

1.1


A. Implement the modell above (fig. 1.1). All instance-variables(?) shall be private and have public access methods. The addApartment-method should add an apartment and the findApartment-method should return an Apartment (nbr in Apartment is unique). If there's no apartment with the actual nbr then null should be returned.

info: area = surface in squaremeters(sqrm), rentPerMonth(rent per month),
- rentPerSqrm = rentPerMonth / area
- rentPerYear = rentPerMonth * 12

B. Implement the methods totalRentPerMonth(): double and area():int in the class House. (figure 1.2)

- area():int should return the total area of all the apartments
- totalRentPerMonth():double should return the total monthly rent for all the apartments

C. Implement a class (main method) that tests all of the methods in the class. The program is to create a house and two apartments and print these out. The methods in B (above) is also to be tested.

Create the following house:
- Housenumber: 1, Address: Main Street 2, Buying price: 25 500 000

Create the following apartments for the house:
- Apartment number: 1, monthly rent: 6300, area 40(sqrm)
- Apartment number: 2, monthly rent: 8400, area 65(sqrm)

D. Write a method that updates the monthly rent for a certain apartment. The method shall take in the apartment number (nbr) and turn the new monthly rent into a parameter. If the apartment doesn't exist, nothing should be done.

- public void updateRentPerMonth( int apartmenNbr, double newRent)

The method should be in the class House.

1.2
Urgent (easy) Java help needed Quote
01-10-2015 , 01:44 PM
So far, this is what I've got:

House class:
Quote:
import java.util.ArrayList;


public class House {

private int houseNbr;
private String address;
private double buyingPrice;
private ArrayList<Apartment> myList = new ArrayList<Apartment>();


public Apartment findApartment(int nbr){
for (int i=0; i<myList.size();i++){
Apartment temp = myList.get(i);
if(nbr == temp.getNbr()){
return temp;
}

}
return null;

}
public void addApartment(Apartment a){
myList.add(a);
}

public int gethouseNbr(){
return houseNbr;
}

public String getaddress(){
return address;
}
public double getbuyingPrice(){
return buyingPrice;
}

public void sethouseNbr(int hnr){
houseNbr = hnr;
}

public void setaddress(String adr){
address = adr;
}

public void setbuyingPrice(double bpr){
buyingPrice = bpr;
}

}
Apartment class:
Quote:
public class Apartment {

private int nbr;
private double rentPerMonth;
private int area;

public double rentPerSqrm(){
return rentPerMonth/area;
}
public double rentPerYear(){
return rentPerMonth*12;
}

public double getRentPerMonth(){
return rentPerMonth;
}
public int getNbr(){
return nbr;
}

public int getArea(){
return area;
}

}
Urgent (easy) Java help needed Quote
01-10-2015 , 03:05 PM
what is it you want help with exactly?

you should post things in [code] blocks not quotes as it makes it easier to read
Urgent (easy) Java help needed Quote
01-10-2015 , 03:16 PM
Also probably the homework thread >.<
Urgent (easy) Java help needed Quote
01-10-2015 , 06:12 PM
Quote:
Originally Posted by Alobar
what is it you want help with exactly?

you should post things in [code] blocks not quotes as it makes it easier to read
I'm looking for help with the parts that I have yet to complete. I'm at a loss, looking at the book we're using, but I'm not getting anywhere.

Will use code tags in the future.
Urgent (easy) Java help needed Quote
01-10-2015 , 11:30 PM
Quote:
Originally Posted by iosys
Awesome, thanks!!

Just one thing, am I supposed to be able to make som input somewhere, cause nothing happens when I run the code.
Urgent (easy) Java help needed Quote
01-10-2015 , 11:36 PM
School needs to teach you how to use an IDE like eclipse to debug and see what is happening.

If you put a break point while debuging, you can move your mouse over objects and see what variables they contain by stepping line by line in the code.

You could put System.out.println in the code if not.
Urgent (easy) Java help needed Quote
01-12-2015 , 02:43 AM
Quote:
Originally Posted by nejo
Awesome, thanks!!

Just one thing, am I supposed to be able to make som input somewhere, cause nothing happens when I run the code.
What do you mean nothing happens when you run the code? You just created two apartments and one house! A lot better that some output text if you ask me...
Urgent (easy) Java help needed Quote
01-12-2015 , 03:09 AM
Quote:
Originally Posted by iosys
School needs to teach you how to use an IDE like eclipse to debug and see what is happening.

If you put a break point while debuging, you can move your mouse over objects and see what variables they contain by stepping line by line in the code.

You could put System.out.println in the code if not.
Eclipse is good for post education but Jesus I loved blue Js debugger for school.
Urgent (easy) Java help needed Quote

      
m