What outpouring support, guys, twas a brain tease at best for me and I agree Jos, 'the remote control' scenario is what makes it work best when dealing with arrays, rest assured I was kidding about adding it to [ ] Well, you know I did, not denying that, and did it out of anger, after muscling through it a bit and could not figure it out:-) but this works fine:
[CODE=JAVA]
Automobile [] automobile; //declare automobile array
automobile = new Automobile[5]; //create automobile array
[/CODE]
And I seem to understand how it works. I call it fixed, but if it is as:
[CODE=JAVA]
int cStyle[];
int[] javaStyle;
[/CODE]
I was under the impression it was somewhat not-fixed, because I would give user the power to decide the size of the array, not setting it as in the case above it...
I am not too savvy in these types of arrays just yet, I had to set the above by hand, from the book, but by hand..
This should have worked, in my mind, it should have:
[CODE=JAVA]
public static void main (String[] args) {
Automobile[] automobile; //declare the Automobile array...
automobile = new Automobile[automobile]; //and then create Automobile array...
String name; //declare variables...
double rating1; //declare variables...
double rating2; //declare variables...
double rating3; //declare variables...
float averageRatingSc ale; //declare variables...
for (int i = 0; i < automobile.leng th; i++) { //run loop as if there's no tomorrow:-)
automobile = Integer.parseIn t(
JOptionPane.sho wInputDialog(nu ll,"Enter Number of Automobiles:")) ; //grab Automobile instances
name = JOptionPane.sho wInputDialog(nu ll,"Enter Favourite Automobile name:"); //demand for automobile name or preference
rating1 = Double.parseDou ble(
JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto # 1:")); //ask to enter rating scale 1 of favourite auto
rating2 = Double.parseDou ble(
JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto# 2:")); //ask to enter rating scale 2 of favourite auto
rating3 = Double.parseDou ble(
JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto # 3:")); //ask to enter rating scale 3 of favourite auto
//create a new Automobile and assign values to each instance
Automobile = new int[automobile];
automobile[i] = new Automobile (); //create your new Automobile
automobile[i].setName ( name ); //create instances of each favourite new Automobile
automobile[i].setrating1 ( rating1 ); //create instances of each favourite new Automobile rating scale 1
automobile[i].setrating2 ( rating2 ); //create instances of each favourite new Automobile rating scale 2
automobile[i].setrating3 ( rating3 ); //create instances of each favourite new Automobile rating scale 3
float sum = 0;
for (int i = 0; i < automobile.leng th; i++) {
sum += automobile[i].getRating1();
}
for (int i = 0; i < automobile.leng th; i++) {
sum += automobile[i].getRating2();
}
for (int i = 0; i < automobile.leng th; i++) {
sum += automobile[i].getRating3();
}
averageRatingSc ale = sum / (float) Automobile.leng th / 3;
System.out.prin tln("Maximum Rating Scale: " + sum);
System.out.prin tln("Average Rating Scale: " + averageRatingSc ale);
//TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
Automobile undoubtablySpor ty, //points to the Semi-Sporty Automobile
stupendouslySpo rty; //points to the Sportiest Automobile
undoubtablySpor ty = stupendouslySpo rty = automobile[0];
for (int i = 1; i < automobile.leng th; i++) {
if ( automobile[i].getRating1() < undoubtablySpor ty.getRating1() ) {
//found a weakest Automobile
undoubtablySpor ty = Automobile[i];
}
else if ( automobile[i].getRating1() > stupendouslySpo rty.getRating1( ) ) {
//found a strongest Automobile in this class...
stupendouslySpo rty = automobile[i];
}
} //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
System.out.prin tln("Rating Scale for Automobile named: " + stupendouslySpo rty.getName()
+ " is " + (stupendouslySp orty.getRating1 ()+ stupendouslySpo rty.getRating2( )+ stupendouslySpo rty.getRating3( ))
/ 3 + " grade point average.");
System.out.prin tln("Rating Scale for Automobile: " + undoubtablySpor ty.getName()
+ " is " + (undoubtablySpo rty.getRating1( )+ undoubtablySpor ty.getRating2() + undoubtablySpor ty.getRating3() )
/ 3 + " grade point average.");
//TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
String fecthAuto = JOptionPane.sho wInputDialog(nu ll,"Name Automobile to fetch:");
int i = 0;
while ( i < automobile.leng th && //still more Automobiles to search
!automobile[i].getName().equa ls( fecthAuto ) ) {
i++;
}
if (i == automobile.leng th) {
//not found - unsuccessful search
System.out.prin tln( fecthAuto + " was not in the array" );
} else {
//found - successful search
System.out.prin tln("Looks like we found " + fecthAuto + i + " level in the storage " + );
System.out.prin tln("Looks like we found " + fecthAuto + " having " + averageRatingSc ale);
}
}
}
}
[/CODE]
I think I lost it after that, so thank you BigDaddyLH for posting and to let me know I am not that far off, or pretty close, the above is pretty uggly, of course you know it does not work. But your examples/adivce will surely help pin it down
[CODE=JAVA]
Automobile [] automobile;
int automobileSize = ...; //compute size
automobile = new Automobile[automobileSize];
or
int automobileSize = ...; //compute size
Automobile [] automobile = new Automobile[automobileSize];
[/CODE]
My book is a true exercise-based book, where you get mismathed bit of code and some explaination on how it works and why, and it moves on to examples and samples of non-related code, important code like the one posted.
But you guys are right, I am probably reading too much into it. There's probably no reason to make the code do what it is not supposed to do.
Thanks for posting, I'll give examples a whirl;-)
Will let you know:-)
Köll
[CODE=JAVA]
Automobile [] automobile; //declare automobile array
automobile = new Automobile[5]; //create automobile array
[/CODE]
And I seem to understand how it works. I call it fixed, but if it is as:
[CODE=JAVA]
int cStyle[];
int[] javaStyle;
[/CODE]
I was under the impression it was somewhat not-fixed, because I would give user the power to decide the size of the array, not setting it as in the case above it...
I am not too savvy in these types of arrays just yet, I had to set the above by hand, from the book, but by hand..
This should have worked, in my mind, it should have:
[CODE=JAVA]
public static void main (String[] args) {
Automobile[] automobile; //declare the Automobile array...
automobile = new Automobile[automobile]; //and then create Automobile array...
String name; //declare variables...
double rating1; //declare variables...
double rating2; //declare variables...
double rating3; //declare variables...
float averageRatingSc ale; //declare variables...
for (int i = 0; i < automobile.leng th; i++) { //run loop as if there's no tomorrow:-)
automobile = Integer.parseIn t(
JOptionPane.sho wInputDialog(nu ll,"Enter Number of Automobiles:")) ; //grab Automobile instances
name = JOptionPane.sho wInputDialog(nu ll,"Enter Favourite Automobile name:"); //demand for automobile name or preference
rating1 = Double.parseDou ble(
JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto # 1:")); //ask to enter rating scale 1 of favourite auto
rating2 = Double.parseDou ble(
JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto# 2:")); //ask to enter rating scale 2 of favourite auto
rating3 = Double.parseDou ble(
JOptionPane.sho wInputDialog(nu ll,"Enter Rating Scale For Auto # 3:")); //ask to enter rating scale 3 of favourite auto
//create a new Automobile and assign values to each instance
Automobile = new int[automobile];
automobile[i] = new Automobile (); //create your new Automobile
automobile[i].setName ( name ); //create instances of each favourite new Automobile
automobile[i].setrating1 ( rating1 ); //create instances of each favourite new Automobile rating scale 1
automobile[i].setrating2 ( rating2 ); //create instances of each favourite new Automobile rating scale 2
automobile[i].setrating3 ( rating3 ); //create instances of each favourite new Automobile rating scale 3
float sum = 0;
for (int i = 0; i < automobile.leng th; i++) {
sum += automobile[i].getRating1();
}
for (int i = 0; i < automobile.leng th; i++) {
sum += automobile[i].getRating2();
}
for (int i = 0; i < automobile.leng th; i++) {
sum += automobile[i].getRating3();
}
averageRatingSc ale = sum / (float) Automobile.leng th / 3;
System.out.prin tln("Maximum Rating Scale: " + sum);
System.out.prin tln("Average Rating Scale: " + averageRatingSc ale);
//TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
Automobile undoubtablySpor ty, //points to the Semi-Sporty Automobile
stupendouslySpo rty; //points to the Sportiest Automobile
undoubtablySpor ty = stupendouslySpo rty = automobile[0];
for (int i = 1; i < automobile.leng th; i++) {
if ( automobile[i].getRating1() < undoubtablySpor ty.getRating1() ) {
//found a weakest Automobile
undoubtablySpor ty = Automobile[i];
}
else if ( automobile[i].getRating1() > stupendouslySpo rty.getRating1( ) ) {
//found a strongest Automobile in this class...
stupendouslySpo rty = automobile[i];
}
} //TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
System.out.prin tln("Rating Scale for Automobile named: " + stupendouslySpo rty.getName()
+ " is " + (stupendouslySp orty.getRating1 ()+ stupendouslySpo rty.getRating2( )+ stupendouslySpo rty.getRating3( ))
/ 3 + " grade point average.");
System.out.prin tln("Rating Scale for Automobile: " + undoubtablySpor ty.getName()
+ " is " + (undoubtablySpo rty.getRating1( )+ undoubtablySpor ty.getRating2() + undoubtablySpor ty.getRating3() )
/ 3 + " grade point average.");
//TO DO: Add/distinguish Rating 1 from Rating 2, and so on, and output ALL separately for all to see...
String fecthAuto = JOptionPane.sho wInputDialog(nu ll,"Name Automobile to fetch:");
int i = 0;
while ( i < automobile.leng th && //still more Automobiles to search
!automobile[i].getName().equa ls( fecthAuto ) ) {
i++;
}
if (i == automobile.leng th) {
//not found - unsuccessful search
System.out.prin tln( fecthAuto + " was not in the array" );
} else {
//found - successful search
System.out.prin tln("Looks like we found " + fecthAuto + i + " level in the storage " + );
System.out.prin tln("Looks like we found " + fecthAuto + " having " + averageRatingSc ale);
}
}
}
}
[/CODE]
I think I lost it after that, so thank you BigDaddyLH for posting and to let me know I am not that far off, or pretty close, the above is pretty uggly, of course you know it does not work. But your examples/adivce will surely help pin it down
[CODE=JAVA]
Automobile [] automobile;
int automobileSize = ...; //compute size
automobile = new Automobile[automobileSize];
or
int automobileSize = ...; //compute size
Automobile [] automobile = new Automobile[automobileSize];
[/CODE]
My book is a true exercise-based book, where you get mismathed bit of code and some explaination on how it works and why, and it moves on to examples and samples of non-related code, important code like the one posted.
But you guys are right, I am probably reading too much into it. There's probably no reason to make the code do what it is not supposed to do.
Thanks for posting, I'll give examples a whirl;-)
Will let you know:-)
Köll
Comment