I had a programme to write for college in Java. I have missed the class so I won't be getting marked on it but wanted to get it done as I have an exam next we. I am really stuck.

I have to create a costInsurance () method taking in the surcharges for 3 conditions yearBuilt, numberOfRooms and hasASpa.

I gave a shot at the ageSurcharge but have no idea if this is right or not?

public class Hotel{

private String name; 
private String address; 
private int yearBuilt;
private int numberOfRooms; 
private boolean hasASpa;




public Hotel(String nameIn, String addressIn, int yearBuiltIn,int numberOfRoomsIn, boolean hasASpaIn){
  this.name= nameIn;
  this.address=addressIn;
  this.yearBuilt = yearBuiltIn;
    if(yearBuiltIn < 0)
    yearBuilt =0;
  numberOfRooms = numberOfRoomsIn;
    if(numberOfRoomsIn <0)
    numberOfRooms =0;
   hasASpa= hasASpaIn;

}

 public Hotel(){
String name ="";
String address="";
int yearBuilt= 0;
int numberOfRooms=0;
boolean hasASpa= false;

 }
   public void print(){
   System.out.println("Hotel name is : " + name);
   System.out.println("Hotel address is : " + address);
   System.out.println("Hotel was built in :" + yearBuilt);
   System.out.println("This hotel has a spa : " + hasASpa);
   }



       public int costInsurance( int ageSurcharge,int yearBuiltIn)
 {
   if((yearBuiltIn >1920) || (yearBuiltIn <1950)){
   ageSurcharge = 1000;
   System.out.println("Surcharge payable is :"+ ageSurcharge);

   }else if((yearBuiltIn>1951) || (yearBuiltIn >1999)){
    ageSurcharge =750;
    System.out.println("Surcharge payable is : " + ageSurcharge);
   }
   else { 
     if ((yearBuiltIn>2000) || (yearBuiltIn <=2014));
     ageSurcharge=500;
     System.out.println("Surcharge payable is: " + ageSurcharge);

   }





}

}

Edited 2 Years Ago by deceptikon: Moved to Java

Java sorry should have said in the title

There's a mistake you make on lines 17, 45, 49 and 54. You try to change the value of a parameter in a method. That's perfectly legal Java, but it won't do what you want. Java parameters are always passed as a copy of the original value. On line 17 etc you change the copy, but that doen't affect the original value. When your method terminates the copy is discarded, and the changes you made are lost.

You declare the method as returning an int, which makes sense if you look at it like:

int getAgeSurcharge(int yearBuilt) {
   // calculates and retuns the age surcharge (if any) 
   // based on the property's age
   ...

ps: In your if tests (lines 44, 48, 53) you need to look at your use of OR vs AND again.

in line 17 I have to set the yearBuilt to 0 if the value for the year the hotel was built is negative set it to 0. If the number of rooms argument is negative set it to 0 also. Which I thought I had done there. Now I am not so sure.

If I do the numRoomsSurchage in the same way it should work.

Thank you I will look at the OR vs AND.

I forgot to say I get a misssing return statement error on line 63 so the second last curly bracket.

This article has been dead for over six months. Start a new discussion instead.