6 Replies - 1296 Views - Last Post: 25 September 2013 - 09:17 PM Rate Topic: -----

#1 atressler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-September 13

Creating a console mode Screen object

Posted 25 September 2013 - 09:19 AM

Hello all, I am new to learning Java and I seem to be struggling. I've done hours of reading but I'm just not grasping the information as quickly as I'd like. I guess it doesn't help that I'm learning 2 other programs as well as java. So here is my question:

Given the following class (only shown in fragments here),

public class Screen
{

    public Screen(int xRes, int yRes)
    {
     ...
    }

    public int numberOfPixels()
    {
     ...
    }

    public void clear(boolean invert)
    { 
     ...
    }

}


write some lines of Java code that create a Screen object. Then call its clear method if (and
only if) its number of pixels is greater than two million. (Don’t worry about things being logical
here; the goal is only to write something that is syntactically correct—i.e., that would compile
if we typed it in.)

I thought I had a good understanding at first, but after trying to write the code, I'm just confusing myself. I have different versions that I've written trying to figure it out, but after talking to some people who know a little about java, they said that I'm probably doing WAY too much than what's needed. So, here's what I have now:


public class Screen

{
    private int xRes;
    private int yRes;


    public Screen(int xRes, int yRes)
    { 
         width = new numberOfPixels(450)
         heigth = new numberOfPixels(550)
    }

    public int numberOfPixels()
    { 
         Screen object = new Screen(450, 550)
    }

    public void clear(boolean invert)
    { 
        if(object.numberOfPixels > 2000000)
        {
            object.clear(true)
        }
    }

}


This is for a class so guidance is better than a straight up answer with no explaination. Any help would be appreciated. Thanks!

*Edited: Topic title changed to be more descriptive. BlueJ has nothing to do with your problem

This post has been edited by pbl: 25 September 2013 - 02:25 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Creating a console mode Screen object

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9213
  • View blog
  • Posts: 34,599
  • Joined: 12-June 08

Re: Creating a console mode Screen object

Posted 25 September 2013 - 09:23 AM

.. moving to Java.
Was This Post Helpful? 0
  • +
  • -

#3 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 526
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Creating a console mode Screen object

Posted 25 September 2013 - 10:03 AM

Looks like the scope of this lesson is the calling code and not the class itself. You might not be supposed to modify the screen class.
Was This Post Helpful? 0
  • +
  • -

#4 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Creating a console mode Screen object

Posted 25 September 2013 - 10:44 AM

First, in your constructor, you cannot use numberOfPixels. The reason is the variables width and height are probably not objects (types that are not int, char double etc). The new keyword is used for creating new objects.
Also, your varaibles width and height aren't declared, however, you do have xRes and yRes declared, which can you use.

Screen(int xRes, int yRes)
{
    this.xRes = xRes;
    this.yRes = yRes;
}



Second, the numberOfPixels method should just return the number of pixels, which is xRes * yRes.
Lastly, the clear method should just reset the values to zero.
Was This Post Helpful? 1
  • +
  • -

#5 atressler  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 25-September 13

Re: Creating a console mode Screen object

Posted 25 September 2013 - 11:10 AM

Salzaar,

Thanks for your help. Here is the code that I created using your feedback:


public class Screen

{
    private int xRes;
    private int yRes;


    public Screen(int xRes, int yRes)
    { 
         this.xRes = xRes;
         this.yRes = yRes;
    }

    public int numberOfPixels()
    { 
         Screen object = new Screen(xRes*yRes);
    }

    public void clear(boolean invert)
    { 
        if(object.numberOfPixels > 2000000);
        {
            object.clear(true);
        }
    }

}


So I took out the 'numberOfPixels' and replaced it with xRes and yRes since they are actually declared. If I wanted to use width and height, I could just change 'private int width;' & 'private int height' respectively? I prefer not to use 'this' keyword since I might get confused (especially in the beginning process of learning).

For the numberOfPixels method, I set it to 450 and 550 because I wanted to set a max between the xRes and yRes. I would like to include the max, so should I put that in the Screen constructor?

Thanks for your help.
Was This Post Helpful? 0
  • +
  • -

#6 The_Toolshed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 24-September 13

Re: Creating a console mode Screen object

Posted 25 September 2013 - 11:11 AM

Think of a class like a water bottle. You know they can exist. So you create one (buy one). That clas you've bought can have METHODS, like HowMuchWaterLeft, or Drink, or Chug, or Fill.
Your methods can take variables, like Drink can specify 10ml to drink, next call might specify 5ml. The Chug method might not take a parameter and will just empty the whole thing.
The Drink and Chug methods might also send you a message saying "Hey, your bottle is EMPTY, Dude"
The Fill method will want to (like your clear method) CHECK the amount already in the bottle and STOP when it gets to a certain amount.

Classes were a lot easier to understand when I brought them into real-world OBJECTS, which is what Object-Oriented is supposed to encompass.
Was This Post Helpful? 1
  • +
  • -

#7 salazar  Icon User is offline

  • D.I.C Addict

Reputation: 87
  • View blog
  • Posts: 539
  • Joined: 26-June 13

Re: Creating a console mode Screen object

Posted 25 September 2013 - 09:17 PM

What exactly do you want your numberOfPixels method to do? Based on the assignment, I would assume it should calculate the number of pixels on the screen. Currently, you're just creating a new Screen object, why?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1