BestJewSinceJC 700 Posting Maven

After creating the c object of Class2, is there anyways i can know which class instatiated it

Yes, because the method that instantiates the 'c' Object is inside of Class1, therefore you already know that it was instantiated inside of Class1. Inside of method1(), the word "this" refers to the Object that called method1().

BestJewSinceJC 700 Posting Maven

bondgirl, if you attach all of your current code as one post, I'll take a look at it for you.

BestJewSinceJC 700 Posting Maven

Put it at the end of your method that calculates the number of vowels, but not inside of a loop. Don't you know how loops work?

BestJewSinceJC 700 Posting Maven

bondgirl: For the prepositions you could add them all to an ArrayList and then use the contains() method to see if one was there. For example:

ArrayList<String> list = new ArrayList<String>();
list.add("on");

if (list.contains("beneath")){
System.out.println("The preposition is in the list! Do something!");
}
BestJewSinceJC 700 Posting Maven

You should only print out the number of vowels outside of the loops. Print out the number of vowels right before the end of the method.

BestJewSinceJC 700 Posting Maven

Follow Moutanna's suggestion to use Comparators. I misread your post the first time around; I thought you wanted to sort based on "1" then sort based on "2" if there was a tie, etc. But what you actually want to do is sort three different ways, so follow Moutanna's suggestion and use three different Comparators.

BestJewSinceJC 700 Posting Maven

On line 44 you said line = br.readLine(); but that shouldn't be inside of that if statement. You want to read in the next line when you're done looking at the previous line; so you should call br.readLine() inside of your while loop but after the for loop exits. You should also use print statements to debug ..

BestJewSinceJC 700 Posting Maven

Because you never called the reset method from within main.

public class ArrayReference {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int[] array = {1, 2, 3, 4, 5};
		setArray(array);
		for (Integer i: array) System.out.println(i);
	}
	
	public static void setArray(int[] array){
		array[0] = 6;
		array[1] = 7;
		array[2] = 8;
		array[3] = 9;
		array[4] = 10;
		
		array = new int[5];
		array[0] = 1;
		array[1] = 2;
		array[2] = 3;
		array[3] = 4;
		array[4] = 5;		
	}

}

What you have to consider is what your 'array' variable actually points to. Changing the contents of your 'array' variable from another method will change the contents of 'array' back in main(). But changing the reference of the 'array' variable from another method will not change the reference back in main. Hence lines 20-25 in my above code have no affect on 'array' back in main because on line 20 what 'array' referred to changed, but only for the method I'm currently in.

BestJewSinceJC 700 Posting Maven

It prints twice because you put your print statement inside of a for loop that has two iterations.

BestJewSinceJC 700 Posting Maven
BestJewSinceJC 700 Posting Maven

am counting words that i have in a "input.txt" file on my computer...

What do you consider to be a "word"? Does it have to be in a particular dictionary? Or is your file guaranteed to only contain dictionary words separated by spaces? You haven't explained your problem sufficiently.

BestJewSinceJC 700 Posting Maven

How do you plan to count the number of words? Do you mean, like, words that would appear in the dictionary? If that is the case then you are going to need to do a dictionary look up of each possible word from your input file. If you count a word as anything preceded and followed by spaces, then that's a different story.

BestJewSinceJC 700 Posting Maven

No, there isn't. Just follow the directions, it told you what to do. And AFAIK you cannot add the same Swing component to multiple containers. It can only be added somewhere once.

BestJewSinceJC 700 Posting Maven
//In a file called First.java
public class First{
... 
}
//File called Second.java
public class Second{
..
main(){
First f = new First();
}

}

Put the classes in a package together... then you can create a new Object and use the class's methods.

pinsickle commented: very helpful +1
BestJewSinceJC 700 Posting Maven

I agree with the previous posters. Don't learn the API, learn to use the API JavaDoc and Google to find what you need when you need it. As you build up experience you'll get to know the most common APIs, and you'll be able to copy/paste from things you have already written and debugged for many more (so never throw away any working code you've written).

Somewhat agreed on both points. Unless you are exceptionally good at organization, keeping every code you write is going to take longer as far as reusability than rewriting certain things will. But in general, holding on to modular code can't hurt. And unless you know a good bit of the API, it will be substantially harder to understand the other parts.

BestJewSinceJC 700 Posting Maven

I'm sure this is not the right place to post this, but I thought I'd shout out to everyone to see if there's anyone can help. I have GOT to get off the internet more and I remember vaguely in the back of my mind from a million years ago that there is a kind of "alarm" you can install to restrict your internet use. Does anyone know what I'm talking about? The idea is that I would have to do other work during the day and not be constantly distracted by email and so forth. I can't find it anywhere. On Google all I get is stuff to keep the kids from looking at porn. Help! Would really appreciate it.

It's called self control, you can find it in aisle three

BestJewSinceJC 700 Posting Maven

The ONLY way is through extensive experience using them.
I doubt anyone knows every method and field in every class of even the core API and knows its correct usage.
And if such a person exists no doubt that knowledge is purely academic and he has no clue as to how to actually apply it to real world situations.

100% agreed. Make sure you learn concepts such as inheritance, polymorphism, interfaces, etc first - otherwise many of the methods in the Java API will not make much sense. I recommend either taking a course on Java or getting a decent beginner's book.

BestJewSinceJC 700 Posting Maven

You could also create your own class that mimics the Scanner class's functionality; you'd basically include a constructor that takes a File Object, and you'd implement your own next() methods, etc... in those methods you could call the Scanner methods and also implement your own custom functionality (calling the AudioClip)

BestJewSinceJC 700 Posting Maven

Calling a String 'charArray' is pretty confusing to say the least. Anyway, here is an example to clear things up. This puts the first rows of chars into the variable temp.

char[][] chars = new char[10][10];
		
		//Put some stuff in the 2d array
		for (int i = 0; i < 10; i++)
			for (int j = 0; j < 10; j++)
				chars[i][j] = (char) (i*j);
		
		// 0 is the row, i is the column
		String temp = "";
		for (int i = 0; i < 10; i++) temp+=chars[0][i];
BestJewSinceJC 700 Posting Maven

Moutanna:

My example is legal Java code. It's more robust than your example because my example makes use of Java's autoboxing features. Number being an abstract class doesn't matter because I never tried to instantiate 'Number'. The example instantiates some class in Number's inheritance hierarchy.

Number n = -2.9;
System.out.println(n.getClass());
char[] chars = (n + "").toCharArray();
for (Character c: chars) System.out.print(c);

Sammich: Mark solved threads as solved.

BestJewSinceJC 700 Posting Maven

probably the same as a binary search tree

just a guess though, since I've never heard of a decision tree before and it looks like a binary search tree to me

BestJewSinceJC 700 Posting Maven

Variable scope. Read about it. The "args" array only exists within the main method, since that is where it was declared (in the method header). If you want to use it from within your ReadFile method, then you had better declare ReadFile as taking a String[] as a parameter. But more likely what you actually want to do is this:

public static void main(String[] args)throws IOException
	{
	
        ....

	int sizeN = ReadFile(Onum, Snum, args[0]);
	
       .....
	
	}
 	
	public static int ReadFile( double[] Onum, double[] Snum, String fileName)throws IOException
	{
		Scanner fileScan = new Scanner(new File(fileName));
		....
	}
BestJewSinceJC 700 Posting Maven

Just so you know, the switch statement you have might need 'break' statements after each 'case' statement is over. Otherwise it will execute the code in the other case statements as well.

BestJewSinceJC 700 Posting Maven
number something = 1234;
String s = something + "";
char[] chars = s.toCharArray();

I'm not sure if that will work; you may want to try it though.

BestJewSinceJC 700 Posting Maven

No problem - mark solved threads as solved

BestJewSinceJC 700 Posting Maven

You're trying to call the static method as if it is a constructor.

http://leepoint.net/notes-java/flow/methods/50static-methods.html

Read the section on calling static methods.

BestJewSinceJC 700 Posting Maven
Calendar myCal = Calendar.getInstance();
System.out.println(myCal.getActualMaximum(Calendar.DATE));

That works, I just tested it on my machine. And according to the Javadoc, DATE is a synonym for DAY_OF_MONTH so day of month should work as well. Although it won't mess anything up, you shouldn't use your instance variable (calendar) to refer to a static field (DAY_OF_MONTH). And of course, that method will return the maximum value for the current month (April) so if you are concerned with a different month you'll have to set the Calendar explicitly first.

BestJewSinceJC 700 Posting Maven

Becoming a good programmer isn't about being a genius. Hard work and perseverance are more important than intelligence. A high level of intelligence makes learning easier, but a lower level usually does not make learning impossible. I find it likely that you're being too rough on yourself and your confidence, not your potential, is really to blame. Stop crying and get to work.

BestJewSinceJC 700 Posting Maven

You declared the variable courses from within the method collectCourseInformation. Therefore it doesn't exist outside of that method. Read about variable scope in Java.

BestJewSinceJC 700 Posting Maven

For amount of time worked, just subtract end hour - start hour and end minute - start minute. You'll need an extra two arrays to record the results. You'll also need to account for the possibility that end minute is < start minute.

Also the program should output the average amount of time worked. Be sure to use good programming style. Modularization & arrays are required.

Someone should tell your teacher that "good programming style" is impossible without using Objects for this problem. Using Objects for this problem is essential to code readability, maintainability, ease of programming, and modularity. I actually think it's funny that your teacher used the word modularity but apparently he/she doesn't know what it means.

For example: Resources about modularity in Object Oriented programming:
http://java.sun.com/docs/books/tutorial/java/concepts/object.html

BestJewSinceJC 700 Posting Maven

Reflection is used for a class to examine its own (or another class's) behavior at runtime. For example, while the program was running, you could determine what methods the class has and invoke those methods.

You can find the source code file by figuring out the class name and locating the file with the name "classname.java". You can probably use getResource to find the .java file, although it can be complicated. If, for some reason, you do not know the class name (which you probably should know in advance) you can use the printClassName() example found at the top of the Class documentation.

If you know the String name of the class (like MyClass.java and you have the String "MyClass") then you can use the Class method "forName" to get a Class Object for it. Again, look at the documentation link I sent you on that.

To get the source code the easiest way is what I mentioned above - figure out the name of the class, which you probably know in advance, append ".java" to it using String concatenation, then open the .java file for reading, read in the contents, and display them somehow.

Salem commented: Nice +20
BestJewSinceJC 700 Posting Maven
for (int x = 0; x < intValue.length; ++x)

Should probably be

for (int x = 0; x < intValue.length; x++)

The same for your other for loop. That is more than likely the reason you're getting the exception.

BestJewSinceJC 700 Posting Maven

Like jwenting said, there are many concepts in OOP and Java that are more important to learn than what you're trying to do. If you're insistent, here's how you could at least learn an important concept along the way. If you make a class that extends JPanel (inheritance), you could add your own setImage method. Then when you used your class in the future, you could do things like JPanel.setImage("imagePath.jpg").

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

BestJewSinceJC 700 Posting Maven

Doable, but a bad idea unless you're trying to create some sort of library of common functions that you use. If that is what you're going for create some static methods in a different class where you can call those methods from your paint method. Pass in your Graphics object or whatever objects you need to pass around.

If you're only doing this to remove bloat, don't do it though. Removing bloat isn't a good reason to introduce overhead and complicate the code more.

BestJewSinceJC 700 Posting Maven

I don't know - what are you missing? Did you do any debugging? You listed some steps:

1. input 10 integer numbers,
2. display the numbers,
3. modify a specific number I have already entered,
4. display the updated numbers again.

Which of those steps isn't working?

BestJewSinceJC 700 Posting Maven

We don't need to, we assume sarcasm is your MO

:p

BestJewSinceJC 700 Posting Maven

I'm torn between liking you and hating you. Hmpf!

BestJewSinceJC 700 Posting Maven

Most of your 2000 posts do not help with programming, they are mainly giving a link to the API, discussing TV or making criticisms. Makes one wonder do you actually know much about programming or are you just one of those people who thinks forum post counts are very important to them as you seem to "mock" my 18 posts.

This isn't about my level of programming skill or yours. The important thing is learning how to ask questions well and showing effort, not expecting people to do your work for you.

BestJewSinceJC 700 Posting Maven

... Yes. And if that answers your question, mark this thread solved.

BestJewSinceJC 700 Posting Maven

Your calculations appear correct, your method of displaying them appears incorrect.

JOptionPane.showMessageDialog(null, input + total);

Saying input + total concatenates input and total into a String which then gets displayed to the user. So for example if the user inputs "10" and the total money should be .70 cents, your code might display "100.70" to the user.

BestJewSinceJC 700 Posting Maven

You see the code button at the top of the reply box? Click it, then insert your code in the middle of it.

BestJewSinceJC 700 Posting Maven

Hints:

Use a static variable to define the number of employees. Then create an array based on that. Example

private static int NR_EMPLOYEES = 10;
Employee[] employees = new Employee[NR_EMPLOYEES];

Obviously this Employee[] means that you will need a class called Employee that holds all of the Employee's information. So the first thing you should do is to create the Employee class, put in all of the variables you need, then provide get() and set() methods for each of those variables.

After you do the above, you can then make methods like calculateHoursWorked, etc. Your also going to need another class, the main class, where you prompt for employees and such. So in summary:

public class Employee{
//Your variables go here - stuff like name, hour arrived at work, 
//hour the employee left work, etc.

..


//Your methods such as get and set methods and other methods
//such as calculateHoursWorked() go here
}

Then your main driver class..

public class EmployeeDriver{
//Array of employees goes here, NR_EMPLOYEES goes here.

public static void main(String[] args){

for (int i =0 ; i < NR_EMPLOYEES; i++){
//call your prompt for employee method.
}

}

public void promptForEmployee(){
//This method should prompt the user for ONE employee
//then create a new Employee Object and put it in the array.
}
}

That should get you started. If you don't understand something ask but don't be surprised if I just give you a link to some resource or another. This …

BestJewSinceJC 700 Posting Maven

Honestly posts like the one above just make you look stupid. I have almost 2000 posts in this forum, almost all of them trying to help people. You have 18 posts, probably all of them asking for help. There's nothing wrong with asking for help, but you are not entitled to receive it. The main reason I even posted in this thread was because your attitude indicated that you thought you were entitled, ("no good having a forum if there's no one to help.."). The people who help here are volunteers. Obviously your posts didn't provide enough info to warrant us going out of our way to help you like I pointed out before.

BestJewSinceJC 700 Posting Maven

It's because you posted a ton of code and it doesn't look like you tried very hard at solving this yourself, so none of us really care enough to help you. Read about ArrayList on google and you'll see what Masijade means. Read about ActionListener and you'll figure out how to implement it.

BestJewSinceJC 700 Posting Maven

Define a class (I'll call it YourThread) that extends Runnable and takes one argument to its constructor: the name of the file you want it to run on. Then in another class, the main/driver class, you can use a for loop and make a new instance of YourThread for each file you have.

http://www.go4expert.com/forums/showthread.php?t=4202

BestJewSinceJC 700 Posting Maven

Your interface isn't very intuitive. I just ran it to try to help you out but I really have no idea what I'm supposed to be doing and what isn't working. It already said "You are incorrect" before I'd even answered, and it asked me to "name the test" but after I named it, it put the name of the test into the answer column....

Also, I'm having trouble helping you because I don't understand your variables. Why is your alphabet array 20 long? Shouldn't it be 26 long? And what is the setAlphabet method supposed to be doing? It looks like it is supposed to be figuring out the program's next guess (i.e. program asks user "what's after u?") but I can't tell. I'm not sure why you need number[] either. Basically, use some comments and document what is going on, and make clear method names.

BestJewSinceJC 700 Posting Maven

Sockets do not implement protocols. Just as you connect to a socket, a socket connects to the protocol you requested. Otherwise, it would be akin to saying that the phone jack on your wall implements the phone system.

Agreed; the protocol is already implemented but the socket makes use of that protocol. If that was a response to my post, I never said that sockets implement TCP, I said that the services necessary for TCP have already been implemented and are made available for us by sockets. @ OP, if you're still around, check this out.

http://beej.us/guide/bgnet/

BestJewSinceJC 700 Posting Maven

There is no such thing as an "int... ints" construct. Besides which, your friends coding is poor. ArrayList shouldn't be used as a raw type like that, specify the type (an example:)

ArrayList<Integer> list = new ArrayList<Integer>();
BestJewSinceJC 700 Posting Maven

Greetings,

1) Do sockets implement TCP? When I uses a socket to send data to another computer, are the computers using TCP or is it my responsibility to implement it (ie write the program such that the methods of the sockets mimic the rules dictated by TCP) I am confused.

Jack

Read this and look at the section on "Socket Types". To answer your question, yes, some types of sockets use TCP. But just because something is a Socket does not mean it has to use TCP. Being a Socket really just means it is a bi-directional connection where you can send data from one machine to another. The order of the data's arrival is not guaranteed due to network properties and that's where TCP comes in. From my understanding, it basically takes your data and forms it into packets where it includes information that it can later use to make sure your data arrives in the correct order to whatever process you sent it to. The services necessary for TCP have already been implemented (and are probably made available by the OS). I'm not positive whether it is actually the OS that makes them available, but it doesn't really matter IMO. The main point to know is that TCP is a protocol that BOTH endpoints must somehow implement in order to guarantee a correct order delivery of data to the receiving process/application. And that someone before you has already implemented it somewhere.

BestJewSinceJC 700 Posting Maven

Paste us the error message.