BestJewSinceJC 700 Posting Maven

S.o.S:

Isn't it true that if you define a constructor, Java does not automatically provide the default one? So by defining any constructor, there would be no default constructor, unless it was programmer defined. ?

BestJewSinceJC 700 Posting Maven

If what you're trying to do is width/2, You need to input the width, then have the program divide it by two for you. Sorry if that doesn't answer your question, I'm not quite sure I understand.

BestJewSinceJC 700 Posting Maven

So that we can initialize our program's variables without knowing ahead of time what they should be initialized to. Consider the String class (that Java provides for you). If there wasn't a constructor String s = new String("This is what it'll say!"); then I couldn't make the String say that. As another example, consider two classes (I'm just making these up here)

public class AutomotiveFactory{

public static void main(String[] args){
Car customizedCar = new Car(true, true, false);
}

}

public class Car{
boolean hasABS = false;
boolean hasAirbags = false;
boolean foreignParts = false;

public Car(boolean hasABS, boolean hasAirbags, boolean foreignParts){
this.hasABS = hasABS;
this.hasAirbags = hasAirbags;
this.foreignParts = foreignParts;
}

}

Without these constructors, how would I create my customized car? I would have to call methods one by one to set the car's features to what I wanted, which is more code. And in the case of Java's String class, since it is immutable, you cannot even change the String once you create it so the constructor is not only helpful, but absolutely necessary.

BestJewSinceJC 700 Posting Maven

I don't know about your second question, but the reason it is moving to the second line of the Text Area is because you are appending \n to the text area, which just adds a new line. If you wanted to set the text to something else, use the setText method. If you want to set the position of the cursor, I believe the setCaretPosition method will help you there. http://java.sun.com/j2se/1.4.2/docs/api/javax/swing/text/JTextComponent.html#setCaretPosition(int)

BestJewSinceJC 700 Posting Maven

Not sure if that is a homework question or not, but that is a good question. Here is one example I found on the web, it seems legitimate, and it makes a good point that you would not be able to have the same functionality without the inner class. http://www.sbras.ru/win/docs/java/tutorial/java/javaOO/innerclasses.html

The Java tutorials also make a few good points about inner classes in Swing, go down to "Inner classes and Anonymous inner classes" here http://java.sun.com/docs/books/tutorial/uiswing/events/generalrules.html

Page two of this article I found on JavaWorld is particularly interesting as well. It demonstrates why inner classes are useful for programming GUIs, using the simple example that multiple buttons probably have different functionality (for each button), so separating the logic using inner classes might be ideal.
http://www.javaworld.com/javaworld/javaqa/2000-03/02-qa-innerclass.html?page=1

BestJewSinceJC 700 Posting Maven

Error 1: The DVD class does not have a method called getItemValue, hence the error. You have to write the method.

Error 2: This isn't actually an error. Once you correct the previous error, it will go away, I believe.

BestJewSinceJC 700 Posting Maven

Like the error message says, you're trying to call a method that takes int,int,int but you're passing int,int,int[]. So either re-declare the method as taking an int[] as one of the parameters, or call the existing method correctly.

BestJewSinceJC 700 Posting Maven

It sounds like what you want is just a JLabel followed by a JTextField.

http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html

BestJewSinceJC 700 Posting Maven

You still have a lot of errors.

  1. The "main" method should be declared as public static void main(String[] args), and this declaration should never be changed to something else. The reason is because it is just a standard that main is the method which will run when you start the program, so changing it's usual declaration is confusing.

  2. You said DVD[] DVD = items, which might work, but does not make sense. Delete it, and instead of saying DVD[0] = whatever, say items[0] = whatever.

Change those couple things and then tell me what errors you are getting. And re-post your code using the Java code tags. To do this, just click everything you normally would, but then when it says (CODE) change it to (CODE=Java) (only for the first CODE tag)

BestJewSinceJC 700 Posting Maven

Do a web search - BubbleSort is a very common algorithm. You won't have trouble finding hundreds of Java code examples. Compare them to what you have and you'll see what sections you need to implement. If you're still confused at that point, then come ask.

BestJewSinceJC 700 Posting Maven

Not supposed to give away code, as this doesn't really help you learn, but any explanation I give you will be much more complicated than the code itself is, and might confuse you, so here is how you do it.

public String toString(){
String s = "";
if (power==true){
s = "Power: ON";
} else{
s = "Power: OFF";
}
return s;
}

All that code does is has an if statement that decides if power was true, and if so, sets a String to say "Power = on". Otherwise, the else statement gets executed and it says "Power = off". Also, please consider that the toString() method is supposed to convey the meaning of whatever Object you have. So the toString() method of a Car class might tell you what type of Car it is (Toyota, Honda, etc). So I'd recommend only override the toString method (what you are doing is called overriding, since the Object class already defines a toString method) if what you're doing actually describes the Object.

BestJewSinceJC 700 Posting Maven

Yeah, because when you extend a class, there is an implicit call to super(). What super() does is it calls the default constructor (the one with no arguments) from the super class. You're getting an error because your class Appliance has no default constructor, so either add a default constructor, or use (this is an example) super(true, new ImageIcon());

(the reason for this is because you have to create everything that your superclass, Appliance, has in it. So the compiler automatically calls super(), which calls your default constructor, to do this. But since you don't have a default constructor, you either have to write one, or call your other constructor, which is super(true, new ImageIcon());

Basically put the call to super(parameters for one of your Appliance class's constructors go here) as the first statement in all of the constructors for any class that extends Appliance. Either that or make a default constructor for the Appliance class.

BestJewSinceJC 700 Posting Maven

http://java.sun.com/docs/books/tutorial/networking/sockets/clientServer.html

You would follow the same process as shown there, except create two client sockets instead of one. Then send the same output to both clients whenever it is necessary.

BestJewSinceJC 700 Posting Maven

You can fix it by declaring the total2 variable. You declared total, you need to do the same thing for total2. You also need to read some java tutorials and learn to fix obvious things like this yourself.

double total2 = 0;

BestJewSinceJC 700 Posting Maven

So you have an array of your numbers? I'm assuming they are integers? Then use

int result = array[index] - the mean;

Your first number is array[0], second number is array[1], and so on. If you had an array that was declared as int[] array = new int[3]; your 3 numbers would be stored at array[0], array[1], and array[2].

BestJewSinceJC 700 Posting Maven

Read
http://java.sun.com/docs/books/tutorial/java/IandI/abstract.html

Particularly, where it defines what an abstract method is.

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon). . .

Then compare that definition to how you have coded your method. You'll notice that you cannot declare a method "abstract" while also defining the method body. So either get rid of the "abstract" and the semicolon, or get rid of the brackets and the return picture.

Invalid:

abstract ImageIcon getPicture();
{
	return picture;
}

Valid:

abstract ImageIcon getPicture();

Valid:

ImageIcon getPicture()
{
	return picture;
}
Laidler commented: Helped me fix my abstract method error +1
BestJewSinceJC 700 Posting Maven

for(int i = 0; i < n; i++)
{
double total2 = total/n;
double[] all2 = Math.sqrt(total2);
}

where in the formula does it say to take the average over and over again? You're only supposed to take the average once when you are finished with the other stuff.

1. Compute the mean (average) of all of your values
2. Compute (your number-the mean) squared. Do this for every number in your list.
3. Add the numbers from step 2.
4. Divide the total from step 3 by the number of numbers you had in your original list.
5. Take the square root of the total in step 4. This is your answer.


Step 4 should not be done inside a for loop, otherwise, it will be done over and over again, when the formula requires that it is done only one time.

BestJewSinceJC 700 Posting Maven

You can't instantiate an abstract class or call an abstract method as far as I know. What exactly are you trying to do? Post the code where you're trying to "get the abstract method to work".

BestJewSinceJC 700 Posting Maven

Your formula for standard deviation only works for an entire population. If you're taking the standard deviation of a sample, then you need to divide by (n-1), not by n. (Note: This might not be important to you, depending on what your teacher said.)

In any case, it looks like you are not following either formula properly (you're leaving parts out), read the "basic example" and you will immediately see what parts you are missing. .

http://en.wikipedia.org/wiki/Standard_deviation

BestJewSinceJC 700 Posting Maven

I think he was being sardonic. That might be the wrong word. In any case, he was giving you as much information on the topic as you're going to get, considering the unclearly written, uncommented code that you posted & the code it uses which you didn't post. Not to mention that we aren't here to explain code to people line by line.

BestJewSinceJC 700 Posting Maven

I don't know very much about KeyListeners, sorry. But I'd do whatever is the most clear to the user (as far as what you're asking them to do) first, and whatever involves the least clicking is secondary.

BestJewSinceJC 700 Posting Maven

My project for netbeans shows the following packages under src folder:

calendar
application
database
org

Where org contains the image files that calendar uses. So I don't know what problem you are having exactly. I'm no expert. But I do know netbeans 6.5.1 made the jar file that I wanted it to make when all the packages were under src folder. I didn't write the code for the calendar, so I'm not sure how it loads the image, but it probably uses the method Ezzaral suggested, which is what you should be using also.

BestJewSinceJC 700 Posting Maven

I don't know too much about KeyListeners, but

if ((e.getKeyCode() == KeyEvent.VK_ALT) && (e.getKeyCode() == KeyEvent.VK_ENTER)) {
				txtArea.setText(txtArea.getText());
				txtArea.setText("\n");
			}
			if (e.getKeyCode() == KeyEvent.VK_ENTER) {
				lblDisplay.setText(txtArea.getText());
				txtArea.setText("");
				txtArea.requestFocus();
			}

doesn't look like it could possibly work. If the first of those statements was true, then the second one would always be true as well. . and don't you only want one of those two things to be executed? You might want to read the Java Sun tutorial on KeyListeners. Furthermore, this statement txtArea.setText(txtArea.getText()); does nothing. You just got the text from txtArea, then set the text in txtArea to what it already was. Why? If I were you I'd check to make sure that the code inside those if statements is actually being reached.

BestJewSinceJC 700 Posting Maven

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html


Next time do a little research on your own.

BestJewSinceJC 700 Posting Maven

Yes, use a JTabbedPane. Tutorial here. And you create buttons using the JButton class. If you have to move the buttons around, you might want to look into different Layout Managers, some are better than others as far as giving you control over where things are placed.

BestJewSinceJC 700 Posting Maven

From where you're currently at, there really isn't much need to erase everything. You just need to put an array in the Invent2 class, then add each dvd to the array after you create it.

Invent2{

//declare an array of DVDs here

main method(){
dvd = new DVD(1, "Pulp Fiction", 8, 12.99);
array[0] = dvd;
System.out.println(dvd);

dvd = new DVD(2, "Resevoir Dogs", 9, 12.99);
array[1] = dvd;
System.out.println(dvd);
}

}

The reason I said you didn't need three classes is because the Inventory class isn't really necessary. You can take all the code you were going to put in Inventory and put it in Invent2 instead. Since all the Inventory class does is contain an array that holds your DVDs, it makes more sense to combine this with the other class.

Also, I apologize if I confused you with my multiple posts. I'm kind of tired and at one point I briefly gave you incorrect information, although I don't think you saw this information (as it was only there for a minute or two).

BestJewSinceJC 700 Posting Maven

Every time you set dvd = new DVD(), you are overwriting what you previously put into dvd. Again, consider this example:

DVD[] allMovies = new DVD[10]; -- This statement creates an array big enough to hold 10 DVDs.

DVD movie = new DVD(lionKing); -- This statement creates a new variable of type DVD called "movie". It then puts the DVD lion king into the movie variable. (What it actually does is a bit more complex, but this is what it amounts to)

movie = new DVD(granTorino); -- Now we just put gran torino into the movie variable. So where is lion king now? Nowhere.

BestJewSinceJC 700 Posting Maven

Another student in your class posted on this forum not too long ago. At least I'm assuming that's the case, since you guys basically have the same exact program other than the movie names. Anyway, your problem is that you are overwriting your dvd in the Invent2 class.

edit: Nevermind. That was you. I can't really improve the advice that I gave you originally, but you did not follow it. To do this program, you should only need two classes: the class where you create the DVDs, and the DVD class. I already told you how to create a DVD and put it into the array:

public class DVD{
DVD[] dvds = new DVD[10];

public static void main(String[] args){
DVD dvd = new DVD(whatever);
dvds[0] = dvd;
DVD dvd2 = new DVD(aDifferentDVD);
dvds[1] = dvd2;
}

}
BestJewSinceJC 700 Posting Maven

What does google sidebar have to do with Java programming?

BestJewSinceJC 700 Posting Maven

A member here suggested this tutorial on animation a few days ago: http://www.developer.com/java/article.php/893471

I don't know how helpful it will be, or if it answers your question, just passing it along since it is an animation tutorial suggested by a reliable member here.

BestJewSinceJC 700 Posting Maven

No, I'm saying you should literally go to the folders on your computer, and change the location of the folder with your images in it from there. I'd give you more specific directions, but I'm no expert with this, I just know it works because I did it less than a week ago. After you do this though, you must click run-> clean & build main project.

BestJewSinceJC 700 Posting Maven

You have to write the method using a method body. What's the point of having a method that does nothing, like your current one would, anyway? Either that or declare the class as abstract.

Listen to your compiler.

BestJewSinceJC 700 Posting Maven

Objects are just representations of things and their data. Each class in Java represents an Object. So, for example, if you made a class called Car

public class Car{
String steeringWheel;
String brake;
int horsepower;
int miles_per_gallon;

public Car(String typeOfWheel, String brakeType, int power, int mpg){
    steeringWheel = typeOfWheel;
    brake = brakeType;
    horsepower = power;
    miles_per_gallon = mpg;
}

public void addHorsepower(int hpAdded){
horsePower+=hpAdded;
}

}

The method you see up there is a constructor. Constructors are always declared as public classname (in this case Car). When you invoke a constructor, you do so using new so you'd say Car myCar = new Car(wheel, brake, horsepw, milespergallon); And you could also declare methods in Car that allow you to edit or return Car's data. For example, the addHorsepower method gives your car more horsepower.

BestJewSinceJC 700 Posting Maven

Why would you give him the code. He will not learn anything from that.

BestJewSinceJC 700 Posting Maven

You could also simply go to the directory where your netbeans projects are, and drop any files that your program needs in the right place. Then netbeans will automatically include them in your jar file and your program will find them correctly.

BestJewSinceJC 700 Posting Maven

No, that does not work. What you need to do is each time you generate a random number, check every index of the array (using a for loop) to see if that random number has been generated before. If it has, generate a random number and check again. If it hasn't, put it into the array. James Cherill showed you how to use a for loop and put numbers into an array. Here is pseudocode for how you could go about filling an array with different random numbers.

int[] array = new int[10];
int number_filled = 0;
while(number_filled != array.length){
//Generate a random number
//using a for loop, check if the number has been used before
//if it has been used before, do nothing
//if it has not been used before, put it in array[number_filled] and increment number_filled by one. (Add one to it)
}

And you aren't going to get many different solutions (other than the one JavaAddict has given, and that I have basically repeated) because there aren't any. There might be methods in Java that do this for you, but any piece of code that is written to generate different random numbers will have to check the array to see if the number was already generated, or else somehow tell the number generator not to ever generate repeats.

BestJewSinceJC 700 Posting Maven

Did you try:

if (turn > 0){
return CCW;
}else if (turn < 0){
return CW;
}

return COLLINEAR;


Worth a try, and does the same thing as you were trying to do with your code above.

BestJewSinceJC 700 Posting Maven

Thanks for the response. I'm a bit late replying, appreciate if you'd check this logic. (And yes, what I was trying to do is what you said: change the combo box entries based on what's in some list - so when the list changes, so do the combo box entries).

So from my understanding, the getTableCellEditorComponent method is what gets called when the user clicks on a cell of the JTable, and inside that method, you are updating the JComboBox by removing everything from it and then adding all the elements from the Vegetarian column to it?

BestJewSinceJC 700 Posting Maven

Yeah, because you are writing code, but it is not inside of any particular method. If you look at where your brackets are located, you will see that those print statements are inside of the class. . but not inside of any method. Put those System.out.printlns and all of the other code that you threw into the class into main or into some other method. For example,

public class BestJewSinceJCExample{
System.out.println("Print me");
   public static void main(String[] args){
   int a = 0;
   System.out.println("a's value is " + a);
   }
}

The code above makes no sense because the main method is executed when the program is run. At what point is the print statement that says "print me" supposed to happen? If you wanted "print me" to be the first thing the program says, then you would have to do the following:

public class BestJewSinceJCExample{
   public static void main(String[] args){
   System.out.println("Print me");
   int a = 0;
   System.out.println("a's value is " + a);
   }
}

Along the same lines, you cannot have random statements and loops (while, for, etc) in the class body. You should only be declaring variables and initializing them in the class body. Anyway, I don't mind helping, but if you are a beginner to Java, you should read some of the tutorials stickied at the top of this forum.

BestJewSinceJC 700 Posting Maven

For any class that implements Map, you can use Map's get method() to get the value, then assuming it is a float, add it to the other float. http://java.sun.com/javase/6/docs/api/java/util/Map.html

eh?

http://www.daniweb.com/forums/thread189520.html

The other thing you should do is not double post threads.

BestJewSinceJC 700 Posting Maven

Well explain what, specifically, you need help with. i.e. X method ]doesn't work, and it is supposed to do Y. Or I need help with reading in the file. etc.

BestJewSinceJC 700 Posting Maven

You are in the Java forum. I imagine you'd get a better answer in the correct forum.

BestJewSinceJC 700 Posting Maven

Errors:

1. "string" is not a type of Object. It is "String". Change that declaration of public static void main string[] args to public static void main String[] args.
2. You declared a, b, c, and d twice. You can only declare a variable once. Saying 'int whatever' declares a variable called whatever.
3. You can't use System.out.println() + System.out.println(). The reason you can say System.out.println("String" + "other string"); is because that operator can be used to concatenate two Strings. So "String" + "other string" = "Stringother string". This simply isn't allowed for what you did.


etc etc

BestJewSinceJC 700 Posting Maven

Yeah sorry, I probably should have told you to look at the Scanner javadoc, since I could not remember what the exact constructor was. I really meant to point you in the direction of Scanner though, not give you exact code, although in this case I should have looked it up or mentioned that I wasn't sure. Anyway, like javaAddict said, you can use a File object to do it properly. That and other ways are shown here

BestJewSinceJC 700 Posting Maven

Yes, it is possible. It is actually pretty easy. Before you get into it though, I'd recommend that if you have different sized panels, you do not use cardlayout, as I tried this, and it is very difficult to resize them.

http://java.sun.com/docs/books/tutorial/uiswing/layout/card.html

Use this link to learn how to do it. You'll need to use the show() method that you see there to switch panels based on what is in the combobox. The code to do that will be in the combobox's actionPerformed method. Ask me if you have any other questions after you read the tutorial. It is a fairly simple tutorial.

BestJewSinceJC 700 Posting Maven

The problem is

1. You didn't post your properly formatted code in code tags so it is hard to read your code.
2. You never explained what you want to be output. What is the content that should be in the file? Is it an array of ints, called myInts? Is it a String describing the type of cheeses called stringCheese? What is it and where in your code is it (if you use proper code tags, this is a line number)?

BestJewSinceJC 700 Posting Maven

Well, you declared a double[] doubleArray. So you have to change the method header of the total_average method to double[], not to double, since you want to pass in an array of doubles. Then in your main method just say

total_average(doubleArray);

BestJewSinceJC 700 Posting Maven

Personally I wouldn't listen to anyone who pastes code at you and doesn't explain it at all, but that's just me. I don't understand what you mean by "median of three", care to explain?

BestJewSinceJC 700 Posting Maven

http://java.sun.com/docs/books/tutorial/uiswing/components/textfield.html

^ Read that. You have no mechanism to let the user enter a value into the text field, then to get it. Your getLatEntered method looks like it gets what is in the text field properly, but you have no way to wait for the user to enter text in it first. You can't do the second step without the first -- you need to implement the ActionListener class, and add an action listener to your text field. The examples you will find in the link I gave you show you how to do both of these things and more.

BestJewSinceJC 700 Posting Maven

I don't know what the name of your class is, but I do think I know why you are getting static reference errors.

You are in a static method, main (look at the declaration of the method - it says static), and you are probably trying to put things into an array you declared in your class body (so inside of your class, but not within any method) that is not declared as static. By default, anything that is not declared as static is automatically declared by the compiler as public, and therefore is not static. Consider the purpose of static-ness (static methods or static variables) to begin with. Static methods and variables have one copy for the entire class, whereas every time you create a new Object, that Object gets a new copy of each of the non-static class variables. So with that in mind, consider that your static main method cannot do anything with non-static variables, since you have to make an Object for them to even exist! And with that in mind, read this for any more information, http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

Also, as far as your code in general: Your input file has two things per line, an X and a Y. Using a 2-D array is not a bad idea at all. You should use the Scanner class's nextInt() method to read in each X and Y. The way the nextInt() method works, it would just give you whatever the nextInt was in the …