Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 5 6 [7] 8 9 ... 795

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 813698 times)

JoshuaFH

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #90 on: January 06, 2012, 03:41:43 pm »

Oh my! I absolutely loved that little tutorial Stargrasper, finally something that frames coding in a way that doesn't make it indistinguishable from black magic.

I do appreciate it, and hope to see more.
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #91 on: January 06, 2012, 04:04:41 pm »

Stargrasper's Java Basics Tutorial Series

You know how I promised the next tutorial would teach how to write code?  I lied.  I decided in the intervening time that it would be wise to give a tutorial on basic use of Eclipse, Netbeans, and simply not using an IDE to code.  Normally, I would be doing all of my coding on Linux.  For purposes of screenshots, I've decided to switch to Windows because the small differences that do exist are probably confusing.  If you have OS related questions, don't hesitate to ask.  I've done Java coding on every major OS in use today.

Now, I'm going to try to hold your hand as tightly as I can while walking you through this.

Eclipse Basics

The current version of Eclipse is 3.7.1.  I've been using Eclipse 4.2 Beta.  They look different.  That means I'm dropping down for this tutorial.

I'm going to assume you can handle downloading and unpacking Eclipse.  When you launch it, the first thing it will do is ask you for a workspace.  This is your working directory, a folder where it will keep all of your stuff.  I believe it will create the directory if you hand it something that doesn't exist.  I think.  It looks like this.
Spoiler (click to show/hide)

After choosing your workspace, if you've never used that workspace before, Eclipse will bring you to its start screen.  Depending on what plugins are installed, it will look something like this.
Spoiler (click to show/hide)
Click on 'Workbench' in the top right corner.

Which brings you to this screen.  It's pretty blank now because no projects have been created.
Spoiler (click to show/hide)
On the left is the Package Explorer.  Projects and the associated file trees will appear in here.  To the right is the Outline.  It's nice for figuring out what's in your code, but I usually close it to give me more room for the code.  The bottom is a series of tabs.  The only two you really care about are Problems, which describes errors in your code, and Console, which is where both stdout  and errout goes by default in Eclipse.  The big open space in the center is where the code editor will go once we create a code file.  So lets do that!

Select the File menu, New, Java Project.  Go ahead and enter "Hello World" as the project name and press finish.
Spoiler (click to show/hide)
I'm using Java 7 because it's the most recent version of Java.  You may only have Java 6.  It's not significant for anything we'll be doing for awhile.

Open Hello World in the Package Explorer and click on the src folder.  Now press New Package in the center of the tool bar or from the File / New menu.
Spoiler (click to show/hide)
Enter a name and press Finish to create your package.

Click on your package in the Package Explorer and click New Class from either the center of the tool bar (next to New Package) or from the File / New menu.
Spoiler (click to show/hide)
Enter a name and check the box that says public static void main(String[] args).

The code editor for your new file will appear in the center of the screen.  Write some code, in my example, the classic Hello World program, and press the Run button as highlighted or press Ctrl + F11 to run your code.
Spoiler (click to show/hide)

Eclipse will open the Console tab and display the output, which may include errors or exceptions if something went wrong.  If the code tries to loop infinitely or something crazy or if you just need to kill the program now, there's a little red button near the bottom right that will kill the running program immediately.

Great!  You know enough about eclipse to get started coding.  But what if you want to share your code with someone else?  What if you want to collaborate?  What if you want help debugging?  What if you want to distribute the working executable without sharing the code?  All except the last could be done by sharing code excerpts like we've been doing elsewhere in this thread.  But that's not very efficient.  The more efficient way is to distribute a .jar file.  A .jar file is literally a .zip file that Sun uses for specialized purposes.  You can make .jar files executable, which means they can run the program contained within them.  You can also choose whether or not to include the source code.  A program can run just fine without .java files using only the compiled .class files.

To create a .jar file, right click the project name in the Package Explorer and select Export.  Select Java and then either Jar File or Runnable Jar File.  A Jar file contains just data.  A Runnable Jar File can be executed to run the program inside.  From there, you need to select the export location and name of the jar file as well as check what you want included.  The class files are needed to run without recompiling.  The source may or may not be desirable to include.
Spoiler (click to show/hide)

So you know how to export a Jar file.  Fantastic!  How do you import it?  File / Import / General / Archive File.  Make sure you've got a project to plug it into.

Eclipse has a metric crap load of other functions and features I'm not talking about here.  This is just the very basics to get you started.

Netbeans Basics

I will abridge this one heavily because Netbeans is similar enough to Eclipse that there is a lot of overlap and because I don't have time to write a massive tutorial with screenshots right now.  That and I don't use Netbeans often and am thus unfamiliar with its intricacies.

I have no idea what the most recent version of Netbeans is and I don't feel like checking.  I happen to have Netbeans 6.8 on my computer from heaven knows when I installed it.

Creating projects in Netbeans is about the same as Eclipse.  File / New Project / Java / Java Application.  Give it a name, location, and check the box that makes it generate a main() method for you.
Spoiler (click to show/hide)
The handy thing is that this just created your package and your class file for you.  That being said, to create your own, you select File / New File and pick either package, class, or whatever from the list.
Spoiler (click to show/hide)

You can run the program by using the Run menu or the Run button.

Netbeans actually builds Jar files for you every time you build the program.  You'll find it under the Files tab.
Spoiler (click to show/hide)

Importing Jar files in unintuitive as near as I can tell.  Follow these instructions to do it.

I feel like I'm snubbing Netbeans, but frankly, I don't really know much about using it and I don't have the time to make as many screenshots as I did for Eclipse.  You'll figure it out and if you don't, you'll ask me and I'll create all the screenshots and videos you want to walk you through it.  Don't be afraid to ask.

Command Line Basics

Remember how I said I'm a Linux user?  It's not uncommon for me to do things on the terminal.  To do it in Windows, you use one of the following commands:
Code: [Select]
java classor
Code: [Select]
java -jar jarFileName
There's no particular reason you need an IDE to code in Java.  You can write your code in any text editor.  VI or EMACS might be traditional.  There's nothing stopping you from using Notepad, though Notepad++ might be more helpful just for syntax highlighting.  If you know Sun's Java specifications, you can produce Java in any environment.

Building Jar files is harder.
Code: [Select]
jar cf jar-file input-file(s)I ripped that directly from an Oracle help page.  I won't go into detail here because nobody reading this is going to try doing it.
http://java.sun.com/developer/Books/javaprogramming/JAR/basics/build.html

Post Epilogue

That took forever to write.  And since nobody has problems setting up their IDEs, nobody will bother to read this.  Oh well.  We can point people to this thread to help get them started.

I'd strongly appreciate any and all comments.  I'd certainly like to know what I'm doing well and poorly writing these things.  I'm happy to clarify anything and answer random questions as well.

I'd better get started on a tutorial that teaches coding before someone lynches me.
Logged

Yannanth

  • Guest
.
« Reply #92 on: January 06, 2012, 06:39:29 pm »

.
« Last Edit: November 22, 2016, 05:13:37 am by Yannanth »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #93 on: January 06, 2012, 06:54:37 pm »

Stargrasper's Java Basics Tutorial Series

Since some of you want to learn to...oh...code...I should probably get on that.  Remember how I mentioned style in a previous tutorial?  I'll be intersparcing stylistic things throughout everything else I write.

Lesson 0.2: Computer

We're programming a computer.  So wouldn't it be nice it could, I don't know, compute something?

Java directly supports the four basic mathematical operations, as well as several others.  You can do math with either whole numbers or decimals.  Try hard enough, and you can use binary, octal, or hexidecimal.  Here's some basic math.
Code: [Select]
/*
 * Written by Stargrasper
 * Updated 1/6/2012
 * (c) Stargrasper 2012
 *
 * This lesson will talk about basic computations
 * with Java.
 */

package com.bay12forums.stargrasper.javatutorial.lesson2;

public class Computer {

public static void main(String[] args) {
// Lets do math, the hard way
// Here are your four basic operations
// With a catch

// Addition is simple as long
// as it doesn't overflow
// More on that later
System.out.println(1 + 1);

// Subtraction is simple as long
// as it doesn't overflow
System.out.println(1 - 1);

// Multiplication is scarier but
// fairly simple
// Pretty high chance that
// it will overflow, though
System.out.println(2 * 3);

// The evil one.  Integer division.
// 1 / 2 = 0
// These are integers, there is no
// remainder, it gets dropped.
System.out.println(1 / 2);

// Mod.  Divide the numbers
// and then give me the remainder
System.out.println(1 % 2);



// Just for fun...
// Here's some interesting math
// The numbers are all doubles
// And they all equal something different

// The first equals 1.0, as we would expect
System.out.println(1.0 / 1.0);

// The second equals 0.0, as we would expect
System.out.println(0.0 / 1.0);

// The third equals Infinity, wait, what?
// 1 / 0 is suppose to not work
// Isn't double math fun?
System.out.println(1.0 / 0.0);

// And this forth one actually doens't work
// It equals NaN, aka 'Not A Number'
System.out.println(0.0 / 0.0);
}

}

Wow!  Look at all the whitespace!  Here it is again in a condenced, I'll shoot you kind of way...
Code: [Select]
package com.bay12forums.stargrasper.javatutorial.lesson2;
public class ComputerCondenced {
public static void main(String[] args) {
System.out.println(1+1);
System.out.println(1-1);
System.out.println(2*3);
System.out.println(1/2);
System.out.println(1%2);
System.out.println(1.0/1.0);
System.out.println(0.0/1.0);
System.out.println(1.0/0.0);
System.out.println(0.0/0.0);
}
}
Again, it's a very simple example, so it still doesn't illustrate why I hate this style.  But it did shorten it from 68 down to 14 lines.  Isn't that good?  Not necessarily.  I lost my comments that explain what in the world I'm doing and why I'm doing it.  I also made it a bit harder to read.  That'll be more pronounced when we do harder computations that involve lots of parenthasis.  There is a Math library in Java, but it'll be awhile before I touch it in these tutorials.

You might notice that I said in an earlier tutorial that System.out.println() sends a text string to the console through stdout.  But if you run this, it's not sending 1 + 1, it's sending 2.  It's evaluating the math and sending the resultant number.  But why is it sending a number when it can only send text strings?  You would not believe the sheer amount of stuff Java does for you.

First of all, Java is doing the math for you, respecting order of operations.  That should be impressive on its own.  It's not asking you whether you want to do the math nor making you tell it to do the math, Java is just doing it.  If you wanted to the equation to the console without evaluating it, you have to surround it in quotations to tell Java it's a text string and not a mathematical equation.
Code: [Select]
System.out.println( "1 + 1" );

But after that, it's still sending a number through something that can only take text strings.  It's actually following a chain of commands that make it take that number and transform it into a text string.  I can't remember the exact details of how String.valueOf(int) works, but it eventually calls that.  I'm not expecting you to understand how it does it or what it's doing.  To you, it's magic.  I just wanted you to know it was doing something unusual for you.

Lesson 0.3: Primitive Variables

About ninty-nine percent of everything in Java is an Object.  Java takes Object Oriented Programming Language to the extreme.  Object is the cosmic-superclass of everything.  Everything has to derive from Object or be a Primitive.  I'd love to describe objects in terms of primitives, but that's a later lesson that I may or may not ever write depending on how good a job Max White did on his tutorial that I embarrasingly still haven't read.

Primitives are as their name implies, primitive data.  They are the simplest forms of data that Java supports.  There are eight primitives types:
   1. boolean - true or false.  Java does not allow you to use 1 and 0 like C programmers are used to.  Really useful when you know that a variable can only potentially have up to two values
   2. byte - An 8-bit integer from -128 .. 127 inclusively.
   3. short - A 16-bit integer from -32768 .. 32767 inclusively.
   4. int - A 32-bit integer from -2147483648 .. 2147483647 inclusively.
   5. long - A 64-bit integer from -9223372036854775808 .. 9223372036854775807 inclusively.
   6. char - A 16-bit unicode character from '\u0000' (0) .. '\uffff' (66535) inclusively.
   7. float - A 32-bit IEEE 754 floating point.
   8. double A 64-bit IEEE 754 floating point.

These are your primitive values.  All variable data in Java is made of up these guys.  You will most commonly be using int and double until you have some idea what you're doing.

Now, variables.  A variable is box.  That box has a name and contents.  The contents are restricted such that only certain things can be in the box.  This includes both restrictions on what is put in and how big it is.  Shoving a char into an int box is kind of like shoving a square peg into a round hole.  You can do it, certainly, but if you aren't careful, things break.

When you declare a variable, you have to tell it what type it is and what its name is.  That type can be either a primitive or an object.  If it's a primitive, you must immedietly give it a default value.  If you don't know what the value will be immedietly, then set it to something impossible or to something generic.  The two nice things about variables are that you can call it by name, rather than value, and you can alter its value.
Code: [Select]
/*
 * Written by Stargrasper
 * Updated 1/6/2012
 * (c) Stargrasper 2012
 */

package com.bay12forums.stargrasper.javatutorial.lesson2;

public class ComputerVariables {

public static void main(String[] args) {
// Instantiate two ints and set them both to zero
int number1 = 0, number2 = 0;



// Set number1 = 0 + 0 + 5 = 5
number1 = number1 + number2 + 5;

// Set number2 = (5 + 0) * 3 = 15
// Parenthesis necessary for
// order of operations
number2 = (number1 + number2) * 3;

// Print out both variables
System.out.println(number1);
System.out.println(number2);
}

}
Stylistically, I feel that variables should have long, descriptive variable names.  I should be able to look at a variable and know both exactly what it is and exactly what it's for.  IDEs like Eclipse and Netbeans, and even some more fancy text editors like Notepad++, come with an autocomplete function so you don't even have to retype them.  It makes your code more readable and easier to understand now and later.

As you can see, I can use variables in equations.  I can reset them in equations.  If I have a variable in an equations when I'm setting that variable equal to the result (number1 = number1 + number2 + 5), then the old value is used in the equation and the new goes to the variable afterward.

What if I want to do 1 / 2?  We already discussed that this doesn't really work.  Lets try it variables.  In a couple of different ways.
Code: [Select]
package com.bay12forums.stargrasper.javatutorial.lesson2;

public class IntegerMath {

public static void main(String[] args) {
// Instantiate two ints
int aNumber = 1;
int anotherNumber = 2;

// Divide and print result thereof
System.out.println(aNumber / anotherNumber);



// Instantiate two doubles
double aDouble = 1;
double anotherDouble = 2;

// Divide and print result thereof
System.out.println(aDouble / anotherDouble);



// More fun with doubles

// These are the same
double d1 = 1;
double d2 = 1.0;

// Lets prove it
System.out.println(d1);
System.out.println(d2);



// But I want to use ints
// Cast one, either one
System.out.println( (double) aNumber / anotherNumber);



// And what if I don't want to use variables

// This still won't work
System.out.println( 1 / 2 );

// But making one of them a double will force
// it to do double division instead of
// integer division
System.out.println( 1.0 / 2 );
System.out.println( 1 / 2.0 );

// Naturally, if both are doubles it works
System.out.println( 1.0 / 2.0 );

// Again, if one or the other is a double
// it forces double division
System.out.println( (double) 1 / 2 );
System.out.println( 1 / (double) 2 );

// Or both
System.out.println( (double) 1 / (double) 2 );
}

}
Okay, take a minute to understand what just happened.  We created a few variables and used them for math.  Still with me?  The first gives 1 / 2 as 0 because of integer division.  Integer division catches experts, too.  We'll talk more about it in about a paragraph.  The second does the same math, but with doubles.  Because doubles have a decimal component and integers don't, this resolves to 0.5.  That's the easy part.  Here's the hard part.  If you don't give a double a decimal component, Java tacks on a .0 for you.  The really confusing part, if you try to divide two ints, Java does integer division.  If you try to divide two doubles, Java does double division.  If you try to divide an int by a double or a double by and int, Java does double division.

It's probably a good time to describe the different types of division.  For now, all you care about are integer division and double division, also known as floating point division.  It's really not that difficult if you already understand basic division.  It does the same thing, but integer division throws away the decimal.  Listen, it throws away the decimal, it does not round.
Code: [Select]
package com.bay12forums.stargrasper.javatutorial.lesson2;

public class IntegerDivision {

public static void main(String[] args) {
// Integer division
System.out.println( 1 / 2 );

// Floating point division
System.out.println( 1 / 2.0 );
}

}
This code demonstrates that fact.  The first resolves to 0 while the second resolves to 0.5.  If it rounded integer math, it would have rounded to 1.

I skipped something that you should be really curious about by now.  Casting.  I don't have a good way of describing casting, so here goes.  Casting is essentially taking the data from one variable and turning it into another type.  When you cast an integer of any size to a floating point of any size, it throws away any size it can't convert and tacks on a decimal.  When you cast any kind of floating point to any kind of integer, it throws away the decimal value.  Again, throws away, not rounds.
Code: [Select]
package com.bay12forums.stargrasper.javatutorial.lesson2;

public class Casting {

public static void main(String[] args) {
// Declare an in
int num = 1;

// Print the difference between the value
// before and after casting
System.out.println( num );
System.out.println( (float) num);



// Declare a float
float fl = (float) 1.6;

// Print the differen between the value
// before and after casting
System.out.println( fl );
System.out.println( (long) fl );
}

}
When declaring that float, I have to cast 1.6 to a float because Sun, in all its wisdom, decided that all decimal values you type into the code are doubles.  I had to cast it down because floats and doubles are not compatible with each other.  C++ would have allowed me to do this without jumping through hoops...

When casting, it helps to make sure the two things you're casting between are somewhat compatible with each other.  If you don't, the system either errors out at compile time or errors out at runtime.  That doesn't necessarily mean the two things you're casting from and to have to be numbers.  You can actually cast between chars and ints just fine.  If you cast from a char to an int, Java will give you that chars unicode integer value.  If you cast between an int and char, Java will give you the char with that unicode integer value.  Just remember that ints are 32-bit and chars are 16-bit.  It'll break if you try to feed it too large of a value.  I've used this plenty of times.

Post Epilogue

Still feeling bad?  We're past 'Hello World'.  Sort of.  This is basically the 'Hello World' of primitives.  There's a ton of information here to learn at the very beginning.  It's kind of hard to start, but it gets much easier when you get further along.

Don't like my heavily text-based tutorials, Yannanth?  Sorry, not much I can do about that.  It'd be nice if people actually used my work.  Or told me to stop wasting my time before I waste too much of my time.  Let me know what you think.
Logged

Darvi

  • Bay Watcher
  • <Cript> Darvi is my wifi.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #94 on: January 06, 2012, 07:11:00 pm »

I think calling it a "calculator" would be less confusing.
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #95 on: January 06, 2012, 07:25:50 pm »

I think calling it a "calculator" would be less confusing.

Technically I think 'calculate' and 'compute' mean the same thing.  I chose computer rather than calculator because that's the device we're (probably) coding for.  You're right, though, it probably would make more sense that way and I haven't yet done much in the tutorials that a four-function calculator can't do.

There seem to be about three conversations going on in this thread and my posts are really long.  Should I split off the tutorials to reduce clutter here?

By the way, it's nice to know someone's reading the tutorials.  Someone I've heard of, even.
Logged

Yannanth

  • Guest
.
« Reply #96 on: January 06, 2012, 07:31:25 pm »

.
« Last Edit: November 22, 2016, 05:13:25 am by Yannanth »
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #97 on: January 06, 2012, 07:35:29 pm »

Just a note: if you copyright it, others are technically not allowed to reuse it or repost it--do anything more than simply look at it. It's a simple program but of course, you can still sue over it. Ethics comes into everything, doesn't it? ;)

Technically not true...unless American copyright law has changed since I last saw it...

Any work is considered copyrighted from the moment of its creation, but you can't claim it in court unless you register the copyright.  I'm not doing that and as far as I know, I don't have to in order to use that symbol.  If I'm wrong about that, please tell me so I can correct it.  I also have no intention of saying or doing anything so long as nobody claims it as their own.  That's the only reason for that notice.  Ethics indeed.
Logged

Yannanth

  • Guest
.
« Reply #98 on: January 06, 2012, 07:47:23 pm »

.
« Last Edit: November 22, 2016, 05:13:16 am by Yannanth »
Logged

Aqizzar

  • Bay Watcher
  • There is no 'U'.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #99 on: January 07, 2012, 06:00:20 pm »


Now I just need to figure out why C# apparently has a different charactermap than Extended ASCII.
Logged
And here is where my beef pops up like a looming awkward boner.
Please amplify your relaxed states.
Quote from: PTTG??
The ancients built these quote pyramids to forever store vast quantities of rage.

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #100 on: January 07, 2012, 06:44:59 pm »


Now I just need to figure out why C# apparently has a different charactermap than Extended ASCII.

Most likely it's using UTF-16.  Now the question is what's the difference between Extended ASCII and UTF-16?
Logged

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #101 on: January 07, 2012, 07:24:22 pm »

Strings are encoded in Unicode in C#. To spice things up a bit, strings are localized, though that should only matter for accents. But porting a string from an American to a Turkish OS may have some unexpected effects.
Logged

RedWarrior0

  • Bay Watcher
  • she/her
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #102 on: January 07, 2012, 07:55:05 pm »

I will follow this thread for now, and ask for help when I run into the inevitable problems for FRC.

Eventually, I'll be needing to use sensors on a robot to figure out range and angle. Hooray.
Logged

Stargrasper

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #103 on: January 07, 2012, 08:12:32 pm »

I will follow this thread for now, and ask for help when I run into the inevitable problems for FRC.

Eventually, I'll be needing to use sensors on a robot to figure out range and angle. Hooray.

Surprisingly enough, I have experience working with robotics.  I can help you with that.
Logged

RedWarrior0

  • Bay Watcher
  • she/her
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #104 on: January 07, 2012, 08:14:55 pm »

However, considering we found out the general objective today, it's unlikely that I'll have to do it any time soon.
Logged
Pages: 1 ... 5 6 [7] 8 9 ... 795