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/what's wrong with this code?Also i don't understand how i...
Noob_Coder
Level 22
Springfield
  • 10/14/20
  • 846views
  • 9comments

what's wrong with this code?Also i don't understand how it works completely

Question about the task Let's put together a rectangle
Java Syntax,  Level 5,  Lesson 7
Resolved


Create a Rectangle class. The data for this class will be top, left, width, height.
Create as many initialize(...) methods as possible

Here are some examples:
- 4 parameters are specified: left, top, width, and height
- width/height is not specified (both are 0)
- height is not specified (it is equal to the width), we'll create a square
- create a copy of another rectangle passed as an argument

Requirements:
  • The program must not read data from the keyboard.
  • The Rectangle class must have int variables top, left, width and height.
  • The class must have at least one initialize method.
  • The class must have at least two initialize methods.
  • The class must have at least three initialize methods.
  • The class must have at least four initialize methods.
package com.codegym.task.task05.task0513; /* Let's put together a rectangle */ public class Rectangle { //write your code here private int top ; private int left; private int width; private int height; public void initialize(int top){ this.top = top; } public void initialize(int top, int left){ this.top = top; this.left = left; width = 0; height = 0; } public void initalize(int top , int left , int width){ this.top = top; this.left = left ; this.width = width; height = width; } public void initalize(int top,int left,int height,int width){ this.left= left; this.top = top; this.height = height; this.width = width; } public static void main(String[] args) { } }
0
Comments (9)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
envy
Level 6 , Detroit, United States
11 February 2021, 05:46
I have a basic question and sorry if this dumb, but just trying to get the concept right. Since the variables here are not class variables, dont they have to be referenced with the this keyword in the methods they are used? For eg in this section, the width and height initializations should be this.height and this.weight
public void initialize(int top, int left){

    this.top = top;
    this.left = left;
    width = 0;
    height = 0;
}
0
Nouser
Level 36 , Germany
11 February 2021, 07:12
The problem is, that top and left variables aren't unambiguous here. They are parameters and instance varibales. In such a case the 'most inner' variables get used. So if you want to refer to the instance variable you need some way to address them and that's this. If the nameing is unambiguous you can omit this but it does not harm either. this means the current object and this.top you can translate with the variable top of the current object. Then this.top = top; means: assign the argument top to the instance variable top of the current object.
0
envy
Level 6 , Detroit, United States
14 February 2021, 08:35
Thanks. The same applies to height and width, as in they are parameters and instance variables, so how come not initialized with this ?
0
Guadalupe Gagnon
Level 37 , Tampa, United States
14 October 2020, 18:39
Check for spelling errors
0
Noob_Coder
Level 22 , Springfield, United States
Expert
15 October 2020, 07:22
Dude,could u give some hints?
0
Misiu
Level 41 , Gdansk, Poland
15 October 2020, 11:18solution
init-i-alize
+3
Guadalupe Gagnon
Level 37 , Tampa, United States
15 October 2020, 14:15solution
Look on line 26 and line 32
+2
Noob_Coder
Level 22 , Springfield, United States
Expert
15 October 2020, 14:20
omg i didn't consider that.
+1
Noob_Coder
Level 22 , Springfield, United States
Expert
15 October 2020, 14:21
Very appreciate for help!
+1
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 © 2026 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2026 CodeGym