Solved

Help building a constructor & class

Posted on 2007-03-28
8
177 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java / Linux and Regular Expressions 11 71
VB Script to add site to Java Exception List 4 53
rhino JavaScript import, load 25 69
hibernate example for saving data 19 6
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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.

863 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now