Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

sortIncrease!

Hi all,
I was able to implement Random(() *1000), but when I tried to sort them I failed.
Can you please advise me what am I doing wrong?
TwoDIntArray.java
import java.text.*;
public class TwoDIntArray {
public static int[][] twoDIntArray(
int xLen, int yLen, int valStart, int valEnd) {
int[][] array = new int[xLen][yLen];
int increment = new Integer((int)(Math.random() * 1000));
int val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
array[i][j] = val;
val += increment;
}
return array;
}
public static void printArray(int[][] array) {
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.print(" " + array[i][j]);
System.out.println();
}
}
public static void main(String args[]) {
int[][] twoD = twoDDoubleArray(4, 4, 0.0, 100.0);
printArray(twoD);
System.out.println("**********************");
int[][] twoD2 = twoDIntArray(2, 2);
printArray(twoD2);
System.out.println("**********************");
int[][] twoD3 = twoDDoubleArray(8, 4, 0.0, 100.0);
printArray(twoD3);
}
} //The End of TwoDIntArray.java
[1362 byte] By [Kinda Electroni] at [2007-11-11 7:48:58]
# 1 Re: sortIncrease!
>int[][] twoD = twoDDoubleArray(...

What do you mean by this line? I could not understand it. is it supposed be a typdef or a class or method...?
Argo at 2007-11-11 22:37:14 >
# 2 Re: sortIncrease!
>int[][] twoD = twoDDoubleArray(...

What do you mean by this line? I could not understand it. is it supposed be a typdef or a class or method...?

Yes it is :
Two Dimension and the numbers are Double.
Kinda Electroni at 2007-11-11 22:38:20 >
# 3 Re: sortIncrease!
Hi everyone,
Yes, it is homework.
Nevertheless, I am stuck like a duck; how can I use:
public void sortIncrease (int [x]);
Or
public void sortDecrease (int [y]);
It does not work for me.
Any help is highly appreciated
Kinda Electroni at 2007-11-11 22:39:18 >
# 4 Re: sortIncrease!
Hi everyone,
Yes, it is homework.
Nevertheless, I am stuck like a duck; how can I use:
public void sortIncrease (int [x]);
Or
public void sortDecrease (int [y]);
It does not work for me.
Any help is highly appreciated

What? What do you need help with?
destin at 2007-11-11 22:40:18 >
# 5 Re: sortIncrease!
Write a java program that implements a simple algorithm for sorting an array of numbers, which are random numbers double type generated with random() of the class java.lang.Math. You must implement two kinds of sorting for increamental order and decreasing order.
Methods sortIncrease(double[]x) and sortDecrease(double[]x) are designed to sort a given array x.
Kinda Electroni at 2007-11-11 22:41:17 >
# 6 Re: sortIncrease!
Are you familiar with bubble sorting?

Here's sortIncrease:

public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}

sortDecrease should be very easy for you now.. you only have to change one thing.
destin at 2007-11-11 22:42:20 >
# 7 Re: sortIncrease!
Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Ahmad>a

C:\Documents and Settings\Ahmad>REM PATH=C:\Program Files\Java\jdk1.5.0_06\bin;

C:\Documents and Settings\Ahmad>REM PATH=.; C:\Program Files\Java\jdk1.5.0_06\bi
n;

C:\Documents and Settings\Ahmad>CD C:\javaprogramming
C:\javaprogramming>javac ArrayMax.java
ArrayMax.java:24: illegal start of expression
public double[] sortIncrease(double[] array) {
^
1 error

C:\javaprogramming>
This is what I have got.
Why it is illegal start?
I am sure the code is good, I am the ....
Kinda Electroni at 2007-11-11 22:43:18 >
# 8 Re: sortIncrease!
Post your code. You might need to make it static.
destin at 2007-11-11 22:44:21 >
# 9 Re: sortIncrease!
import java.util.*;
import java.text.*;

public class TwoDDoubleArray {
public static double[][] twoDDoubleArray(
int xLen, int yLen, double valStart, double valEnd) {
double[][] array = new double[xLen][yLen];
double increment = (valEnd - valStart)/(xLen * yLen);
double val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
array[i][j] = val;
val += increment;
}
return array;
}
public static void printArray(double[][] array) {
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.print(" " + array[i][j]);
System.out.println();
}
}
public static void main(String args[]) {
double[][] twoD = twoDDoubleArray(4, 6, 47.0, 99.0);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
System.out.println("**********************");
double[][] twoD3 = twoDDoubleArray(9, 5, 47.0, 99.0);
printArray(twoD3);
public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
}
} //The End of TwoDDoubleArray.java
Kinda Electroni at 2007-11-11 22:45:24 >
# 10 Re: sortIncrease!
wow you're confused

public static void main(String args[]) {
double[][] twoD = twoDDoubleArray(4, 6, 47.0, 99.0);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
System.out.println("**********************");
double[][] twoD3 = twoDDoubleArray(9, 5, 47.0, 99.0);
printArray(twoD3);
}

public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
destin at 2007-11-11 22:46:30 >
# 11 Re: sortIncrease!
I tried to find it out myself, but I am still missing the point.

This piece is to call the arry and give the Dimensions.
I would like to stro the result of it; Is this right?
What am I missing?

public static void main(String args[]) {
/*
double[][] twoD = twoDDoubleArray(4, 6, 47.0, 99.0);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
System.out.println("**********************");
double[][] twoD3 = twoDDoubleArray(9, 5, 47.0, 99.0);
printArray(twoD3);
*/
// public double[] sortIncrease(double[] array) {
double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
}
} //The End of TwoDDoubleArray.java
Kinda Electroni at 2007-11-11 22:47:28 >
# 12 Re: sortIncrease!
No. Did you look at my code?
destin at 2007-11-11 22:48:31 >
# 13 Re: sortIncrease!
import java.util.*;
import java.text.*;

public class TwoDDoubleArray {
public static double[][] twoDDoubleArray(
int xLen, int yLen, double valStart, double valEnd) {
double[][] array = new double[xLen][yLen];
double increment = (valEnd - valStart)/(xLen * yLen);
double val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
array[i][j] = val;
val += increment;
}
return array;
/*
public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
*/
}


public static void printArray(double[][] array) {
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.print(" " + array[i][j]);
System.out.println();
}
}
public static void main(String args[]) {

double[][] twoD = twoDDoubleArray(4, 6, 9.0, 99.0);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
System.out.println("**********************");
double[][] twoD3 = twoDDoubleArray(9, 5, 9.0, 99.0);
printArray(twoD3);

// public double[] sortIncrease(double[] array) {

}
} //The End of TwoDDoubleArray.java
I've tied them all.
I read them, but I still could not figure it out.
Kinda Electroni at 2007-11-11 22:49:27 >
# 14 Re: sortIncrease!
Kinda, it's just a method.

public class SortIncreaseDemo {
static public void main(String args[]) {
double[] array = new double[] { 3.45, 56.6, 230.3, 84.9, -43.4, 2.9 };
array = sortIncrease(array);
printArray(array);
}

static public void printArray(double[] array) {
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}

static public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
}

I think you need to go through some tutorials (http://java.sun.com/docs/books/tutorial/java/index.html)...
destin at 2007-11-11 22:50:27 >
# 15 Re: sortIncrease!
I thank you very much.
Yes I need this one:
I think you need to go through some tutorials...
It is still fuzzy for me. I did not reply right away, as I was reading and trying to understand.
Now I can say that I know what is going on.
Once again thank you.
Kinda Electroni at 2007-11-11 22:51:25 >
# 16 Re: sortIncrease!
I thank you very much,

public class SortDecreaseDemo {
static public void main(String args[]) {
double[] array = new double[] { 3.45, 56.6, 230.3, 84.9, -43.4, 2.9 };
array = sortIncrease(array);
printArray(array);
array = sortDecrease(array);
printArray(array);
}

static public void printArray( double[] array) {
System.out.println(" ");
fancyPrint2("Sort Array Increase");
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
System.out.println(" ");
fancyPrint1("Sort Array Decrease");
}

public static void fancyPrint1(String s) {
System.out.println("***********");
System.out.println(s);
System.out.println("***********");
return;
}
public static void fancyPrint2(String s) {
System.out.println("***********");
System.out.println(s);
System.out.println("***********");
return;
}


static public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
static public double[] sortDecrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] > array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}

}
// The End of SortDecreaseDemo.java
Your help is highly appreciated.
Kinda Electroni at 2007-11-11 22:52:29 >
# 17 Re: sortIncrease!
public class SortDecreaseArray {
static public void main(String args[]) {
double[] array = new double[] { 3.45, 56.6, 230.3, 84.9, -43.4, 2.9 };
array = sortIncrease(array);
fancyPrint2("Sort Array Increase");
printArray(array);

array = sortDecrease(array);
fancyPrint1("Sort Array Decrease");
printArray(array);

}

static public void printArray( double[] array) {
System.out.println(" ");
for (int i = 0; i < array.length; i++) {
System.out.println("i=" + i + " " + array[i]);
}
System.out.println(" ");
//fancyPrint1("Sort Array Decrease");
}

public static void fancyPrint1(String s) {
System.out.println("********************");
System.out.println(s);
System.out.println("********************");
return;
}
public static void fancyPrint2(String s) {
System.out.println("********************");
System.out.println(s);
System.out.println("********************");
return;
}


static public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
static public double[] sortDecrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] > array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}

}
// The End of SortDecreaseArray.java

Now, I have to make the numbers come out of Random ()
Kinda Electroni at 2007-11-11 22:54:37 >
# 18 Re: sortIncrease!
Now, I have to make the numbers come out of Random ()
Awesome. If you need more help, feel free to post. PS.. use the
code ([code][ /code]) tags when posting code.
destin at 2007-11-11 22:55:31 >
# 19 Re: sortIncrease!
import java.util.*;
import java.text.*;

public class TwoDDoubleArray {
public static double[][] twoDDoubleArray(int xLen, int yLen, double valStart, double valEnd) {
double[][] array = new double[xLen][yLen];
//double increment = new Integer((int)(Math.random() * 10));//(valEnd - valStart)/(xLen * yLen);
//double val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
//array[i][j] = val;
// val += increment;
array[i][j] = new Integer((int)(Math.random() * 100));

}
return array;
}
static double[][] sortIncrease(double[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[i][j] < array[i][j - 1]) {
double temp = array[i][j - 1];
array[i][j - 1] = array[i][j];
array[i][j] = temp;
}
}
}
return array;
}

public static void printArray(double[][] array) {
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.print(" " + array[i][j]);
System.out.println();
}
}
public static void fancyPrint1(String s) {
System.out.println("************************");
System.out.println(s);
System.out.println("************************");
return;
}
public static void main(String args[]) {

/*
double[][] twoD = twoDDoubleArray(4, 6, 9.00, 99.00);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
*/

fancyPrint1("Random Not Sorted Array");
double[][] twoD3 = twoDDoubleArray(1, 10, 0.00, 99.00);
printArray(twoD3);
System.out.println("*************************");
fancyPrint1("Random Sorted Array");
//sortIncrease(double[][] array);
twoD3 = sortIncrease(twoD3);
printArray(twoD3);
System.out.println("*************************");

}
} //The End of TwoDDoubleArray.java

This line is not working properly:
twoD3 = sortIncrease(twoD3);
I think the proble with j and j-1 ....
Kinda Electroni at 2007-11-11 22:56:34 >
# 20 Re: sortIncrease!
I posted a method to sort a one dimensional array. You're trying to sort a
two dimensional array. What exactly are you trying to do? There are several
ways to sort a two dimensional array. Please specify. Also, please
use the code tags when posting code.
destin at 2007-11-11 22:57:41 >
# 21 Re: sortIncrease!
Also, please
use the code tags when posting code.
What do you mean by code tags?
I would like to sort two dimenssional array.
How can I modify yours to do so?
Or should I do something different?
Kinda Electroni at 2007-11-11 22:58:37 >
# 22 Re: sortIncrease!
What do you mean by code tags?
Code Tags ( http://forums.dev-archive.com/misc.php?do=bbcode#code)
I would like to sort two dimenssional array.
Why? What does the two dimensional array consist of? doubles? I don't see
why you would need to do this.
destin at 2007-11-11 22:59:40 >
# 23 Re: sortIncrease!
I was wondering "How can I do it"
Code
The tag switches to a fixed-width (monospace) font and preseves all spacing.
Usage [code]value
Example Usage
<script type="text/javascript">
<!--
alert("Hello world!");
//-->
</script>

Example Output <script type="text/javascript">
<!--
alert("Hello world!");
//-->
</script>
Thank you very much.
I do appreciate all.
If you would like to hlep more, it is kind from you. I am learning.
Kinda Electroni at 2007-11-11 23:00:41 >
# 24 Re: sortIncrease!
Please post the assignment so I can get a better idea of what you are
trying to do.
destin at 2007-11-11 23:01:35 >
# 25 Re: sortIncrease!
I do not know how to do it, as it ask to use BlueJ to be able to get the same window as same as TME1.
I could not copy TME1 and Past it.
In general it is asking to get sortIncreaseArray, and sortdecreaseArray for fixed arry [1x6]. double and integer.
Then repeat for array generated be random and sort it both ways. Int and double as well.
You must implement each class shown in figure tme1 and meet the folowing:
Methods sortIncrease(doubl[] x)
Methods sortIncrease(int[] x) are designed to generate random numbers.
Creat them call sortIncrease and print them as they are shown at Figuretme1.
repeat for....
Kinda Electroni at 2007-11-11 23:02:39 >
# 26 Re: sortIncrease!
You can notice that I have to call: twoD3 = sortIncrease(twoD3); Four times. But, hay; it works.
Still, I do not know how to implement Code[code\]
I need acctuale example.
I know myself. it is thick. does not take from the first time.

Thank you man.

import java.util.*;
import java.text.*;

public class TwoDDoubleArray {
public static double[][] twoDDoubleArray(int xLen, int yLen, double valStart, double valEnd) {
double[][] array = new double[xLen][yLen];
//double increment = new Integer((int)(Math.random() * 10));//(valEnd - valStart)/(xLen * yLen);
//double val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
//array[i][j] = val;
// val += increment;
array[i][j] = new Integer((int)(Math.random() * 100));

}
return array;
}
static double[][] sortIncrease(double[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[i][j] < array[i][j - 1]) {
double temp = array[i][j - 1];
array[i][j - 1] = array[i][j];
array[i][j] = temp;
}
}
}
return array;
}

public static void printArray(double[][] array) {
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.print(" " + array[i][j]);
System.out.println();
}
}
public static void fancyPrint1(String s) {
System.out.println("****************************");
System.out.println(s);
System.out.println("****************************");
return;
}
public static void main(String args[]) {

/*
double[][] twoD = twoDDoubleArray(4, 6, 9.00, 99.00);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
*/

fancyPrint1("Random Not Sorted Array");
double[][] twoD3 = twoDDoubleArray(5, 5, 0.00, 99.00);
printArray(twoD3);
System.out.println("****************************");
fancyPrint1("Random Sorted Array");
//sortIncrease(double[][] array);
twoD3 = sortIncrease(twoD3);
twoD3 = sortIncrease(twoD3);
twoD3 = sortIncrease(twoD3);
twoD3 = sortIncrease(twoD3);
printArray(twoD3);
System.out.println("****************************");

}
} //The End of TwoDDoubleArray.java
/*
static public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}

$myvar = 'Hello World!';
for ($i = 0; $i < 10; $i++)
{
echo $myvar . "\n";
}
*/
Kinda Electroni at 2007-11-11 23:03:45 >
# 27 Re: sortIncrease!
I still don't understand why you are trying to sort a two dimensional array.
destin at 2007-11-11 23:04:45 >
# 28 Re: sortIncrease!
Your question is logical one, maybe I missunderstand the assignment.
Now, I am after getting the info. from a file and then sort it.
The TME1 will be done.
Once again, thank you
Kinda Electroni at 2007-11-11 23:05:40 >
# 29 Re: sortIncrease!
Okay, so correct me if I'm wrong.
You have a file with an unknown amount of doubles in it. You need to store all those
numbers in an array (or ArrayList), then sort it.
Is that all?
destin at 2007-11-11 23:06:46 >
# 30 Re: sortIncrease!
That is right, but I have to use the same sortIncrease class that I have used so far.
i. e. there is a package and inheret...import...all in one jar file.
Kinda Electroni at 2007-11-11 23:07:42 >
# 31 Re: sortIncrease!
Write a Java program that has a given class diagrams described in figure tme1 with BlueJ tool (v2.0.3 or greater version). The program implements a simple algorithm for sorting an array of numbers, which are random numbers (double type) generated with method random() of the class java.lang.Math. You must implement two kinds of sorting for incremental order and decreasing order. The result must be printed from array on the screen as figure tme1 shows.

You must implement each class shown in figure tme1 and meet the following functions and architecture requirements:

Methods sortIncrease(double[ ] x) and sortDecrease(double[] x) are designed to sort a given array x.
Methods sortIncrease(double[ ] x) and sortDecrease(double[] x) are defined in the interface MyapiA, which is implemented with class SortA.
Methods sortIncrease(int x) and sortDecrease(int x) are designed to generate random numbers, create array containing these numbers, call sortIncrease(double[ ] x) for sorting, and then print result out as figure tme1 shows (one time running and output). Variable int x assigns total number of the random numbers to be generated.
Methods sortIncrease(int x) and sortDecrease(int x) are defined in the interface MyapiB, which is implemented with class SortB.
Class Sort contains main() method and provides entry point for the whole program. The total number of the random numbers must be assigned with method main(args)s args when program is running, as shown in figure tme1.
The whole program must be coded and runnable with BlueJ tool. Your program is required to show the same class diagrams and produce result in the same output style as figure tme1 does.
Kinda Electroni at 2007-11-11 23:08:42 >
# 32 Re: sortIncrease!
Ah, okay. We weren't on the right track. Okay, so we need to make 3 classes, Sort (this will be the main class), SortA, and SortB. SortA and SortB implement MyapiA and MyapiB, respectively. SortA and SortB have sort methods (from the interfaces) that generate an array of random numbers and sort them.

Try this, and post back if you have any trouble. If you're posting code please, please, please use the code tags (http://forums.dev-archive.com/misc.php?do=bbcode#code).

Good luck! :)
destin at 2007-11-11 23:09:51 >
# 33 Re: sortIncrease!
I failed
Kinda Electroni at 2007-11-11 23:10:47 >
# 34 Re: sortIncrease!
I failed
... I really don't know what to say. Okay. Next time you try to post code,
click on the # (underneath the color choice pull down menu) and post your code in there.
destin at 2007-11-11 23:11:51 >
# 35 Re: sortIncrease!
import java.util.*;
import java.util.*;
import java.text.*;

public class TwoDDSortIncDecArray {
public static double[][] twoDDoubleArray(int xLen, int yLen, double valStart, double valEnd) {
double[][] array = new double[xLen][yLen];
//double increment = new Integer((int)(Math.random() * 10));//(valEnd - valStart)/(xLen * yLen);
//double val = valStart;
for(int i = 0; i < array.length; i++)
for(int j = 0; j < array[i].length; j++) {
//array[i][j] = val;
// val += increment;
array[i][j] = new Integer((int)(Math.random() * 100));

}
return array;
}
static double[][] sortIncrease(double[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[i][j] < array[i][j - 1]) {
double temp = array[i][j - 1];
array[i][j - 1] = array[i][j];
array[i][j] = temp;
}
}
}
return array;
}

static double[][] sortDecrease(double[][] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[i][j] > array[i][j - 1]) {
double temp = array[i][j - 1];
array[i][j - 1] = array[i][j];
array[i][j] = temp;
}
}
}
return array;
}

public static void printArray(double[][] array) {
for(int i = 0; i < array.length; i++) {
for(int j = 0; j < array[i].length; j++)
System.out.print(" " + array[i][j]);
System.out.println();
}
}
public static void fancyPrint1(String s) {
System.out.println("****************************");
System.out.println(s);
System.out.println("****************************");
return;
}
public static void main(String args[]) {

/*
double[][] twoD = twoDDoubleArray(4, 6, 9.00, 99.00);
printArray(twoD);
System.out.println("**********************");
double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
printArray(twoD2);
*/

fancyPrint1("Random Not Sorted Array");
double[][] twoD3 = twoDDoubleArray(5, 5, 0.00, 99.00);
printArray(twoD3);
System.out.println("****************************");
fancyPrint1("Random Increase Sorted Array");
twoD3 = sortIncrease(twoD3);
twoD3 = sortIncrease(twoD3);
twoD3 = sortIncrease(twoD3);
twoD3 = sortIncrease(twoD3);
printArray(twoD3);
System.out.println("****************************");
fancyPrint1("Random Decrease Sorted Array");
twoD3 = sortDecrease(twoD3);
twoD3 = sortDecrease(twoD3);
twoD3 = sortDecrease(twoD3);
twoD3 = sortDecrease(twoD3);
printArray(twoD3);
System.out.println("****************************");

}
} //The End of TwoDDSortIncDecArray.java
Kinda Electroni at 2007-11-11 23:12:56 >
# 36 Re: sortIncrease!
Hurrah! You posted in the code tags... yay. Okay, unfortunately, you're not
doing any thing in the assignment posted. Here's a basic outline. I'm leaving you to fill in the blanks.

public class Sort {
static public void main(String[] args) {
(new SortA()).sortIncrease(randomDoubleArray);
(new SortA()).sortDecrease(randomDoubleArray);
(new SortB()).sortIncrease(randomIntArray);
(new SortB()).sortDecrease(randomIntArray);
}
}

class SortA implements MyapiA {
public void sortIncrease(double[] array) {}
public void sortDecrease(double[] array) {}
}

class SortB implements MyapiB {
public void sortIncrease(int[] array) {}
public void sortDecrease(int[] array) {}
}

interface MyapiA {
public void sortIncrease(double[] array);
public void sortDecrease(double[] array);
}

interface MyapiB {
public void sortIncrease(int[] array);
public void sortDecrease(int[] array);
}
destin at 2007-11-11 23:13:52 >
# 37 Re: sortIncrease!
I am working on it.
Kinda Electroni at 2007-11-11 23:14:49 >
# 38 Re: sortIncrease!
public class TME1Test {
public static double[] oneDArray(int xLen) {
double[] array = new double[xLen];

for(int i = 0; i < array.length; i++){

array[i]= new Integer((int)(Math.random() * 100));
}
return array;
}
Kinda Electroni at 2007-11-11 23:15:52 >
# 39 Re: sortIncrease!
You have an array of doubles, not of Integers.
Change
array[i]= new Integer((int)(Math.random() * 100));
to
array[i] = Math.random() * 100;
destin at 2007-11-11 23:16:57 >
# 40 Re: sortIncrease!
import java.util.*;
import java.text.*;

public class TME1Test {
public static double[] oneDArray(int xLen) {
double[] array = new double[xLen];

for(int i = 0; i < array.length; i++){

//array[i]= new Integer((int)(Math.random() * 100));
array[i] = Math.random() * 100;
printArray(array);
}
return array;
}

public static void main(String args[]) {

fancyPrint1("Random Not Sorted Array");
double[]randomDoubleArray = oneDArray(6);
printArray(array);
}



public static void fancyPrint1(String s) {
System.out.println("****************************");
System.out.println(s);
System.out.println("****************************");
return;
}

public static void printArray( double[] array) {
System.out.println(" ");
for (int i = 0; i < array.length; i++) {
System.out.println("i=" + i + " " + array[i]);
}
System.out.println(" ");
//fancyPrint1("Sort Array Decrease");
}
}//The End of TME1Test.java
Kinda Electroni at 2007-11-11 23:17:53 >
# 41 Re: sortIncrease!
Please stop just posting code. Explain your problem, post exact errors from the compiler, etc.

public class MyClass {
static public void main(String args[]) {
printArray(rndDoubleArray(6));
}

static public double[] rndDoubleArray(int len) {
double[] array = new double[len];

for (int i = 0; i < len; i++){
array[i] = Math.random() * 100;
}
return array;
}

static public void printArray(double[] array) {
System.out.print("{ ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println("}");
}
}
destin at 2007-11-11 23:18:52 >
# 42 Re: sortIncrease!
public class Sort {

static public void main(String args[]) {
double[] x = rndDoubleArray(6);
fancyPrint1("Random Not Sorted Array");
printArray(x);
fancyPrint1("Random Increase Sorted Array");
x = sortIncrease(x);
printArray(x);


fancyPrint1("Random Decrease Sorted Array");
x = sortDecrease(x);
printArray(x);
}

static public double[] rndDoubleArray(int len) {
double[] array = new double[len];

for (int i = 0; i < array.length; i++){
array[i] = Math.random() * 100;
}
return array;
}

/* static public void printArray(double[] array) {
System.out.print("{ ");
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println("}");
}
*/
static public double[] sortIncrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] < array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}
static public double[] sortDecrease(double[] array) {
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < array.length; j++) {
if (array[j] > array[j - 1]) {
double temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
return array;
}


public static void printArray( double[] array) {
System.out.println(" ");
for (int i = 0; i < array.length; i++) {
System.out.println("i=" + i + " " + array[i]);
}
System.out.println(" ");
System.out.println("****************************");
}
public static void fancyPrint1(String s) {
System.out.println("****************************");
System.out.println(s);
System.out.println("****************************");
return;
}
}//The End of Sort.java
Kinda Electroni at 2007-11-11 23:20:02 >
# 43 Re: sortIncrease!
You didn't follow the instructions. You need to make two interfaces and two classes.
destin at 2007-11-11 23:20:56 >
# 44 Re: sortIncrease!
Any way, I have to make two classes and two Interfaces, and but them in two packages one for classes and the other for Interfaces.
I have tried many ways to make use of :
public class sort {
static public void main(String[] args) {
(new sortA()).sortIncrease(randomDoubleArray);
(new sortA()).sortDecrease(randomDoubleArray);
(new sortB()).sortIncrease(randomIntArray);
(new sortB()).sortDecrease(randomIntArray);
}
}
But no luck.
I was able to get :
sortIncrease(rndDoubleArray); and sortDecrease(rndDoubleArray);
but, I failed to get (new sortA()).sortIncrease(rndDoubleArray);
Any advice?
Thanks in advance.
Kinda Electroni at 2007-11-11 23:21:56 >
# 45 Re: sortIncrease!
You have to create the classes SortA and SortB. These classes will contain the sortIncrease and sortDecrease methods. The Sort class will not contain these methods.
destin at 2007-11-11 23:23:00 >
# 46 Re: sortIncrease!
Now, how sortIncrease or sortDecrease will call rndArray?
Should I put:
Package Sort as the first line of each file and put the three files in this directory?
Thank you in advance.
Kinda Electroni at 2007-11-11 23:23:59 >
# 47 Re: sortIncrease!
Here's Sort, SortA, and MyapiA. You still need SortB and MyapiB.

public class Sort {
static public void main(String[] args) {
SortA sa = new SortA(15);

sa.sortIncrease(sa.getArray());
for (int i = 0; i < sa.getArray().length; i++) {
System.out.print(sa.getArray()[i] + ", ");
if ((i + 1) % 3 == 0) {
System.out.println();
}
}
}
}

class SortA implements MyapiA {
double[] array;

public SortA(int len) {
array = new double[len];

for (int i = 0; i < array.length; i++) {
array[i] = Math.random() * 100;
}
}

public void sortIncrease(double[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = 1; j < x.length; j++) {
if (x[j] < x[j - 1]) {
double temp = x[j];
x[j] = x[j - 1];
x[j - 1] = temp;
}
}
}
}

public void sortDecrease(double[] x) {
for (int i = 0; i < x.length; i++) {
for (int j = 1; j < x.length; j++) {
if (x[j] > x[j - 1]) {
double temp = x[j];
x[j] = x[j - 1];
x[j - 1] = temp;
}
}
}
}

public double[] getArray() {
return array;
}
}

interface MyapiA {
public void sortIncrease(double[] x);
public void sortDecrease(double[] x);
}
destin at 2007-11-11 23:25:07 >