The Artima Developer Community
Sponsored Link

Java Answers Forum
oh the java probrlems......help me !!!! multiple problems

9 replies on 1 page. Most recent reply: May 11, 2002 6:00 AM by Evgen

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 9 replies on 1 page
dork

Posts: 4
Nickname: eggy
Registered: May, 2002

oh the java probrlems......help me !!!! multiple problems Posted: May 2, 2002 1:25 AM
Reply to this message Reply
Advertisement
i got a few problems here.....
1.BUBBLE QUESTION:

Write a sort method using the bubble sort algorithm. The bubble sort algorithm makes several passes through the array. On each pass, neighbouring pairs are compared successively. If a pair is in decreasing order, its values are swapped; otherwise, the values remain un changed. The technique is called a bubble sort or sinking sort because the smaller values gradualy "bubble" their way to the top and the larger values sink to the bottom.
The Algorithm can be described as follows:
boolean changed = true;
do
{
changed = false;
for (int j=0; j<list.length-1; j++)
if (list [j] > list[j+1])
{
swap list[**in here**j] with list[j+1];
changed = true;
}
}
while (changed);

whenever i try to use this algorithm it says that a ] is required after the swap list (**in here**)
WHY!?!?!?! it is from a text book for christ sake

2.
REVERSE QUESTION:

Write a program that will read 10 integers and display them in reverse order.Im using JBuilder 5 if that is any difference

package reverse2;

/**
*Title:Ex 5.1
*Description: This program lets a user enter 10 integer numbers and then
*diaplays them in reverse order as to what they were entered in
*/
import chapter2.MyInput;

public class reverseorder2

{




/**Main method*/
public static void main(String[] args)
{

int[] integer = new int[10];
//Read in all integers
for (int i=0; i<integer.length; i++)
{
System.out.println("Please enter a new integer number: ");
integer = MyInput.readInt();
}



//Display the results
System.out.print("The integer numbers entered are displayed " +
"in reverse order: ");
for(int i=0; i<integer.length; i++)
{
System.out.print(integer + ", ");
}
}
}



(i have this working accept i cant display them in reverse, they just come out in original order)


3.
Duno if anyone can/will be bothered to help me with this one but what the hell....

Rectangles QUESTION:

Write a class named Rectangle to represent rectangles. The data fields are width, height and color. Use double for width and height, and String for color. Suppose that all rectangles have the same color. Use a static variable for color. You need to provide the accessor moethods for the properties and a method findArea() for computing the area of the rectangle.
the outline of the class is:
public class Rectangle
{
private double width = 1;
private double height = 1;
private static string color = "white";

public Rectangle()
{
}

public Rectangle (double width, double height, String color)
{
}

public double getWidth()
{
}

public void setWidth(double width)
{
}

public double getHeight()
{
}

public void setHeight(double Height)
{
}

public static String getColor()
{
}

public static void setColor(String color)
{
}

public double findArea()
{
}

}

Write a client program to test the class Rectangle. In the client program, create two Rectangle objects. Assign any widths and heights to the two objects. Assign the first object the color red and the second yellow. Display both object properties and find their areas.

Ive done some code but it always says i dont have the main method...n e one wana help me write one ? :)


Eric Hoffheiser

Posts: 2
Nickname: caalis
Registered: May, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 2, 2002 2:37 AM
Reply to this message Reply
I've just started with Java, so I don't have any ideas for #1 or #3. But I was thinking for #2 that you could try the following:

public class Backwards
{
public static void main(String[] args)
{
int[] integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// Output integers in reverse order
System.out.println("The numbers displayed backwards are: ");

for(int i = 9; i < integer.length && i >= 0; i--)
{
System.out.print(integer + " ");
}
}
}

You'll probably have to change a few parts of the code, but that works for me.

dork

Posts: 4
Nickname: eggy
Registered: May, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 3, 2002 8:39 PM
Reply to this message Reply
cheers for ya help...betta than nothgin, that solution i gave me worked too
THANKS.
n e one out there able to help me with the rest of it ???

Evgen

Posts: 14
Nickname: evgen79
Registered: May, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 5, 2002 4:36 AM
Reply to this message Reply
I'm not good speek English, but that code have to work at me.
Java VM get start on main method of class whose name the same as filename.

// filename test.java
class test {
public static void main(String[] args) {
Rectangle r1, r2;
r1 = new Rectangle();
r2 = new Rectangle();
System.out.println("Colors: " + r1.getColor() + " and " + r2.getColor());
}
}

Evgen

Posts: 14
Nickname: evgen79
Registered: May, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 5, 2002 4:59 AM
Reply to this message Reply
for #1. You must replase "swap list[**in here**j] with ist[j+1]"
because swap pertain to java.util.Collections
-----------------------------------------------
public static void swap(List list,
int i,
int j)Swaps the elements at the specified positions in the specified list. (If the specified positions are equal, invoking this method leaves the list unchanged.)

Parameters:
list - The list in which to swap elements.
i - the index of one element to be swapped.
j - the index of the other element to be swapped.

-----------------------------------------------------
you can replase with code like this:
tmp = list[j];
list[j] = list[j+1];
list[j+1] = tmp;

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 5, 2002 5:18 AM
Reply to this message Reply

/* ArraySort.java
* @author: Charles Bell
* @version: May 5, 2002
*/

/** ArraySort
*/
public class ArraySort{

/**
*/
public static void main(String[] args){

ArraySort arraysort = new ArraySort();
/* An array of 10 integers. */
int[] intArray = {7,3,5,2,9,1,10,4,6,8};
System.out.println("Original Array");
for (int i = 0; i < intArray.length;i++){
System.out.println(String.valueOf(intArray[i]));
}
int[] sortedArray = arraysort.forwardSort(intArray);
System.out.println("Sorted Array");
for (int i = 0; i < sortedArray.length;i++){
System.out.println(String.valueOf(sortedArray[i]));
}
int[] reversedArray = arraysort.reverseSort(intArray);
System.out.println("Reversed Array");
for (int i = 0; i < sortedArray.length;i++){
System.out.println(String.valueOf(sortedArray[i]));
}



}

/** Uses Bubble Sort to sort an array of int values
*/
public int[] forwardSort(int[] items){
int[] newArray = items;
for (int i = 1; i < newArray.length;++i){
for (int j = newArray.length - 1; j >= i; --j){
/* Compare adjacent array elements but use > */
if (newArray[j-1] > newArray[j]) {
/* exchange elements */
int temp = newArray[j-1];
newArray[j-1] = newArray[j];
newArray[j] = temp;
}
}
}
return newArray;
}

/** Uses Bubble Sort to sort an array of int values in reverse order.
*/
public int[] reverseSort(int[] items){

int[] newArray = items;
for (int i = 1; i < newArray.length;++i){
for (int j = newArray.length - 1; j >= i; --j){
/* Compare adjacent array elements. */
if (newArray[j-1] < newArray[j]) {
/* exchange elements */
int temp = newArray[j-1];
newArray[j-1] = newArray[j];
newArray[j] = temp;
}
}
}
return newArray;

}

}

dork

Posts: 4
Nickname: eggy
Registered: May, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 7, 2002 11:12 PM
Reply to this message Reply
Ok, well im closer but there is still something mising..
The bubble sort question seems to have smoothened out however....I get a msg saying the java virtual machine could not find the main class
This is my code:
package bubble;

/**
* Title:Ex 5.5
* Description: This program uses a bubble sort to sort numbers
* from highest to lowest and then display them to screen.
*/

public class bubblesort
{
/**Main method*/
public static void main(String[] args)
{
// Initialize the list
double[] myList = {5.0, 4.4, 1.9, 2.9, 3.4, 3.5};


// Show the original list
System.out.println("The list before the bubble sort is: ");
printList(myList);

// Sort the list
selectionSort(myList);

// Print the sorted list
System.out.println();
System.out.println("The list after the bubble sort is: ");
printList(myList);
}

/**The method for displaying the list*/
static void printList(double[] list)
{
for (int i=0; i<list.length; i++)
System.out.print(list + " ");
System.out.println();
}

/**The method to sort the list*/
static void selectionSort(double[] list)
{
double currentMax;
int currentMaxIndex;
double temp;

for (int i=list.length-1; i>=1; i--)
{

// Sort the list
boolean changed = true;
do
{
changed = false;
for (int j=0; j<list.length-1; j++)
if (list[j] > list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;

}
}

while (changed);


}
}
}

Can anyone see errors as to why this aint working??????

Evgen

Posts: 14
Nickname: evgen79
Registered: May, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 8, 2002 3:51 AM
Reply to this message Reply
One class file cann't contain at the same time package and executable code.

Your exemple started when you remove:
"package bubble;"

dork

Posts: 4
Nickname: eggy
Registered: May, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 10, 2002 7:21 PM
Reply to this message Reply
i cant remove package bubble..im using JBuilder to create it and it requires that at the start to run......must be something else, and i cant change from JBuilder cos its for an assingment and its what we use

Evgen

Posts: 14
Nickname: evgen79
Registered: May, 2002

Re: oh the java probrlems......help me !!!! multiple problems Posted: May 11, 2002 6:00 AM
Reply to this message Reply
I installed JBilder on my PC and create next code.


/**
* Title: Answer for Forum
* Description: Bubble Sort
* Copyright: Copyright (c) 2002
* @author Evgen
* @version 1.0
*/

public class bubblesort
{
/**Main method*/
public static void main(String[] args)
{
// Initialize the list
double[] myList = {5.0, 4.4, 1.9, 2.9, 3.4, 3.5};


// Show the original list
System.out.println("The list before the bubble sort is: ");
printList(myList);

// Sort the list
selectionSort(myList);

// Print the sorted list
System.out.println();
System.out.println("The list after the bubble sort is: ");
printList(myList);
}

/**The method for displaying the list*/
static void printList(double[] list)
{
for (int i=0; i<list.length; i++)
System.out.print(list + " ");
System.out.println();
}

/**The method to sort the list*/
static void selectionSort(double[] list)
{
double currentMax;
int currentMaxIndex;
double temp;

for (int i=list.length-1; i>=1; i--)
{

// Sort the list
boolean changed = true;
do
{
changed = false;
for (int j=0; j<list.length-1; j++)
if (list[j] > list[j+1])
{
temp = list[j];
list[j] = list[j+1];
list[j+1] = temp;

}
}

while (changed);
}
}
}


I's worked. I can send to you all this project's files, if you want (my email: evgenk@ukr.net).

Flat View: This topic has 9 replies on 1 page
Topic: Changing the blue-color, os-menubar, with java Previous Topic   Next Topic Topic: FileBrowser in web page

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use