JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I believe that each and every instance has a minimum 8 byte fixed overhead and is aligned on an 8 byte boundary in the current Oracle JVMs (obviously this can and will vary with versions of the JVM). On that basis I would expect 16 bytes per instance.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are asking me what the requirements for this exercise are? I have no idea, but presumably your teacher gave you them. It would help if you shared them with us.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Also note that Integer is a class, so you are creating 1000 new instances of that class. That's not the same as 1000 ints (primitives) that would be just 4 bytes each

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

YOu didn't post the requirements, but this line:
System.out.println("The number of integers is " + num);
implies that num should be a count of how many integers were entered, but what you are printing is just the latest integer. You don't count them.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Changing the println on line 4 to a print will eliminate the newline that your automated test dislikes.

I have no idea how you are expected to get the right result if you haven't been told the input value that was used.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That is totally ridiculous. The "error" seems to be that your output is on 2 lines instead of one (although two lines is better IMHO). What is the celsius temp that was input? - the output doesn't show it.

ps: You must read kal_crazy's post. It's essential.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Please post the exact complete error message(s).

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In Java when you enter a floating point literal like 9.0 the compiler creates a double value, not a float. That's where ypur doubles are coming from. So when you then assign that value to a float you have a possible loss of precision - hence the warnings. Either make your literals floats (eg 9.0f) or, better, make your variables doubles.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I want, I want...
There are lots of things we all want, but life isn't like that.

DaniWeb Member Rules (which you agreed to when you signed up) include:
"Do provide evidence of having done some work yourself if posting questions from school or work assignments"
http://www.daniweb.com/community/rules

ddanbe commented: I want, I want... Nice. :) +15
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Did you read my previous post? I explained why you get precision-related errors, and how to fix them, but you seem to have ignored it.

<M/> commented: sorry +9
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I also "just read the code", and it's a shambles, full of obvious mistakes. No need to compile it.
Old Apache took the time to point out some of the more obvious ones. Maybe confusing an instance variable with a class name was "just a typo", but how was anyone to know what you really meant - we can only comment on the code you post.
The second blunder - passing values of 65,5,5 to a method that only does anything with values <4 - was the subject of OA's second post, which it seems you still don't understand.
You're lucky he didn't get into the overall logic of your method. Despite its meaningless name, I cannot believe that it is supposed to assign a value to the same array element 16 times and print out that same value 16 times.
Or... you pass a char value of 'A' (decimal 65) to use as an array index for an array of size 4. My best guess is that you got the order of the parameters confused - which was also discussed by O.A. - or maybe you are mistaken in your understanding about using chars for array indexes?
And as for why you create an instance of a class that has no instance members, then use the constructor to initialise a static array to the same values that Java has already initialised it to...

You asked for help with code that needs a great deal …

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have to import java.util.Scanner; to use the class name "Scanner" without fully-qualifying the name.

The constant 9.0 is a double value (that's how the language is defined), so the calculation is done in double, with a theoretical possible loss of precision when you assign that result to a float. If this bothers you either make all your variables doubles, or change your constants to float (eg 9.0f).

Apart from that there's nothing wrong with your code.

<M/> commented: thanks :D +9
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That code (with a Scanner declaration for kbinput, aqnd placed inside a method) compiles without any errors or warning in NetBeans, and gives the right answers when executed. Maybe the problem is not in that code but caused by something in the rest of the class?

<M/> commented: :D +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A. Develop a working application that implements an interface.

So far you have no interface in your code. If only to meet the requirements of the exercise you could make Lootable an interface - it already sounds more like an interface (it's an adjective) not a class (its's not a noun).

If you look at primary sources, like Oracle's own documentation, you will see that it refers to "implementing" an interface by implementing its methods. Your tutor's use of "overriding" in point 2 seems to be a mistake; I would assume they mean "implement" - but as always if your tutor is wrong it's safer to stick with their version at least until yur paper has been marked!

StephenopolousINC commented: Thank you you are correct I did not have and interface. I have rewritten the code to use and interface just to check and see if the various containers are lootable. I could still use some help with adding some overriding to my app. I will post the code be +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Here's a screenshot from my NetBeans projects browser showing an images folder for one of my projects, placed alongide the java source code packages...

Just add your resouces folder like that, using the Project browser, then leave it to NetBeans to do the rest.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Java binds instance methods at run time, so when it executes objecty.playMusic(); it looks at the actual object that refers to, and uses its methods as appropriate. The type of the reference vaiable is not relevant at run time, it's just used at compile time to ensure that any object referred to by that variabke will have a playMusic() method.

(static methods, on the other hand, are bound at compile time)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The API requires you to pass a Vector of Vectors. If you do that, it runs OK. If you pass a null it fails. Why is this surprising?
Remember AllSubjects is a reference. It's supposed to point to a Vector of Vectors. If you init it to null then it doesn't point to Vector of Vectors, so you have violated the conditions of the API, and the code doesn't work. It really is as simple as that!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's OK so far... except do you really want to stop taking input when the sum reaches 125?

ps System.out.print is just like System.out.println except it doesn't go to a new line at the end, so it would look more like the sample input in your post

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There you go! The Text class has a variable called "text" that presumably holds the "humpty" or "dumpty" or whatever. That's what you can return from toString, so one of those values is what you will see when you print a Text object

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Helpmeplease123:
This is not a "we do your homework" service. Try to solve your problems yourself before asking for help. For example, 30 seconds with Google will give you all kinds of information and examples of using ArrayLists. Before asking for any more help, show that you have made some effort yourself first.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

How about writing your text as an HTML file. That's really easy.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I use that code in many programs without problems. Can you show the actual contents of your jar file (use any zip utility to open it)?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Exactly. It's not overriding because the superclass does not have a method called PaintComponent. Here's a hint: Java names are case sensitive.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is why the @Overrride annotation was invented!
If you prefix that annotation to your PaintComponent method you will discover that it doesn't override anything. Here's a hint: Java names are case sensitive.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Hi Pob, welcome back!
You can use something along the lines of

BufferedImage image = 
     ImageIO.read(getClass().getResource("/images/xxx.png"));
myMainWindow.setIconImage(image);

wher /images is a folder in the same jar that the current class was loaded from

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

After a quick look I can't see an "index" variable defined that's in scope at line 52 (the one on line 29 is local to the getGrossSales method only).
(And you seem to be missing a loop in calcComissions anyway)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Maybe you can do it by coding your own ListModel then you can maintain the set of values sorted in alpha order. There are a number of list model classes that you could subclass (eg AbstractListModel) to handle the housekeeping

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is one of those irrelevant technical questions that interviewers like to ask, but don't ever matter in practice:

String s1 = "asdf";
String s2 = "asdf";

the compiler is smart enough to create just one String object.

String s1 = "asdf";
String s2 = new String("asdf");

the compiler will create two String objects.

Bercause Strings are immutable there is no reason I know of to use the second version.

kenadams commented: Okayyy, I think your answer just removed my mist. So in the normal case, we would most likely use String s1 ="asdf", right? +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Everything to do with Swing (eg painting the screen, running actionPerformed methods) happens on a single thread - the "Event Dispatch Thread", or "EDT", or "Swing thread".
That means that once your actionPerformed method starts NOTHING else will happen in Swing, including no screen updates, until your method finishes. You can update stuff and loop and sleep as much as you like, but none of that will affect what's on the screen until your method has terminated.
If you want to do stuff after a period of time, here is the right way:
In your actionPerformed start a javax.swing.Timer, and return. When the timer fires you can update whatever needs updating and return. While the timer is running Swing will be free to update the screen.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's a very good question! You set the size of an array when you create it, and it can never be changed. So your main options are:
1. Ask the user how many numbers they want to enter, and use that to create an array of the right size
2. Guess a reasonably large size, but then if they enter too many numbers you create a larger array, copy the existing entries into it, then use the new array and let the old one be garbage collected.
3. Use an ArrayList instead of an array - internally it implements option 2 for you. Google for examples and tutorials if necessary.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The key to creating frames in specified formats is to chose the right layout manager. This tutorial is a good start.

http://docs.oracle.com/javase/tutorial/uiswing/layout/visual.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you need variables to be shared between different methods, then declare them in the class but outside any one method. Variables declared at the class level can be used in any method

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I was playing around with this problem for fun today, and it's interesting just how simple it can be. Here, just for your amusement is a 3 line method that, given a URL of a web page (supplied as a String), returns the HTML for that page, also as a single String...

    // reads and returns the HTML text from the specified URL 
    // eg "http://docs.oracle.com/javase/7/docs"

    static String readHTMLfrom(String urlString) throws IOException {
        URLConnection urlConnection = new URL(urlString).openConnection();
        InputStream in = urlConnection.getInputStream();
        return new Scanner(in,"UTF-8").useDelimiter("\\A").next();
    }

The use of a Scanner to copy a whole input stream to a String is a favourite little shortcut - the secret is in the definition of the "\A" Pattern used as the delimiter ;)

After that it's a metter of parsing the HTML for the data you need - maybe you can do this in an ad-hoc "indexOf" kind of way, or maybe you will need to use a full XML parser. It all depends on the exact design of the web page.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
lilazn commented: Thanks this was really useful. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just trying things at random is a v e r y slow way to write a correct program. I still recommend that you do some examples on paper and get the logic 100% clear in your own head before going anywhere near a computer.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In my opinion that would be hard to impossible in a Java application - it would need to interact with the system at quite a low level and be immune from the user stopping it. There may well be other ways to do it, eg via Group Policies in Windows - try posting the question in the Microsoft Windows or Linux and Unix forums as approprate.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK.
Line 1: incorrect syntax
Line 2: what's that if test for? Certainly violates the spec
Line 3: what's the point of an unused variable?
Line 4: assuming that the single delete is working perfectly, this line is OK.

Misc: You didn't verify index/count as required.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's a common requirement to associate a value or values with a button, and the previous post shows some of the techniques that people often use to do that.

But there's a better way that most people don't know about....
All JComponents have a "client property" Map where you can store and retrieve anything you like as key/value pairs. So, for example, if you want to associate chars with buttons you can set their client properties like

buttonThree.putClientProperty("character", '3');

then you can retrieve that with something like

char c = (char) ((JButton) event.getSource()).getClientProperty("character");
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Masybe you could use a boolean array or list, same size as the secret word, that shows which letters have been found. As the user guesses a letter correctly you set the corresponding entry to true. You can then use that array to control the printing of the secret word - only showing letters where the corresponding boolean is true.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No. Once you have used an interator you have to get a new one for the next loop. It's just one tiny line of code to copy/paste, so it's not a problem.

You could forget iterators, and use an enhanced for loop like

for (Publisher p : publisherList) { // for each Publisher in publisherList
    System.out.println(p);
}
aslam.junaid786 commented: Thanks Sir. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

does this part update it?

That updates the existing array to overwrite the "deleted" entry. Eg if the array was {2,4,6,8} and you delete index 1 the array will now be {2,6,8,8} - ie the correct entry has been deleted, but you now have a "spare" entry at the end. There's also a problem with the currentObject variable - with the example above, if currentObject was 0 that's OK, if it was 2 or 3 it will now be referring to the wrong entry because they have moved. And what if it was 1 and you just deleted that?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Thanks for sharing your final solution. I had a quick scan thru the code and can't see anything objectionable, so well done. (Rather than static methods I would generally prefer to see an instance-based implemention, but in this case it really doesn't matter.)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You init the array to null, then try to add things to it with add(...). That's two fundamental mistakes for a start. You need to init it with an actual size, as in in array = new String[size], then add values by array[index] = whatever.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Something as simple as swapping round the first couple of terms will do it: 3.14*4/3*r*r*r - the first thing to be evaluated is 3.14*4, which will be done in floating point, so then dividing that by 3 will also be done in floating point

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Check out the % (remainder) operator - gives you the remainder after dividing, eg
27/6 = 4
27%6 = 3
ie 27 is 6*4 + 3

or

secs = 140;
secs/60 = 2 (minutes)
secs%60 = 20 (seconds)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You can read whole lines one at a time from the console into a String, then use the String split method to split the line into an array of strings, each containing one value from the input string (the blanks are used to split the string, then discarded). You can then loop thru that array converting each value from string to int and putting it into your res1 array.

manel1989 commented: 5 +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is so frutrating! I know that you know how to do this, but it seems I'm going to have to break my own rules and spell out the code...

to push an item something onto a Stack stack1 the code is

stack1.push(something);
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Normally I agree with mKorbel, but in this particular case (one-time logon screen -> the rest of the application) I don't. I'd use a dialog for the logon, then dispose() it and hand over the application's main window.