Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using data from one class in another in a single java file?  RSS feed

 
Jason Hart
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to create a second class in a single java file (first time trying this) and want to use data from the first class in the second class but I am not able to do it. What am I missing (total newb here by the way):



 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jason, Welcome to the Ranch!

When you post code, you want to UseCodeTags (←click that link to see how), not quote tags.
 
Jason Hart
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Hi Jason, Welcome to the Ranch!

When you post code, you want to UseCodeTags (←click that link to see how), not quote tags.


Ok thanks, I see how to use that [code] tag now.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do you think you need another class?
 
Jason Hart
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Why do you think you need another class?


This is one of my requirements for the assignment:

The application should have at least one class, in addition to the application’s controlling class.


So I am assuming I need two classes.
 
Junilu Lacar
Sheriff
Posts: 11476
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And do you understand what the controlling class was supposed to do? What is it "controlling"?
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You haven't got any information in that class. I can only see information in the main method, which is not accessible in the remainder of the class. Remind yourself about how you write fields; if you haven't got another source of information try the Java® Tutorials, which seem to call fields “member variables”.
Give all your fields private access and if you need access in a different class make them available via getXXX methods.
 
Jason Hart
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:And do you understand what the controlling class was supposed to do? What is it "controlling"?


Hi, I am brand new to Java, only in my third week of class and this is my third program with the first being "Hello World", and the second a single salesperson commission program and now a modified version of that program. I am still learning the terminology and after three weeks can be considered to know nothing really. If anyone could baby talk me in this that would be awesome. Anyway this is what the assignment is:

Modify the Week Two Java™ application using Java™ NetBeans™ IDE to meet these additional and changed business requirements:

The application will now compare the total annual compensation of at least two salespersons.
It will calculate the additional amount of sales that each salesperson must achieve to match or exceed the higher of the two earners.
The application should ask for the name of each salesperson being compared.

The Java™ application should also meet these technical requirements:
The application should have at least one class, in addition to the application’s controlling class.
The source code must demonstrate the use of Array or ArrayList.
There should be proper documentation in the source code.

Submit a ZIP file containing the ".java" file


I am not sure what a controlling class even is to answer that question. Is that the first class I made? The one I called SimpleCommissionCalculation?
 
Campbell Ritchie
Marshal
Posts: 56520
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would think that a controlling class is one with a main method in, and from that class' methods you call methods on the other classes. I would suggest you create the controlling class last, but at a very early stage, when all your other classes only have one method each. You have a method in the controlling class which runs through the features of the other class, and you enlarge that as you add methods. It will look something like this, but you will obviously give everything different names:-you should already have the other class partially built because it says you should be enhancing last week's assignment. That is how most programming is done, incrementally. You will notice that your assignment asks you to solve a specific problem and answer a specific question.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!