| [OT] Teaching OO / Java (was: Concentric Circles Loosing It) |
|
 |
Index ‹ java-programmer
|
- Previous
- 2
- Servlet HelpDear Friends
pls help in running servlet in Tomcat. i m not getting which setting
should i do for it.
pls send me how to execute a servlet HalloWorld
paresh
- 3
- Shuffled Poker DeckHi all,
Im new to Java, and as Training for me I tried to build an Application,
which returns a shuffled Pokerdeck. (Random from 1 to 52)
Maybe you will laugh, but i needed a whole day to solve it, and I think its
even not good solution.
Well, at least it works, but please could you give me a hint, how to solve
this problem easier.
Heres the code:
/**
* @(#)Poker.java
*
*
* @author Martin Krainer
* @version 1.00 2007/1/22
*/
public class Poker {
static int[] deck = new int[52];
void buildDeck() { // builds a deck with 52
(hopefully) different Integers
for (int i=0; i<52; i++) {
deck[i] = (int)(Math.random()*10E7);
}
}
void shuffledDeck(int[] a) { // now here I tried hard and
long to get a field with numbers from 1 to 52
long[] zahl= new long[52];
for (int i=0; i<10E7; i++) {
for (int j=0; j<52; j++) {
if (i == deck[j]) {
zahl[j] = j;
System.out.print( " " + (zahl[j]+1) );
}
}
}
}
public static void main(String[] args) {
Poker p = new Poker();
p.buildDeck();
p.shuffledDeck(deck); //well, it works, but...
what do you think?
}
}
- 4
- Java regex imposture re: Perl regex compatibility// java has claimed to have added Perl style regular expressions ,
however
// writing a regular expression containing a variable is ugly in java.
// in Perl: $rec =~ s/$delimiter//g is easy to write and to read.
// in java: if delimiter may hold say "||" or "!" isntead of [a-z]
text,
// you have to quote it as below with preceding \\Q (not just \Q ) and
// trailing \\E. ugly and difficult to code!
rec = rec.replaceAll("\\Q" + delimiter + "\\E" ,"");
So even though java has perl style regex in principle, in practice,
in java they seem nearly unusable!
-A.C. Attlee
- 4
- Java in AT&T PWPHi,
I was referred to you because I'm having a problem getting this to run in
AT&T PWP (Personal Web Pages). I compile the Java Program using NetBeans
IDE 3.6. I upload the .java, the .class and the .html files to my PWP
library.
import java.awt.*;
import java.applet.*;
import javax.swing.*;
public class AppHelloWorld extends JApplet
{
public void init()
{
resize(700,100);
}
public void paint(Graphics g)
{
g.drawString("Hello, Applet world!", 10, 25);
}
}
---------------------------------------------------------------
This is the page I run in the browser (Internet Explorer and Netscape).
<HTML>
<HEAD>
<TITLE>Applet HTML Page</TITLE>
</HEAD>
<BODY>
<H3><HR WIDTH="100%">Applet HTML Page<HR WIDTH="100%"></H3>
<P>
<APPLET code="AppHelloWorld.class" width=350 height=200>
</APPLET>
</P>
<HR WIDTH="100%"><FONT SIZE=-1><I>Generated by NetBeans IDE</I></FONT>
</BODY>
</HTML>
This is the message I get from the browser:
java.lang.ClassFormatError: AppHelloWorld (Extra bytes at the end of the
class file)
The page will run in Edit*Plus and IE (FILE, OPEN, BROWSE) and Netscape
(FILE, OPEN FILES, select document).
Can you provide any information on how to get this to run in the browsers?
Wayne Rawls
- 5
- Good programming questionI was just wondering as to what is a better standard to program by. If
I have a class like this:
class MyClass {
private int myVariable;
private void myMethod1(){
myVariable = 1;
}
private void myMethod2(){
this.myVariable = 1;
}
}
Which method is better? Is there a better choice? Does it make any
difference which way I do it? Maybe this sounds like an insignificant
question but I have done this both ways and just don't know if there is
a good reason to go one way or the other.
- 5
- Pooling of connectionsHi all,
I've read a few articles about how pooled connections are more
efficient because you don't need to repeatedly setup/teardown a
connection - you just acquire connection from the pool.
Exactly what steps in setting up/tearing down a connection are skipped
by acquiring an already existing connection? Perhaps the steps in
establishing/tearing down a connection would help in answering this
question?
Thanks
Taras
- 5
- Grid Bag question.I am trying to put together a panel which needs to consist of 20 lines,
each containing :
JLabel - 2 columns
JTextField - 1 colomn
JTextField - 1 column
JLabel - 2 Columns
The JText fields would be filled out from arrays, so it would be layed
out with the help of a 'for' loop.
I put together a test program (provided below) to try to emulate this,
but the layout manager only seems to want to give each entity (JLabel,
JTextField) the same amount of width each regardless of what I specify
in the GridWidth fields. Can this be done with specifying sizes for the
individual JTextfields or labels? Please advise - Thanks - Lou
import java.awt.*;
import javax.swing.*;
public class Test_GridBag2 {
public static void addComponentsToPane(JPanel pane) {
JButton button;
JLabel label;
JTextField jtf;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
c.fill = GridBagConstraints.HORIZONTAL;
//pane.setPreferredSize(new Dimension(300,200));
for (int i = 0; i<3;i++)
{
label = new JLabel("test");
c.gridx = 0;
c.gridwidth = 2; //2 columns wide
c.gridy = i; //third row
c.weightx = 1.0;
//c.anchor = GridBagConstraints.EAST; //bottom of space
pane.add(label, c);
jtf = new JTextField();
c.gridx = 2; //aligned with button 2
c.gridwidth = 1; //2 columns wide
c.gridy = i;
c.weightx = 1.0; //request any extra vertical space
//c.anchor = GridBagConstraints.EAST;//third row
pane.add(jtf, c);
jtf = new JTextField();
c.gridx = 3; //aligned with button 2
c.gridwidth = 1; //2 columns wide
c.gridy = i;
c.weightx = 1.0; //request any extra vertical space
//c.anchor = GridBagConstraints.EAST;//third row
pane.add(jtf, c);
label = new JLabel("");
c.gridx = 4; //aligned with button 2
c.gridwidth = 2; //2 columns wide
c.gridy = i;
//c.insets = new Insets(0,0,0,10);
c.weightx = 1.0; //request any extra vertical space
//c.anchor = GridBagConstraints.EAST;//third row
pane.add(label, c);
}
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("GridBagLayoutDemo");
JFrame.setDefaultLookAndFeelDecorated(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
addComponentsToPane(panel);
frame.getContentPane().add( panel );
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
- 5
- Layout problem driving me insane - panels, resizing, etcIchBin wrote:
> email***@***.com wrote:
>>> Get NetBeans. (Get the latest one, 5.5) You don't need any plugins,
>>> everything is there for you already. Go through the tutorial below,
>>> it'll take about 30 minutes. If your eyes don't pop out of your head
>>> like Rodger Rabbit, then you can safely ignore it.
>>>
>>> http://www.netbeans.org/kb/50/quickstart-gui.html
>>
>> 60 MB - yikes. I'll download it tonight and play with it a little
>> bit... and also try all the other suggestions. I'll try and post my
>> conclusions tomorrow.
>>
[snip]
Just looked at the code again. It's ok but wanted to slim it down one
more time..
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.debug.FormDebugPanel;
import com.jgoodies.forms.debug.FormDebugUtils;
import com.jgoodies.forms.factories.ButtonBarFactory;
import com.jgoodies.forms.layout.FormLayout;
public class LayoutProblem implements ActionListener
{
public static void main(String[] args)
{
SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
new LayoutProblem();
}
});
}
public LayoutProblem()
{
jFrame.setDefaultCloseOperation(jFrame.EXIT_ON_CLOSE);
jFrame.add(buildMainPanel());
jFrame.setPreferredSize(new Dimension(475,600));
jFrame.pack();
jFrame.setVisible(true);
}
private JPanel buildMainPanel()
{
FormLayout layout = new FormLayout (
/* Columns */ "default:grow",
/* Rows */ "pref, 12dlu, fill:0:grow(0.50), 20dlu,
fill:0:grow(0.50)"
);
DefaultFormBuilder builder = DEBUGMODE
? new DefaultFormBuilder(layout,new FormDebugPanel())
: new DefaultFormBuilder(layout);
builder.setDefaultDialogBorder();
builder.nextLine(2);
builder.append(buildControl(jTable));
builder.append(buildButtonBar());
builder.append(buildControl(jTextArea));
if (DEBUGMODE)
{
FormDebugUtils.dumpAll(builder.getPanel());
}
return builder.getPanel();
}
private JComponent buildButtonBar()
{
JPanel jPanel = new JPanel();
jPanel = ButtonBarFactory.buildCenteredBar(
buildControl(BUTTON1),
buildControl(BUTTON2),
buildControl(BUTTON3),
buildControl(TEXT_EXIT));
jPanel.setBorder(BorderFactory.createRaisedBevelBorder());
return jPanel;
}
private JButton buildControl(String labelText)
{
jButton = new JButton(labelText);
jButton.addActionListener(this);
return jButton;
}
private JComponent buildControl(JTable jTable)
{
jTable = new JTable(dataValues, columnNames);
JScrollPane scrollPane = new JScrollPane(jTable);
jTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
return scrollPane;
}
private JComponent buildControl(JTextArea jTextArea)
{
jTextArea = new JTextArea(logAreaHdr);
JScrollPane scrollPane = new JScrollPane(jTextArea );
return scrollPane;
}
public void actionPerformed(ActionEvent e)
{
final String method = "jBotton_actionPerformed(ActionEvent " +
e + "): ";
if (DEBUGMODE)
{
System.out.println(DEBUGHEATER + method);
}
if (BUTTON1.equals(e.getActionCommand()))
{
}
else if (BUTTON2.equals(e.getActionCommand()))
{
}
else if (BUTTON3.equals(e.getActionCommand()))
{
}
else if (TEXT_EXIT.equals(e.getActionCommand()))
{
System.exit(0);
}
}
private static final String PROGRAM = (((new
Throwable()).getStackTrace())[0].getClassName())+".";
private static final String DEBUGHEATER = "( DEBUG ) " + PROGRAM;
private static final boolean DEBUGMODE = false;
private JFrame jFrame = new JFrame("JGoodies
Forms Layout Demo");
private JTable jTable;
private JTextArea jTextArea;
private JButton jButton;
private final String BUTTON1 = "Button 1";
private final String BUTTON2 = "Button 2";
private final String BUTTON3 = "Button 3";
private final String TEXT_EXIT = "EXIT";
private final String logAreaHdr =
"---------------------------\n---------------------------\n---------------------------\n";
private final String columnNames[] = {"Column 1", "Column
2", "Column 3"};
private String dataValues[][] = {
{"0aa", "bbb", "ccc"},
{"0dd", "eee", "fff"},
{"0gg", "hhh", "iii"},
{"1aa", "bbb", "ccc"},
{"1dd", "eee", "fff"},
{"1gg", "hhh", "iii"},
{"2aa", "bbb", "ccc"},
{"2dd", "eee", "fff"},
{"2gg", "hhh", "iii"},
{"3aa", "bbb", "ccc"},
{"3dd", "eee", "fff"},
{"3gg", "hhh", "iii"},
{"4aa", "bbb", "ccc"},
{"4dd", "eee", "fff"},
{"4gg", "hhh", "iii"},
{"5aa", "bbb", "ccc"},
{"5dd", "eee", "fff"},
{"5gg", "hhh", "iii"},
{"6aa", "bbb", "ccc"},
{"6dd", "eee", "fff"},
{"6gg", "hhh", "iii"},
{"7aa", "bbb", "ccc"},
{"7dd", "eee", "fff"},
{"7gg", "hhh", "iii"},
{"8aa", "bbb", "ccc"},
{"8dd", "eee", "fff"},
{"8gg", "hhh", "iii"},
{"9aa", "bbb", "ccc"},
{"9dd", "eee", "fff"},
{"9gg", "hhh", "iii"},
{"aaa", "bbb", "ccc"},
{"ddd", "eee", "fff"},
{"ggg", "hhh", "iii"},};
}
--
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA
http://weconsultants.servebeer.com/JHackerAppManager
__________________________________________________________________________
'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
- 6
- 6
- polymorphic voodoo?Ok, here is what I am trying to decipher. I have the following code
private void someMethod() {
ArrayList list1 = null;
LinkedList list2 = null;
cleverFunction(list1,list2);
}
private void cleverFunction(List first, List second) {
System.out.println("Static type for first is " + .....);
System.out.println("Static type for second is " + .....);
}
Ok, so, given a polymorphic call like this, is it possible, through
whatever witchcraft necessary, to determine what the static types of
the type polymorphic parameters are?
Maybe some sort of interrogation of the stack? Maybe some sorta
reflection?
Ideas?
christian
http://christian.bongiorno.org/resume.PDF
- 8
- How to open dialup connection in Java?Hi everyone,
Who know, how to open dialup connection from Java? (I found only one
way - to run another program (*.bat file), but it's not good idea). Any
another suggestions?
Haim.
- 8
- HAVI packages, where?Hi everyone, I want to download HAVI API packages for java but i don't
know where can i download its. Thank you
- 8
- checking for org.omg.CORBA.OBJECT_NOT_EXISTHi,
I'm writing a java CORBA client, using the Visibroker ORB. Sometimes
when I call methods on the remote object references I get the following
exception
org.omg.CORBA.OBJECT_NOT_EXIST: vmcid: OMG minor code: 0 completed:
No
Is there some way that I can check beforehand whether this exception
will be thrown, i.e. whether the remote object still exists? I thought
that I could do it like this:
if (myRef._non_existent()) {
// get a new myRef
}
myRef.doRemoteMethod();
But I've found that _non_existent() throws exceptions. Is there a
better way to check for the state that causes an OBJECT_NOT_EXIST
exception?
Regards,
Dan
- 9
- Java RPCHi All,
We have a set of application that communicates with embeded devices
that runs linux. Now we decide to use xml-rpc to send commands to the
devices with Apache's xmlrpc (or any other). We only need to generate,
and parse the structured xml string that contains the method name and
parameters. In other words we dont want to use a rpc server.
My question is how can I obtain the xml string of RPC command by only
suplying the method name and a vector that contains the parameters.
ps. In addition to primitive data types, parameters may contain
struct, array and etc.
Thanks in advance for your answer..
- 16
- basics of ANThi, i have downloaded and started reading the ant tutorial.
i am confused becuase some of the concepts , the tutorial (jakarta )
is not making clear.
my objective is to compile and run a java program.
my questions are as follows
it is called in order to compile and run you need a "build.xml" file .
where is that file ?
QUESTION 1: is it within ant distribution OR i have to make a
build.xml file myself?
The tutorial does not say anything about it. it just starts straight
forward with a demo "build.xml" file.
where do i put this "build.xml" file ?
QUESTION 2 :
i have a java code here.
>md src
package oata;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
location of this code is "src/oata/HelloWorld.java"
Now tell, where do i put "build.xml" file ?
|
| Author |
Message |
Michael Voss

|
Posted: 2003-12-8 21:00:00 |
Top |
java-programmer, [OT] Teaching OO / Java (was: Concentric Circles Loosing It)
Hi !
"Anthony Borla" wrote:
>
> "Alan P" <email***@***.com> wrote in message
> news:br0efp$ola$email***@***.com...
> >
> > I'm a newbie Java student as well, and AFAIK we all start
> > the same way -> by using a 'Turtle' class. Remember those
> > little robot things from infant school, with the penUp(),
> > penDown() etc.
> >
> > The idea is not stunning effects, but an introduction to OOPS
[...]
> I'm just a very firm believer [based mainly anectdotal, not statistical,
> evidence] in teaching programming concepts from first principles which, to
> me, means:
>
> * Teaching the rudiments of console windows / command-line
> usage, which includes the use of editors, compilers, and similar
> tools.
O.k., this is meant to teach the "DE" (leaving out the leading 'I'). But
maybe some people want their students to concentrate on the course theme, so
they decide to use an IDE. Personally, I think one has to know how to deal
with a language without any external (!) IDE, so you should know about
"javac" and "java"...
>
> Taking a Windows environment as an example, all that is needed
> is knowing how to:
>
> - Open up a Command Window
> - Load and use Notepad [or one of its more versatile
> replacements]
> - Invoke javac.exe and java.exe
>
> * Teaching language syntax basics using a serial execution
> programming model, a fancy description for a program
> which executes by starting at point A and proceeding
> downwards towards towards point B e.g.
>
> ...
> public static void main(String[] args)
> {
> // Point A
> ...
> ...
> // Point B
> }
> ...
>
> Once students have grasped the basics of structured / object-based
> programming, all in a serial, non-GUI enviroment, the rudiments of OOP
> [inheritance, polymophism] can be taught.
This will teach students to write programs the serial way first. In my
opinion, it would be the better approach to teach basic OO concepts first
(before even invoking an editor for the first time, maybe using some
pseudo-language).
Using your approach, students would learn how to write an imperative program
frist (one big main() method). Then you would introduce a completely new
paradigm, the structured programming, using functions (other than the main()
method, but still implementing a single class). Later on, you would
introduce yet another paradigm, the object-oriented programming implementing
specialized classes which will focus on certain tasks.
Have you ever tried to teach oo concepts to a - let's say - COBOL programmer
(nothing to say against COBOL programmer, of course, replace with almost any
3GL you (don't) like)? You will have a very hard time unless he (or she)
will understand them, because the imperative paradigm will block their view
onto objects. So I would always prefer to teach the object-oriented way
first. For most people, it will be quite easy to write "one big main()
method" if they are forced to do it, and the oo paradigm will not stand in
their way.
> Once these basics have been
> mastered, the next steps could be taken, which would probably be one, or
> more, of the following:
>
> * Algorithms / Data Structures
Data Structures might well be renamed "Object Oriented Design"... ;-)
> * Concurrent / Advanced Programming
> * Event-driven / GUI Programming
>
> I guess you would describe me as decidedly 'old school' in this regard :)
!
>
> In a nutshell, I'm not comfortable with the notion that GUI's and graphics
> simplify the teaching of very important, fundamental programming ideas,
but
> I probably hold a minority opinion here.
>
> Programming, like mathematics, can be an exciting field. However, much
> effort, most of it in mundane areas, must be expended to satisfactorily
> grasp the basics on which all further learning progress rests. I can
> appreciate course designers wanting to make programming more interesting
or
> accessable, hence the use of GUI's and similar tools, or presenting ideas
in
> graphical form. IMO, though, it is best to avoid such things as they sport
> hidden complexities, and keep things as simple as possible, at least until
a
> firm grounding in the basics has been obtained.
Yes. But one might define "basics" somewhat different. You must not have (I
might even say "should not have") any knowledge about "serial" or
"functional" programming to learn to write good OO programs.
(O.k., I might have started another thread on this, but then...)
Cheers,
Michael
|
| |
|
| |
 |
tom.cowdery

|
Posted: 2003-12-9 7:51:00 |
Top |
java-programmer >> [OT] Teaching OO / Java (was: Concentric Circles Loosing It)
Having tried both approaches, I can tell you that an "objects-first"
approach works much better when teaching an OOP language. Starting
with a procedural approach and then trying to shift to OOP mode later
is counter-productive.
I don't have a problem starting with console windows and a text
editor, in fact I encourage it for the reasons stated below.
Especially in Java, since it doesn't produce native executables, kids
who only use an IDE, have trouble separating the IDE from the
language. If they've never run a program without the IDE, they may
not understand that it CAN run without the IDE.
But that is a separate issue. You can create a GUI program (i.e.
Turtles) in a console window so long as the program runs in a
graphical environment. The Turtle class that I use, will pop open a
nice looking window when executed from the command line. The kids
learn about method calls, subclassing, and objects right from the
get-go. When I introduce primitive elements, like ints, booleans,
etc., they are introduced in a manner that shows how they would be
used in objects and to control objects.
"Michael Voss" <email***@***.com> wrote in message news:<3fd475ea$1@news>...
> Hi !
> "Anthony Borla" wrote:
> >
> > "Alan P" <email***@***.com> wrote in message
> > news:br0efp$ola$email***@***.com...
> > >
> > > I'm a newbie Java student as well, and AFAIK we all start
> > > the same way -> by using a 'Turtle' class. Remember those
> > > little robot things from infant school, with the penUp(),
> > > penDown() etc.
> > >
> > > The idea is not stunning effects, but an introduction to OOPS
> [...]
> > I'm just a very firm believer [based mainly anectdotal, not statistical,
> > evidence] in teaching programming concepts from first principles which, to
> > me, means:
> >
> > * Teaching the rudiments of console windows / command-line
> > usage, which includes the use of editors, compilers, and similar
> > tools.
>
> O.k., this is meant to teach the "DE" (leaving out the leading 'I'). But
> maybe some people want their students to concentrate on the course theme, so
> they decide to use an IDE. Personally, I think one has to know how to deal
> with a language without any external (!) IDE, so you should know about
> "javac" and "java"...
>
> >
> > Taking a Windows environment as an example, all that is needed
> > is knowing how to:
> >
> > - Open up a Command Window
> > - Load and use Notepad [or one of its more versatile
> > replacements]
> > - Invoke javac.exe and java.exe
> >
> > * Teaching language syntax basics using a serial execution
> > programming model, a fancy description for a program
> > which executes by starting at point A and proceeding
> > downwards towards towards point B e.g.
> >
> > ...
> > public static void main(String[] args)
> > {
> > // Point A
> > ...
> > ...
> > // Point B
> > }
> > ...
> >
> > Once students have grasped the basics of structured / object-based
> > programming, all in a serial, non-GUI enviroment, the rudiments of OOP
> > [inheritance, polymophism] can be taught.
>
> This will teach students to write programs the serial way first. In my
> opinion, it would be the better approach to teach basic OO concepts first
> (before even invoking an editor for the first time, maybe using some
> pseudo-language).
>
> Using your approach, students would learn how to write an imperative program
> frist (one big main() method). Then you would introduce a completely new
> paradigm, the structured programming, using functions (other than the main()
> method, but still implementing a single class). Later on, you would
> introduce yet another paradigm, the object-oriented programming implementing
> specialized classes which will focus on certain tasks.
>
> Have you ever tried to teach oo concepts to a - let's say - COBOL programmer
> (nothing to say against COBOL programmer, of course, replace with almost any
> 3GL you (don't) like)? You will have a very hard time unless he (or she)
> will understand them, because the imperative paradigm will block their view
> onto objects. So I would always prefer to teach the object-oriented way
> first. For most people, it will be quite easy to write "one big main()
> method" if they are forced to do it, and the oo paradigm will not stand in
> their way.
>
> > Once these basics have been
> > mastered, the next steps could be taken, which would probably be one, or
> > more, of the following:
> >
> > * Algorithms / Data Structures
>
> Data Structures might well be renamed "Object Oriented Design"... ;-)
>
> > * Concurrent / Advanced Programming
> > * Event-driven / GUI Programming
> >
> > I guess you would describe me as decidedly 'old school' in this regard :)
> !
> >
> > In a nutshell, I'm not comfortable with the notion that GUI's and graphics
> > simplify the teaching of very important, fundamental programming ideas,
> but
> > I probably hold a minority opinion here.
> >
> > Programming, like mathematics, can be an exciting field. However, much
> > effort, most of it in mundane areas, must be expended to satisfactorily
> > grasp the basics on which all further learning progress rests. I can
> > appreciate course designers wanting to make programming more interesting
> or
> > accessable, hence the use of GUI's and similar tools, or presenting ideas
> in
> > graphical form. IMO, though, it is best to avoid such things as they sport
> > hidden complexities, and keep things as simple as possible, at least until
> a
> > firm grounding in the basics has been obtained.
>
> Yes. But one might define "basics" somewhat different. You must not have (I
> might even say "should not have") any knowledge about "serial" or
> "functional" programming to learn to write good OO programs.
>
> (O.k., I might have started another thread on this, but then...)
>
> Cheers,
> Michael
|
| |
|
| |
 |
Anthony Borla

|
Posted: 2003-12-17 21:55:00 |
Top |
java-programmer >> [OT] Teaching OO / Java (was: Concentric Circles Loosing It)
"Michael Voss" <email***@***.com> wrote in message
news:3fd475ea$1@news...
> >
> > "Alan P" <email***@***.com> wrote in message
> > news:br0efp$ola$email***@***.com...
> > >
> > > I'm a newbie Java student as well, and AFAIK we all start
> > > the same way -> by using a 'Turtle' class. Remember those
> > > little robot things from infant school, with the penUp(),
> > > penDown() etc.
> > >
> > > The idea is not stunning effects, but an introduction to
> > > OOPS
Apologies for not having responded earlier [particularly about such an
important issue] - this message somehow remained hidden in my e-mail drafts
folder until now.
<SNIP>
>
> O.k., this is meant to teach the "DE" (leaving out the leading 'I').
> But maybe some people want their students to concentrate on
> the course theme, so they decide to use an IDE. Personally,
> I think one has to know how to deal with a language without
> any external (!) IDE, so you should know about "javac" and
> "java"...
>
I believe programming students, before they can grasp [and meaningfully
apply] abstract ideas like, for example, inheritance and polymophism, should
be knowledgeable of the computer environment, as well as of the practical
aspects of software development. It looks like we both hold similar views
about this.
Given this view, I'd argue that theme-based subjects not be taught until
'the basics' [as previously outlined] have been covered. This is
particularly important, I feel, in effectively teaching programming /
software development.
For example, an approach reliant 'basics first' is the model used to teach
mathematics in schools. Here, 'the basics' are covered in primary / grade
school, and 'themes' [i.e. more abstract areas] are taught in secondary /
high school, if I'm not mistaken ? Themes only make sense once the
groundwork has been covered and mastered. I truly feel this approach is
vital in the teaching of programming.
>
<SNIP>
>
> This will teach students to write programs the serial way
> first. In my opinion, it would be the better approach to
> teach basic OO concepts first (before even invoking an
> editor for the first time, maybe using some pseudo-language).
>
I believe procedural thinking is far more natural or intuitive. Who, for
instance, hasn't followed verbal directions to some destination, or followed
a set of instructions for building a gadget, or for preparing a meal ? I
don't think its difficult to teach imperative / procedural programming
precisely because it follows a thinking model with which we are all,
already, quite familiar.
I think abstract concepts, like OO, have a much better chance of being
understood if they can be linked to more concrete things. For example, the
teaching of imperative / procedural programming would also have introduced
the concept of code resuability - packing code into generic procedures
allows them to be reused in other projects. With students having a solid
grasp of this idea, the concept of inheritance for code resusability becomes
far easier to comprehend, and its benefits more evident.
A key benefit of inheritance is lost on students who have no idea of code
resuability.
>
> Using your approach, students would learn how to write
> an imperative program first (one big main() method). Then
> you would introduce a completely new paradigm, the
> structured programming, using functions (other than the
> main() method, but still implementing a single class). Later
> on, you would introduce yet another paradigm, the
> object-oriented programming implementing specialized
> classes which will focus on certain tasks.
>
I believe this is a perfectly reasonable approach, though, of course, this
wouldn't be attempted within the one course, but would be covered over a few
semesters as separate subjects.
As a first step you would show students how to write 'executable recipes',
that is, a series of steps that accomplishes some larger task [imperative
programming]. The next step might be to show how this could be improved by
combining steps into procedures [structured programming etc]. Further on
from this you could teach how procedures could be combined into larger units
[modules / objects] whereby each procedure acts on the data within the
module / object [object-based programming]. Next up you could extend this to
include the classic OO concepts of inheritance, and polymorphism, thus
taking you into the realm of object-oriented programming.
It is, I feel, a fairly natural progression from imperative programming to
OOP, and I don't think knowledge of non-OOP programming will interfere with
the learning of these ideas. If anything, it should make them clearer -
students now understand two or more ways of accomplishing the same task, and
can be taught why one way is superior to the other given a particular set of
circumstances.
>
> Have you ever tried to teach oo concepts to a - let's say -
> COBOL programmer (nothing to say against COBOL
> programmer, of course, replace with almost any 3GL
> you (don't) like)? You will have a very hard time unless he
> (or she) will understand them, because the imperative
> paradigm will block their view onto objects.
>
I think your'e underestimating the flexibility of the human mind. I'd say a
student or programmer who had the intellectual nouse to master a 3GL would
also be able to set aside preconceived ideas, and try to understand and
apply the new concepts being taught.
I'd agree with your argument in the case of a professional programmer who is
well schooled in imperative / procedural programming [say, a C programmer],
and is being trained in OOP techniques [being taught C++, or possibly Java].
It is only natural to expect such a person to 'fall back' and use tried and
true programming techniques simply because they are more productive using
them.
Also, I should add that OO extensions have been added to many 3GL's
including COBOL. It is possible, though quite cumbersome, to write OO
programs in COBOL ! I should add that the way these have been grafted onto
such a big, complex language as COBOL, and done so in such an unobstrusive
manner, is IMO, nothing short of brilliant.
>
> So I would always prefer to teach the object-oriented way
> first. For most people, it will be quite easy to write "one
> big main() method" if they are forced to do it, and the oo
> paradigm will not stand in their way.
>
Fair enough. I stand by my previous assertions.
>
> Data Structures might well be renamed "Object Oriented
> Design"... ;-)
>
Yes, and no. A stack, for example, is an abstract machine characterised by
certain behaviours, and one which can be implemented in any paradigm. In
other words, the notion or essence of 'stack' trancends paradigms, and it is
this generality that any 'Data Structures' course should be aiming to
convey. Object oriented design is a restricted type of design since it has
to follow OO rules - data structures are language and paradigm independant.
>
> Yes. But one might define "basics" somewhat different. You
> must not have (I might even say "should not have") any
> knowledge about "serial" or "functional" programming to
> learn to write good OO programs.
>
I believe a firm basis in imperative / procedural programming is essential
in order for most of the other paradigms to make sense. As described
earlier, there seems a fairly natural prograssion from imperative /
procedural programming to OOP. Functional programming, also, can be seen as
a restricted sort of imperative programming, so it would seem appropriate to
cover it before this paradigm as well [you can also *more meaningfully*
present the benefits of a functional approach by showing what it does not do
i.e. no side-effect programming, avoiding pass-by-reference etc].
It seems to me that the only programming paradigm that *can* be taught
without first covering imperative / procedural programming is logic
programming. In fact, it would appear that this be taught in parallel to
imperative / procedural programming as teaching either one first actually
impedes students' understanding of the other [e.g. the notion of 'variable'
in Prolog is so totally different to that in C, Java, or even LISP].
>
> (O.k., I might have started another thread on this,
> but then...)
>
That's perfectly fine - I just wish I'd responded sooner
Cheers,
Anthony Borla
|
| |
|
| |
 |
Michael Voss

|
Posted: 2004-1-7 20:07:00 |
Top |
java-programmer >> [OT] Teaching OO / Java (was: Concentric Circles Loosing It)
Hi everybody !
"Anthony Borla" wrote:
[...]
> Apologies for not having responded earlier
Apologies from me, too. I was taking an excessive christmas vacation
(no computers or even internet around :-( ), so I was not able to
read and answer your posting before...
[...]
> For example, an approach reliant 'basics first' is the model used to teach
> mathematics in schools.
[...]
> Themes only make sense once the
> groundwork has been covered and mastered. I truly feel this approach is
> vital in the teaching of programming.
This is true for such things as mathematics, as each topic is somewhat based
on those covered before. But it is not necessarily true for programming
paradigms. I am quite sure you will be able to learn, say, LISP and become a
good LISP programmer without knowing anything about imperative programming
in advance. But if you know an imperative language like BASIC before, you
will have to forget a lot of things before you will truly master LISP. This
does not apply to mathematics at all. Each programming paradigm has it's own
groundwork.
[...]
> I believe procedural thinking is far more natural or intuitive.
[...]
> it follows a thinking model with which we are all,
> already, quite familiar.
Yes. And everybody will try do do things in the way that is most familiar to
him / her. So many people will tend to do it "the imperative way" if they
are
familiar with it even in an oo environment.
> I think abstract concepts, like OO, have a much better chance of being
> understood if they can be linked to more concrete things.
To more concrete things, yes. Give your students an example from their well
known environment to work on in an object oriented way...
> For example, the
> teaching of imperative / procedural programming would also have introduced
> the concept of code resuability - packing code into generic procedures
> allows them to be reused in other projects.
Yes. But it does not necessarily teach them to encapsule data and
functionality
and view at it as a unity and to reuse the whole bundle (which we will
eventually call "object" a few lessons later)...
> With students having a solid
> grasp of this idea, the concept of inheritance for code resusability
becomes
> far easier to comprehend, and its benefits more evident.
There is a big difference between code reusability and object oriented
programming. You may split c code into different files, including one or the
other to reuse those functions in another program. Arranging data and
methods
in classes is comlpetely different, and a lot of people will end up writing
one
or two classes with lots of static functions as an result of learning to
reuse
functions in a procedural manner.
>
> A key benefit of inheritance is lost on students who have no idea of code
> resuability.
>
These are two quite different approaches of code reusability: Reusing
objects or
inheriting from existing object on one hand and reusing existing functions
or
procedures ("code libraries") on the other. I don't think one will
necessarily
have to tell one's students that you can use functions stored in code
libraries
if the students are to learn inheritance. Showing them to reuse functions
(or to
use code libraries) may of course help to make them aware of not having to
write
the same piece of code again and again in each program. But I think it is
independent of the paradigm. You will teach your students to look for
objects
already accomplishing the task they have to instead of rewriting them.
[...]
> It is, I feel, a fairly natural progression from imperative programming to
> OOP, and I don't think knowledge of non-OOP programming will interfere
with
> the learning of these ideas. If anything, it should make them clearer -
> students now understand two or more ways of accomplishing the same task,
and
> can be taught why one way is superior to the other given a particular set
of
> circumstances.
This progression represents a part of the development of programming
languages.
Would you start teaching your students programming in assembler first in
order to
teach them oop? I wouldn't, and I am sure most teachers wouldn't, too. Of
course,
the students must be able to write a method. Of course, it helps to be able
to
write an imperative program to accomplish this task.
[...]
> I think your'e underestimating the flexibility of the human mind. I'd say
a
> student or programmer who had the intellectual nouse to master a 3GL would
> also be able to set aside preconceived ideas, and try to understand and
> apply the new concepts being taught.
This will depend on the time one spent using this concept. So I would try to
present these oo concept as quickly as possible to the students, not giving
them
time to get used to the procedural approach.
>
> I'd agree with your argument in the case of a professional programmer who
is
> well schooled in imperative / procedural programming [say, a C
programmer],
> and is being trained in OOP techniques [being taught C++, or possibly
Java].
> It is only natural to expect such a person to 'fall back' and use tried
and
> true programming techniques simply because they are more productive using
> them.
I caught myself doing this lots of times when I started oo programming and
find
myself doing this now and then even after nearly ten years of pure oo
programming
with smalltalk and java.
[...]
> > Data Structures might well be renamed "Object Oriented
> > Design"... ;-)
> >
>
> Yes, and no. A stack, for example, is an abstract machine characterised by
> certain behaviours, and one which can be implemented in any paradigm. In
> other words, the notion or essence of 'stack' trancends paradigms, and it
is
> this generality that any 'Data Structures' course should be aiming to
> convey. Object oriented design is a restricted type of design since it has
> to follow OO rules - data structures are language and paradigm
independant.
That's completely true.
>
> >
> > Yes. But one might define "basics" somewhat different. You
> > must not have (I might even say "should not have") any
> > knowledge about "serial" or "functional" programming to
> > learn to write good OO programs.
> >
>
> I believe a firm basis in imperative / procedural programming is essential
> in order for most of the other paradigms to make sense. As described
> earlier, there seems a fairly natural prograssion from imperative /
> procedural programming to OOP. Functional programming, also, can be seen
as
> a restricted sort of imperative programming, so it would seem appropriate
to
> cover it before this paradigm as well [you can also *more meaningfully*
> present the benefits of a functional approach by showing what it does not
do
> i.e. no side-effect programming, avoiding pass-by-reference etc].
I see your point. But as stated earlier, in my opinion the time between the
introduction of the oo paradigm should be rather short...
>
> It seems to me that the only programming paradigm that *can* be taught
> without first covering imperative / procedural programming is logic
> programming. In fact, it would appear that this be taught in parallel to
> imperative / procedural programming as teaching either one first actually
> impedes students' understanding of the other [e.g. the notion of
'variable'
> in Prolog is so totally different to that in C, Java, or even LISP].
Yes, that would be a very good idea, but you rarely have the chance do do
this.
[...]
> That's perfectly fine - I just wish I'd responded sooner
[...]
Same applies to me, again.
Sorry for responding late,
Michael
|
| |
|
| |
 |
Michael Voss

|
Posted: 2004-1-12 22:21:00 |
Top |
java-programmer >> [OT] Teaching OO / Java (was: Concentric Circles Loosing It)
"Anthony Borla" <email***@***.com> wrote:
<SNIP>
> Apologies for not having responded earlier [particularly about such an
> important issue] - this message somehow remained hidden in my e-mail
drafts
> folder until now.
Well, I thougth I posted another reply on this nerly two weeks ago, but
since I don't see it in this thread, i'll just post another one. Hope you
don't mind.
<SNIP>
> For example, an approach reliant 'basics first' is the model used to teach
> mathematics in schools. Here, 'the basics' are covered in primary / grade
> school, and 'themes' [i.e. more abstract areas] are taught in secondary /
> high school, if I'm not mistaken ? Themes only make sense once the
> groundwork has been covered and mastered. I truly feel this approach is
> vital in the teaching of programming.
Yes, but then, you will not be able to perform diofferential calculus
without being able to add and subtract properly, whereas I think one may
well write good oo programs without knowing anything about imperative
programming.
<SNIP>
> I believe procedural thinking is far more natural or intuitive. Who, for
> instance, hasn't followed verbal directions to some destination, or
followed
> a set of instructions for building a gadget, or for preparing a meal ? I
> don't think its difficult to teach imperative / procedural programming
> precisely because it follows a thinking model with which we are all,
> already, quite familiar.
I agree. Many people will try to use these familiar patterns of imperative
programming even when using an oo language, as a result.
>
> I think abstract concepts, like OO, have a much better chance of being
> understood if they can be linked to more concrete things. For example, the
> teaching of imperative / procedural programming would also have introduced
> the concept of code resuability - packing code into generic procedures
> allows them to be reused in other projects. With students having a solid
> grasp of this idea, the concept of inheritance for code resusability
becomes
> far easier to comprehend, and its benefits more evident.
Again, I agree. But I think these "concrete things" should rather be good
real-world examples. There is a fundamental difference in code reusability
between procedural and object-oriented programming. The oo way will be
mastered a lot easier if the student will not try to write a function and
reuse it file in another program merely by including it's source file.
>
> A key benefit of inheritance is lost on students who have no idea of code
> resuability.
Of course, students have to know that they won't have to reinvent the wheel
each and every time they write a program.
<SNIP>
>
> I believe this is a perfectly reasonable approach, though, of course, this
> wouldn't be attempted within the one course, but would be covered over a
few
> semesters as separate subjects.
Have you experienced students getting used to the procedural paradigm during
those semesters, thus writing worse oo programs ? Or can they switch between
these paradigms well ?
>
> As a first step you would show students how to write 'executable recipes',
> that is, a series of steps that accomplishes some larger task [imperative
> programming]. The next step might be to show how this could be improved by
> combining steps into procedures [structured programming etc]. Further on
> from this you could teach how procedures could be combined into larger
units
> [modules / objects] whereby each procedure acts on the data within the
> module / object [object-based programming]. Next up you could extend this
to
> include the classic OO concepts of inheritance, and polymorphism, thus
> taking you into the realm of object-oriented programming.
>
> It is, I feel, a fairly natural progression from imperative programming to
> OOP, and I don't think knowledge of non-OOP programming will interfere
with
> the learning of these ideas. If anything, it should make them clearer -
> students now understand two or more ways of accomplishing the same task,
and
> can be taught why one way is superior to the other given a particular set
of
> circumstances.
Yes, but with the danger of falling back to the easier way of writing some
functions and a main program instead of defining proper objects and giving
them the proper methods to perform their tasks.
<SNIP>
> I think your'e underestimating the flexibility of the human mind. I'd say
a
> student or programmer who had the intellectual nouse to master a 3GL would
> also be able to set aside preconceived ideas, and try to understand and
> apply the new concepts being taught.
>
> I'd agree with your argument in the case of a professional programmer who
is
> well schooled in imperative / procedural programming [say, a C
programmer],
> and is being trained in OOP techniques [being taught C++, or possibly
Java].
> It is only natural to expect such a person to 'fall back' and use tried
and
> true programming techniques simply because they are more productive using
> them.
>
> Also, I should add that OO extensions have been added to many 3GL's
> including COBOL. It is possible, though quite cumbersome, to write OO
> programs in COBOL ! I should add that the way these have been grafted onto
> such a big, complex language as COBOL, and done so in such an unobstrusive
> manner, is IMO, nothing short of brilliant.
>
<SNIP>
> >
> > Data Structures might well be renamed "Object Oriented
> > Design"... ;-)
> >
>
> Yes, and no. A stack, for example, is an abstract machine characterised by
> certain behaviours, and one which can be implemented in any paradigm. In
> other words, the notion or essence of 'stack' trancends paradigms, and it
is
> this generality that any 'Data Structures' course should be aiming to
> convey. Object oriented design is a restricted type of design since it has
> to follow OO rules - data structures are language and paradigm
independant.
Of course!
>
> >
> > Yes. But one might define "basics" somewhat different. You
> > must not have (I might even say "should not have") any
> > knowledge about "serial" or "functional" programming to
> > learn to write good OO programs.
> >
>
> I believe a firm basis in imperative / procedural programming is essential
> in order for most of the other paradigms to make sense. As described
> earlier, there seems a fairly natural prograssion from imperative /
> procedural programming to OOP. Functional programming, also, can be seen
as
> a restricted sort of imperative programming, so it would seem appropriate
to
> cover it before this paradigm as well [you can also *more meaningfully*
> present the benefits of a functional approach by showing what it does not
do
> i.e. no side-effect programming, avoiding pass-by-reference etc].
But your students will not have to learn assembler before starting C, won't
they ;-)
>
> It seems to me that the only programming paradigm that *can* be taught
> without first covering imperative / procedural programming is logic
> programming. In fact, it would appear that this be taught in parallel to
> imperative / procedural programming as teaching either one first actually
> impedes students' understanding of the other [e.g. the notion of
'variable'
> in Prolog is so totally different to that in C, Java, or even LISP].
>
<SNIP>
Maybe it could be a good approach to teach all of these three paradigms in
parallel, but this might lead to a total confusion.
Sorry for respondig so late,
Michael
|
| |
|
| |
 |
| |
 |
Index ‹ java-programmer |
- Next
- 1
- Need server-side solution to linking in framesDear all,
I am doing some maintainance in a JSP website which has been built
using frames (and I cannot remove that unfortunately).
So, I have 4 frames (top, and 3 vertical frames underneath the top).
The frame on the left is supposed to be for navigation, but when I
click on some navigation items, I am enforced to set the target to the
own navigation frame, because I need to do some other processing
(which I cannot avoid), and in the end I am using some javascript code
to change the location of the center frame (which in an ideal case
would be the target of the link on the navigation).
Does anyone know a way to avoid the javascript? Is there anyway that I
can load 2 different urls in 2 different frames via a single link? The
first URL is just a different set of parameters to the navigation
menu, and the second URL is the target of the navigation.
Best regards,
Pablo
- 2
- Need help in validating the period in an email address. (new Java student)I am a new Java student and would appreciate a little help.
What I need is to be able to validate the period as the fourth from
the last character in an email address and return focus if the address
is invalid. The best I have been able to do is a good address on the
first attempt. If an incorrect address is entered first I am unable to
get it to accept a good address from then on. The code below is what I
have left after removing everything that did not work.
Is there anyone here that can provide some assistance. Thanks
// validate the e-mail
var RegeMail=document.Register.eMail.value
var atSign = RegeMail.indexOf("@")
if (RegeMail == " " || atSign == -1) {
alert("Please enter your e-mail address")
document.Register.eMail.value = " "
document.Register.eMail.focus()
}
- 3
- Text/Console mode UIHi All,
is there anything like swing but working in text mode? I need a simple
user interface working in the console mode for my little app.
thanks
schw
- 4
- 5
- XMLencoder customHey all,
I'm using XML encoder for long term storage. However, I'd like to be
able to customize what is saved at runtime. Why? There are several
data structures, which can balloon the size of the file to a few
megabytes. This data is not something every user might want to save
on load. I'd like to give them the option of whether or not to encode
these particular data structures. The rest of the code is perfectly
capable to dealing with these structures set to null. They often are
null.
I was thinking the easiest way for me to do this would be as follows:
I know this is not XMLEncoder code (but java serialization type
code...but that's what I need help with :) ) I have a feeling this is
going into the realm of persistance delegates, but I really have no
idea.
private Hashtable details= null;
private void writeObject(ObjectOutputStream s) throws IOException
{
if( dontSaveDetails)
{
Hashtable detailsBackup = details;
//clear the details, so they're not saved
setDetails(new Hashtable());
s.defaultWriteObject();
//restore details for the current session
details = bckdetails;
}
else
{
s.defaultWriteObject();
}
}
Any ideas on how to put this logic into the XMLencoder world?
Thanks,
Ymain
- 6
- What it the type of the Class without modifierRoedy Green <email***@***.com> wrote:
> This is one of my pet peeves . It does not have a name. It is
> referred to with religious reverence by an indirect name "default"
> which tells you where in is used. Some heretics dare name it
> "package".
Hey, are you calling me a heretic?!? :)
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
- 7
- Raymond on Sun's strangleholdanoncoward wrote:
> JTK wrote:
>
>>> This made me think of an issue that has kicked around in my head for a
>>> whle. Just to be clear, IANAL. However, I seem to remember that one
>>> requirement of a patent be that the needed to be non-obvious to a
>>> skilled practitioner. Does someone know if this is correct?
>>
>>
>> Correct but irrelevant. Money talks, justice walks. If Sun wants to
>> shut down a Java implementation, all they need to do is litigate it to
>> death.
>
> How about Mono and .NET. Microsoft has patented the whole lot.
> Mono is *massively* infringing on copyrighted and patented Microsoft IP.
> What's your word on that?
Why would I have a "word on that", especially in a Java forum? You're
the one who love C# so much, you tell us why you prefer it to Java!
- 8
- Maximum number of ThreadsI'm developing a simple Java client that runs over a CORBA server.
The main client thread is waiting for notification from this server.
On each notification,
The client creates a new thread executing some logic with the server and close.
Each session spend most of the time waiting for the server.
At most there can be ~1000 open session.
My questions are:
1. How many threads can be open at the same time
(with response time of up to ~1 second)?
2. Does the JVM open a native thread for each java thread?
I know both question is OS and JVM dependent,
so I'm not expecting exact numbers here. The client will probably run on Solaris or NT.
Thanks.
- 9
- Changing the Background Color for One Cell in a JTableI've been working on this for a few hours. I've read tutorials and seen
different examples, but this is still driving me nuts.
I have a table and want to be able to change the color of one cell in it.
It seems to do this, I can't get a cell renderer and just change the color,
I have to create my own class to do that. I've tried that and a number of
other things and can't get it to work.
I finally took the SimpleTableDemo from Sun and checked to make sure it was
running. Once it was, I added four lines:
boolean isSelected = table.isCellSelected(1, 1);
DefaultTableCellRenderer defRender = (DefaultTableCellRenderer)
table.getCellRenderer(1, 1);
Component cellRenderer = defRender.getTableCellRendererComponent(table,
"Huml", isSelected, false, 1, 1);
cellRenderer.setBackground(Color.blue);
(The whole example is at the bottom of this post.)
When I use this example, it turns ALL the cells in the table blue, not just
the one at 1,1.
I've got several questions about this:
1) Do I have to create my own cell renderer to change a cell background
color, or can't I just do it like above? Even when I used my own renderer,
I still got the actual Component named cellRenderer using the same
parameters, so what difference would doing it in a separate class make?
2) The renderer needs the value that is in the cell currently. In my own
program, I got that from the DefaultTableModel I was using. Here I just
used the text that was put in that cell. Is there any way to get the cell
value directly from the table? In this example, I tried getting a
DefaultTableModel (by casting) from the table, but it didn't work, so I
couldn't get the data by reading the cell.
3) Why does this code (above) set the entire table and not just the cell at
1,1?
If anyone has a simpler example of how to set the background color in just a
few lines, I'd be glad to see it. I can't believe how much I need to do
just to change the background color in one table cell!
Thanks for any help/insight!
Hal
----------------example code----------------
/*
* SimpleTableDemo.java requires no other files.
*/
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
import com.hal.gui.HalCellRenderer;
public class SimpleTableDemo extends JPanel {
private boolean DEBUG = false;
public SimpleTableDemo() {
super(new GridLayout(1,0));
String[] columnNames = {"First Name",
"Last Name",
"Sport",
"# of Years",
"Vegetarian"};
Object[][] data = {
{"Mary", "Campione",
"Snowboarding", new Integer(5), new Boolean(false)},
{"Alison", "Huml",
"Rowing", new Integer(3), new Boolean(true)},
{"Kathy", "Walrath",
"Knitting", new Integer(2), new Boolean(false)},
{"Sharon", "Zakhour",
"Speed reading", new Integer(20), new Boolean(true)},
{"Philip", "Milne",
"Pool", new Integer(10), new Boolean(false)}
};
final JTable table = new JTable(data, columnNames);
table.setPreferredScrollableViewportSize(new Dimension(500, 70));
// table.setFillsViewportHeight(true);
if (DEBUG) {
table.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
printDebugData(table);
}
});
}
//Create the scroll pane and add the table to it.
JScrollPane scrollPane = new JScrollPane(table);
//Add the scroll pane to this panel.
add(scrollPane);
//======================================================
//Only part I have added is the next four lines
//======================================================
boolean isSelected = table.isCellSelected(1, 1);
DefaultTableCellRenderer defRender = (DefaultTableCellRenderer)
table.getCellRenderer(1, 1);
Component cellRenderer =
defRender.getTableCellRendererComponent(table, "Huml", isSelected, false,
1, 1);
cellRenderer.setBackground(Color.blue);
}
private void printDebugData(JTable table) {
int numRows = table.getRowCount();
int numCols = table.getColumnCount();
javax.swing.table.TableModel model = table.getModel();
System.out.println("Value of data: ");
for (int i=0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j=0; j < numCols; j++) {
System.out.print(" " + model.getValueAt(i, j));
}
System.out.println();
}
System.out.println("--------------------------");
}
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("SimpleTableDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
SimpleTableDemo newContentPane = new SimpleTableDemo();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
- 10
- Writing ASCII for r-232 output into induction machine??I have a machine that uses induction to bond metals together. The
machine has a remote heat station that you set the wattage, and the
time, etc (with up and down buttons) and it will run on its own.
However, every time I need to bond different metals, thicknesses,
widths, etc... I need to reset the info and go through the whole
process again. The machine, has an rs-232 port on the back of it,
which will accept codes written in ascii that will send that pertinent
information to it. I would like some help (Im an ascii newbie) with
writing these programs, so that I can store all the metals,
thicknesses, etc in a laptop connected to this remote heat station, and
then I can have anyone run it so long as they can read.
Any help would be greatly appreciated
Scott
PLEASE email me at email***@***.com
- 11
- I only invest in Jeff Relf.Hi Hot Tamales !,
You related another gem ( What a guy ),
" Internet and technology stocks
have netted me a loss of approximately 20%.
FCEL, my one energy investment,
has netted me a gain of 20%.
I think Energy
( cells, hydrogen, renewable, even nuclear )
are the next wave. "
The value of FCEL didn't go up,
the value of the dollar,
( i.e. the stock of the U.S. government ), went down.
I only invest in Jeff Relf, not that I'm doing any better.
- 12
- Classes generated by eclipse vs javac have different serialVersionUIDHi,
I'm using eclipse 3.2 for development and have configured my project to
use JDK 1.5.0_07 with Compiler compliance level 1.3 (using default
compliance settings) and selected all "Classfile Generation" options
except "Inline finally blocks." The problem I'm having is that the
class files that are generated by eclipse are not the same as those
generated by javac (also using JDK 1.5.0_07), which is used in a make
script. javac is invoked like so:
javac -source 1.3 -g -d classes -nowarn -deprecation -classpath
<classpath> ...
The different class files cause a problem because I have serialized
some objects already with the javac classes so when I try to
deserialize them with the eclipse classes, I get the
InvalidClassException. When I run serialver on the offending class
file, I get two different values for the class file generated with
javac and the one generated using eclipse.
Has anyone ever had this or some similar problem? How do I correct this
(besides not using the eclipse class files)? I've spent many hours
scouring the web to find a solution, but have had no success. Any help
would be really appreciated!
Thanks,
Tai
- 13
- Paramaters in EL?Hi All!
Is it possible to do this:
<%String deptName = (String)request.getParameter("deptName");
Department dept = org.getDepartment(deptName); %>
With just c:set or jsp:useBean tags?
Rob
:)
- 14
- Long texts in JTable cellsMy application displays a table having several columns in a JTable.
(the GUI component is bound to a view iterator in Oracle ADF, if
anyone is familiar with that framework.) The problem:
One of the columns contains long text, including line breaks, which
is shown as a very long single line in the table cell. How can I
improve on this? (I guess I want something like an editable text-area,
except it should be part of the table).
Thanks for a hint, Sebastian
- 15
- Display XML Formatted in JTextPaneI'm displaying XML in a JTextPane. I was wondering if there was a way
to display the XML formatted with color/text/etc. (Basically, I'm
wondering if I can associated an XML style.) I've searched a bit, but
I haven't been able to get anything to work.
Thanks
|
|
|