Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Elements in double array (java)

1. ## Elements in double array (java)

Hello folks i have been working on a code for a couple hours now and decided i need help. I am a beginner in java but usually learn quick. Ill first post the info on the code then post the two versions i have. Any help is greatly appreciated.

Code Info:

A 10-element double array, called ref, holds the weekly reference temperatures used in a
physical process. The process is run five days a week and the 10 temperatures actually
observed in the process on a single day are recorded in an array called obs (number of elements in
obs is 10). The elements in the two arrays have a one-to-one correspondence; e.g., ref and obs refer to
the reference and observed temperatures of the same physical entity.

For a given day of the week, the total deviation (for all the 10 entities) of the observed values from the reference
values is measured as the RMS (root mean squared) deviation:
daily total deviation = sqrt((1/10) * sum_{i= 1 to 10 elements} (ref[i] - obs[i])^{2}))

What it should do:

The program should simulate the RMS deviations for each of the five days of the week.
For a given week, the reference temperatures are fixed (all five days of a given week have the same
reference temperatures), but for two different weeks they are different. The actual temperatures are observed and recorded daily.Print the reference temperatures and the total RMS deviation for each of the five days of a given week.

The input to the program is:

an integer between 1 and 52 to indicate the week of the year.

The outputs are:

(i) the 10 reference values for the week and (ii) 5 RMS deviations, one for each day of the given week.
Use the java random number generator Random and the method nextDouble() (as discussed in class) to obtain
the temperature values. Use initial seed value(s) (don't use system-supplied seeds) so that different
runs of your program with the same input produce the same results (i.e., your program should produce exactly the same output when supplied with the same initial seed(s) and the same input). Note that ref[] and obs[] are one-dimensional arrays: do not make obs two-dimensional in an attempt to hold all 5 days. Use a loop for the five days of the week.

HELP:

My 1st code: Temperature.java

```
public class Temperature {

public static void main(String[] args) {
char[] ref  = createArray();

System.out.println("Enter a integer between 1 and 52 to indicate the week o
f the year: ");
displayArray(ref);

int[] counts = countLetters(chars);

System.out.println();
System.out.println("The 10 reference values for the week are: ");
displayCounts(counts);

public static char[] createArray() {

char[] ref = new char;

for (int i = 0; i < chars.length; i++)
chars[i] = Random```

My 2nd code: weektemp.java

```
public class weektemp {

public static void main(String[] args) {
// declares an array of integers
int[] anArray;

// allocates memory for 10 integers
anArray = new int;

// initialize first element
anArray = 100;
// initialize second element
anArray = 200;
// etc.
anArray = 300;
anArray = 400;
anArray = 500;
anArray = 600;
anArray = 700;
anArray = 800;
anArray = 900;
anArray = 1000;

System.out.println("Reference value 0: "
+ anArray);
System.out.println("Reference value 1: "
+ anArray);
System.out.println("Reference value 2: "
+ anArray);
System.out.println("Reference value 3: "
+ anArray);
System.out.println("Reference value 4: "
+ anArray);
System.out.println("Reference value 5: "
+ anArray);
System.out.println("Reference value 6: "
+ anArray);
System.out.println("Reference value 7: "
+ anArray);
System.out.println("Reference value 8: "
+ anArray);
System.out.println("Reference value 9: "
+ anArray);
}
}

public static double rms(double[] nums){
double ms = 0;
for (int i = 0; i < nums.length; i++)
ms += nums[i] * nums[i];
ms /= nums.length;
return Math.sqrt(ms);
}

public static       void main(String[] args){
double[] nums = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
System.out.println("The 5 RMS deviations are: + rms(nums)");
}
}```  Reply With Quote

3. ## Where are your comments outlining what you're trying to accomplish in your class? I find that i have an easier time solving my problem if I outline what I'm supposed to be doing in my code. Hope at helps.  Reply With Quote

4. ## Re: Elements in double array (java)

i usually write mine on a pc of paper like a pseudo code on what i need to do and then try to match it step by step with my code.  Reply With Quote

5. ## Re: Elements in double array (java)

Also, what is the problem? "I need help" doesn't say much.

Sorry to sound abrupt, but even before I look at the code it would be good to know: does it compile? (if not what lines do the compiler messages refer to) does it do something unwanted or unintended at runtime? (if it throws an exception, what is it? if it gives "wrong" output for given input, what was the input, the output and what you expected the output to be?)  Reply With Quote

6. ## Re: Elements in double array (java)

Sorry about that. I believe my code is still not done. I guess i want to know if i am on the correct path and if not i need help with that. Remember that i have two versions of code. The reason for that is i started one and then thought of another way to maybe do it. Hope this may help you help me.

Temperature.java compilation:

```
javac Temperature.java
Temperature.java:18: illegal start of expression
public static char[] createArray() {
^
Temperature.java:24: ';' expected

^
2 errors```

weektemp.java Compilation:

```
% javac weektemp.java
weektemp.java:66: 'class' or 'interface' expected
public static double rms(double[] nums){
^
weektemp.java:78: 'class' or 'interface' expected
}
^
weektemp.java:128: 'class' or 'interface' expected
^
3 errors```  Reply With Quote

7. ## Re: Elements in double array (java)

Check that your { }s are properly paired.
One method should end with a } before another method begins.  Reply With Quote

8. ## Re: Elements in double array (java) Originally Posted by wrx12 ```
javac Temperature.java
Temperature.java:18: illegal start of expression
public static char[] createArray() {
^
Temperature.java:24: ';' expected

^
2 errors```
You are getting these errors because you are attempting to declare the createArray() method *inside* the main() method, which confuses the compiler. Make sure you have finished the main() method - ie you have reached the closing } - before you start on the createArray() method.

```
% javac weektemp.java
weektemp.java:66: 'class' or 'interface' expected
public static double rms(double[] nums){
^
weektemp.java:78: 'class' or 'interface' expected
}
^
weektemp.java:128: 'class' or 'interface' expected
^
3 errors```
This one is the opposite problem. If you count the closing } brackets you will see that you have not only finished the main() method but you have also finished the weektemp class before the code for rms() and main() is reached. Notice that the number of { and } in this code do not match - there is an extra }. Basically the } just before rms() should be removed.

This class (btw class names should start with a capital letter and be descriptive of what they represent) has a second main() method declared. It's almost as if you pasted the two method definitions onto an already existing class definition.

-----

Are the instructions as you posted them what you were given? I ask because the description of the input to the program being the week number is rather odd: the week number is not actually used anywhere. Also the first order of business of your program would seem to be creating an array holding the 10 reference values for that week. But, where do these reference values come from? The description of the problem does not say. The temperature values are to be created randomly (with no hint given as to the range or shape of distribution) but that comes later.

So, if I were you I would begin by creating the array ref and populating it. Check, if necessary, to see where its values are to come from. And if they are also to be created randomly what theparameters of the underlying random distribution should be.

All this has to happen before you can make any headway on calculating the variations of temperature values from these reference values.

 got sidetracked halfway through composing this post and, so, didn't see Norm's above...  Reply With Quote

9. ## Re: Elements in double array (java)

ok so i took your advice and started a new code called Temp.java with creating a array ref and populating it with random numbers. Hopefully its right if not let me know what is correct. Also if it is correct what would be the next best possible step. We can take it step by step therefor we can satisfy the above instructions of my program. We can focus on this code now (Temp.java) that way we dont get confused.

Temp.java code:

```
public class Temp {

public static void main(String[] args) {
int ref[] = new int ;

for (int i = 0; 1 < 10; i++)
{
ref [i] = (int) (Math.random () * 10);
}
}
}```  Reply With Quote

10. ## Re: Elements in double array (java)

OK. Just an aside, but your instructions appear to my eye at least to be pretty random. There's nothing you can do about this, of course...

-----

My guess would be that ref should be an array of double values rather than int. In any case you are going to do floating point arithmetic with them and casting is pretty tedious.

Also one thing the instructions *are* clear about is that you should use the Random class initialised with a seed value rather than using Math.random(). The idea is that the program should give the same output every time it is run. Something like (untested):

```public class Temp {
private static Random rand = new Random(666L);

public static void main(String[] args) {
// etc
ref[i] = rand.nextDouble() * 10;
}
}```

Thirdly, you will now be in a position to produce the first output that you are asked for: "(i) the 10 reference values for the week".

-----

You still haven't got any temperatures much less rms() values! However it's a question of working one step at a time. At this point, if I were doing this problem I would probably want to move code out of main() which has already grown too large for my taste. Something like this (the comments being as important as the code I haven't included):

```public class Temp {
/** The random number generator. */
private static Random rand = new Random(666L);
/** The reference values for this week. */
private static double[] ref;

public static void main(String[] args) {
createRef();
printRef();
}

/** Creates the ref array and fills it with randomly generated values. */
private static void createRef() {
// code here
}

/** Prints the reference values. */
private static void printRef() {
// code here
}
}```

Do you understand the significance of moving the declaration of ref outside main()?

These 4 things will get you to a point where you can compile and run your program to see (the same) 10 reference values printed out. Test (ie confirm that). Then think about the next thing the question is asking you to do.  Reply With Quote