-
Measurable , new..
I want to create an interface called Measurable and a class called Measurement that implements Measurable.
Measurable must have at least a metod called getValue() that returns a measurable number.
To create a class called collecter that join all these number.
This will have a form of Measurable implements object.
Collecter will implements Measurable too(a collecter will hava as a measurable number an other
collecter).
The program will be like this:
public class MyClass{
public static void main(String[] args)
Collecter c1,c2;
c1 = new Collecter();
c1.addMeasurement(new Measurement(20));
c1.addMeasurement(new Measurement(33));
c1.addMeasurement(new Measurement(10));
c1.addMeasurement(new Measurement(12));
c1.addMeasurement(new Measurement(42));
System.out.println(c1.getAverage());//23.4
System.out.println(c1.getMedianen());//20
c2 = new Collecter();
c2.addMeasurement(new Measurement(24));
c2.addMeasurement(new Measurement(31));
c2.addMeasurement(c1);
System.out.println(c2.getAverage());//24.57
System.out.println(c2.getMedianen());//24
}
my code (question to my problem is in the class Collecter)
==========================================================
interface Measurable{
public int getValue();
}
==========================================================
class Measurement implements Measurable{
private int ettTal;
public Measurement(int tal){
ettTal=tal;
}
public int getValue(){
return ettTal;
}
public void visaTalet(){
System.out.println(this);
}
public String toString(){
String n = new String(String.valueOf(ettTal));
return n;
}
public static void main(String [] args){
Measurement measur = new Measurement(42);
System.out.println(measur);
}
}
===============================================================
import javax.swing.*;
import java.text.*;
import java.util.*;
class Collecter implements Measurable{
private int count=0;
private Measurement m;
private int []mynumber = new int[10];
public int getValue(){
return m.getValue();
}
//------- MY PROBLEM *****
void addMeasurement(Collecter c){ //MY PROBLEM: c is object Collecter
//as String I want convert to array to
String s =String.valueOf(c); //add an other array.Is possible to
int nr =Integer.parseInt(s); //do that or I have to use another
// way to lose this problem?
System.out.println(nr);
/*
m = new Measurement(9);
int ett=m.getValue();
minatal[antal]=ett;
antal++;
*/
}
//-----------
public void addMeasurement(Measurement meas){
String st = String.valueOf(meas);
int nr = Integer.parseInt(st);
m = new Measurement(nr);
int nr2 = m.getValue();
mynumber[count] = nr2;
count++;
}
public double getAverage(){
int sum=0;
double total=0.0;
for(int i=0;i<count;i++)
sum=sum+mynumber[i];
String temp1=String.valueOf(sum);
total =Double.parseDouble(temp1);
return (total/count);
}
public int getMedian(){
int []arrayTemp = new int[count];
for(int i=0;i<count;i++)
arrayTemp[i]=mynumber[i];
Arrays.sort(arrayTemp);
return (arrayTemp[count/2]);
}
public String toString(){
StringBuffer buf = new StringBuffer();
for(int i=0;i<count;i++)
buf.append(mynumber[i]);
String num = new String(buf.toString());
return num;
}
}
//===============================================================
public class Exercise{
public static void main(String[] args){
Collecter c1,c2;
c1 = new Collecter();
c1.addMeasurement(new Measurement(42));
c1.addMeasurement(new Measurement(12));
c1.addMeasurement(new Measurement(10));
c1.addMeasurement(new Measurement(33));
c1.addMeasurement(new Measurement(20));
System.out.println(c1.getMedian());
c2 = new Collecter();
c2.addMeasurement(new Measurement(24));
c2.addMeasurement(new Measurement(31));
//c2.addMeasurement(c1);
//System.out.println(c1.getMedian());
}
}
-
Your explanation is still a bit unclear
Anyway, I have made a little setup here the way i think this is supposed
to work. As for the Collecter class, I have used an internal ArrayList. If you
are supposed to 'roll your own' object collection I can supply you with my
suggestion for that. I have left it out here since that does not seem to be
the objective of the excercise.
Code:
import java.util.*;
interface Measurable {
public int getValue();
}
class Measurement implements Measurable {
private int value;
public Measurement(int value) {
this.value = value;
}
/**
* Measurable interface implementation
* @return
*/
public int getValue() {
return this.value;
}
}
/**
* Collecter, an ArrayList 'wrapper'. ArrayList accepts all kinds of
* objects, so this wrapper class has only two purposes:
* 1: restrict the arraylist to only accept Measurable objects.
* 2: utilizes the Measurable interface to facilitate a uniform way
* to do the getValue() processing.
* All objects in Collecter must implement the Measurable interface,
* in other words: the Collecter class 'sees' all its elements as
* Measurable elements, and cares for nothing else since it only
* communicates with these elements using the methods defined in
* the Measurable interface.
*
* With this setup A Collecter can contain other Collecters that again
* may contain Collecters (etc.).
*/
class Collecter implements Measurable {
private ArrayList list=null;
public Collecter () {
list = new ArrayList();
}
/**
* Add a Measurable to the collecter.
* @param mes
*/
public void addMeasurable (Measurable mes) {
this.list.add(mes);
}
/**
* Measurable interface implementation
* Returns the sum of all its elements.
* For Measurement objects it will return the actual value,
* For Collecter objects it will return the sum of the values.
* @return
*/
public int getValue() {
int sum=0;
for (int i=0;i<list.size();i++) {
// what we get here may be a Measurement or a Collecter but who cares ?
// after all its just a Measurable. If it is a Collecter, then that
// Collecter will sum up its own Measurables.....
Measurable mes=(Measurable)list.get(i);
sum += mes.getValue();
}
return sum;
}
}
public class Exercise {
public Exercise() {
Collecter c1, c2;
c1 = new Collecter();
c1.addMeasurable(new Measurement(42));
c1.addMeasurable(new Measurement(12));
c1.addMeasurable(new Measurement(10));
c1.addMeasurable(new Measurement(33));
c1.addMeasurable(new Measurement(20));
//System.out.println(c1.getMedian());
c2 = new Collecter();
c2.addMeasurable(new Measurement(24));
c2.addMeasurable(new Measurement(31));
c1.addMeasurable(c2);
System.out.println(c1);
System.out.println(c2);
}
public static void main(String[] args) {
Exercise ex = new Exercise();
}
}
eschew obfuscation
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks