Okay, I am having more difficulty with my Java class again. My professor assigned us a 2-D chart program... This is the output he wants:

This program displays the annual cost of gasoline for different mpg
and price per gallon of gas assuming 18,000 miles driven annually.

MPG 1.50 1.75 2.00 2.25 2.50 2.75 3.00 3.25
-------------------------------------------------------------------
5 5,400 6,300 7,200 8,100 9,000 9,900 10,800 11,700
10 2,700 3,150 3,600 4,050 4,500 4,950 5,400 5,850
15 1,800 2,100 2,400 2,700 3,000 3,300 3,600 3,900
20 1,350 1,575 1,800 2,025 2,250 2,475 2,700 2,925
25 1,080 1,260 1,440 1,620 1,800 1,980 2,160 2,340
30 900 1,050 1,200 1,350 1,500 1,650 1,800 1,950
35 771 900 1,028 1,157 1,285 1,414 1,542 1,671
40 675 788 900 1,013 1,125 1,238 1,350 1,463
45 600 700 800 900 1,000 1,100 1,200 1,300

Use the principles of encapsulation and generalization design your program with efficient functions. The formula for annual cost is:

annualCost = (18000 / mpg) * pricePerGallon

Below is what I have so far...

``````public class HW04_jsn
{
public static void main(String [] args)
{
double fuelCost[][] = new double [9][8];
System.out.println("MPG\t\t1.50\t\t1.75\t\t2.00\t\t2.25\t\t2.50\t\t2.75\t\t3.00\t\t3.00");
System.out.println("------------------------------------------------------------------------------------------------------");
int mpg = 5;
for (int m = 0; m < 9; m++);
{
System.out.println(mpg + "\t");
for (int cost = 0; cost < 8; cost++)
{
System.out.print(annualCost.format(mpg[m][cost]));
System.out.print("\t");
}
mpg++;
System.out.println();
}
}
public static double annualCost
(double m, double cost)
{
double annualCost = (18000 / m) * cost;
return annualCost;
}
}``````

The errors I am getting are:
cannot find variable m
array required, but int found
cannot find variable annualCost

(All on line 23)

I just don't know what I am doing wrong... every time I make a change, something else is sending an error. Could anyone please explain to me (as simply as possible) how to program a chart?

I do not ask that you give me the answer to my assignment, just some help to clarify things for me. You all were very helpful with the last assignment that I had problems with and I do greatly appreciate the advice.

You have syntax errors. Read through the code character by character and get rid of the syntax errors.

Now when I see this line, I observe two problems :-

``System.out.print(annualCost.format(mpg[m][cost]));``

Now when I see this line, I observe two problems :-

• There is no such as "annualCost" object declared or initialized in the main, and you are trying to call the method format(int) on it.
• If you are trying to call annualCost() method then you have got it all wrong.
• mpg[m][cost] :- Now you have no array called as mpg, it is just an int, which gives an indication that you haven't grasped the basic concepts of Java yet.

Just As The "Slammander Man" said reading the code (and might I add understanding the purpose of each) character by character would have easily fixed the problem and made you a whole lot wiser on the debugging front.
Also I would suggest going through a few tutorials like this and this to get a firm grasp on the basics of Java.

Yes, I admit I am having difficulty grasping the concepts of Java. I am in an introductory course... I hope to major in programming, but I am beginning to question my ability. I am determined to learn Java, but my instructor isn't the greatest at explaining things, the textbook is mediocre... I end up researching everything over the internet and I even bought a Java for Dummies book. Trust me, I read all the resources I can get. Thanks for the help.

After working on my program some more... (I completely changed it), This is what I came up with so far:

``````public class HW04_jsn
{
public int cost;
public int mpg;
public int annCost;

public void main(String [] args)
{
double cost;
while (cost < 4)
cost = 1.50 + .25;
System.out.println("\t" + cost + "\t");
System.out.println("----------------------------------------------------------------------");
printTable();
double annCost = calcAnnCost(cost, mpg);

System.out.println("\t" + annCost + "\t");
}

public static void printTable()
{
int i = 1;

while (i <= 9) {
i++;
}
}
public static double calcAnnCost(double cost, double mpg)
{
double annCost = (18000/mpg) * cost;
return annCost;
}
}``````

I am still coming up with issues, but now the changes I made have done something to cause the program not to print. This is the message I get when I try to print the program:

java.lang.NoSuchMethodError: main
Process completed.

I am so confused. I am trying to follow my books and what I have found over the internet, but nothing seems to work.

Okay here is another attempt. I am only partially done with this program, but is running properly as of now.

``````public class HW04_jsn
{
public static void main(String [] args)
{
System.out.println("\tMPG\t\t1.50\t1.75\t2.00\t2.25\t2.50\t2.75");
System.out.println("------------------------------------------------------------");
printTable();
}

public static void printTable()
{
int i = 1;

while (i <= 9)
{
printMultiples(i);
i++;
}
}
public static void printMultiples(int n)
{
int i = 5;

while (i <= 5)
{
System.out.printf("%5d", n * i);
i++;
}
System.out.println("");
}
}``````

I know I have only written a part of this program right... I just don't know how to incorporate the math method since I basically used the price per gallon as a heading rather than variable... I have to use this formula: annualCost = (18000 / mpg) * pricePerGallon (in place of the n * i) to calculate the fill information for the chart. A problem I ran into was trying to create a loop to output the price per gallon... Everytime I wrote it as a double, error because for that method doubles could not be used and when I tried float, an error stated that I was losing precision... UGH... So the only way I found that would print the column and row headings was this program. (The row heading is the mpg variable and the column heading is the price per gallon (supposed) variable.)

I hope this makes sense... because I do need the help. I appologize for my lack of knowledge... I am trying to learn Java, but it is more challenging than I anticipated. I appreciate the help.

Now I would like you to also go through the Sun Coding Conventions for Java so that you can name your classes and methods appropriately.
Also if you are just learning Java the Sticky "Starting Java" has linked to quite a few excellent resources.

Now the price per gallon list you could have stored it using Array. Like this :-

``double pricePerGallon[] = { 1.50, 1.75, 2.00, 2.25, 2.50, 2.75 };``

Also Take a look at the Object Oriented Programming Tutorial I had linked to earlier and also this one I picked from the sticky.
Because Java is best used as an Object Oriented Programming Language and the earlier you start developing your thinking in Object Oriented terms the more easier you will find it later to design your programs / applications using OOP.

Now I would like you to also go through the Sun Coding Conventions for Java so that you can name your classes and methods appropriately.

You should skip this. You already have enough to worry about, and spending mental energy worrying about 11 chapters of arbitrary rules at this stage is a form of insanity. You will pick up awareness of standard practices by writing Java code and reading others' code.

You should skip this. You already have enough to worry about, and spending mental energy worrying about 11 chapters of arbitrary rules at this stage is a form of insanity. You will pick up awareness of standard practices by writing Java code and reading others' code.

Wow !! Directly contradicting someone elses suggestion without actually adding anything useful. I do not want to be hostile here but I did not ask the O.P to do a Ph.D on it, I only asked him to go through it. Also following some simple uniform conventions for variable naming and indentations etc help reduce silly mistakes which beginners are more susceptible to.
And the most important part is he is learning to program then he might as well learn some good things along the way rather than just skip all the good pratices which these programs are supposed in grain in him so that he doesn't fall part when he actually has to make something more important than a home work assignment.

Wow !! Directly contradicting someone elses suggestion without actually adding anything useful.

The useful thing I added was the direct contradiction and reversal of bad advice. I think yours is well off the track of any optimal educational path.

Well thanks you two... And SHE (sorry had to correct) will definately read up on any material that will help me understand Java (period). As Stephen84s said, I am learning this for the future... hopefully for a career (not just homework assignments). So far all I have learned in my class is how to work on homework and its not flowing together very well. I partially blame that on myself for lack of understanding, but I also think my profressor is a little hasty when explaining things. I appreciate the advice and I will try to apply arrays to this program as you suggested. Thanks again. I will post a new program as soon as I get it altered (and hopefully functioning better).

commented: Shucks this is the third time, I guess I have to pay more attention to profiles before assuming the gender :P +5

Well, I am half way done with my program. Here is what I have thus far:

``````/**
* HW04_jsn.java
* Jacqualyn Nelson
* 2009/2/2
*
* Two-Dimensional Table
*/

public class HW04_jsn
{
double[][] annualCost = new double[9][8];
public static void main(String [] args)
{
System.out.println("MPG\t\t1.50\t1.75\t2.00\t2.25\t2.50\t2.75\t3.00\t3.25");
System.out.println("--------------------------------------------------------------------");
{
int[] milesPerGallon = new int[] {5, 10, 15, 20, 25, 30, 35, 40, 45};
for (int i = 0; i <= 45; i++)
System.out.println(milesPerGallon[i] + "\t\t");
}
}
}``````

I have two main concerns.
1) When I build the program in JCreator, there are no errors found, but when I run the program, it prints correctly, but it states this Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
at HW04_jsn.main(HW04_jsn.java:20)
prior to completion. As far as I know, I have set the boundaries correclty... but no matter how I change the boundaries, that always prints after the program.

2) The major portion of this program is to compute annual cost for gas, I understand how to create a method for performing this function, but is it different with arrays? Because all the examples I have seen don't explain how to incorporate this. And, when I tried to write the method myself, it didn't mesh with the program because of arrays.

I am just a little confused as to how to go about writing the portion of the program that fills in the main chart information. It is not as simple as a * b, so I can't just tell it to print arrayA * arrayB like most examples show.

Thanks again for the advice... it is helping a lot!

Your array has nine elements. The expression `milesPerGallon[9]` tries to access the tenth. So the index is out of bounds.

You're iterating i from 0 to 45, which makes no sense. You should be iterating i from 0 to 8, because those are the valid indices for your array.

Thanks... I thought it was one less for boundaries, but that makes sense. And I was iterating 45 because I thought it was for the amount of the variable, not the number of variables... but again thanks. That does help.

Now that I have that portion of the program running as it should... Is there any advice or links for information on how to use the variables in those arrays for the portion of the program that is supposed to calculate 18000/milesPerGallon * pricePerGallon?
Thar portion is supposed to fill the info in the chart.

I have tried several different ways to write this part of the program, but I mutilated it so bad that I don't even want to post what I came up with. Ugh!

Thanks.

My suggestion would be use another Array for storing your "pricePerGallon".
Like this:-

``double pricePerGallon[] = { 1.50, 1.75, 2.00, 2.25, 2.50, 2.75 };``

Then you will need to modify your "for" loop and add another nested for loop inside to perform the calculations and print the results for each line in this manner:-

``````for (int i = 0; i <= 45; i++) {
System.out.print(milesPerGallon[i] + "\t\t");
for(int j=0;j<pricePerGallon;j++) {
// Perform your calculations here using the corresponding
// values from milesPerGallon and pricePerGallon arrays.

}
// We will want to go to the new line after printing
// the calculations of the current milesPerGallon value
System.out.println();
}``````

Thank you for the help, I hope I followed your advice correctly... as I am still getting an error.

``````/**
* HW04_jsn.java
* Jacqualyn Nelson
* 2009/2/2
*
* Two-Dimensional Table
*/

public class HW04_jsn
{
double[][] annualCost = new double[10][8];
public static void main(String [] args)
{
System.out.println("MPG\t\t1.50\t1.75\t2.00\t2.25\t2.50\t2.75\t3.00\t3.25");
System.out.println("--------------------------------------------------------------------");
{
int[] milesPerGallon = new int[] {5, 10, 15, 20, 25, 30, 35, 40, 45};
for (int m = 0; m <= 8; m++)
System.out.println(milesPerGallon[m] + "\t\t");
}
}
{
double[] milesPerGallon = {5, 10, 15, 20, 25, 30, 35, 40, 45};
{
double[] pricePerGallon = {1.50, 1.75, 2.00, 2.25, 2.50, 2.75};
for (int m = 0; m <= 45; m++)
{
System.out.print(milesPerGallon[m] + "\t");
for(int p = 0; p < pricePerGallon; p++)
{
System.out.print((18000/m) * p);
}
System.out.println();
}
}
}
}``````

The error I am getting is:

Line 30 - operartor < cannot be applied to in, double[]

What am I do wrong that would throw that error?

You will need to move this declaration inside the main.

``double[][] annualCost = new double[10][8];``

<EDIT>
In fact you can remove it altogether because the only reason you are not getting any errors because of it is due to the fact that you are never using it in your main.
</EDIT>

Now by this `p < pricePerGallon` , I am guessing you meant `p < pricePerGallon.length` ,

Also by `for (int m = 0; m <= 45; m++)` , you will need to change that to `for (int m = 0; m <=milesPerGallon.length ; m++)` else you will get the array index out of bounds exception, because the size of your
milesPerGallon array is only 9, whereas the for loop would run from m=0 to m=45, and when "m" crosses the value of 8 you will get the array index out of bounds exception as your array "milesPerGallon" has only 9 indexes or elements indexed from 0 to 8.
And just for your info whenever you call <ArrayName>.length, it gives you the size of the array.

For some reason when I replied earlier, it didn't post. Oh well.

Anyways, yes I understand the <Array>.length, I think I was just overwhelmed with the program and forgot to add it in. Thanks for the reminder though. I appreciate your help and took your suggestions... Here is the new code:

``````/**
* HW04_jsn.java
* Jacqualyn Nelson
* 2009/2/2
*
* Two-Dimensional Table
*/

public class HW04_jsn
{
public static void main(String [] args)
{
System.out.println("MPG\t\t1.50\t1.75\t2.00\t2.25\t2.50\t2.75\t3.00\t3.25");
System.out.println("--------------------------------------------------------------------");
{
int[] milesPerGallon = new int[] {5, 10, 15, 20, 25, 30, 35, 40, 45};
for (int m = 0; m <= 8; m++)
System.out.println(milesPerGallon[m] + "\t\t");
}
}
{
double[] milesPerGallon = {5, 10, 15, 20, 25, 30, 35, 40, 45};
{
double[] pricePerGallon = {1.50, 1.75, 2.00, 2.25, 2.50, 2.75};
for (int m = 0; m <= milesPerGallon.length; m++)
{
System.out.print(milesPerGallon[m] + "\t");
for(int p = 0; p <= pricePerGallon.length; p++)
{
System.out.print((18000/m) * p);
}
System.out.println();
}
}
}
}``````

For some reason, although no errors are found... the chart is still not filling. The 'headings' for rows and columns print appropriately now... just not the rest. Am I missing something obvious here? I checked over the code and I think I wrote it correctly, but who knows... I am wrong a lot! I have been reading up on Java, more specifically arrays this week and I think I am catching on... I just need A LOT of practice!!

Thanks again for all the help!

To see if the portion of the program for the math function is written correctly at all, I wrote a separate code with just that in it...

``````/**
* @(#)test.java
*
*
* @author
* @version 1.00 2009/2/8
*/

public class test
{
public static void main(String [] args)
{
double[] milesPerGallon = {5, 10, 15, 20, 25, 30, 35, 40, 45};
{
double[] pricePerGallon = {1.50, 1.75, 2.00, 2.25, 2.50, 2.75};
for (int m = 0; m <= milesPerGallon.length; m++)
{
System.out.print(milesPerGallon[m] + "\t");
for(int p = 0; p <= pricePerGallon.length; p++)
{
System.out.print((18000/m) * p);
{
System.out.println();
}
}
}
}
}
}``````

There were no errors found, but when I ran the file, this is what printed:

5.0 Exception in thread "main" java.lang.ArithmeticException: / by zero
at test.main(test.java:22)

Process completed.

Does that mean it is trying to divide by zero? If so, why? What can I do to rectify this?

Okay, just playing around with the "test" program, I made one minor change:

``````double[] pricePerGallon = {1.50, 1.75, 2.00, 2.25, 2.50, 2.75};
for (int m = 1; m <= milesPerGallon.length; m++)
{
System.out.print(milesPerGallon[m] + "\t");
for(int p = 1; p <= pricePerGallon.length; p++)``````

The program printed the output!!! But for some reason, when I made the same change to my original program, nothing printed... WHY??? How can it work in one program, but not the other? Could there be a glitch in my software or something that would cause this?

Okay, nevermind... It has some false output and an out of bounds code... ugh. back to work.

Ok another simple change to the test program that holds only the math portion:

``````double[] pricePerGallon = {1.50, 1.75, 2.00, 2.25, 2.50, 2.75};
for (double m = 0.0; m <= milesPerGallon.length; m++)
{
for(double p = 0.0; p <= pricePerGallon.length; p++)``````

This resulted in output, just the wrong output. And if I apply the same changes to the original program, it still will not print.

``System.out.print((18000/m) * p);``

This is what caused the divide by zero problem. If you trace back correctly the value of "m" at the start of the loop ( `for (int m = 0; m <= milesPerGallon.length; m++)` ) is "0" and as per that formula you are dividing by zero on the first iteration.

Also you seem to have missed the concept of using arrays here.
In the following formula `(18000/m) * p` , we do not want the value of "m" nor "p", we want the value at index "m" in the milesPerGallon array and the value at index "p" in the pricePerGallon array respectively. That is the formula would become :-

``System.out.print((18000/milesPerGallon[m]) * pricePerGallon[p]);``

And to print the table correctly in your original code you will need to chuck out this section at the start:-

``````for (int m = 0; m <= 8; m++)
System.out.println(milesPerGallon[m] + "\t\t");
}``````

This will cause the milesPerGallon to be printed on separate lines with just empty values, kind of like :-

``````MPG 1.50 1.75 2.00 2.25 2.50 2.75 3.00 3.25
-------------------------------------------------------------------
5
10
15
20
25
30
35
40
45``````