Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Brand New to Java - Community College Student  RSS feed

 
Ryan Komons
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey all. New to the forums, New to Java. Just a little background, I'm taking two classes this semester in programming. A Java basics class and a Mobile Programming class which has a prerequisite of the Java Basics class, but the allowed me to take both simultaneously. I don't have my first Java Basics class until later this week, but I'm trying to do the Mobile Programming homework, and I realize now I'm in a world of trouble. The book doesn't offer ANY instructions on how to program and assumes you know some basics (hence the prerequisite).

I think that once I get rolling in the basics class, I'll be able to use what I learn that week to do my assignments in the Mobile Programming class. But for now, I'm completely stumped. I've set up with NetBeans and all that happy horseshit. Took me a few hours of research and trial of other programs before I came across it. So far it works, I wrote my exercise one program. Write a program that displays the numbers 1-10 onscreen. While this may seem easy to many of you, it required a lot of reading and work for someone with zero programming experience:


So I'm on the second assignment I need to do for the week and I'm completely stumped. I've tried searching and reading in both my text books and have no idea. Looking for some guidance and hopefully some education on this stuff so maybe I can get through this class.

Program I need to write is this: Write a program that uses a loop to display the first 10 positive, even integers onscreen.

I know this probably seems like the easiest thing in the world for most people, I'm just hoping someone can provide me with some knowledge on how to write this, but more importantly an explanation of the code and how it works.

Regards

TravisTehChimp
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm also a java student, but I'll try to help. So first, there's three types of loops. Do-while loop, while loop, and for loop. Personally, I prefer for-loops. The set up explained in my book is this:



If you're going to do more than one statement you need to use brackets.



initialization is setting up a value
test is a boolean seeing if something is true or false to determine if the loop should keep going or not
updateyou can use to increment/decrement a value and make the loop stop

ex.


So that will print out the word "Hello" each time the loop runs and then the value of x which I originally initialized to 1 is increased by 1 after each loop. So once the statement in the middle x < 4 is no longer true, the loop will stop running.

To check if a number is even or odd, you can use a modulus operator % which returns a remainder. Like 5 / 2 = 2.5 when you use modulus 5 % 2 = 0.5 so to check if a number is even when you use modulus it should be equal to 0. To check equality in Java you use two equal signs == because one equal sign = is an assignment operator. Usually its used to assign a value to a variable.
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nicole Anon wrote:. . . operator % which returns a remainder. Like 5 / 2 = 2.5 when you use modulus 5 % 2 = 0.5 . . .
That ain't right. The remainder when you divide whole number is always a whole number.
 
Ryan Komons
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the quick replies. I should point out that I have taken Programming with Alice (which was kind of a joke)

Anyone familiar will know that you don't physically do any programming as it's a VERY basic drag and drop.

I did however learn terms and functions in program, such as Loops.

I'm just at a loss for how to create them in Java. But I will start with the above post. definitely very helpful.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Nicole Anon wrote:. . . operator % which returns a remainder. Like 5 / 2 = 2.5 when you use modulus 5 % 2 = 0.5 . . .
That ain't right. The remainder when you divide whole number is always a whole number.


You're right. Sorry about that. 5 mod 2 would be 1 just like 16 mod 6 would be 4. I goofed on that one, sorry.
 
Nikki Smith
Ranch Hand
Posts: 65
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Komons wrote:Thanks for the quick replies. I should point out that I have taken Programming with Alice (which was kind of a joke).


I had go through that same course. It.....kinda helped me to understand the concept of classes and objects and things like that. At first I thought it was a waste, and in regards to actual coding it kinda is, but it helped me to make more sense of things once I started in Java.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nikki Smith wrote: there's three types of loops. Do-while loop, while loop, and for loop. Personally, I prefer for-loops.

I prefer for-loops when there is a pre-determined number of loops that need to be done. Do and do-while loops are better used when there is some condition that needs to be met (I.e keep entering numbers until the total is greater than 500). There's also a for-each loop, but we don't need to worry about that just yet.

Nikki Smith wrote:
The set up explained in my book is this:



If you're going to do more than one statement you need to use brackets.


There's really never a good reason NOT to use brackets. Sure, you don't need them now, but if you don't put them in, some day, somewhere, you will forget the rule and add in debugging statements or such and your program will do all kinds of crazy things. Get in the habit of ALWAYS using them now, even if you do only have one statement.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!