Jump to content

how to join these two programs together? I'm stuck!!!!!

- - - - -

  • Please log in to reply
2 replies to this topic

#1
owh786

owh786

    Newbie

  • Members
  • Pip
  • 7 posts
How do I join these two programs to answer these questions?

1) Create a method RandomArray that takes in as parameter an integer n and returns a ArrayList of Integers (ArrayList<Integer>) that consists of n random numbers between 0 and 255 (i.e. the returned array is of size n). Appendix A contains some example code that might be useful.
2) Create a method ShowArray that takes in as a parameter an ArrayList of Integers (ArrayList<Integer>) and displays the contents of the ArrayList.
3) Test these two methods, i.e. create a random list and then display it.


import java.util.ArrayList;
import java.util.Random;

public class LabTest6 {

@SuppressWarnings("unused")
public static void main(String[] args) {

ArrayList<Integer> randomArrayList;int n = 255;
{
ArrayList<Integer> list = new ArrayList<Integer>();
Random random = new Random();

for (int i = 0; i <= n; i++)
{
list.add(new Integer(random.nextInt(255)));
}
System.out.println(list);
}

ArrayList<Integer> CopyArray;ArrayList<Integer> a;
{
ArrayList<Integer> resa = new ArrayList<Integer>(a.size());
for(int i=0;i<a.size();++i) resa.add(a.get(i));
return;
}
ArrayList<Integer> SortA;ArrayList<Integer> a1;
{
ArrayList<Integer> array = CopyArray(a1);
int n1 = a1.size(),i;
boolean noswaps = false;

while (noswaps == false)
{
noswaps = true;
for(i=0;i<n1-1;++i)
{
if (array.get(i) < array.get(i+1))
{
Integer temp = array.get(i);
array.set(i,array.get(i+1));
array.set(i+1,temp);
noswaps = false;
}
}
}
return;
}
private static ArrayList<Integer> randomArray(int n) { ... }
private static void showArray(ArrayList<Integer> a) { ... }
private static void test() {
int n = 13;
ArrayList<Integer> a = randomArray(n);
showArray(a);
}


}

}

#2
JackPanzer

JackPanzer

    Newbie

  • Members
  • Pip
  • 4 posts
I thik this is too much, why not to create the two methods instead of creating twice the same?

import java.util.ArrayList;

public class LabTest6
{
public ArrayList<Integer> RandomArray(int number)
{
ArrayList<Integer> ret = new ArrayList<Integer>();
//Code here
return ret;
}
public void ShowArray(ArrayList<Integer> lst)
{
//Code here
}

public static void main(String[] args)
{
LabTest6 test = new LabTest6();
ArrayList<Integer> values;
values = test.RandomArray(7); //7 for example x)
test.ShowArray(values);
}
}

#3
owh786

owh786

    Newbie

  • Members
  • Pip
  • 7 posts
hello. I've got it working, however the last Array, Sorted C Method outputs this: SORTED C METHOD: 179, 181, 238, 190, 105, 144, 26, 63, 90, 14, SORTED C METHOD: 190, 238, 181, 179, 105, 144, 26, 63, 90, 14, SORTED C METHOD: 238, 190, 181, 179, 105, 144, 26, 63, 90, 14, SORTED C METHOD: 238, 190, 181, 179, 144, 105, 26, 63, 90, 14, SORTED C METHOD: 238, 190, 181, 179, 144, 105, 90, 63, 26, 14, .

Why does it repeat so many times when Sorted A Method and Sorted B Method only output one occurence?
Below is the code:

Main Class:
import java.util.ArrayList;
import java.util.Random;
public class main
{
public static void main(String[] args)
{
ThreeSorts.SortA(randArray(0));
System.out.println("\n");
ThreeSorts.SortB(randArray(0));
System.out.println("\n");
ThreeSorts.SortC(randArray(0));
System.out.println("\n");
}
public static ArrayList<Integer> randArray(int n)
{
ArrayList<Integer> a = new ArrayList<Integer>(n);
Random rand = new Random();
rand.setSeed(System.currentTimeMillis());

int[] x = new int[10];
for (int i=0; i<x.length; i++)
{
Integer r = Math.abs(rand.nextInt() % 256);
a.add®;
}
showArray(a);
return a;
}
public static void showArray(ArrayList<Integer>a)
{
int n = a.size();
System.out.println("RANDOM NUMBERS GENERATED 0 TO 255:");
for (int i=0; i<n; i++)
{
int r = a.get(i);
System.out.print("|"+r+"| ");
}
System.out.println("\n");
}
public static void showA(ArrayList<Integer>array)
{
int n = array.size();
System.out.print("SORTED A METHOD: ");
for (int i=0; i<n; i++)
{
int r = array.get(i);
System.out.print(r+", ");
}
}
public static void showB(ArrayList<Integer>array)
{
int n = array.size();
System.out.print("SORTED B METHOD: ");
for (int i=0; i<n; i++)
{
int r = array.get(i);
System.out.print(r+", ");
}
}
public static void showC(ArrayList<Integer>array)
{
int n = array.size();
//for (int s=0; s<n; s++)
System.out.print("SORTED C METHOD: ");
for (int i=0; i<n; i++)
{
int r = array.get(i);
System.out.print(r+", ");
}
}

}

Sorted Arrays Class:

//Java Code for Laboratory 6 – Week 6 Sorting Algorithms
//Brunel University, DISC Module CS2004, 2011-2012

import java.util.*;
public class ThreeSorts
{
private static ArrayList<Integer> CopyArray(ArrayList<Integer> a)
{
ArrayList<Integer> resa = new ArrayList<Integer>(a.size());
for(int i=0;i<a.size();++i)
{
resa.add(a.get(i));
}
return(resa);
}
public static ArrayList<Integer> SortA(ArrayList<Integer> a)
{
ArrayList<Integer> array = CopyArray(a);
int n = a.size(),i;
boolean noswaps = false;

while (noswaps == false)
{
noswaps = true;
for(i=0;i<n-1;++i)
{
if (array.get(i) < array.get(i+1))
{
Integer temp = array.get(i);
array.set(i,array.get(i+1));
array.set(i+1,temp);
noswaps = false;
}
}
}
main.showA(array);
return(array);

}
public static ArrayList<Integer> SortB(ArrayList<Integer> a)
{
ArrayList<Integer> array = CopyArray(a);
Integer[] zero = new Integer[a.size()];
Integer[] one = new Integer[a.size()];
int i,b;
Integer x,p;
//Change from 8 to 32 for whole integers - will run 4 times slower
for(b=0;b<8;++b)
{
int zc = 0;
int oc = 0;
for(i=0;i<array.size();++i)
{
x = array.get(i);
p = 1 << b;
if ((x & p) == 0)
{
zero[zc++] = array.get(i);
}
else
{
one[oc++] = array.get(i);
}
}
for(i=0;i<oc;++i) array.set(i,one[i]);
for(i=0;i<zc;++i) array.set(i+oc,zero[i]);
}
main.showB(array);
return(array);
}
public static ArrayList<Integer> SortC(ArrayList<Integer> a)
{
ArrayList<Integer> array = CopyArray(a);
SortC(array,0,array.size()-1);
return(array);
}
public static void SortC(ArrayList<Integer> array,int first,int last)
{
if (first < last)
{
int pivot = PivotList(array,first,last);
SortC(array,first,pivot-1);
SortC(array,pivot+1,last);
}

}
private static void Swap(ArrayList<Integer> array,int a,int b)
{
Integer temp = array.get(a);
array.set(a,array.get(b));
array.set(b,temp);
}
private static int PivotList(ArrayList<Integer> array,int first,int last)
{
Integer PivotValue = array.get(first);
int PivotPoint = first;
for(int index=first+1;index<=last;++index)
{
if (array.get(index) > PivotValue)
{
PivotPoint = PivotPoint+1;
Swap(array,PivotPoint,index);
}
}
main.showC(array);
Swap(array,first,PivotPoint);
return(PivotPoint);
}
}




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users