Solved

Help building a constructor & class

Posted on 2007-03-28
8
181 Views
Last Modified: 2013-11-23
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
0
Comment
Question by:Stephen Manderson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18810994
>> 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
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 18811169
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
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 18811215
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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 10

Accepted Solution

by:
ADSLMark earned 500 total points
ID: 18811969
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
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 18812610
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
 
LVL 19

Author Comment

by:Stephen Manderson
ID: 18813076
Silly question afterall Mark you had already done so, figured it out a few mins after posting.

Thanks again for your help

Steve
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18813099
You need to call the totalCost () method and it will be taken care of....
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18813101
Ok cool :)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to get all the API from website? 11 161
How to determine if a string is a valid SHA value 7 57
What is the use of Forwarding Class in java 1 54
Java regex 5 22
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question