create an array class

java array class and with constructor and methods

Page 1 of 1

8 Replies - 3041 Views - Last Post: 24 June 2010 - 03:34 PM Rate Topic: -----

#1 baneyu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-January 10

create an array class

Posted 24 June 2010 - 01:59 AM

Hi,
I am recent in Java, I can read and understand code, know the high level (can do UML) and just recently I finally started to understand how to structure code so I can start writing.
I need help for the following, which will provide me another milestone into the understanding of the code
NOTE: if anyone can point to a tutorial that would explain with full examples (not only a constructor out of context, or a class, but also for that example how to call it, and what is returned..., I would gladly appreciate.
for now that would help me.

also, if I am steering in the wrong direction, please let me know and correct me in a line or two.

I have a main, where the input is taken from the user for 1 value (this valuewill define the rows and colons for an array) = this is ok.

I need to create the array.
then I need to populate it with random numbers
(after that I will add more manipulations on it, but then I will be able to do so, as long as I pass that stage)
from that value I will need to generate more arrays (with derived sizes) but leave them with null values.

now I already did that in my main, but that is linear programming, and once again: I need to bridge the gap between UML and finished code.

for that I went the way of
1_ creating a class MyArr
2_ create a constructor with takes an int mySide as argument and use the int to create a 2 dimension array[mySide][mySide]
as I understand, when called from the main, that should instantiate an object of MyArr class.
3_ create a method "populate" which can be called from main with the object instantiated passed to it as an argument.
it takes that object and populate the array created (not certain of the terminology here)

in other words, seen from the main.
create a new instance of the Myarr (which in fact is / contains an array of the size passed in call.

then if needed, call Myarr.populate(????) that will populate that array.
then I can create another instance of Myarr with a different size provided and not populate it (or leave it filled with null
I paste a skeleton.
as you can see, I left empthy most of the components for the class

but creating the array when the class is instantiated would look like:
int[][] a1 = new int[mySide][mySide]

then calling it would be something like:
MyArr baneArr = new MyArr(mySideInt);
and here is the first problem I face: baneArr will be an object.

first question: is that correct?
if yes, then second step:

populate will be a nested loop with random numbers (I have no problem there besides how to access that object's array?
something like:
myArr.populate(baneArr)

and if that is the way how to I use it in populate:
baneArr[counterX][counterY] = myRandomNumber ?

I tried many ways over the past week end, and still face too many issues where I am not passing the correct type of argument, or if I fix that then I cannot set the values in the command described above.

I have 15 years in telecoms, and never touched programming besides reading code, and heavy bash /perl scripting. this thing is not part of a project, nor an assignment, it is the only way I can understand this and a single example would be very good to help me move forward. I learned everything by taking examples and playing with them, unfortunately all the example/tutorial I see beyond hello world and such only show pieces of code without the full working code. then it jumps to code so complex that is it beyond reverse engineering for intermediate learners. in between I cannot find information that would fill the gap, and I have been looking for years; this time I am not giving up. please help

public class Main {

    public static void main(String[] args) {
        // take the size the user wants
        String mySizeStr = JOptionPane.showInputDialog(null, "enter the required size", "input the size",JOptionPane.QUESTION_MESSAGE);
        
        // get the square root for that given size
        double mySringDbl = Double.parseDouble(mySizeStr);
        double mySideDbl = Math.sqrt(mySringDbl);
        // convert it to an int (required for an array)
        int mySideInt = (int) mySideDbl;
    
       // call MyArray  ???


    }
public class MyArr {
         // here I need a constructor
         public MyArr(int size){
         
         }
         public void populate( ???? ){

         }
}


Edited by macosxnerd101: Welcome to DIC! :) Please remember to post your code using code tags, like so: :code:.

This post has been edited by macosxnerd101: 24 June 2010 - 09:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: create an array class

#2 baneyu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-January 10

Re: create an array class

Posted 24 June 2010 - 02:08 AM

I re-read my post, and realized that
1_ mySringDbl was meant to be: myStringDbl, however you can leave it as it is, as the only call to it (following line) uses the mySringDbl. no issues there , just in case.

2_ populate must be a method, the array cannot be populated at the class instantiation.
the reason is that I will have to generate many more arrays, some empty, some with different data (random numbers are here just so I understand that process)
I should populate only when I need to do so and will use overloading to populate in different ways (that par I already get fully)

I want to do that way (even if it is not the most efficient to whoever reads it, as the target is to learn that part.
Was This Post Helpful? 0
  • +
  • -

#3 baneyu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-January 10

Re: create an array class

Posted 24 June 2010 - 02:38 AM

For Info.
I figured it out
it might not be the cleanest, and for other beginners, it might not be the best practice, unless approved by more experienced readers.
here is my code:
--------Main.java----------------
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package mycompression001;

import javax.swing.JOptionPane;

public class Main {

    public static void main(String[] args) {
        // take the size the user wants
        String mySizeStr = JOptionPane.showInputDialog(null, "enter the required size", "input the size",JOptionPane.QUESTION_MESSAGE);

        // get the square root for that given size
        double mySringDbl = Double.parseDouble(mySizeStr);
        double mySideDbl = Math.sqrt(mySringDbl);
        // convert the double to a string as arrays require integers
        int mySideInt = (int) mySideDbl;
  
       // call MyArray , with the integer passed, 
       // there is no return value from the call, but an object created
        MyArr baneArr = new MyArr(mySideInt);

        // call the populate method, and passing as argument the object.<name of
        // the array in the object
        MyArr.populate(baneArr.myArrayObjects);
    }
}
-------MyArr.java--------------

package mycompression001;
import java.util.Random;

public class MyArr {
    int capacity;
    int[][] myArrayObjects;

    // constructor, this creates an array 2 dimensional, square with the size passed
    // as an argument when the object was created
    public MyArr(int size){
        this.capacity = size;
        this.myArrayObjects = new int[capacity][capacity];
        }
    
    // method to populate, whit passed : a 2 dimension array I call a2, as this is 
    // a reference to the array and not the array, the changes will not be local 
    // only , instead changes made to the array here will be valid outside of that 
    // method
    public static void populate(int[][] a2){
        Random generator = new Random();
        for (int counterX = 0; counterX < a2.length; counterX++){
            for (int counterY = 0; counterY < a2.length; counterY++){
                int myNum = generator.nextInt(255) + 1;
                a2[counterX][counterY] = myNum;
            }
        }
    }
    public static void main(int[][] args) {
        }
    }


This post has been edited by macosxnerd101: 24 June 2010 - 09:14 AM

Was This Post Helpful? 0
  • +
  • -

#4 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: create an array class

Posted 24 June 2010 - 07:48 AM

First of all, welcome to Dream In Code! I'm sorry someone didn't get you your post sooner, but it seems that you've grasped the concepts of a constructor pretty well. In the future, if you could paste your code inside the < code tages > (there is a button for it on the tool bar) it would be much appreciated. Is there anything else that you would like for us to answer?

Though you don't seem to need it, here is a tutorial for constructors that may answer any other questions about the subject that you may have.

I hope, on behalf of all of us, that you continue on in your quest to learn Java and OOP programming.
Was This Post Helpful? 1
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: create an array class

Posted 24 June 2010 - 09:17 AM

Not really good practice to define a main() method that isn't used to run your program, as you do here: public static void main(int[][] args) {}. The only main() method you should have is in your Main class to run your program. Its header should read: public static void main(String[] args)
Was This Post Helpful? 1
  • +
  • -

#6 baneyu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-January 10

Re: create an array class

Posted 24 June 2010 - 11:02 AM

thank's to both of you: macosxnerd101 and luckless.
I take the input.

Luckless: I will definitely use the insert code (I had no idea)
macosxnerd101: nice info, I tried to check why it was there, and as I am using Netbeans 6.8, you use NEW CLASS on the GUI and I cannot select "invlude main" or deselect, there is no such option I found.
I think there is one in Eclipse though!
I will pay attention, see if that could be configured, if not, I will just create a new class, empty it, and continue typing my own code inside.
Was This Post Helpful? 0
  • +
  • -

#7 Luckless  Icon User is offline

  • </luck>
  • member icon

Reputation: 293
  • View blog
  • Posts: 1,146
  • Joined: 31-August 09

Re: create an array class

Posted 24 June 2010 - 11:04 AM

Happy to help, it is just much easier to find errors like the one mac found when the code looks like code :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10803
  • View blog
  • Posts: 40,262
  • Joined: 27-December 08

Re: create an array class

Posted 24 June 2010 - 11:32 AM

You really should write your GUI code by hand rather than using a GUI Generator, as GUI Generators produce cumbersome, inefficient, and unmaintainable code. You can write much cleaner code by hand. Plus, Swing GUI is so easy to use. In fact, there are a bunch of Swing tutorials in the Java tutorials section, if you want to check them out.
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: create an array class

Posted 24 June 2010 - 03:34 PM

View Postmacosxnerd101, on 24 June 2010 - 10:17 AM, said:

Not really good practice to define a main() method that isn't used to run your program, as you do here: public static void main(int[][] args) {}. The only main() method you should have is in your Main class to run your program. Its header should read: public static void main(String[] args)

You should have a main() method in ALL your classes for unit test

Even if this is main() method wouldn't be invoked when you will run your application it is good to have one to check your constructors/methods behaviour

Here is an example

class Person {
   String firstName, lastName;

   Person(String firstName, String lastName) {
     this.firstName = firstName;
     this.lastName = lastName;
   }

   public String toString() {
     return firstName + " " + lastName;
   }

   public static void main(String[] args) {
     Person p = new Person("John", "Smith");
     System.out.println("I have created person: " + p);
   }
}



Now you can run the main of this Java program and check that it produces the desired results.

Later on you can have a class FriendsBook which will be populated a list a Person
The "main" application will be run invoking the main() method of FriendsBook and will create Person object, but you are confident in your unit tests ran from the Person class that the toString() method works
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1