CodeGym
Promotion
CodeGym University
Learning
Courses
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
CodeGym/Help with Java Tasks/pls help
Prahlad Singh
Level 6
Mumbai
  • 13.09.2018
  • 1957views
  • 4comments

pls help

Question about the task Price of apples
Java Syntax,  Level 4,  Lesson 1
Under discussion


Calculate the total cost of apples.
The total cost of apples corresponds to public static int applePrice.

Requirements:
  • 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.
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 static int a = 0; public static void addPrice(int applePrice) { //write your code here applePrice = applePrice + applePrice; } } }
+2
Comments (4)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Shahanshah Alam
Level 9 , Mohali, India
14 September 2018, 18:05
to differentiate between class and method variable use classname.variablename
0
Collin M
Level 22 , Germiston, South Africa
14 September 2018, 06:08
The way you accessed the applePrice variable in the println method call in main, it's exactly how you need to access it in the addPrice method. I recommend you do a brief study of the keyword "static"
0
Khurram
Level 16 , Lahore, Pakistan
13 September 2018, 06:36
go through the lesson about how to access static class variables.
0
Artem Divertitto Senior Android Developer at United Tech
13 September 2018, 06:25
In method addPrice, you interact only with method's argument with name applePrice. You need use class name to call static variable
+2
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning, and a Java developer’s career.
Follow us
Interface language
English
Deutsch Español हिन्दी Français Português Polski বাংলা 简体中文 मराठी தமிழ் Italiano Bahasa Indonesia 繁體中文 Nederlands 日本語 한국어 Bulgarian Danish Hungarian Basa Jawa Malay Norwegian Romanian Swedish Telugu Thai Українська Filipino Turkish Azərbaycan Русский Vietnamese
Programmers Are Made, Not Born © 2025 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2025 CodeGym