Need help, cannot find error
Here is the code:
Code :
import java.util.ArrayList;
/**
* A class to model an Analyser
*
* @author (XXXX)
* @version (1.0)
*/
public class Analyser
{
// instance variables - replace the example below with your own
public ArrayList<Sample> samples;
/**
* Constructor for objects of class Analyser
*/
public Analyser()
{
// initialise instance variables
samples = new ArrayList<Sample>();
}
/**
* An example of a method - replace this comment with your own
*/
public void addSample(String location, int value)
{
if(value >= 0) {
samples.add(new Sample(location, value));
}
else {
System.out.println("The temperature is lower than absolute zero" + "\n" + "Temperature must be zero or greater");
}
}
public void populate()
{
samples = new ArrayList<Sample>();
samples.add(new Sample("Meston" + " " + 284));
samples.add(new Sample("Fraser Noble" + " " + 274));
samples.add(new Sample("Edward Wright" + " " + 309));
samples.add(new Sample("New Kings" + " " + 260));
samples.add(new Sample("Kings College" + " " + 265));
}
public void showSamples()
{
int index = 0;
while(index < samples.size()) {
System.out.println(samples.get(index));
index++;
}
}
public int highestTempRecorded()
{
int highest = 0;
for(int index = 0; index < samples.size(); index++) {
if(samples.get(index).getSampleValue() > highest)
highest = samples.get(index).getSampleValue();
}
System.out.println("The highest temperature recorded is:" + " " + highest);
}
public void showFaultySensors()
{
int faultyLow = 260;
int faultyHigh = 309;
for(int index = 0; index < sample.size(); index++)
if(samples.get.index().getSampleValue() < faultyLow) {
faultyLow = samples.get(index).getSampleValue();
System.out.println("Temperature is below normal:" + " " + faultyLow);
}
else if(samples.get.index().getSampleValue() > faultyHigh) {
faultyHigh = samples.get(index).getSampleValue();
System.out.println("Temperature is above normal:" + " " + faultyHigh);
}
}
}
Have problem with populate method. I am getting next mesage: cannot find symbol - constructor sample(java.lang.String)
I have also another class called Sample:
Code :
/**
* Write a description of class Sample here.
*
* @author (XXX)
* @version (1.0)
*/
public class Sample
{
// representing location and values.
private String location;
private int value;
/**
* Constructor for objects of class Sample
*/
public Sample(String location, int value)
{
this.location = location;
this.value = value;
}
public String toString()
{
return "[" + location + "]" + " " + value;
}
}
Where I am wrong?
Re: need help with my code
The code to create the Sample objects trying to use a constructor that accepts a single string, however the Sample class defines its constructor to accept a String and an int, perhaps you were trying to do the following:
Code :
new Sample("Meston", 284)
Re: need help with my code
solved, thanks! :))
next error appearing here
Code :
public int highestTempRecorded()
{
int highest = 0;
for(int index = 0; index < samples.size(); index++) {
[B] if(samples.get(index).getSampleValue() > highest)[/B]
highest = samples.get(index).getSampleValue();
}
System.out.println("The highest temperature recorded is:" + " " + highest);
}
Re: Need help, cannot find error
I Think its this:
Code :
samples.add(new Sample("Kings College" + " " + 265));
You're treating the input as one String.
try
Code :
samples.add(new Sample("Kings College",265));
that goes for all similar lines.
Re: Need help, cannot find error
Thanks, this problem is solved!)
I got next error here:
Code :
public int highestTempRecorded()
{
int highest = 0;
for(int index = 0; index < samples.size(); index++) {
[B]if(samples.get(index).getSampleValue() > highest)[/B]
highest = samples.get(index).getSampleValue();
}
System.out.println("The highest temperature recorded is:" + " " + highest);
}
Dunno what is wrong (
Re: Need help, cannot find error
Post your exception error message
Re: Need help, cannot find error
cannot find symbol - method getSampleValue()
Re: Need help, cannot find error
you havent got a getSampleValue() method anywhere in your classes.
Either create one or seek an alternative way of doing what you need.
Re: Need help, cannot find error
made it like this:
public int highestTempRecorded()
{
int highestTemp = samples[0];
int highestTempRecorded = 0;
for(int index = 1; index < samples.length; index++) {
if(highestTempRecorded < samples[index]) {
highestTempRecorded = samples[index];
highestTempRecorded = index;
}
}
}
got such a message: array required, but java.util.ArrayList<Sample> found
Re: Need help, cannot find error
Any ideas how to solve problem?
Re: Need help, cannot find error
Array[] and ArrayList are two different things
Re: need help with my code
Your Sample class doesn't have the method getSampleValue() (at least not as far as I can tell).
You need to explicitly declare this method and define it.
Re: Need help, cannot find error
In the future, please don't ask the same question in multiple topics. I've merged the two.
Also, please use highlight or code tags with your code.
[code]your code goes here[/code]
looks like:
[highlight=Java]//your code goes here[/highlight]
looks like: