Suliman Benhalim Suliman Benhalim - 4 months ago 9
Java Question

How To Make Declarations of an Array type Class

I am taking Data Structure Course, And I have a Question at the end of the Chapter that I couldn't solve.

class WeatherRecType {
int AvgHiTemp;
int AvgLoTemp;
float ActualRain;
float RecordRain;
}


Question:


Declare a one-dimensional array,
WeatherListType
of
WeatherRecType
components, contains ten elements.


Answer:

WeatherRecType WeatherListType[] = new WeatherRecType[10];





Question:


Assign the value 1.05 to the
ActualRain
field of the seventh record in
WeatherListType
.


Answer: ???

How to assign the value to the
ActualRain
in the Array
WeatherListType
field number 7?

I tried it like this, but it doesn't work:
Problem Image




The problem is solved, thanks to everyone who shared their knowledge with me.
Here is the correct answer:
Correct Answer Image

Answer

Seventh record of an array in most basic programming languages (C, Java etc.) translates to index 6, as the counting begins with the index 0. So you would require:

WeatherListType[6] = new WeatherRecType();
WeatherListType[6].ActualRain = 1.05f;

But since this is a Data Structure course you are referring to, 7 may work if you are talking in terms of Pseudo code or something.