Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. I check, compile and run every program. Contact me at: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


6,869 questions

9,775 answers

573 users

How to create a class that provide a simple array of integers in Java

1 Answer

0 votes
// java code that create a class that provide a simple array of integers

package javaapplication1;

import java.util.Random;

public class JavaApplication1 {
  
    private static final int DEFAULT_SIZE = 3;
    
    private int[] arr;
    
    public JavaApplication1() {
        this(DEFAULT_SIZE);
    }
    public JavaApplication1(int size) {
        if (size > 0)
            arr = new int[size];
        else
            arr = new int[DEFAULT_SIZE];
        init();
    }
    private void init() {
       Random rnd = new Random(); 
       for (int i = 0; i < arr.length; i++)
           arr[i] = rnd.nextInt(100);
    }
    public void show()
    {
         for (int i = 0; i < arr.length; i++)
           System.out.println(arr[i]);
    }
    public static void main(String[] args) {
        try
        {   
            JavaApplication1 arr1 = new JavaApplication1();
            JavaApplication1 arr2 = new JavaApplication1(5);
            JavaApplication1 arr3 = new JavaApplication1(0);
        
            arr1.show();
            System.out.println();
            arr2.show();            
            System.out.println();
            arr3.show();
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
}


/*
run:
 
7
98
32

67
5
87
62
30

11
17
95

*/

 




answered Jun 30, 2017 by avibootz
...