Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8727653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:28:33+00:00 2026-06-13T08:28:33+00:00

I got an annoying question why is it when I first attempted to compile

  • 0

I got an annoying question why is it when I first attempted to compile this code with the variables imageSize and memorySize declared immediately under CameraPhone class compiler gave me a logic error but works perfectly when I declare the variables at the end of the code???

Assume the existence of a Phone class. Define a subclass, CameraPhone that contains two instance variables: an integer named, imageSize, representing the size in megapixels (for simplicity assume a pixel takes up one byte– thus megapixels equals megabytes) of each picture (i.e., 2 means each image is composed of 2 megapixels), and an integer named memorySize, representing the number of gigabytes in the camera’s memory (i.e., 4 means 4 Gigabyes of memory). There is a constructor that accepts two integer parameters corresponding to the above two instance variables and which are used to initialize the respective instance variables. There is also a method named numPictures that returns (as an integer) the number of pictures the camera’s memory can hold. Don’t forget that a gigabyte is 1,000 megabytes.

public class CameraPhone extends Phone { 

    public CameraPhone(int imageSize, int memorySize) { 
        this.imageSize  = imageSize; 
        this.memorySize = memorySize; 
    } 

    public int numPictures() {
        return memorySize * 1000 / imageSize;
    } 

    private int imageSize; private int memorySize;

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:28:34+00:00Added an answer on June 13, 2026 at 8:28 am

    And what’s the “logic error” that you were getting? If you copy and paste the code in the question it will work without problems, it doesn’t matter at all where yo declared the attributes (a.k.a. “instance variables”) – at the beginning, at the end, it’s all the same as long as you actually declared and initialized them somewhere. What I mean is that as long as these lines appear anywhere inside the class declaration (not inside a method, mind you) the code will compile:

    private int imageSize;
    private int memorySize;
    

    Also, it’s perfectly legal to use this inside a constructor, that is not causing the error. And the attributes can be initialized directly where you declared them, or inside the constructor.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got a annoying situation: Under /path1/to, I vim a file name file1, but
Got a question for some jQuery wizard who might stumble upon this. When I
I know this seams a trivial question, but how can I disable the annoying
This might seems silly question but for me it is annoying. I have installed
( again annoying question... ) after asking this before - ( which is partly
I've got this really annoying space after the li tags of my unordered list
got high hopes seeing how well my first question was answered so I will
I previously asked a question regarding this , got an interesting answer which got
The other day I got this annoying oracle error: ORA-12705: Cannot access NLS data
I've got a really annoying problem, and as much as I've searched, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.