Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,166 questions

40,722 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
...