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
LVL 19
Stephen MandersonSoftware EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mayank SAssociate Director - Product EngineeringCommented:
>> 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] ;
0
Stephen MandersonSoftware EngineerAuthor Commented:
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
0
Stephen MandersonSoftware EngineerAuthor Commented:
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
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

ADSLMarkCommented:
Something like this I guess:
//Example.java//
public class Example
{
    public static void main(String[] args)
    {
        Accomodation[] houses = new Accomodation[4] ;
        houses[0] = new House(4, 15.30);  //4 rooms
        houses[1] = new Flat(3, 5, 16.5); //3 floors, 5 rooms on average
        houses[2] = new House(10, 17.0);  //10 rooms
        houses[3] = new Flat(9, 6, 17.5); //9 floors, 6 rooms on average

        double totalCost = 0.0;
        for(int i=0;i<houses.length;i++)
        {
            totalCost = totalCost + houses[i].totalCost();
        }
        System.out.println(totalCost);
    }
}

//Accomodation.java//
abstract class Accomodation
{
    public abstract double totalCost();
}

//Flat.java//
class Flat
    extends Accomodation
{
    private int floors;
    private int roomsPerFloor;
    private double radiatorCost;

    //Constructor
    public Flat(int floors, int roomsPerFloor, double radiatorCost)
    {
        this.floors = floors;
        this.roomsPerFloor = roomsPerFloor;
        this.radiatorCost = radiatorCost;

    }

    //Calculate total cost
    public double totalCost()
    {
        return (this.roomsPerFloor * this.radiatorCost - 20) * this.floors;
    }
}

//House.java//
class House
    extends Accomodation
{
    private int rooms;
    private double radiatorCost;

    //Constructor
    public House(int rooms, double radiatorCost)
    {
        this.rooms = rooms;
        this.radiatorCost = radiatorCost;

    }

    //Calculate total cost
    public double totalCost()
    {
        return this.rooms * this.radiatorCost - 20;
    }
}

This creates 2 houses and 2 flats, adds them to an array of Accomodations. Since House and Flat both *extend* the Accomodation class this is possible. Next you loop through this array with a simple for-loop and you call the function totalCost(), which calculates the total cost for a House or Flat, depending on the instance at hand.
You could also use a LinkedList here, instead of an array, with some modifications it would allow you to add more houses/flats without having to adjust the size of the array.

I hope this works for you,
Mark

PS:
to compile:
  javac *.java -d .
to run:
  java Example
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Stephen MandersonSoftware EngineerAuthor Commented:
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
0
Stephen MandersonSoftware EngineerAuthor Commented:
Silly question afterall Mark you had already done so, figured it out a few mins after posting.

Thanks again for your help

Steve
0
Mayank SAssociate Director - Product EngineeringCommented:
You need to call the totalCost () method and it will be taken care of....
0
Mayank SAssociate Director - Product EngineeringCommented:
Ok cool :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.