Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

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


7,788 questions

10,874 answers

573 users

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

1 Answer

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

package javaapplication1;

import java.util.Random;

public class JavaApplication1 {
  
    private static final int DEFAULT_SIZE = 2;
    
    private int[] arr;
    int current = 0;
    
    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 add(int n) {
        resize();
        arr[arr.length - 1] = n;
    }
    private void resize() {
        int[] tmp = new int[arr.length + 1];
        
        System.arraycopy(arr, 0, tmp, 0, arr.length);
        
        arr = tmp;
    }
    @Override
    public String toString(){
        String result = "";
        
        for(int i = 0; i < arr.length; i++)
            result += arr[i] + ", ";

        return result;
    }
     public boolean equal(JavaApplication1 o) {
        boolean result = true;
        if(o == null)
            result = false;
        else if(this == o)
            result = true;
        else if(this.getClass() != o.getClass())
            result = false;
        else {
            int i = 0;
            while(i < arr.length && result) {
                result = this.arr[i] == o.arr[i];
                i++;
            }
        }
        return result;     
    }
    public void setValue(int i, int n)
    {
        arr[i ] = n;
    }
    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(3);
            JavaApplication1 arr3 = new JavaApplication1(0);
        
            System.out.println("arr1:");
            arr1.show();
            
            System.out.println("\narr2:");
            arr2.show();            
            
            System.out.println("\narr3:");
            arr3.show();
           
            System.out.println("\narr3:");
            arr3.add(100);
            arr3.show();
            
            System.out.println("\narr3:");
            String s = arr3.toString();
            System.out.println(s);
            
            System.out.println("\narr1.equal(arr2):");
            System.out.println(arr1.equal(arr2));
            
            JavaApplication1 arr4 = new JavaApplication1();
            System.out.println("\narr4:");
            arr4.show();
            
            arr4.setValue(0, 97);
            arr4.setValue(0, 63);
            
            System.out.println("\narr1.equal(arr4):");
            System.out.println(arr4.equal(arr4));
            
        }
        catch(Exception e) {
            System.out.println(e);
        }
    }
}


/*
run:
 
arr1:
87
41

arr2:
68
33
46

arr3:
38
73

arr3:
38
73
100

arr3:
38, 73, 100, 

arr1.equal(arr2):
false

arr4:
80
27

arr1.equal(arr4):
true

*/

 




answered Jul 1, 2017 by avibootz
...