Solved

Help building a constructor & class

Posted on 2007-03-28
8
176 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:mayankeagle
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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:mayankeagle
ID: 18813099
You need to call the totalCost () method and it will be taken care of....
0
 
LVL 30

Expert Comment

by:mayankeagle
ID: 18813101
Ok cool :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Increment alphanumeric sequence 6 80
wordappend challenge 8 85
Systems talking to each other 5 109
Java and GPO 11 47
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
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.

705 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

17 Experts available now in Live!

Get 1:1 Help Now