i'm trying to build a code for different actions in arrays with different methods (Constructors) but i cannot seem to find a way to link them...I've made a array with 100 elements and a random variable to fill it....i'd like to know how to get my random elements from the first methods to the second one to make the comparing.... and also i'd like to not include in the 0 element in the random generator..any help?
this is my code
import java.util.*;
public class prova1{
public int min;
public void tabele(){
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
System.out.println(e[i]);
}
}
public void emin(){
Random r = new Random();
int d;
int e[]=new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(100);
e[i]=d;
if(e[i]<min){
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
prova.tabele();
prova.emin();
}
}
Return the array from tabele to a local variable and send it as parameter to emin
import java.util.*;
public class prova1 {
public int min;
public int[] tabele() {
Random r = new Random();
int d;
int e[]= new int[100];
for(int i=0; i<e.length;i++){
d=r.nextInt(99) + 1;
e[i]=d;
System.out.println(e[i]);
}
return e;
}
public void emin(int[] e) {
Random r = new Random();
int d;
for(int i=0; i<e.length;i++) {
if(e[i]<min) {
min=e[i];
}
}
System.out.println("Vlera me e vogel eshte: " +min);
}
public static void main(String []args){
prova1 prova = new prova1();
int[] arr = prova.tabele();
prova.emin(arr);
}
}
Related
sorry I'm really new to all of this. I know this is a stupid/easy question, but how would I display the shuffled array after i've set it all up. I have my code below and made the class that creates the array and has the algorithm for shuffling the integers inside the array. But I can't figure out how to display the shuffled array. Heres my code below:
My main:
package lab4b;
import java.util.Scanner;
public class Lab4B {
public static void main(String[] args) {
Shuffler test = new Shuffler(15);
test.Shuffle();
test.display();
}
}
and my Shuffle class:
package lab4b;
import java.security.SecureRandom;
public class Shuffler {
private static final SecureRandom randomNumbers = new SecureRandom();
private int [] data;
public Shuffler (int size){
data = new int [size];
for(int i = 0; i<data.length;i++){
data[i]= i+1;
}
}
public void Shuffle(){
int temp;
for(int first = 0; first<data.length; first++){
int second = randomNumbers.nextInt(data.length);
temp = data[first];
data[first] = data[second];
data[second] = temp;
}
}
public void display()
{
for(int counter =0; counter<data.length; counter++ ){
System.out.print(data[counter]+ " ");
}
System.out.println();
}
}
In this loop you are reset the value of the dataarray
for(int counter =0; counter<data.length; counter++ ){
// data[counter] = counter + 1; - do not do this
System.out.print(data[counter]+ " ");
}
I'm trying to append all of numbers that the program generates, into an array.
I used ArrayList method. However i can't manage program to read arr elements.
import java.util.Random;
import java.util.ArrayList;
public class Numbers {
public ArrayList<Double> getNumbers( )
{
Random getNum=new Random();
ArrayList<Double> arr=new ArrayList<Double>(1000);
int size=1000;
for(int i=0;i<size;i++)
{
double gauss=getNum.nextGaussian()*15+70;
arr.add(gauss);
}
System.out.println(arr);
return arr;
}
}
Furthermore; i'm trying all generated numbers to be read via another method.
(That part is the following)
How can the program read all generated numbers?
double [] numbers= StdIn.getNumbers(); // Stuck at here.
Is it possible to calling getNumbers method like that?
You can read it directly by creating the array list of type double:
import java.util.Random;
import java.util.Scanner;
import java.util.ArrayList;
public class Numbers {
public ArrayList<Double> getNumbers() {
Random getNum = new Random();
ArrayList<Double> arr = new ArrayList<Double>(1000);
int size = 1000;
for (int i = 0; i < size; i++) {
double gauss = getNum.nextGaussian() * 15 + 70;
arr.add(gauss);
}
return arr;
}
public static void main(String[] args) {
Numbers number = new Numbers();
ArrayList<Double> arr = number.getNumbers();
System.out.println(arr);
}
}
You can call from other class as bellow
import java.util.Random;
import java.util.ArrayList;
public class PrintArray {
public static void main(String args[]){
ArrayList<Double> arrelements=OtherClass.getNumbers();
java.util.Iterator<Double> iterator = arrelements.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}
class OtherClass{
public static ArrayList<Double> getNumbers( )
{
Random getNum=new Random();
ArrayList<Double> arr=new ArrayList<Double>(1000);
int size=1000;
for(int i=0;i<size;i++)
{
double gauss=getNum.nextGaussian()*15+70;
arr.add(gauss);
}
System.out.println(arr);
return arr;
}
}
You can iterate through the ArrayList to read numbers from your ArrayList.
import java.util.Random;
import java.util.ArrayList;
import javax.swing.text.html.HTMLDocument.Iterator;
public class PrintArray {
public static void main(String args[]){
ArrayList<Double> arrelements=getNumbers();
java.util.Iterator<Double> iterator = arrelements.iterator();
while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
public static ArrayList<Double> getNumbers( )
{
Random getNum=new Random();
ArrayList<Double> arr=new ArrayList<Double>(1000);
int size=1000;
for(int i=0;i<size;i++)
{
double gauss=getNum.nextGaussian()*15+70;
arr.add(gauss);
}
System.out.println(arr);
return arr;
}
}
You might think of something like that:
Initiate on object of your class and call the function
public class Numbers {
public ArrayList<Double> getNumbers( )
{
Random getNum=new Random();
ArrayList<Double> arr=new ArrayList<Double>(1000);
int size=1000;
for(int i=0;i<size;i++)
{
double gauss=getNum.nextGaussian()*15+70;
arr.add(gauss);
}
return arr;
}
public static void main(String[] args) {
Numbers n = new Numbers();
List<Double> numbers = n.getNumbers();
for (Double number : numbers) {
System.out.println(number);
}
}
}
Numbers class
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public final class Numbers {
private Numbers() {}
public static List<Double> getArrayListOfNumbers() { // ArrayList
List<Double> numbers = new ArrayList<>();
for(double d : getArrayOfNumbers()) {
numbers.add(d);
}
return numbers;
}
public static double[] getArrayOfNumbers() { // Array
Random random = new Random();
double[] numbers = new double[1000];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = random.nextGaussian() * 15 + 70;
}
return numbers;
}
}
Test Drive:
import java.util.List;
public class TestDrive {
public static void main(String[] args) {
// Printing arrayList numbers
List<Double> arrayListOfNumbers = Numbers.getArrayListOfNumbers();
arrayListOfNumbers.stream().forEach(System.out::println);
// Printing certain arrayList object
System.out.println("\nArrayList #7: " + arrayListOfNumbers.get(7));
}
}
Extra info:
Usually when there's need for fixed size/length (total values/objects), arrays are used (in your case it's 1000).
While ArrayList is a collection implementing interface List, which is used when there's no need for fixed size/length or when there's need for size/length flexibility.
Write a method called setTo5 which is passed a reference to an array of ints and sets the contents of that array to all 5s. This is what I have so far, and it's giving me an error on the second line after public static void main(...);. How to make a better setter?
public class Set5 {
private static int n = 8;
static int[] boop = new int[n];
public static void main(String[] args){
int[] roop = new int[n];
roop.setTo5(5);
}
public void setTo5(int poop){
for(int i = 0; i<n ; i++){
poop = boop[i];
}
}
}
Try something like this:
public class Set5 {
private static int n = 8;
static int[] boop = new int[n];
public static void main(String[] args){
int[] roop = new int[n];
//Create instance of Set5 class to call setter
Set5 set5reference = new Set5();
set5reference.setTo5(roop);
//All the values in roop will now be 5 as set by setter.
}
//change this method to accept array reference like this
public void setTo5(int[] poop){
for(int i = 0; i<n ; i++){
poop[i] = 5;
}
}
}
To fill an array entirely with some value use:
java.util.Arrays.fill(poop, 5)
In your case:
public class Set5 {
private static int n = 8;
//static int[] boop = new int[n]; // unused variable
public static void main(String[] args){
int[] roop = new int[n];
setTo5(roop);
print(roop);
}
public static void setTo5(int[] poop){
java.util.Arrays.fill(poop, 5)
// for(int i = 0; i<poop.length ; i++){
// poop[i]=5;
//}
}
public static void print(int[] poop){
for(int i = 0; i<poop.length ; i++){
System.out.println("array["+i+"] = "+poop[i]);
}
}
}
anyone has any idea how can I limit this program to print only 5 of the 20 elements of the array.
Greetings and thanks.
import java.util.*;
public class lot {
public static void main(String[] args) {
int n[] = {1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
Random rd = new Random();
for (int i = 0; i < n.length; i++) {
System.out.println(rd.nextInt(n[i]) + 1);
}
}
}
import java.util.*;
public class Lot {
public static void main(String[] args) {
int n[] = {1,2,3,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
Random rd = new Random();
for (int i = 0; i < 5; i++) {
System.out.println(rd.nextInt(n[i]) + 1);
}
}
}
Learn how to use a for loop http://en.wikipedia.org/wiki/For_loop#Java
I am trying to add random numbers to an empty array 20 numbers 0-99. When I run the code below it prints out 51 numbers and they are all 0.
Can someone please help me figure out what I am doing wrong here.
import java.util.Random;
public class SortedArray
{
int randomValues;
int[] value;
public SortedArray()
{
}
public int getRandom()
{
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues = random.nextInt(100);
}
return randomValues;
}
public int getArray()
{
int result = 0;
value = new int[randomValues];
for(int item : value)
{
System.out.println("The array contains " + item);
}
return result;
}
}
Here is my main method
public class ReturnSortedArray
{
public static void main(String[] args)
{
SortedArray newArray = new SortedArray();
int random = newArray.getRandom();
int array = newArray.getArray();
System.out.println(array);
}
}
In your method getArray
the code
value = new int[randomValues];
is simply creating a new empty int array of size ramdomValues.
As the default value of an int is 0, that is what you are getting
Also in your method getRandom you are setting the same value time and time again
for (...)
randomValues = random.nextInt(100);
try
public int[] getRandomArr()
{
int randomValues [] = new int [20];
Random random = new Random();
for(int j=0; j<20; j++)
{
randomValues[j] = random.nextInt(100);
}
return randomValues;
}
I see a few issues, you should probably set the values in your constructor. You could also call it a set method (since it's not actually a get). Also, your getArray() doesn't return an array. So, I think you really wanted something like this,
public class SortedArray {
private Random random = new Random();
private int[] value = new int[20];
public SortedArray() {
super();
setRandomValues();
}
public void setRandomValues() {
for (int j = 0; j < value.length; j++) {
value[j] = random.nextInt(100);
}
}
public int[] getArray() {
return value;
}
}
And then your main method, should be updated like
public static void main(String[] args) {
SortedArray newArray = new SortedArray();
int[] array = newArray.getArray();
System.out.println(Arrays.toString(array));
}