newbie to java  
Author Message
rahul8143





PostPosted: 2005-9-23 21:04:00 Top

java-programmer, newbie to java hello,
In following code there is a abstract class so i am not allowed to
create instance of class.
so Figure f = new Figure(10, 10); is wrong but why following statement
is valid?
Figure figref;

CLASS CODE =>
abstract class Figure {
double dim1;
double dim2;

Figure(double a, double b) {
dim1 = a;
dim2 = b;
}
abstract double area();
}

 
Aki Laukkanen





PostPosted: 2005-9-23 21:26:00 Top

java-programmer >> newbie to java email***@***.com wrote:

> In following code there is a abstract class so i am not allowed to
> create instance of class.
> so Figure f = new Figure(10, 10); is wrong but why following statement
> is valid?
> Figure figref;

This does not create an instance of Figure. It just defines figref as a
variable of type Figure.

P.S. "newbie to java" is a poor (albeit much used) choice for "subject".
Please try to put a definition of your problem as the subject in the
future. That way, it's easier to see what kind of a problem the post is
about.

--
-Aki "Sus" Laukkanen
 
Roedy Green





PostPosted: 2005-9-24 8:29:00 Top

java-programmer >> newbie to java On 23 Sep 2005 06:04:14 -0700, email***@***.com wrote or quoted :

>so Figure f = new Figure(10, 10); is wrong but why following statement
>is valid?
>Figure figref;
because Figure figref points to a Figure (impossible) or ANY SUBCLASS
of Figure (quite possible).

When you use Figure figref instead of Triangle figref ( presuming
Triangle extends Figure), then you are limited to Figure methods with
figref. You can't accidentally use a method unique to Triangle. This
means your code will still work if you later point figref to a
Rectangle instead.


--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
Roedy Green





PostPosted: 2005-9-24 8:31:00 Top

java-programmer >> newbie to java On Fri, 23 Sep 2005 16:25:30 +0300, Aki Laukkanen
<email***@***.com> wrote or quoted :

>P.S. "newbie to java" is a poor (albeit much used) choice for "subject".
>Please try to put a definition of your problem as the subject in the
>future. That way, it's easier to see what kind of a problem the post is
>about.

Every post here could be accurately so labelled. So it does not give
much discrimination.

You might ideally have said something like, "Why aren't abstract
references illegal?"
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
 
 
Jack





PostPosted: 2006-5-19 12:52:00 Top

java-programmer >> newbie to java import java.lang.Thread;

public class HelloWorld extends Thread {

public void run()
{
System.out.println("Hello World");
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Thread t = new HelloWorld();
CharacterBuffer cb;
cb.addChar('c'); <<<<<<<<<<<<<<< error
t.start();
}

}

=============================


public class CharacterBuffer
{
private byte[] data_ = null;
private int len_ = 0;

synchronized public void addChar(byte c)
{
if (data_ == null || len_ == data_.length)
{
byte[] newData = new byte[len_+128];
if (data_ != null)
System.arraycopy(data_, 0, newData,0, len_);
data_ = newData;
}
data_[len_++] = c;
}
synchronized public void writeBuffer()
{
System.out.write (data_, 0, len_);
System.out.flush();
len_ = 0;
}
}
======================================
besides I'd like to make the thread print "abc123", how do I make it
work like that?
Thanks
Jack

 
 
Tobias Schr鰁r





PostPosted: 2006-5-19 15:43:00 Top

java-programmer >> newbie to java Jack schrieb:
> import java.lang.Thread;
>
> public class HelloWorld extends Thread {
>
> public void run()
> {
> System.out.println("Hello World");
> }
> /**
> * @param args
> */
> public static void main(String[] args) {
> // TODO Auto-generated method stub
> Thread t = new HelloWorld();
> CharacterBuffer cb;
^^^^^^^^^^^^^^^^^^^^
You have to assign a value to cb, like "new
CharacterBuffer()"

> cb.addChar('c'); <<<<<<<<<<<<<<< error
> t.start();
> }
>
> }
>
> =============================
>
>
> public class CharacterBuffer
> {
> private byte[] data_ = null;
> private int len_ = 0;
>
> synchronized public void addChar(byte c)
> {
> if (data_ == null || len_ == data_.length)
> {
> byte[] newData = new byte[len_+128];
> if (data_ != null)
> System.arraycopy(data_, 0, newData,0, len_);
> data_ = newData;
> }
> data_[len_++] = c;
> }
> synchronized public void writeBuffer()
> {
> System.out.write (data_, 0, len_);
> System.out.flush();
> len_ = 0;
> }
> }
> ======================================
> besides I'd like to make the thread print "abc123", how do I make it
> work like that?

If you want the thread to print it, you should use the CharacterBuffer
in its run() method.

Hint:
To print "123abc", look at the CharacterBuffer class and find out was it
does. It has two methods that cover your demands.

> Thanks
> Jack
>

Tobi
 
 
Jack





PostPosted: 2006-5-19 17:11:00 Top

java-programmer >> newbie to java Thanks for replying.
I have added the line according to your comments.
But byte and char is not interchangable in java.
In C++, I can cast to from byte to char
How do I do the same in Java?
Thank you once more
Jack

 
 
Oliver Wong





PostPosted: 2006-5-19 23:39:00 Top

java-programmer >> newbie to java
"Jack" <email***@***.com> wrote in message
news:email***@***.com...
> Thanks for replying.
> I have added the line according to your comments.
> But byte and char is not interchangable in java.
> In C++, I can cast to from byte to char
> How do I do the same in Java?

Are you sure you're trying to cast a byte and not, for example, a byte
array?

I'm just guessing here. Hard to say without seeing what your code looks
like.

- Oliver

 
 
Jack





PostPosted: 2006-5-22 15:14:00 Top

java-programmer >> newbie to java Hi Oliver,
I just want to cast from a byte to a char...
I often used C++, so I did not know how to do the same in Java
Thanks
Jack

 
 
Bjorn Abelli





PostPosted: 2006-5-22 15:49:00 Top

java-programmer >> newbie to java
"Jack" wrote...

> But byte and char is not interchangable in java.

Sure they are, just like int and long are "interchangeable", but only on a
primitive level...

> In C++, I can cast to from byte to char
> How do I do the same in Java?

To do a simple "cast", you do it as in C++, e.g.:

byte b = 99;
char c = (char) b;

But when looking at your code, I believe your problem was the other way
around, from char to byte, which is superficially equally simple:

char c = 'c';
byte b = (byte) c;

But you need to be careful here, and not believe that a cast is sufficient
to do a correct conversion of characters, but then again, that depends on
how you plan to use it.

Characters in Java uses Unicode, which means that in some cases you'll get
unexpected results. That's why some give comments on different encodings,
etc, when talking about conversions between bytes and chars.

For the immediate problem you have, I'd guess you can use a simple cast:

CharacterBuffer cb = new CharacterBuffer();
cb.addChar((byte)'c');

But, as described above, your "CharacterBuffer" also has a misleading name,
as it's not a buffer of characters, but a buffer of bytes.

/// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
 
 
Thomas Weidenfeller





PostPosted: 2006-5-22 16:39:00 Top

java-programmer >> newbie to java Jack wrote:
> I just want to cast from a byte to a char...

You don't do that in Java.

> I often used C++, so I did not know how to do the same in Java

And Java is not C++. You have to get your head around it. Things in Java
are done differently than in C++. If you try to do things the same way
you are just ending up with more and ugly code.

Your CharacterBuffer class is fundamentally flawed because it tries to
use bytes to buffer chars.

It is also flawed, because there are several standard library classes
which might already do what you want: StringBuffer (in 1.4 and 1.5) and
StringBuilder (in 1.5) just assemble characters. And things like
BufferedWriter do buffer characters for output, and BufferedOutputStream
do buffer bytes for output.

In general, one typically designs a Java application in a way that chars
and bytes are treated separately and that they are not mixed. This
starts right from the beginning when e.g. reading some data. You have,
and you should use, different I/O classes for reading text (...Reader)
and for reading binary data (...InputStream). And you should use
different ones for writing chars (...Writer) and binary data
(...OutputStream).

In the event that you indeed need to convert chars to bytes and vice
versa, you are supposed to implicitly or explicitly use a charset
encoding/decoding. You do not just cast.

--
The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/
 
 
Roedy Green





PostPosted: 2006-5-24 2:47:00 Top

java-programmer >> newbie to java On 22 May 2006 00:13:45 -0700, "Jack" <email***@***.com>
wrote, quoted or indirectly quoted someone who said :

>I just want to cast from a byte to a char...

for all you conversion needs (some of which are done with casts in a
very inconsistent way) see http://mindprod.com/applets/converter.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
 
Dom





PostPosted: 2006-11-22 23:10:00 Top

java-programmer >> newbie to java Completely new to Java. Bought my first book on the subject (one of
the Dummies books) just last week. I always think that having a
newsgroup is a good first step. Some questions:

1. Can I post questions on NetBeans here?
2. If no, where?
3. If yes, then why does NB not see my projects as ProjectFolders,
just as usual folders, so I can never reload them?

TIA,
Dom

 
 
Rhino





PostPosted: 2006-11-23 0:36:00 Top

java-programmer >> newbie to java
"Dom" <email***@***.com> wrote in message
news:email***@***.com...
> Completely new to Java. Bought my first book on the subject (one of
> the Dummies books) just last week. I always think that having a
> newsgroup is a good first step. Some questions:
>
> 1. Can I post questions on NetBeans here?

Yes.

> 2. If no, where?

Here is fine. Lots of people post NetBeans questions here.

> 3. If yes, then why does NB not see my projects as ProjectFolders,
> just as usual folders, so I can never reload them?
>
I don't know; I'm not a NetBeans user. Check back again; someone who is will
likely answer you shortly.


 
 
Oliver Wong





PostPosted: 2006-11-23 2:33:00 Top

java-programmer >> newbie to java
"Dom" <email***@***.com> wrote in message
news:email***@***.com...
> Completely new to Java. Bought my first book on the subject (one of
> the Dummies books) just last week. I always think that having a
> newsgroup is a good first step. Some questions:
>
> 1. Can I post questions on NetBeans here?
> 2. If no, where?
> 3. If yes, then why does NB not see my projects as ProjectFolders,
> just as usual folders, so I can never reload them?

Here's a tip. Next time, if your question is specific to NetBeans,
mention NetBeans somewhere in the subject.

- Oliver