 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Fri Aug 10, 2012 3:49 pm Post subject: Data Encapsulation Question |
|
|
Dear Group,
I am a new learner of Java language. I was trying to write the following code for Data Encapsulation. I am using Eclipse as IDE.
But how can I compile this code, I am not getting. If any body may help. Please also let me know if the code is fine,too.
*********************************************************************************
THE CODE
********************************************************************************
public class Box {
private int size;
// Provide public getters and setters
public int getSize() { return size; }
public void setSize(int newSize) {
size = newSize;
}
}
public class BoxMain {
/**
* @param args
*/
public static void main(String[] args) {
//create instance of Box
Box b=new Box();
//access methods trough box objects
b.setSize(25);
System.out.println("Size:"+b.getsize());
}
// TODO Auto-generated method stub
}
Thanks in Advance,
Regards,
Subhabrata. |
|
| Back to top |
|
 |
Joerg Meier Guest
|
Posted: Fri Aug 10, 2012 3:49 pm Post subject: Re: Data Encapsulation Question |
|
|
On Fri, 10 Aug 2012 08:49:19 -0700 (PDT), subhabangalore (AT) gmail (DOT) com wrote:
| Quote: | I am a new learner of Java language. I was trying to write the following code for Data Encapsulation. I am using Eclipse as IDE.
But how can I compile this code, I am not getting. If any body may help. Please also let me know if the code is fine,too.
|
Your code looks fine on first glance. Just put it in apropriately named
files (Box.java and BoxMain.java) and press ctrl+s to save, and that will
automatically compile. To run, go into the BoxMain file, and press alt+x,
then j. Do you have a specific problem ?
Regards,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen. |
|
| Back to top |
|
 |
Jeff Higgins Guest
|
Posted: Fri Aug 10, 2012 3:49 pm Post subject: Re: Data Encapsulation Question |
|
|
On 08/10/2012 11:49 AM, subhabangalore (AT) gmail (DOT) com wrote:
| Quote: | Dear Group,
I am a new learner of Java language. I was trying to write the following code for Data Encapsulation. I am using Eclipse as IDE.
But how can I compile this code, I am not getting. If any body may help. Please also let me know if the code is fine,too.
http://www.vogella.com/articles/Eclipse/article.html#firstjava |
|
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 10, 2012 7:13 pm Post subject: Re: Data Encapsulation Question |
|
|
On Friday, August 10, 2012 9:19:19 PM UTC+5:30, (unknown) wrote:
| Quote: | Dear Group,
I am a new learner of Java language. I was trying to write the following code for Data Encapsulation. I am using Eclipse as IDE.
But how can I compile this code, I am not getting. If any body may help. Please also let me know if the code is fine,too.
*********************************************************************************
THE CODE
********************************************************************************
public class Box {
private int size;
// Provide public getters and setters
public int getSize() { return size; }
public void setSize(int newSize) {
size = newSize;
}
}
public class BoxMain {
/**
* @param args
*/
public static void main(String[] args) {
//create instance of Box
Box b=new Box();
//access methods trough box objects
b.setSize(25);
System.out.println("Size:"+b.getsize());
}
// TODO Auto-generated method stub
}
Thanks in Advance,
Regards,
Subhabrata.
Hi Joerg, |
Thanks it worked. The code is working but only "Size:" of System.out.println("Size:"+b.getsize()); is giving error, if you drop it --it is coming nicely.
Regards,
Subhabrata. |
|
| Back to top |
|
 |
Lew Guest
|
Posted: Fri Aug 10, 2012 7:31 pm Post subject: Re: Data Encapsulation Question |
|
|
(unknown) wrote:
| Quote: | I am a new learner of Java language. I was trying to write the following code for Data Encapsulation. I am using Eclipse as IDE.
|
At some not-too-distant point you should also learn two other ways,
command line and Ant.
http://docs.oracle.com/javase/7/docs/technotes/guides/javac/index.html
| Quote: | But how can I compile this code, I am not getting. If any body may help. Please also let me know if the code is fine,too.
|
Eclipse menu: Project => Build project
| Quote: | ********************************************************************************
public class Box {
private int size;
// Provide public getters and setters
public int getSize() { return size; }
public void setSize(int newSize) {
size = newSize;
}
}
public class BoxMain {
/**
|
Don't use TAB characters to indent code you publish publicly,
e.g., here on Usenet.
It's fine for examples like here, and kudos for providing such a
good example, but in real code you should complete the Javadocs.
| Quote: | * @param args
*/
public static void main(String[] args) {
//create instance of Box
|
Yeah, the code already says this. The comment is a bit unnecessary.
| Quote: | Box b=new Box();
//access methods trough box objects
b.setSize(25);
System.out.println("Size:"+b.getsize());
}
// TODO Auto-generated method stub
}
|
Eclipse preferences let you set editors to "Insert spaces for tabs".
--
Lew |
|
| Back to top |
|
 |
Lew Guest
|
Posted: Fri Aug 10, 2012 7:32 pm Post subject: Re: Data Encapsulation Question |
|
|
(unknown) wrote:
| Quote: | Thanks it worked. The code is working but only "Size:" of System.out.println("Size:"+b.getsize()); is giving error, if you drop it --it is coming nicely.
|
"It worked" and "It is giving [an] error" are contradictory statements.
What error?
Why did it happen?
--
Lew |
|
| Back to top |
|
 |
Guest
|
Posted: Fri Aug 10, 2012 7:58 pm Post subject: Re: Data Encapsulation Question |
|
|
On Saturday, August 11, 2012 1:02:16 AM UTC+5:30, Lew wrote:
| Quote: | (unknown) wrote:
Thanks it worked. The code is working but only "Size:" of System.out.println("Size:"+b.getsize()); is giving error, if you drop it --it is coming nicely.
"It worked" and "It is giving [an] error" are contradictory statements.
What error?
Why did it happen?
--
Lew
|
I said it worked because, the main module worked. That is core code. Anything we are giving within " " string notation(unless it is a string manipulation code) it is sort of comment only. Right? May be some typing mistake I am doing.
Regards,
Subhabrata. |
|
| Back to top |
|
 |
Lew Guest
|
Posted: Fri Aug 10, 2012 10:37 pm Post subject: Re: Data Encapsulation Question |
|
|
(unknown) wrote:
| Quote: | Lew wrote:
(unknown) wrote:
Thanks it worked. The code is working but only "Size:" of System.out.println("Size:"+b.getsize()); is giving error, if you drop it --it is coming nicely.
"It worked" and "It is giving [an] error" are contradictory statements.
What error?
Why did it happen?
I said it worked because, the main module worked. That is core code. Anything we are giving within " " string notation(unless it is a string manipulation code) it is sort of comment only..Right?
|
Not right at all. It is a program object and nothing at all even in the slightest like a comment.
| Quote: | May be some typing mistake I am doing.
|
You need to answer my questions.
--
Lew |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Aug 11, 2012 4:59 am Post subject: Re: Data Encapsulation Question |
|
|
On Saturday, August 11, 2012 4:07:06 AM UTC+5:30, Lew wrote:
| Quote: | (unknown) wrote:
Lew wrote:
(unknown) wrote:
Thanks it worked. The code is working but only "Size:" of System.out.println("Size:"+b.getsize()); is giving error, if you drop it --it is coming nicely.
"It worked" and "It is giving [an] error" are contradictory statements..
What error?
Why did it happen?
I said it worked because, the main module worked. That is core code. Anything we are giving within " " string notation(unless it is a string manipulation code) it is sort of comment only..Right?
Not right at all. It is a program object and nothing at all even in the slightest like a comment.
May be some typing mistake I am doing.
You need to answer my questions.
--
Lew
|
Hi,
Thank you for taking time with me.
What I know is that, if you are not manipulating strings like concatenation, search etc. strings can be of two kinds in a code. One is comment which is not compiled. The other one is, the expressions for the variables. These may go like,
int num1=100;
int age=60;
String name="Lincoln";
etc.
These may be associated with the expression of variables,
System.out.println("The Denotation of One Hundred in Numerical is:"+num1);
System.out.println("The Age of Independent India is:"+age);
System.out.println("The Name of Famous US President is:"+name);
etc.
Now these expressions are compiled but not part of code because no variable is assigned thus can not be processed, they die where they are assigned. Generally, comments and variable expressions should be congruent with the code because they are the context of code,
but instead of,
System.out.println("Hello World");
If I write
System.out.println("Hello Earth");
or even
System.out.println("I am not saying Hello to the World,Would you?");
would not matter much as our job is to see if the code is giving proper output which we are trying to assign, so is the case here, if I say,
System.out.println("Century Means:"+num1);
System.out.println(age);
System.out.println("The Name of the opera house is:"+name);
etc.
It does not matter.
Other than these there is another kind of variable expression like
System.out.println("The Denotation of num1 in word is One Hundred")
Here the String is compiled because the variable is present. But I am not expert in handling in these kind of strings so prefer to avoid.
Other than this, if I am going wrong anywhere, please let me know, I would be happy to learn and improve. And also let me know your suggestion over this code.
Regards,
Subhabrata. |
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Aug 11, 2012 5:08 pm Post subject: Re: Data Encapsulation Question |
|
|
On Fri, 10 Aug 2012 08:49:19 -0700 (PDT), subhabangalore (AT) gmail (DOT) com
wrote, quoted or indirectly quoted someone who said :
| Quote: | I am a new learner of Java language. I was trying to write the following code for Data Encapsulation. I am using Eclipse as IDE.
But how can I compile this code, I am not getting. If any body may help. Please also let me know if the code is fine,too.
|
I don't see anything wrong with your code. I would have just used one
class myself. What happens when you try to compile?
--
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04 |
|
| Back to top |
|
 |
Roedy Green Guest
|
Posted: Sat Aug 11, 2012 5:12 pm Post subject: Re: Data Encapsulation Question |
|
|
On Fri, 10 Aug 2012 12:13:00 -0700 (PDT), subhabangalore (AT) gmail (DOT) com
wrote, quoted or indirectly quoted someone who said :
| Quote: | Thanks it worked. The code is working but only "Size:" of System.out.println("Size:"+b.getsize()); is giving error, if you drop it --it is coming nicely.
|
Just copy and paste the output of the program. The code looks fine.
--
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04 |
|
| Back to top |
|
 |
Joerg Meier Guest
|
Posted: Sat Aug 11, 2012 6:35 pm Post subject: Re: Data Encapsulation Question |
|
|
On Sat, 11 Aug 2012 13:29:30 -0700 (PDT), subhabangalore (AT) gmail (DOT) com wrote:
| Quote: | Thank you for your kind concern. The following error message is coming:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getsize() is undefined for the type Box
at BoxMain.main(BoxMain.java:16)
|
Just a minor typo on your part (and an oversight on ours - none of us
caught it either) - "getsize" is not the same as "getSize" (notice the
capital S).
Liebe Gruesse,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen. |
|
| Back to top |
|
 |
Jeff Higgins Guest
|
Posted: Sat Aug 11, 2012 7:29 pm Post subject: Re: Data Encapsulation Question |
|
|
On 08/11/2012 05:13 PM, subhabangalore (AT) gmail (DOT) com wrote:
| Quote: | Dear Group,
No. The Code had a very silly mistake. If you see, I am writing System.out.println("Size:"+b.getsize()); while I am calling, but while declaring I am writing it as,
public void setSize(int newSize) {
size = newSize;
}
The 'S' of "setSize" is capital here.
I changed and worked fine. When all of you were saying the syntax and structure is fine, I tried to zoom into the code. Thank you all for your kind time.
By the way, Lew was trying to suggest me something regarding command line, Ant, etc. I am bit curious to know them. If you can help.
For the beginner Java programmer. If you can learn to navigate |
all of the Java documentation from the link given below you
will be so far ahead of the competition that they will never see you.
<http://docs.oracle.com/javase/7/docs/> |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Aug 11, 2012 8:29 pm Post subject: Re: Data Encapsulation Question |
|
|
On Sunday, August 12, 2012 12:42:14 AM UTC+5:30, Roedy Green wrote:
| Quote: | On Fri, 10 Aug 2012 12:13:00 -0700 (PDT), subhabangalore (AT) gmail (DOT) com
wrote, quoted or indirectly quoted someone who said :
Thanks it worked. The code is working but only "Size:" of System.out.println("Size:"+b.getsize()); is giving error, if you drop it --it is coming nicely.
Just copy and paste the output of the program. The code looks fine.
--
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04
|
Dear Sir,
Thank you for your kind concern. The following error message is coming:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getsize() is undefined for the type Box
at BoxMain.main(BoxMain.java:16)
Regards,
Subhabrata. |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Aug 11, 2012 8:40 pm Post subject: Re: Data Encapsulation Question |
|
|
On Sunday, August 12, 2012 1:59:30 AM UTC+5:30, (unknown) wrote:
| Quote: | On Sunday, August 12, 2012 12:42:14 AM UTC+5:30, Roedy Green wrote:
On Fri, 10 Aug 2012 12:13:00 -0700 (PDT), subhabangalore (AT) gmail (DOT) com
wrote, quoted or indirectly quoted someone who said :
Thanks it worked. The code is working but only "Size:" of System.out.println("Size:"+b.getsize()); is giving error, if you drop it --it is coming nicely.
Just copy and paste the output of the program. The code looks fine.
--
Roedy Green Canadian Mind Products http://mindprod.com
A new scientific truth does not triumph by convincing its opponents and making them see the light,
but rather because its opponents eventually die, and a new generation grows up that is familiar with it.
~ Max Planck 1858-04-23 1947-10-04
Dear Sir,
Thank you for your kind concern. The following error message is coming:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method getsize() is undefined for the type Box
at BoxMain.main(BoxMain.java:16)
Regards,
Subhabrata.
|
Dear Group,
No. The Code had a very silly mistake. If you see, I am writing System.out.println("Size:"+b.getsize()); while I am calling, but while declaring I am writing it as,
public void setSize(int newSize) {
size = newSize;
}
The 'S' of "setSize" is capital here.
I changed and worked fine. When all of you were saying the syntax and structure is fine, I tried to zoom into the code. Thank you all for your kind time.
By the way, Lew was trying to suggest me something regarding command line, Ant, etc. I am bit curious to know them. If you can help.
Regards,
Subhabrata. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|