This week's book giveaway is in the Spring forum.
We're giving away four copies of Java Persistence with Spring Data and Hibernate and have Cătălin Tudose on-line!
See this thread for details.
Win a copy of Java Persistence with Spring Data and Hibernate this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

class 1

 
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class 1
Before this class, you should have read chapters 1 and 4 in "Just Java 1.2".
Before the next class, read chapter 7.

Assignment 1: Write a program that will read in a name from the command line and write it out 100 times.
In other words, I want to type

java Hundred Gertrude
and see
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude
Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude Gertrude

Formatting is not necessary, but extra credit if you can make it so words are never split up.


Assignment 2: Write a program that will read in a number from the command line and tell me if it is even or odd.
In other words, I want to type

java EvenOrOdd 28
and see
even

 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How do you want the homework posted? DO you want us just to post the source code or email you the .class file?
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
E-mail me the .java file.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help. I'm having trouble getting started. I have a general idea of what I need to do, but am lacking in background -- I don't have programming experience. Is there a reference you can suggest that would give me some very basic information?
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The best thing to do is to start off with a "hello world" program.
Do you know how to use a text editor?
Have you downloaded the JDK?
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I'll give that a shot. Yes on the text editor and I downloaded the JDK 1.2.
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first big leap is to get a hello world program working. I think the Just Java book spells it out pretty clearly. But if you need more help, just say so.
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I'm working on a "hello world" program as you suggested. I attempted a modified version of the example in Just Java and am running into some trouble that I think might be unrelated to my bad coding.
I was able to run the compiler and see all the mistakes I'd created. When I'd taken care of what I thought was the final error, I started to get MORE error messages. I decided to take a look at some of the example programs and was unable to run or compile them. Now,when I first got the book, I successfully compiled the "myframe" program and it ran without a hitch. I ran some of the other example programs as well. This evening I discovered that neither "myframe" or any of the other example programs run -- I get either one of two error messages:
"Can't read: [filename].java" OR
"Exception in thread "main" java.lang.NoClassDefFoundError: [filename}/java"
I uninstalled and reinstalled the JDK, which had no effect. Do you have any ideas about what might be going on here?

[This message has been edited by Shawn Mountcastle (edited February 28, 1999).]
 
Tim Uckun
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I run into this problem too. I if I rename the class to all lower case it works fine. If maybe that both the filename and the class name has to be the same case. Try this and see if it works. I realize that in the m$ worls case sensitivity does not matter but in the Java world it does.
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, you shouldn't be monkeying with frames right now. This program should work fine:
<pre>
class HelloWorld
{
static void main( String args[] )
{
System.out.println("hello world!");
}
}
</pre>
I just now highlighted the code in Netscape and did a control-c to copy it to the windows clipboard. Then I loaded up my text editor and pasted. I saved the file as HelloWorld.java (case is very important here). I then typed
javac HelloWorld.java
and it compiled without a hitch. Then I typed
java HelloWorld
and the program ran without a hitch.
Let's start here and see what problems you come up with.

 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm getting "error: Can't read: HelloWorld.java" error when I try to compile. Just to be sure I'm doing this part right: I copied the coding, pasted it into WordPad and saved it as text. Then I typed "javac HelloWorld.java" from the command line.
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the file is definitely called HelloWorld.java (exact same case)?
What does your classpath look like?
 
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I've had this exact same problem before and it drove me nuts!

Solution (for my problem was): One wierd thing about WordPad (on Windows98, not NT) is that although you save your file as a .txt file (HelloWorld.java), it actually saves it as "HelloWorld.java.txt"!
Try going to the DOS prompt, go to where you saved the file and type DIR. If the name is "HelloWorld.java.txt" you'll have to change it manually at the DOS prompt.
Try: MOVE HelloWorld.java.txt HelloWorld.java
Windows98 WordPad is bizzare this way, at least on my machine at home. Hope this helps.
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file is properly named (exact matches in case).
The classpath -- is this the path set up in the autoexec.bat file? If so, it's c:\JDK1.2\BIN
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's an interesting wrinkle about WordPad and Windows 98. I'm using Win95, in which WordPad seems to write the file properly -- without adding the .txt. (Thanks for giving me another reason to not move to Win98.)
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, the question about the classpath got me thinking. I tried compiling the file from within the jdk directory and it works. What am I missing?
 
Matt Midcap
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmmmm....
In your autoexec.bat try:
<PRE>
SET PATH=C:\JDK1.2\BIN;

SET CLASSPATH=C:\JDK1.2\LIB;.;
</PRE>
The "LIB;.;" is needed as well. Thats what I have in mine.
btw: not only does WordPad put the ".txt" behind your document, so does Notepad! > Thanks again to Uncle Bill.
[This message has been edited by Matt Midcap (edited March 01, 1999).]
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm thinking that the "dot" in the classpath will fix everything. Although I thought that later versions of the JDK didn't have this prob. Wacky.
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried adding the CLASSPATH to autoexec.bat, but am still running into the same trouble. There must be some other funky thing going on... Since I seem to be able to run and compile from the JDK1.2 directory, I guess I'll not worry too much about it for now. If anything occurs to anyone, let me know, I'd like to unravel this puzzle at some point!
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When it doesn't work, check the classpath. Is the dot there?
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The dot is there. What does it do, by the way?
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so I'm finally on the homework again. I think my first problem is that I have no idea what method to call. What command performs what action?
On the Hundred assignment, I know that I have to read the name from the command line input, then output that name times 100.
1) How do I capture the keyboard input?
2) Can you take character input and perform a mathematical operation on it and repeat those exact characters a predefined number of times?
3) Am I on the right track?
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The dot: References the current directory.
So when you experience the problem, you examine your environment and the dot is there. (forget about autoexec.bat for now)
Is the environment variable "classpath" or "CLASSPATH"?
Homework questions:
1) the input comes from the command line, so you'll get it via "args[0]".
2) don't worry about characters. Focus on strings for now.
3) nope. One of the programming pearls I live by is "Thompson's rule for first time telescope makers: It is faster to make a four-inch mirror then a six-inch mirror than to make a six-inch mirror." In other words, myabe you should first write a program that displays whatever is passed over the command line just once.

 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's CLASSPATH
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try using notepad instead of wordpad.
Okay, let's start a new thread about how this isn't working and give us all of the details again. I think it is very important that you be able to use the JDK from your own work directory.
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question on 1st homework assignment problem:
Can I read in the name from the command line and simply multiply the result by an integer?
Question on 2nd problem:
Can I compare the last digit of the number input at the command line and say if its 0, 2, 4, 6, or 8 return "EVEN" else return "ODD"?
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Originally, I sent in this program which is supposed to read in a word input at the command line:
class readSome
{
static void main ( String args[] )
{
String name = args[0];
System.out.println ( name );
}
}
I made the suggested changes:
"public class ReadSome" and "public static main( String args[] )" so the program looks like this:
public class readSome
{
public static void main ( String args[] )
{
String name = args[0];
System.out.println ( name );
}
}
but when I compiled, I got this error:
"Invalid method declaration; return type required.
public static main (String args [])" with a carat below the "m" in main.
What's wrong here, and what do I need to do to fix it?

 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"Question on 1st homework assignment problem: Can I read in the name from the command line and simply multiply the result by an integer?"
You will want to read the name in from the command line and then write it out 100 times. Probably set up a "for" loop.
"Question on 2nd problem: Can I compare the last digit of the number input at the command line and say if its 0, 2, 4, 6, or 8 return "EVEN" else return "ODD"? "
That would be one way of doing it. There is an easier way if you use the Integer class to conver the number to an int and then use the modulo operator.

 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have "public static main"
it wants "public static void main"

 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm still having trouble getting this first assignment. What I think I need is some rudimentary vocabulary--some nouns, verbs, objects, their definitions, proper usage, that kind of thing. Does such a resource exist? If so, what is it? If not, is there something that comes close?
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you mean assignment 1?
You will need to access the "args[]" array and you will need to use a for loop. You will also need to use System.out.print();
Does this help?
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Umm, it does help in the sense that it narrows the field some. However, I'm not quite there yet. I guess I still don't know how to access an array. I don't know appropriate usages in their contexts. Likewise, I don't know what a for loop is supposed to look like. I've read the reference in Just Java (p.177) and tried to use it, but I'm randomly experimenting -- no method to the madness, you might say. That's why I keep asking for definitions, vocabulary, etc. Any suggestions as to how I might generally proceed?
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try this. All applications must have at least this shell:
<pre>
public class Whatever
{
public static void main( String[] args )
{
}
}
</pre>
args is an array of strings. args.length will tell you how many Strings there are in the array. Maybe zero. Maybe one. Maybe two. etc. Suppose there is one. To get that string, you would ask for args[0].
Put this program in:
<pre>
public class Echo
{
public static void main( String[] args )
{
System.out.println( args[0] );
}
}
</pre>
Compile it and then type
<pre>
java Echo Spoon!
</pre>
And the program will write "Spoon!" on the screen. But watch out! If you pass nothing, it will give you an exception cuz I'm trying to access the first element in the array and in that case there isn't one!
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay. So I know this is an array, not a string because of "String[] args" as opposed to "String args[]"?
What is the nature of the "for" statement?
 
Matt Midcap
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Shawn,
I think I've found the "plain english" documentation that you are looking for. It is from an author of a Java book and he is a professor at a University teaching Java.
I was refered to his on-line class notes from a friend and it has helped me in a big way. I just got done e-mailing him to see if I can put a link to his class notes on this site. I hope his responce is favorable as he has a real flair for describing programming concepts. If not I will e-mail you the notes that I have already down loaded for tutoring myself.
Hang in there and I'll let everyone know as soon as I get a reply!
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"String[] args" is exactly the same thing as "String args[]". Each is "an array of String objects."
A standalone string would look like this "String arg".
The nature of the for loop is that you want to perform a certain activity a fixed number of times. Pseudocode is
for( initialize ; test for being done ; stepper )
a typical for loop in Java would look like this:
<pre>
int i ;
for( i = 0 ; i < 10 ; i++ )
{
// code goes here
}
</pre>
This works exactly the same as
<pre>
int i = 0 ;
while( i < 10 )
{
// code goes here
i++ ;
}
</pre>

[This message has been edited by Paul Wheaton (edited March 18, 1999).]
 
Matt Midcap
Sheriff
Posts: 440
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
O.K. everyone, Mr. Elliotte Rusty Harold said it was o.k. so go to:
http://metalab.unc.edu/javafaq/course/
His class notes are very helpfull for anyone trying to learn Java and is a great resource to this class.
Shawn, please let me know if this is what you were looking for. Good luck!
Special thanks to Mr. Harold below:
+-----------------------+------------------------+-------------------+
| Elliotte Rusty Harold | elharo@metalab.unc.edu | Writer/Programmer |
+-----------------------+------------------------+-------------------+
| XML: Extensible Markup Language (IDG Books 1998) |
| http://www.amazon.com/exec/obidos/ISBN=0764531999/cafeaulaitA/ |
+----------------------------------+---------------------------------+
| Read Cafe au Lait for Java News: http://sunsite.unc.edu/javafaq/ |
| Read Cafe con Leche for XML News: http://sunsite.unc.edu/xml/ |
+----------------------------------+---------------------------------+
[This message has been edited by Matt Midcap (edited March 19, 1999).]
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matt, yeah, Mr Harold's notes are, in part what I've been looking for. The other part would consist of descriptions or discussions of programming concepts that a beginner (like me) would understand. A lot of explanation seems to get done in terms of other programming languages. Maybe that's the nature of the beast.
 
Shawn Mountcastle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What's a modulo?
 
paul wheaton
Trailboss
Posts: 23644
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
modulo is like "remainder after division".
So 20 modulo 5 is 0 because 20 divided by 5 is 4 with no remainder.
21 modulo 5 is 1
22 modulo 5 is 2
23 modulo 5 is 3
24 modulo 5 is 4
25 modulo 5 is 0
In C, C++ and Java, modulo is represented as the percent sign.
So
int a = 20 % 5 ;
sets a to be zero.
 
Mo-om! You're embarassing me! Can you just read a tiny ad like a normal person?
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic