Skip to content

Instantly share code, notes, and snippets.

@mamiwinsfall93
Created January 17, 2020 14:08
Show Gist options
  • Save mamiwinsfall93/496142efd87aafa16523f58afa0d9689 to your computer and use it in GitHub Desktop.
Save mamiwinsfall93/496142efd87aafa16523f58afa0d9689 to your computer and use it in GitHub Desktop.
Price of apples
/*
• The program should display text on the screen.
• The Apple class's addPrice method should not display text on the screen.
• The Apple class's applePrice variable must be a static int initialized to zero.
• The main method should call the addPrice method only twice.
• The Apple class's addPrice method should increase the cost of apples by the passed-in value.
*/
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 static void addPrice(int applePrice) {
Apple.applePrice= Apple.applePrice + applePrice;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment