CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Jul 2010
    Posts
    5

    Is private void the wrong thing to be using here?

    Edit for Removal
    Last edited by Hannahfox123; July 3rd, 2011 at 10:19 PM.

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Is private void the wrong thing to be using here?

    Good try with the code tags but you need to use square brackets [] and not greater than/less than symbols <>.

    You haven't shown how you are using the Quadratic class or the class it inherits from so it's hard to understand your description of the problem.

    One thing that leaps out of the page though is the input() method reads 3 ints into local variables and then doesn't do anything with them so once the input() method completes the values in a, b & c are lost.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Is private void the wrong thing to be using here?

    Quote Originally Posted by Hannahfox123 View Post
    ... I am getting no syntex errors. The program just doesn't print anything.
    You should be getting syntax errors if the Quadratic code you posted is what you're using, because it's not correct Java and it won't compile.

    So before we all waste our time trying to fix code that isn't what you're using, perhaps you'd like to post the actual code you're compiling and running.

    While you're at it, maybe you could explain what the program is supposed to do.

    A tip: don't give a variable and a method the same name, it will only cause confusion.

    Programs must be written for people to read, and only incidentally for machines to execute...
    H. Abelson and G. Sussman
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  4. #4
    Join Date
    Jul 2010
    Posts
    5

    Re: Is private void the wrong thing to be using here?

    Edit for Removal
    Last edited by Hannahfox123; July 3rd, 2011 at 10:18 PM.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Is private void the wrong thing to be using here?

    Should I be using something other than a method to clean the code up, or is this the point of methods.
    There are lots of reasons for using methods some of which should be apparent from the simple example you have coded. For example:

    Reuse: You have used the sqrt() method twice and so have saved yourself the effort of having to write out the whole formula twice.

    Hiding Implementation Detail: By placing the code for a discrete task in a method you hide the implementation detail (this is far more useful if you give the method a good descriptive name). When reading the code it's easy to understand that a call to Math.sqrt() is getting the square root of a number. But if you put the math to perform this computation in the your code it would be difficult to easily see what the code does. Of course your method called sqrt() isn't well named as it doesn't just do a sqrt it computes part of the formula.

    Cleaning up the code has more to do with your coding style than specifically using methods. Your call to the solutions() method code uses methods but is certainly not clean easy understandable code. You would be much better off doing something like:

    Code:
    double sqrtResult = sqrt(a, b, c);
    double xquadplusResult = xquadplus(sqrt(a, b, c), a, b);
    double xquadminusResult = xquadminus(sqrt(a, b, c), a, b);
    
    solutions(sqrtResult, xquadplusResult, xquadminusResult, a, b, c);
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Is private void the wrong thing to be using here?

    Quote Originally Posted by keang View Post
    Cleaning up the code has more to do with your coding style than specifically using methods. Your call to the solutions() method code uses methods but is certainly not clean easy understandable code. You would be much better off doing something like:

    Code:
    double sqrtResult = sqrt(a, b, c);
    double xquadplusResult = xquadplus(sqrt(a, b, c), a, b);
    double xquadminusResult = xquadminus(sqrt(a, b, c), a, b);
    
    solutions(sqrtResult, xquadplusResult, xquadminusResult, a, b, c);
    Or even reuse the evaluated sqrtResult:
    Code:
    double sqrtResult = sqrt(a, b, c);
    double xquadplusResult = xquadplus(sqrtResult, a, b);
    double xquadminusResult = xquadminus(sqrtResult, a, b);
    
    solutions(sqrtResult, xquadplusResult, xquadminusResult, a, b, c);
    Inside every well-written large program is a well-written small program...
    C.A.R. Hoare
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  7. #7
    Join Date
    Jun 2007
    Location
    Aurora CO USA
    Posts
    137

    Re: Is private void the wrong thing to be using here?

    Something I notice from your text is that you really don't seem to understand what public, private, and protected are. Also, void seems fuzzy to you.

    In Java, methods normally have return types (int, double, String, etc.) As you saw, this is how you get results from your method back to the method that called it. When a method has a defined return type, it must return something of that type. The keyword void is used if there is no need for a return type. In this case, it simply tells the compiler the method doesn't need to return a value.

    Class members in Java have something called access levels or visibility. This describes how other classes are going to be able to see the parts of your class. If you make something private, only members in your class will be able to see it. So your public run() method can be seen from outside your class, but your sqrt(), xquadpulus(), and xquadminus() would not.

    In object oriented design, we use this feature to prevent access to parts of the class that others shouldn't use. For example, say everything in your class was public:

    Code:
    public class Quadratic extends ConsoleProgram 
    {
    	public void run(){...}
    	public double sqrt(int a, int b, int c){...}
    	public double xquadplus(double sqrt, int a, int b){...}
    	public double xquadminus(double sqrt, int a, int b){...}
    	public void solutions(double sqrt, double xquadplus, double xquadminus, int a, int b, int c){
    		if ((b*b)-(4*a*c)<0){
    			println ("The result is not real because the value under the square root is less than 0");	
    		} else {
    			println ("The first solution is: "+ xquadplus +".");
    			println ("The second solution is: "+ xquadminus +".");
    		}
    
    	}	
    }
    If you decide to change the implementation of solutions(), and no longer need xquadplus() and xquadminus(), you would still have to keep them around because they are part of the public interface of your class (usually, they'd be deprecated).

    However, if you defined some of your class as private:
    Code:
    public class Quadratic extends ConsoleProgram 
    {
    	public void run(){...}
    	private double sqrt(int a, int b, int c){...}
    	private double xquadplus(double sqrt, int a, int b){...}
    	private double xquadminus(double sqrt, int a, int b){...}
    	public void solutions(double sqrt, double xquadplus, double xquadminus, int a, int b, int c){
    		if ((b*b)-(4*a*c)<0){
    			println ("The result is not real because the value under the square root is less than 0");	
    		} else {
    			println ("The first solution is: "+ xquadplus +".");
    			println ("The second solution is: "+ xquadminus +".");
    		}
    
    	}	
    }
    Now, classes outside yours can see or use the private methods. This means that you can change the implementation of solutions() and not have to worry if you no longer need some of the other methods. The only class that can use them is yours.

    One other important use of visibility is for member variables. Normally class level variables are declared private so they can't be accessed from outside the class. Those that need to be accessed are given getters and setters to allow this.

    Code:
    public class WrkComments
    {
        private String commentator;
        private String commentDetail;
     .
     .
     .
        //
        // getters and setters
        //
        public String getCommentator()
        {
            return commentator;
        }
        public void setCommentator(String commentator)
        {
            this.commentator = commentator;
        }
        public String getCommentDetail()
        {
            return commentDetail;
        }
        public void setCommentDetail(String commentDetail)
        {
            this.commentDetail = commentDetail;
        }
    }
    Sorry this is so long, but the topic is important and you really need to understand how it works for good OO design. You can check the Java Tutorials for more information.

  8. #8
    Join Date
    Jul 2010
    Posts
    5

    Re: Is private void the wrong thing to be using here?

    Edit for Removal
    Last edited by Hannahfox123; July 3rd, 2011 at 10:18 PM.

  9. #9
    Join Date
    Jun 2007
    Location
    Aurora CO USA
    Posts
    137

    Re: Is private void the wrong thing to be using here?

    I felt like I was banging my head against the wall with trying to tell one method to use something from another method, which used something from another method...
    That's the way it works sometimes. You have to use 2 or 4 (or more) methods to build the data you need in the current method. And each of them might call other methods as well.

    It's like building a tool from components (and sometimes you have to build the components).

    When you're writing all your code from scratch, often the hardest part is deciding where to break things down. As keang said, you need to decide what parts of the code can be reused (either multiple times in your code or in code you'll write later), where you'd like to do a bit of complexity hiding, etc. Once you've done it for a while, it starts to become second nature.

    So, what I'm saying is there is nothing major wrong with how you broke down your final code. You might want to follow the suggestions keang and dlorde gave you, as they will make your code more readable. But, overall, you're getting the hang of it.
    Last edited by ajhampson; July 17th, 2010 at 10:18 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured