in java, why does it say the variable revenueHouston \"cannot be resolved to a v
ID: 3879819 • Letter: I
Question
in java, why does it say the variable revenueHouston "cannot be resolved to a variable" in the following Java code:
public class groceryStore {
public static void main(String[] args) {
grocery houstonStore = new grocery ();
grocery seattleStore = new grocery ();
grocery orlandoStore = new grocery ();
houstonStore.applesSoldYearly = 534;
houstonStore.appleRetailPrice = 0.99;
houstonStore.orangesSoldYearly = 429;
houstonStore.orangeRetailPrice = 0.87;
seattleStore.applesSoldYearly = 765;
seattleStore.appleRetailPrice = 0.86;
seattleStore.orangesSoldYearly = 842;
seattleStore.orangeRetailPrice = 0.91;
orlandoStore.applesSoldYearly = 402;
orlandoStore.appleRetailPrice = 0.77;
orlandoStore.orangesSoldYearly = 398;
orlandoStore.orangeRetailPrice = 0.79;
/*System.out.println("The Houston store sold: " + houstonStore.applesSoldYearly + " apples. " + " The apple retail price was: $" +
houstonStore.appleRetailPrice + " The Houston store sold: " + houstonStore.orangesSoldYearly + " oranges" +
" The orange retail price was : $" + houstonStore.orangeRetailPrice );*/
revenueHouston = (houstonStore.applesSoldYearly * houstonStore.appleRetailPrice) + (houstonStore.orangesSoldYearly *
houstonStore.orangeRetailPrice);
}
}
class grocery {
int applesSoldYearly;
double appleRetailPrice;
int orangesSoldYearly;
double orangeRetailPrice;
}
/* double GrossRev(){
double total;
total = applesSoldYearly * appleRetailPrice + orangesSoldYearly * orangeRetailPrice;
System.out.println("The total gross revenue for this particular store is: $" + total);
return total;
2 public class groceryStore 3 public static void main(String] args) ( grocery houston$tore new grocery (); grocery seattleStore new grocery (); grocery orlandoStorenew grocery; houstonStore.applesSoldYearly 534; houstonStore.appleRetailPrice 0.99; houstonStore.orangesSoldYearly429; houstonStore.orangeRetailPrice0.87; seattleStore.applesSoldVearly = 765; seattleStore.appleRetailPrice 0.86; seattleStore.orangesSoldYearly842; seattleStore.orangeRetailPrice 0.91 orlandostore.applesSoldYearly 402; orlandoStore.appleRetailPrice = 0.77; orlandoStore.orangesSoldVearly 398; orlandoStore.orangeRetailPrice- 0.79 System.out-println("The Houston store sold: " + houstonStore.applesSo1dYearly + " apples. " + " The apple retail price was: $ 25 6 houstonStore.appleRetailPrice "nThe Houston store sold: " + houstonStore.orangesSoldYearlyoranges" " The orange retail price was : S" + houstonStore . orangeRetailPrice );"/ revenueHouston = (houstonStore.applesSoldYearly * houstonStore.appleRetailPrice) + (houstonStore . orangesSoldYearly * 1 houstonStore.orangeRetailPrice); 4 8 39 0 class grocery int applesSoldYearly; double appleRetailPrice; int orangesSoldYearly; double orangeRetailPrice; 8 double GrossRev) double total; total applesSoldYearly appleRetailPrice orangesSoldYearly orangeRetailPrice; System.out.println("The total gross revenue for this particular store is:"total); return total; 3)Explanation / Answer
Here we have not declared any data type to the variable revenueHouston hence the error that it cannot be resolved to variable.
Like we have four variables in class Grocery
applesSoldYearly of type int
appleRetailPrice of type double
orangesSoldYearly of type int
orangeRetailPrice of type double.
Now in our code we are trying to store revenue calculated on the basis of provided logic and store it in revenueHouston but this needs to have a data type like other variable we saw on the basis of what kind of data it may hold. And as per the expression i could see revenueHouston should be declared double type.
So if we make following change
double revenueHouston = (houstonStore.applesSoldYearly * houstonStore.appleRetailPrice) + (houstonStore.orangesSoldYearly * houstonStore.orangeRetailPrice);
Then we will not get any errors.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.