JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What would my classes and methods skeleton look like in this case?

You have class names and some method calls there - so that's your starting point. Just define the classes, add some (private) variables for the obvious data, accessor methods as required for the data, constructor(s), other methods as mentioned...
You'll find that once you get started it will fall into place.
ps: HashMap is a good way to improve your V2 code, but with the OO version it's not needed because the value of a Card is an instance variable of the Card class and each instance has it's own value.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Also, would it be possible for someone to outline how I achieve the most amount of OOP for BlackJack please? I would really like to finally get to grips with OOP.

There are some pointers in my last full post to your previous thread...
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That's good progress - you learn fast!
Let's get rid of the duplicated code for getting the value of a card... maybe a method that has all the "if card equals... value = ..." lines
public static int getCardValue(String card) { ...
then you can simplify code like this

for(int i = 0; i < dealersCards.size(); i++) {
    dValue += getCardValue(dealersCards.get(i));
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This one's marked "solved", so people may not realise there's new info. Better to start a new topic.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK! Glad to help.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, something along those lines is OK, at least for now...

I can't do a complete into to OO here- there are lots aleady on the web, but for a card game you typically start with Card class (with members rank (Ace,2 etc) and suit). Then you have collection (eg ArrayList) of 52 Cards that you can shuffle and deal from. You have a Player class which has a collection ("hand") of Cards and methods to determine whnether that Player wants to twist or whatever. YOu may have a specialised sub-class of Player for the Dealer because the Dealer is almost but not quite the same as an ordinary player.
All this puts you in a position where you can code the top-level so its so clear that it needs no comments...

Player user1 = new Player();
Dealer dealer = new Dealer();
Deck  deck = new Deck();
deck.shuffle();
user1.takeCards(deck.deal(2));  
if (user1.twists()) then ...
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, maybe a bit more like:

public class BlackjackGame {

   public static void main(String[] args) {
      // get out of static by creating an instance...
      new BlackjackGame();
   }

   BlackjackGame() {    // constructor calls methods to play a new Game
      dealUserCards();  // break code into easy steps
      dealDealerCards();
      ... etc
   }

   void dealUserCards()n {
      ... etc

   .. etc

   // utility methods...
   public int getValue(String card) {
      ... etc

}

Still not very object-oriented, but one step at a time...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A couple of quick observations:
You repeat the code to get the numeric value of a card a few times - repeating code is never agood idea.
You can put all that into a method public int getValue(String card) ... that you can call whenever you want the value of a card.
In that method you can use a switch to avoid all those nested if statements.

For a more "object oriented" approach you would create a Card class that contains the name of the card and its numeric value. You'll fund that tidies up and clarifies the code quite a lot.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You write a loop that reads the file in smaller pieces and writes each piece to the output file.
See this thread (read right to the end for the correct solution)
http://www.daniweb.com/software-development/java/threads/426232/incorrect-uploading-of-file

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You need to see how many bytes were actually read into the array (the array may not have been filled)
int numBytes = in.read(ch) ...
then write the same number of bytes
write(ch,0,numBytes)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Welcome to the DaniWeb Java forum!
OK, rule number 1. Don't just say "it gets errors". Always post the full text of any error messages, including the line numbers. If there are no error messages but the behaviour is wrong, explain exactly what you expected vs what actually happened.
That way we won't be debugging in the daqrk.

kvprajapati commented: :) +14
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think you have to loop. However, auto-unboxing will allow you to use Integer objects in many places where an int is required.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's very dangerous to be on 1.4 - you are missing many years of essential security fixes (not to mention language enhancements). Your computer is at risk. You really should update to 1.7 immediately

DavidKroukamp commented: Great advice +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Use SimpleDateFormat to parse the user input into a Date object. You can then subtract that from today's date to see if the difference is >= 21 years.

DavidKroukamp commented: nice +8
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Length of an array is .length - not a method call so no ().

Non static members have a different value for every instance of the class, so its not valid to reference them without knowing which instance you are referring to. In a static method there is no particular instance, so you can't refer to a nonstatic member without qualifying it with an instance.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I doubt that anyone will make the effort to understand code that has meaningless variable names and no comments.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Can you write a "place-holder" value, eg 0, where the size needs to go, then you can overwrite that with the final value later (using a random access file).
ps One byte doesn't allow for a very big file!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Please help me to how to solve the error.

  1. The error message contains the line number where the error happened; go to that line.
  2. Immediately before that line add a print statement to print the value(s) of the index variable(s)/expressions(s) to see exactly when they are going out of range.
  3. Back-track through your code to see why they are going out of range. Add more print statements to show the values of key variables to help identify the exact source of the error.
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your setBaseSalary() function should be tagged as throwing an exception. IE:

No, that's not correct. IllegalArgumentException is a runtime exception a.k.a. unchecked excpetion, and does not have to be declared or caught.

The problem, as I said before, is that line 4 guarantees that the if on line 5 is never true.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

After line 1 baseSalary can never be negative, it will always be either a positive salary value, or zero. So the test on line 2 will always be false.

Typically, if you have a "set" method that does validation, it does all the validation first, before trying to do anything with the values.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Composition is where you use an existing class to hold some of the data and methods for your class - in this case you use an instance of ComissionEmployee to hold all the basic info for your BasePlusCE. It's an alternative to extending ComissionEmployee which takes more work, but gets round the "can only extend one class" limitation.
In your constructor you have all the info parameters - the ones that BasePlusCE needs to pass on to your BasePlusCE's constructor, the rest you deal with yourself.

"this" is used to refer to the current instance where a method is running. It's used in various ways, but the relevant ones for constructors are
1. To refer to another constructor of the same class, eg you have 2 constructors

MyClass (int a, int b) { ... }  // explicit values for a and b
MyClass() { ...}  // default values

in that case you could write the second constructor like this

MyClass() { 
    this(0, 0); // use the other constructor to save the default values
}
  1. The other use is to refer to instance variables when you have "hidden" theor names by using the same name for a parameter, eg

    int x;                      // instance variable
    MyClass() {int x) {         // parameter "hides" the instance variable of the same name
        System.out.println(x);  // refers to the parameter 
        System.out.println(this.x); // refers to the instance variable
        this.x = x;             // copies the value of the parameter to the instance …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

so its used mainly in arrays?

No, not just arrays. Anything that can iterate - including Sets, Lists etc

> ArrayList<String> myStrings = ... etc
> for ( String tempString : myStrings ) // works just like for arrays etc
>     System.out.println(tempString);

in fact it's more useful with Collection objects because the old-fashioned loop is harder to code and get right.

ps it's called a "for-each" loop, becuase when youy read it out loud you say "for each String s in myStrings..."

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

We're not mind readers.
What exactly is the complete error message?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just replace your current JPanel with your own subclass that overrides paintComponent (NOT paint).
Have a look at this: http://java.sun.com/products/jfc/tsc/articles/painting/#callbacks

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
> OK sorted it.
>     `System.out.println(numberOfOrdersInArray); `
> Now returns:
>    ` 44`
> (22 items of information, 2 records)

Each array contains 2 items, so you must only loop up to index 1 in each array - the number you need is the number of records, NOT the number of items.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, you can use a single reader just lik ethat. But every try must have its own catch.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have giant arrays of 10,000 elements each, of which only the first 2 elements are populated and the remainder are null. When you loop you loop through the whole array, so you are guaranteed to hit nulls sooner or later.
You need a variable that tells you how many actual records there are, and use that as the upper limit for your loops, eg

    for (int i = 0; i < numberOfRecords, i++) {
          if (orderID[i].equals(searchID))  // arrays[i] contain the record you were looking for
    }
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Welcome.
We are happy to help anyone, beginner or expert, as long as they are willing to listen and put in some effort.

might not have been initialized

Means what it says. The compiler has worked out the possible routes through ypur program, and seen that one or more results in the var not being initialised before it's used. In this case it's probably what happens if a try block fails so the program goes via the catch instead. If you initialise them when you declare them this problem cannot happen - eg String firstNumber = "";

ps You don't need all those BufferedReaders - in fact they may cause problems by interfering with each other. Just have one reader and use that for all your input.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It's up to you. Just traversing the entire tree in L-R order and dumping the strings on the console will show if yur tree works or not. It's just a very short recursive routine that every tree class should have anyway.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Without reading all the code, that certainly looks sensible. Just write a small test to see for yourself if it is right... all you need is a method to print the contents of the tree after you have inserted some data.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, I really can't bear the thought of you having to resort to C++
Here's a tiny runnable program that shows wrting all possible byte values to a file, then readingthem in and displaying them as signed values, unsigned values, and characters...

import java.io.*;

public class ByteIODemo {
// simple demo of reading/writing bytes as unsigned ints or chars

   public static void main(String[] args) {
      // define a file in the current temp directory
      File tempDir = new File(System.getProperty("java.io.tmpdir"));
      File byteFile = new File(tempDir, "bytes.data");

      // write all 256 possible bytes values to the file...
      try {
         FileOutputStream out = new FileOutputStream(byteFile);
         for (int i = 0; i<=255; i++) out.write(i);
         out.close();
      } catch (Exception e) {
         e.printStackTrace();
      }

      // read all the bytes...
      byte[] theBytes = new byte[256]; // input buffer
      try {
         FileInputStream in = new FileInputStream(byteFile);
         in.read(theBytes);
         in.close();
      } catch (Exception e) {
         e.printStackTrace();
      }

      // interpret each byte as unsigned value and character
      for (byte b : theBytes) {
         int unsigned = b & 0xff; // mask out unwanted sign bits
         char c = (char) b;
         System.out.println(b + " " + unsigned + " " + c);
      }

   }   

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

Yes, FlowLayout is not one to use for your requirement - it doesn't resize it just adds space before/after. Use a GridLayout, or go the whole hog and use a GridBagLayout for total control

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Normally you would use a Listener to respond to changes in the table's data, and feed those into SQL statements to update the database. The details depend on exactly how your overall systeem is put together, but you will find many relevant examples on the web.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

There's no code there that can update the database, so how exactly are you trying to update the values?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

^ basically right, except that nowhere does Java define any mapping between 0 and 1s vs the boolean values false/true.
Bitwize operators work on each bit of an int value, the logical operators work on individual boolean true/false values, but there's no connection between these two versions.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I just had a quick look, so maybe I missed something , but anyway...
The original correctly uses a Timer to call its actionPerformed method at regular intervals. That's how the animation is updated and finally repaint() is called to tell Java that the screen needs to be updated. (Java will then call the overridden paint(Graphics g) method.) I don't see the Timer in your code - a single sleep isn't remotely equivalent.
It's a classic beginner's mistake to code hundreds of lines before running any tests to check if the basic ideas are right. I recommend that you set this aside and write a minimally short program that uses a Timer to move a simple rectangle once across the screen. That will give you an understanding of the Timer, the actionPerformed method, and the paint method, and the relationship between them. Then you can go back to your more complex code and you will know what you are doing.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
  1. In a linked list the nodes are not numbered, so it makes no difference whether you chose the count from 1 or 0 as long as you count the correct number of total entries in a consistent way.
  2. Each node in a linked list contains an Object, and a reference (pointer) to the next node. The "head" variable usually contains a reference to the first node, which contains an Object, and a reference to the second none (etc).
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

nevermind

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

An integer literal is of type long if it ends with the letter L or l; otherwise it is of type int. It is recommended that you use the upper case letter L because the lower case letter l is hard to distinguish from the digit 1.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That looks like your keys are Strings. In the Comparator you could parse them into integers and compare those values.

arathy nair commented: But how to use an integer iterator????There are lots of integer keys from 1-17 in this treemap +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

ps
If you import static the class then you can use its static methods without prefixing the class name
ie

import static java.lang.Math.*;
...
x = cos(y);  // static method cos is resolved via the import static for Math
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

In Java you do this by creating a utility class with all those functions defined as static (so they don't need an instance to be created). A good example is the Math class in the standard Java API
http://docs.oracle.com/javase/6/docs/api/java/lang/Math.html
you can simply import this class at the start of your program, then call the methods like Math.cos(x) or whatever.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
sciwizeh commented: I was going to suggest the same thing +5
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You have overidden JApplet's paint method, which is the method responsible for ensuring the contents are painted. That's why they don't get painted. Add a call to super.paint(g) as the first line of your paint mathod. This will ensure that everything in the applet gets painted before you draw your line.
Read this for more info, including the use of paintComponent rather than paint:
http://java.sun.com/products/jfc/tsc/articles/painting/#callbacks

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You're heading in a right direction, but there's something missing between line 41 (where you prompt the user) and line 42 where you test the user's input (that you haven't read yet!).

Johannady2 commented: oh wait.. now it asks for 2 inputs in set Seconds. and I still can't enter "n" to stop it.. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Line 26 closes the method definition, so the if statement is not in any method, which is illegal and has confused the compiler.

Johannady2 commented: the if statement is inside a different method.. I didn't paste it here anymore because it's too long. +0
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You are missing some essential pieces to display your barcode.
1. To display something on the screen you need to put it some kind of window, eg a JFrame.
2. The thing you put in the window is a JPanel, but you need a special one because you are going to draw the contents yoursefl.
3. You do that by defining your own class that extends JPanel. Then you create an instance of that and add it to the JFrame.
4. When Java tries to display your panel it will call its paintComponent(Graphics g) method. You have to override this method to do your own customised painting. You have the code for that already, but its in your DrawBarcode method. You need to put that code in your paintComnponent instead.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
  1. You can combine multiple tests in one if, in this case ORing them together, as in
    if ( site.next(1) == null || site.next(2) == null ...

  2. The contents of the if must be a boolean expression, so you don't need an if to turn that into a booelan result, eg
    return ! ( site.next(1) == null || site.next(2) == null

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

/ means divide! % means remainder
1/3 = .33333, truncated to 0 in int arithmetic
1%3 = 1 (3 into 1 goes 0 times with a remainder of 1)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Sure. Suppose I had an array of Car objects, I could find the Fords with something like

for (Car c : myArrayOfCars) {
   if (c.getManufacturer().equals("Ford") ...
}