Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding methods to my program?  RSS feed

 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even though my program executed properly, my professor wanted my to create different methods throughout the program.  I'm unclear on how to do this, can anyone help?


 
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
Welcome to CodeRanch!

Thank you for using code tag to post code. Indeed professor gave you good advice, It is not good practice to write that code in main method, to know why please click here --->MainIsAPain

What is this program about? Please go through How to define methods <--- click here
 
Knute Snortum
Sheriff
Posts: 4091
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For starters, this wiki will help you get your code out of the main() method.  Once that happens, you can create methods like getName(), getCredits(), addDashes(), etc.

If you need more help, just ask.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

If you think of your main() method as an outline or table of contents, your methods will be the sections of your program that has the details. The section comments you have written hint at which sections of code you can "extract" from your main() method and put into their own method. Your comments are not very reflective of what those sections actually do though so I have given you some suggestions below.

You can read more here https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Welcome to the Ranch!

If you think of your main() method as an outline or table of contents, your methods will be the sections of your program that has the details. The section comments you have written hint at which sections of code you can "extract" from your main() method and put into their own method. Your comments are not very reflective of what those sections actually do though so I have given you some suggestions below.

You can read more here https://docs.oracle.com/javase/tutorial/java/javaOO/methods.html


Can you give me an example of how to add the method to getUserInput (as you named it)?  I understand that I have to call the method in my main, but I'm struggling to enter the correct input....
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the tutorial that has been cited. It has all the information you need. I can't imagine your instructor telling you to create methods and not give you any instruction on how to do that; maybe you should go back over your class notes to see what you missed.
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Please read the tutorial that has been cited. It has all the information you need. I can't imagine your instructor telling you to create methods and not give you any instruction on how to do that; maybe you should go back over your class notes to see what you missed.


This is an online accelerated course, so we are moving very quickly.  We are cramming 16 weeks of information into 8 weeks.  This is actually my last program that was already graded.  I was just hoping to get some clarity about it, so I can reference back to it in my current program.  I understand the theory of what I'm "supposed" to do, but I'm having trouble actually doing it.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, what have you tried? You are more likely to get help if you ShowSomeEffort (← click on that link)b
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, look for examples for refactoring extract method
 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Also, look for examples for refactoring extract method


I've tried this, but it isn't right

 
Daniel Martos
Ranch Hand
Posts: 123
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Martos wrote:
Junilu Lacar wrote:Also, look for examples for refactoring extract method


I've tried this, but it isn't right



I mean this:
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Your code is not properly indented, making it difficult to read.
2. You are not calling the method properly. Go back to the tutorials or your course notes.
3. You don't seem to understand how method parameters work. Again, re-read your course. notes and/or the tutorials.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, you also seem to have far too much code in the main method, and it looks as if your inconsistent indentation had misled you into trying to declare two methods inside each other.
 
Liutauras Vilda
Marshal
Posts: 4670
320
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel, some of your comments are not informative, others - disinformative. Probably you have been asked your instructor to write them down, so you need to spend more time to get them right.

1. Very first comment sounds:
"Import the java.util package in order to work with the scanner class."
---------
And right below you got import statement "import java.util.*;", where comment actually explains a bit what your intent is, but think if you'd change such import statement to:
"import java.util.Scanner;", so that would make your comment redundant, because import statement would tell everything you may want to know. Consider making so.

2. Why have you named your class "Martos3"? Maybe better would be to give some meaning name what this app is meant to do?

3. I don't understand variable name "PER_CREDIT". I don't think it is a good name. Don't understand in which context it could be used. Could you think off a better name?

4. Indentation. Important. See below how indentation would be looking better. Every deeper level supposed to be indented by four space characters (4 x times space keystroke pressed).

5. Methods being invoked by providing round brackets (). For instance "getName(argument);".
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:. . . indented by four space characters (4 x times space keystroke pressed). . .
It is probably better to use a decent text editor with the options in that link: use automatic indentaiton and automatic converion of one tab→four spaces.
 
Junilu Lacar
Sheriff
Posts: 11169
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To demonstrate how proper indentation can help you do what you've been asked very easily, let's start with this code:

This is how your code and comments should be formatted. It clearly shows how all the code is a part of the main() method.

Now, to refactor (extract method), you can do it one section of code at a time. You first add a method call and an empty method:

Notice that we keep the code properly indented. Now you move the first section of code from main to the empty method:

This code does exactly the same thing as before but it's now better organized. Then you do the same thing with the other sections until you end up with something like this:

That's all there really is to it. The process can get a little more complicated when there are variables declared locally in main() involved. Those may need to be turned into instance variables, class variables, or parameters, depending on how widely the values need to be shared but the mechanics are basically the same.
 
Campbell Ritchie
Marshal
Posts: 55793
164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What a nice example of refactoring
I would suggest there are two steps before that:-
  • 1: Turn the code into an object.
  • 2: Move all the code out of the main method, as Winston describes here.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!