This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating a Java class that should have instance variables  RSS feed

 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to take a Java class for my Masters.  I am not a coder at all, so...

The first step in my final project is creating a Java class that should have instance variables
firstName and lastName. The firstName and lastName instance variables hold a value for first and last names respectively.

I have no idea how to start this.
 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vorpe wrote:I have to take a Java class for my Masters.  I am not a coder at all, so...

The first step in my final project is creating a Java class that should have instance variables
firstName and lastName. The firstName and lastName instance variables hold a value for first and last names respectively.

I have no idea how to start this.


One more thing:

Class Instance Variables: Include required instance variables and restrict access by using the private specifier.

Again...where to start?  I know, RTFF.  I did.
 
Stephan van Hulst
Saloon Keeper
Posts: 7808
142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, do you know how to declare classes? Do you know how to declare variables? If not, you should probably go through the Oracle tutorials again: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html
 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch, David Vorpe.

I have changed your topic subject line, so it reveals its intention a bit clearer to your readers.

Did you get any requirements for your final project you need to follow? Or it is your decision what you are up for?
 
Campbell Ritchie
Marshal
Posts: 55717
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vorpe wrote:. . . Class Instance Variables: Include required instance variables and restrict access by using the private specifier. . . .
Did you copy that exactly from your instructions? Most of us would either call a variable instance or class, but not both.

And welcome again
 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
David Vorpe wrote:. . . Class Instance Variables: Include required instance variables and restrict access by using the private specifier. . . .
Did you copy that exactly from your instructions? Most of us would either call a variable instance or class, but not both.

And welcome again


Yessir.  I copied it straight from the "Milestone One".  I'll present the entire instructions below (I removed the name of the class and the overall project to protect the innocent):

--In Module Two, you will submit your Java class. You will create your initial version of theclass. The class should have instance variables
firstName and lastName. The firstName and lastName instance variables hold a value for first and last names respectively. You will demonstrate your
understanding of encapsulation using the private specifier to restrict access to your instance variables.--


Again, I am not a coder.  It is just not my bailiwick.  I have read Oracle docs, watched YouTube videos and have been pretty much hazed in every way possible on many other Java forums.  I don't understand this stuff.

 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Welcome to the Ranch, David Vorpe.

I have changed your topic subject line, so it reveals its intention a bit clearer to your readers.

Did you get any requirements for your final project you need to follow? Or it is your decision what you are up for?


It's a virtual world Java application.  I could post the entire document but I don't really want to use up disk space like that.  This project has probably been used many times by different universities.
 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote: So, do you know how to declare classes?


Actually, no, I don't.

Stephan van Hulst wrote: Do you know how to declare variables? If not, you should probably go through the Oracle tutorials again: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html


Again...no, I don't.  The tutorials are nice reads, really.  I can't translate what I read into something that actually works.  Yes, I am that bad.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you try to write a simple program which just prints value of an instance variable Or a message something like that after reading that tutorial? Or you can post your program whatever you have tried and ask what you haven't understood in it. Please use code tag to post your code, refer UseCodeTags <--- click here.
 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:Did you try to write a simple program which just prints value of an instance variable Or a message something like that after reading that tutorial? Or you can post your program whatever you have tried and ask what you haven't understood in it. Please use code tag to post your code, refer UseCodeTags <--- click here.


Here is what I have so far.  I think it is OK but again, no clue.

 
Liutauras Vilda
Marshal
Posts: 4641
318
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on the requirements you have provided, that is nearly it.

Two things. Class names supposed to start with an upper case, so that would read as MyClone. And you don't need main() method as you haven't been asked to write one.

[edit] and it seems you got too much closing braces.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job   and thank you for using code tags.

Did you compile the code?
BTW class name always starts with capital letter. Please refer this click here ---> Naming Conventions

 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:Good job   and thank you for using code tags.

Did you compile the code?
BTW class name always starts with capital letter. Please refer this click here ---> Naming Conventions



I guess I compiled it, I don't have any errors.  I commented out the main method right now (I know I'll need it eventually) and I capitalized "My" (MyClone).  Now, the next step, the introduction() method. I am not going to ask here yet, I will post more code later as I build this thing.  Thank you all for your understanding.  I will by a lot of pie later
 
David Vorpe
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:Good job   and thank you for using code tags.

Did you compile the code?
BTW class name always starts with capital letter. Please refer this click here ---> Naming Conventions



Is there a way to rename the ".Java" file?  Now MetBeans doesn't like the change.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
David Vorpe wrote:Is there a way to rename the ".Java" file?  Now MetBeans doesn't like the change.
Yes you can rename .java file in NetBeans.
At left side in projects window
  • One way is just select that .java file press Ctrl + R Or In your code select class name by double clicking on class name and press Ctrl + R.
  • Another way is right click on that file then go to Refractor --> Rename
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!