#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Canada
    Posts
    12
    Rep Power
    0

    Simple code help


    Hi.

    I typed this simple code and it counts up by three's.



    import acm.program.*;

    public class Freespace extends ConsoleProgram {

    private static final int MAX_COUNT = 300;


    public void run() {

    println("Count upwards by 3's");
    for (int i = 0; i <= MAX_COUNT; i = i + 3) {
    println(i);
    }
    }
    }

    I want to create a private method that does the counting for me and then returns it to the public method, but I am stuck.

    I basically want the method to be called countThree and have the calculations and for loop done there. Please help. My textbook is vague on its example and Google results are always in a different library I dont know the syntax of yet.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    88
    Rep Power
    22
    public void run() {
    count();
    }

    private void count() {
    //put your loop code here
    }
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Canada
    Posts
    12
    Rep Power
    0
    import acm.program.*;

    public class Freespace extends ConsoleProgram {

    private static final int MAX_COUNT = 300;


    public void run() {

    println("Count upwards by 3's");
    count();

    }



    private int count() {

    for (int i = 0; i <= MAX_COUNT; i = i + 3) {
    println(i);
    }



    }
    }


    how do I return it to the public method?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    88
    Rep Power
    22
    What do you want returned? You already print out all of the numbers in your method so what information do you want to give back to the run method?
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Canada
    Posts
    12
    Rep Power
    0
    I want the count method to return a count up by threes to the public method.

    I get the compiler error that this method must return a result of type int.

    Appreciate the help
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Canada
    Posts
    12
    Rep Power
    0

    this code does what I want


    I was finally able to make a mini breakthrough on my return call. I was wondering if there is not a simpler way to code this?



    import acm.program.*;

    public class countByThreeRETURNMETHOD extends ConsoleProgram {
    private static final int MAX_COUNT = 300;


    public void run() {

    println("Count upwards by 3's");
    for (int i = 0; i <= MAX_COUNT; i ++) {

    println(count(i));
    }
    }


    private int count(int n) {

    int result = 0;
    for (int i = 0; i <= n; i = i + 3) {
    result = i;


    }
    return result;


    }
    }
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    88
    Rep Power
    22
    Ok, I still don't quite understand what you're saying but I will take a guess. You want the private method, count, to return the closest multiple of three less than n?

    if so, then yes, your code works, but there is a MUCH easier way of doing it (that is, if you are allowed to use the modulo '%' operator for your class). Take a look at this code:

    private int count(int n) {
    return n-(n%3);
    }

    So to break it up, 'n%3' will find the remainder of the operation of 'n/3'. And then we will subtract that from n which will bring n down to a multiple of three. Example: n=7. 7/3 = 2 R1 therefore 7%3 = 1 and then 7-(7%3) = 7-1 = 6.

    I hope this helps. If this is not what you wanted, then please explain in much more detail what you need.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Canada
    Posts
    12
    Rep Power
    0
    Thx for the reply.

    Ok. Since my textbook has only intorduced me to a third party acm library I am limited to my exposure to "regular" java libraries.

    The count by three program was just a little example of a simple program that I wanted to count up from 0 to 300 by threes. eg ; 0,3,6,9,12,15,18 etc.

    I went with this simple concept because I have only had exposure to working within the public void run () method. The text is trying to teach me how to "information hide" and create our own methods that take calculations etc away from the run method and have them do their functions and return a result back to that method.

    I am having difficulty both elaborating this as well as grasping the concept of the required syntax.

    I wanted to create a working example that I can reference that shows me how I can take the count by three function and have it perform its desired calculation and then return it to the main method. (sorry if my lingo is wrong...NEWBIE!)

    my original program :

    public void run() {

    println("Count upwards by 3's");
    for (int i = 0; i <= MAX_COUNT; i = i + 3) {
    println(i);
    }
    }
    }

    gave me the desired results. Count up from 0 to 300 in 3's.

    I wanted to know the syntax on how to have the "work" the count by three functions done in a new method that I created. I understand that as programs get more complex the act of information hiding is a good practice as to hid the insignificant methods etc from sight so the program is easier to decipher.

    I am still confused, but seeing some light. I hope that I can find some insight through different simple method calls and return values etc examples so I can learn the syntax and steps needed to understand my shortcomings.

    make sense?
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    88
    Rep Power
    22
    Ok, so if you are just trying to understand the idea of information hiding, then I will provide some code examples below that hopefully will help you because I am getting the feeling that neither of us really understand what the book is asking for. If it is really important that you do this exact problem then I will need these questions answered:

    What gets printed and where? is the count method printing 0,3,6,9... or is it just printing the final number or is it just returning the final number for the main method to print? These are the details I need to help you.

    Otherwise here are some examples unrelated to your problem:

    #1: method calls
    Code:
    import acm.program.*;
    public class Rectangle extends ConsoleProgram {
    
    public void run() {
       int height = 20;
       int width = 30;
       println(getArea(height, width));
       println(getPerimeter(height, width));
    }
    
    private int getArea(int height, int width) {
       int area = height * width;
       return area;
    }
    
    private int getPerimeter(int height, int width) {
       int perimeter = 2*height + 2*width;
       return perimeter;
    }
    
    }
    #2: private static variables
    Code:
    import acm.program.*;
    public class Rectangle extends ConsoleProgram {
    
    private static int HEIGHT = 20;
    private static int WIDTH = 30;
    
    public void run() {
       println(getArea());
       println(getPerimeter());
    }
    
    private int getArea() {
       int area = HEIGHT * WIDTH;
       return area;
    }
    
    private int getPerimeter() {
       int perimeter = 2*HEIGHT + 2*WIDTH;
       return perimeter;
    }
    
    }
    #3: private global, non-static variables
    Code:
    import acm.program.*;
    public class Rectangle extends ConsoleProgram {
    
    private int height = 0;
    private int width = 0;
    
    public void run() {
       height = 20; //run can access private globals even though its a public method because it is inside of the Rectangle class.
       width = 30;
       println(getArea());
       println(getPerimeter());
    }
    
    private int getArea() {
       int area = height * width;
       return area;
    }
    
    private int getPerimeter() {
       int perimeter = 2*height + 2*width;
       return perimeter;
    }
    
    }
    Hope you find these useful. Sorry that I havn't been much help on your other code.
  18. #10
  19. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    Canada
    Posts
    12
    Rep Power
    0

    thank you


    You are super helpful. I wanted examples just like that. I thank u so much!

    How did u indent your code like that with the border line on this site?

    Makes it easier to post!

    Thanks again
  20. #11
  21. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2010
    Posts
    88
    Rep Power
    22
    No problem!

    I just used three spaces as an indent since tab doesn't work. lol And to get the border, just surround it in code tags by highlighting your code and clicking on the # symbol above the reply box.

IMN logo majestic logo threadwatch logo seochat tools logo