JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

if (a) ...
else if (b) ...
else if (a && b) ... never executed because the else if won't be executed if (a==true) (see line 1) and (a && b) fails if (a==false)

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Vector<Question> questionList;
...
out.writeObject(questionList);

This will need the Question class to be declared as " implements Serializable ".
As it only has Strings and an int, there's no need to do anything else

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You copied code from another Daniweb user and posted it as part of your own post without giving the author any recognition or credit. Even if you had intention of misleading anyone, that's still bad manners. Anyway, enough said, let's move on.

can you just tell me on which class do i need to work on, and implement those methods

You need to
1. Add the JLabel to your window before anything is displayed (I think you already have that)
2. Set the label's text correctly when you create the initial set of shapes
3. Update the label's text whenever you add or remove a shape
You know your code best. Where would you put the extra code so that it's executed at the right time(s)?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

so - you have created 3 variables and used them to count the number of each shape as the shapes are created? That looks like a valid approach to me. Now you just need to use those 3 variables to create the line of text that goes into your JLabel.

ps: I just saw where you stole that code from. Shame on you.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Don't know what array you are talking about here, sorry.
I was just thinking of the arrays that you already have in your DrawPanel class

private MyLine lines [];
private MyRectangle rects [];

you know how big they are, so you know how many of each shape there is.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

25 posts and you still don't know about posting your code properly indented and in code tags. Your compiler error messages have the line numbers, but do you expect people to count the lines in your code to see which they are? We're willing to help, but we are not psychics.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Just to get this back on track...
the OP's problem is "'javac' is not recognized as an internal or external command,
operable program or batch file."

This is unlikely to be a classpath problem, but even so the installer will set the classpath, there's no need to modify it for a standard installation.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It looks like you have installed the Java Runtime Environment (JRE) which allows you the run Java programs, but not the Java Development Kit (JDK) that allows you to create them. If you haven't already done so, download and install the latest JDK.

ps java.exe runs java programs in a DOS-type window
javaw.exe is the same, but it doesn't show the DOS window. This is the one you normally use to run Java GUIs.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, I didn't suggest changing anything.
You asked "how do i count them?"
I wanted you to think about the code you already have that loops thru your arrays of lines and rects. Can you see how easy it would be to use the same kind of loops to count how many there are in each of those arrays?
There are also quite a few other ways to get these counts; I suggested another one in my previous post.
Here's one more: you store your shapes in arrays that you fill up with the appropriate shapes. So the count of each shape is the same as the size of the array...
This is really a lot easier than many of the things you have already done. You can do it if you just keep a clear head and think constructively.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Way back you had this code:

for (MyLine line : lines)
line.draw (g);
for (MyRectangle rect : rects)
rect.draw (g);

You can use the same loop constructs to count the number of lines & rects.

Another approach is to have a static int (eg "instanceCounter")in each of your subclasses, with a static accessor method to get its value. Then in the constructor you can just increment the int each time an instance is created. This is a common approach to keeping track of how many instances of a given class there are.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

What label? Where is the code that declares, initialises, and updates the label. Where is the coed that adds the label to some visible window?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK, that seems clear enough. No-one here is going to do it for you. Have a go, see how you get on, come back here with specific questions if you get stuck.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Yes, I understood that those were the steps. I was asking which one(s) the OP needed help with.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm confused. Do you need help getting the counts, or displaying them in a label, or adding a label to the bottom of your window, or creating a superclass???

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Of all the layout managers GridBagLayout is by far the most capable. There's a bit of a learning curve because there are so many options, but IMHO it's worth it. You can play with the easier ones for little homework apps, but if you are going to use Java seriously then sooner or later you will end up using GridBagLayout. Why waste time mastering the others? I say go for it!
http://download.oracle.com/javase/tutorial/uiswing/layout/gridbag.html

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

OK. Sorry I was answering the wrong question.
Simple answer AFAIK is "no", but there's always a way round if you try hard enough (eg Jon's deep copy). Is there a specific situation you're asking about, or is this just a general seeking of knowledge?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"I think if RefObject is an object (and not a primitive), then modify should modify the original."
Yes, absolutely. I think that's what I said in my final sentence? You can modify an object when passed a reference to that Object, but you can't change the parameter, ie the reference itself.
I think this is the other side of the coin to the old chestnut "How do you write a simple swap method in Java like you can in C++ ?"

public void swap(int a, int b) {
  int temp =a;
  a = b;
  b = temp;
  // doh!
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

All java parameters are call-by-value. (Remember that all variables that are not primitives are references to Objects, so it's the reference that's passed by value)
So in your example on line 3 parameter is a reference variable that contains a copy of the reference that was used in calling the method. Line 3 changes the value of parameter, but it has no effect whatsoever whatever was passed to the method. It is therefore impossible for a method to change the value of anything passed as a parameter.
Despite that, given a (copy of) a reference to some Object, a method can still modify the Object whose reference was passed, if that Object has mutator methods or public variables.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

OK. These are the relevant bits - you'll have to read and understand them to see how they work. Have fun:

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import static java.util.Calendar.*;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.SimpleDateFormat;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import static java.awt.GridBagConstraints.*;

public class CalendarPanel extends JPanel {

   // A simple calendar date selector for pop-up or embedded use.
   // It's a subclass of JPanel and can therefore be embedded and managed
   // in the usual ways in any Swing GUI context.
   // Date selection by the user is signalled by a callback to one or more
   // implementors of CalendarPanel.DateSelectionListener which must be
   // registered with the CalendarPanel instance via addDateSelectionListener
   //
   // Copyright 2002, 2005 (Minor updates for Java 1.5) James Cherrill. 

   public static void main(String[] args) {
      // A minimal test/demo harness.Just prints selected dates until closed.
      JFrame frame = new JFrame("Calendar demo");
      CalendarPanel calendar = new CalendarPanel();
      frame.add(calendar);
      frame.pack();
      frame.setLocationRelativeTo(null);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setVisible(true);
      calendar.addDateSelectionListener(new DateSelectionListener() {

         @Override
         public void dateSelected(Date d) {
            System.out.println("Selected " + d);
         }
      });
   }

   // Public section /////////////////////////////////////////////////////////////

   public interface DateSelectionListener {

      // called when user clicks a day in the calendar...
      public void dateSelected(Date d);
   }

   public void addDateSelectionListener(DateSelectionListener listener) {
      // standard observer/listener pattern for date selection events
      listeners.add(listener);
   }

   public CalendarPanel() {
      initGraphics();
      setMonth(0);
   }

   // End of public section //////////////////////////////////////////////////////

   private static final long serialVersionUID = 1L;

   private static final String dayLetter[] = { …
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@tracydo: if your question is now fully answered, please mark this thread as "solved".

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster
if (x>5)        // false
  y=1;          // not executed
else            // executed (matching if is false)
  if (x<5) {    // true
    if (x<3)    // false
      y=2;      // not executed
    else        // executed (matching if is false)
      y=3;      // executed 
    }
  else          // not executed (matching if is true)
    y=4;        // not executed
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You seem unaware that " " is not the same as "".
The fact you coded this seems to indicate that you still haven't understood Jon's point. Take a break. Take a deep breath. Read Jon's posts again. Repeat until you understand.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

This is a lot easier if you indent the code so you can see which else goes with which if:

if (x>5)
  y=1;
else 
  if (x<5) {
    if (x<3)
      y=2;
    else
      y=3;
    }
  else
    y=4;
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

"its only printing the object" doesn't make sense unless you are executing
System.out.println("Player's details: " + p);

Please copy your latest version of the print statement and paste it here.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, you're not getting it. His post is about formatting. It's quite likely that your program is OK, it's just the way you format (or fail to format) the output.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I think 2118789 is the object. as I was suppose to store the player's info such as age,weight,height,games played and goals scored. and using the club class I was suppose to fetch a player's info using his name. the thing is, it only fetches the name and 2118789 instead of the supposing objects. Player stored are the objects.

Read jon kiparsky's post again. He's almost certainly right.

jon.kiparsky commented: Gee, that's nice to see first thing in the morning. :) +2
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

never say never :)
There are special cases in which it's required, but they're special cases.

<Genuine question, not an attempt to start an argument>For example...?</Genuine question, not an attempt to start an argument>

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@cretaros Is this for a course? Will it be marked? Or is it just something you are doing for your own interest/fun?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

It would help if you told us the line number where the NPE is thrown.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I'm gonna vote on the side of never having public instance variables (except static final immutables), even if you do believe your class is safe against some bozo arbitrarily changing its values from some arbitrary concurrent thread.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Whether the use of the boolean array is right depends on what is the algorithm you are implementing. There's no clue as to your intended algorithm, so no way for anyone to comment.
Do you have a written description of the algorithm? Do you have it in your head? Could you use it with pencil & paper if someone gave you an arbitrary word?
If you can't answer "yes" to at least one of those questions then it's too soon to be coding anything.
ps: The obvious way to do this involves recursion, but there are bound to be other ways as well.

Again! Hi Jon.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

but I'd never told that's complicated for any of Java Guru's

The Java isn't too difficult if you are happy with 2D arrays of JComponents, gridbag layouts, listener patterns etc - so it's OK for an experienced Java coder, but pretty hard for a newbie.

The challenge I had was getting the right days into the right rows/cols for any arbitrary month, and I wasted time on a couple of poor algorithms. As with so many problems like that, it because easy once I got a good statement of the requirement - ie that the date in the top left corner has to be the nearest Monday on or before the first day of the month.

If @cretaros can guarantee that this isn't for a marked assignment I can give you all a bit of a laugh and share my old code...

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

If you know how to work with a char array then a boolean array is just the same except that all the values are true or false. Where exactly are you stuck?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Any particular reason for doing this? - it's not easy, in fact I would rate it as a fairly advanced GUI exercise.

ps I did this for an app about 8 years ago, and just dug out the code. It's under 200 lines, but I seem to remember having a really knotty time getting the logic for populating the right days into the right cells of the month grid.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

A very good reason for not just copying code snippets from the web!

catch blocks contain the code you want executed when an error is detected. An empty catch block says "I don't want anything done about any problem, don't tell me about it, I don't want to know". There are some circumstances where that's right, but normally not.

If you have no better idea, it's a good idea to just put
e.printStackTrace();
in the catch, eg:

catch(Exception e) {
   e.printStackTrace(); 
}

That will print a complete explanation of what the error was and where it happened.

cwarn23 commented: Excellent reply and easy to understand :) +6
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

No, that's not what masijade was referring to. It's your empty catch blocks like

} catch (MalformedURLException e) {
} catch (IOException e) {
}

If either of these exceptions is thrown you will never know.
If you have a e.printStackTrace(); in the catch block you will know exactly what exceptions are thrown and where.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

You declare and use newMoney (etc) in the abstract superclass. This variable is inherited by all the subclasses EXECPT that you declare it private, then declare new variables with the same names in your subclasses. So the "newMoney" that you calc in the subclass is a different variable from the "newMoney" that the inherited superclass methods access. Your code will only work if the subclasses are using the inherited variables from the superclass.
As a general rule you should never declare a variable in a subclass that has the same name as a variable in the superclass - it's guaranteed to cause confusion, at best. And never declare variables that should be inherited as "private" because that prevents them from being accessed in any subclass.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

My pleasure, but please don't call me "sir"!

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

The printStatement method signature looks wrong to me. I would expect it to have no parameters, and just print a statement for the current account ("this"), OR, to take a list of accounts as parameter, in which case it should be a static method (ie belong to the class, not any one specific instance of an account).
If it's the second option, you need to call getInterest etc on each member of the list:

for (BankAccount temp : ar1) {  // modern version of the for loop
   temp.calcInterest();
   double theInterest = [B]temp.[/B]getInterest();
   // etc
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

An instance of the Class class represents a class in your program. Using that instance you can find and access all the attributes of that class (variables, methods, annotations etc etc). Its used as part of Java reflection (that's the keyword to Google), which is used to find out the attributes of a class at runtime because you don't know them at compile time.
Here's a good place to start
http://download.oracle.com/javase/tutorial/reflect/index.html

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Your overall approach, with the public getData is good. The problem lies in

FormA page = new FormA(); // displays a new copy of the form
String user = page.getData();  // gets the data immediately - before the user can enter anything!

Somewhere you should have a button or some such that the user presses after entering the data, and that's when the call to getData needs to be executed. Either formA needs a reference to formB, or formB needs a ref to the existing formA for that to work (ie you need to call getData on the existing form, not a new one)- details depend on exactly how you have structured the rest of your code.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

1. To use Button's getLabel() method you must cast the Object returned by getSource() to a Button
2. You can't compare Strings with ==, use equals(String) instead, if you must.
3. If you want to know which button was pressed, test that directly

if(  (ae.getSource() == ok) ...

just remember to declare ok as

final Button ok = new Button("ok");

in your class, not in the init method.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Use an AlphaComposite to set the drawing mode and alpha before drawing your image. Here's a simple sample...

Image myImage = ...
float alpha = 0.5;
@Override
public void paintComponent(Graphics g) {
	Graphics2D g2D = (Graphics2D) g;

	AlphaComposite composite = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha);
	g2D.setComposite(composite); // Set current alpha
	g2D.drawImage(myImage, 0, 0, null);
}
JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

@ztini is right.
But if you can't do that for some reason then you can enhance your sort methods to sort both arrays at the same time, eg
in sortGradesByNames you swap elements with

int temp = grades[largest];
grades[largest] = grades[i];
grades[i] = temp;

so then, in the same place, you need to swap the same elements in the other array to keep them in step

String temp = names[i];
names[i] = names[smallest];
names[smallest] = temp;

but really, make a Student class.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

I made leftSide a container and that worked. I didn't realize that I hadn't done that in the first place. Thank you very much.

OK, glad to help. Mark this solved?

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

firewall problem and/or a router problem ... Do you know how I can fix this?

Sorry man, that depends on exactly what hardware & software you are running in exactly what configuration. I'm not best person to ask. There are loads of excellent sites on the web dealing with network config issues like this - it shouldn't be hard to find some relevant instructions.
J

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

Second problem sounds like a firewall problem and/or a router problem blocking and/or not forwarding inbound traffic.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

leftSide is the contentpane for the window (previous listing line 5)
bothSides is also the contentpane for the window (previous listing line 14)
so on line 17 you are trying to add the content pane to itself, == error.

At a guess, leftSide should be a new container.

JamesCherrill 4,733 Most Valuable Poster Team Colleague Featured Poster

That looks OK to me. Only suggestion is to put a full e.printStackTrace(); into your catch block so you can see the full text of any error message generated by Java.