Failed Requirements:
2. The Apple class's addPrice method should not display text on the screen.
RECOMMENDATION FROM YOUR MENTOR
Be sure that the Apple class has a public static void addPrice(int applePrice) method.
4. The main method should call the addPrice method only twice.
RECOMMENDATION FROM YOUR MENTOR
Check the main method. Call the addPrice method only twice.
5. The Apple class's addPrice method should increase the cost of apples by the passed-in value.
I know "a static method can't access non-static variables and methods (since it has no 'this' to pass to these methods)", so I change it to non-static.
I didn't see the addPrice method displays text. Why the addPrice method should be static?
Does the main method call the addPrice method more than twice? How?
The screen output from the program says "The cost of apples is 150", but I still failed. Why?
package com.codegym.task.task04.task0402;
/*
Price of apples
*/
public class Solution {
public static void main(String[] args) {
Apple apple = new Apple();
apple.addPrice(50);
Apple apple2 = new Apple();
apple2.addPrice(100);
System.out.println("The cost of apples is " + Apple.applePrice);
}
public static class Apple {
public static int applePrice = 0;
public void addPrice(int applePrice) {
this.applePrice = applePrice + this.applePrice;
}
}
}