Link to home
Start Free TrialLog in
Avatar of Stephen Manderson
Stephen MandersonFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Help building a constructor & class

Hi all,

Im looking for a little help on the Java front, Im a vb & vb.Net programmer, so going to java to do this simple program is a bit of a pain for me with the syntax. So am in this TA looking for some quick help to this 2 part problem.

Its just a simple problem so thought it would be best to post here.

Its has 2 Classes Accommodation and Charges & contains 2 fields
Rooms int
RadiatorCost double

Firstly how would I go about modifying the constructor & add a method (Say HouseTotal) that passes the #Rooms & Radiator Cost from the Charges Class into the Accommodation class subtracts 20 from the total and returns a double for the total amount of radiators?

Ie passing data from Charges
HouseTota H0 = New HouseTotal(21, 56.76)

Lastly if I was wanting to create an array named Houses in the Charges class that would contain multipul results for rooms & radiator costs loop through the array and output the a double say OverallTotal?

Many Thanks
Steve
Avatar of Mayank S
Mayank S
Flag of India image

>> 2 Classes Accommodation and Charges & contains 2 fields

Which of the 2 classes contains these 2 fields?

>> would I go about modifying the constructor & add a method (Say HouseTotal)

Assuming your class name is HouseTotal, you would add an overload like:

public HouseTotal ( int rooms, double radiatorCost )
{
  // do your processing here
}

>> Lastly if I was wanting to create an array named Houses in the Charges

Is that an array of HouseTotals? If so, it will be like

HouseTotal[] houses = new HouseTotal[count] ;
Avatar of Stephen Manderson

ASKER

Thanks for the quick reply,

So what would the syntax be for the calculation of the HouseTotal?

Something along the lines of ?

private int HRoom;
private double HCost;
private double roomTotal;

public HouseTotal ( int rooms, double radiatorCost )
{
  HRoom = rooms;
  HCost = radiatorCost;
  roomTotal = HRoom * HCost - 20;
  return roomTotal
}

As far as the syntax goes for the array and loop

import java.util.ArrayList;
HouseTotal = new ArrayList();

HouseTotal[] houses = new HouseTotal[4] ;
double totalCost = 0.0;

for(int i = 0; i < houses.size();i++)
{
 ... unsure how I would pass the values of the array into the loop to totalCost
}

Thanks Again
Steve
Heres a list of data for expample I would be looking to add to the array to total.

HouseTotal[] houses = new HouseTotal[4] ;
    houses [0] = new HouseTotal(12, 15.30);
    houses [1] = new FlatTotal(11, 16.5);
    houses [2] = new HouseTotal(10, 17.0);
    houses [3] = new FlatTotal(9, 17.5);

With the FlatTotal method I would be including a subrtaction of 50 opposed to the 20 in the HouseTotal method

import java.util.ArrayList;
HouseTotal = new ArrayList();

HouseTotal[] houses = new HouseTotal[4] ;
double totalCost = 0.0;

for(int i = 0; i < houses.size();i++)
{
 totalCost = totalCost + houses[i];
}

System.out.println("Total Cost for radiators is " + totalCost);

Am I on the right track?

Regards
Steve
ASKER CERTIFIED SOLUTION
Avatar of ADSLMark
ADSLMark

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Mark

thanks for that, helps alot. just to ask though how could I return the value for just the FlatTotal or the HouseTotal? if exmaple just FlatTota(6, 25.5) would multiply 6*25.5 - 50 and return the value when the method is called seperatly?

that should wrap up my problems if you can help me to just return the total value for the flats and houses seperaty

Many Thanks
Steve
Silly question afterall Mark you had already done so, figured it out a few mins after posting.

Thanks again for your help

Steve
You need to call the totalCost () method and it will be taken care of....
Ok cool :)