Got super side - tracked with some projects and a new semester. Really got sucked into Project Euler in the last couple of days and thus havent really posted!
Project Euler is the perfect set of problems to get someone going on a new language or to improve their skills on any language Here is a link! Check out some problems, have a look around.
I had hoped to post all my euler project solutions to a github repository but i am having lots of problems figuring out how to push from eclipse to github.... so I have been wrestling with that to no avail. I may look around the web and see if anyone is just posting their code on blogs and etc. and post my solutions here.
The best part about the Euler site is that once you solve the problem you get access to a forum where people have posted the code they used to solve the problem, which has so far turned out to be very helpful and a great learning experience.
I will post my little project Euler stats box on my blog for motivation and bragging rights!
Anyways, my goals for this week are to get back on track with working through my text and also to get this github stuff working and work through some more project euler problems!
Three day weekend so hopefully I make some progress!
Thursday, September 1, 2011
Wednesday, August 17, 2011
Chapter 3 part 3
Logical operators and Case statements.
Logical operators in Java
! not
&& and
|| or
^ exclusive or
! is logical negation
&& is logical conjunction
|| logical disjunction
^ logical exclusion
Ruby with the exception of ^ uses the same logical operators, there are also 3 additional options to use which are simply "and" "not" "or."
It's complicated and I don't exactly understand the subtle differences but the symbols have precedence over the actual word versions.
Case statements are used because too many nested if statements can make code very confusing to read. Basically, you can simplify having to make multiple true-false decisions by setting up a case statement for each option.
In Java the syntax works like this
switch (status){
case 0 : do this line of code;
break;
case 1: do this line of code;
break;
case 2: do this line of code;
break;
case 3: do this line of code;
break;
default: System.out.println("you chose an invalid option);
System.exit(0);
}
the program only executes the code according to the case it represents, the break statement tells the program to skip all the other code in the block once it is executed. The default option is there in case a user inputs an option that doesn't match any of the values of the case status. In the above switch statement, the default ouputs that an invalid option has been chosen and terminates the program.
In Ruby you use the term "when" to set up the case like this:
when a=1
do this line of code
when a=2
dot this line of code
else
puts "invalid option selected"
end
In Ruby you use "else" instead of "default"
I also found perusing the googles that you can use multiple values in your when statement.
Here is a link to a very concise and well written explanation about Ruby switch statements.
(Actually quite a bit of good reading on that blog about several different programming subjects)
There we have it for Chapter 3.
Now on to the exercises.
Logical operators in Java
! not
&& and
|| or
^ exclusive or
! is logical negation
&& is logical conjunction
|| logical disjunction
^ logical exclusion
Ruby with the exception of ^ uses the same logical operators, there are also 3 additional options to use which are simply "and" "not" "or."
It's complicated and I don't exactly understand the subtle differences but the symbols have precedence over the actual word versions.
Case statements are used because too many nested if statements can make code very confusing to read. Basically, you can simplify having to make multiple true-false decisions by setting up a case statement for each option.
In Java the syntax works like this
switch (status){
case 0 : do this line of code;
break;
case 1: do this line of code;
break;
case 2: do this line of code;
break;
case 3: do this line of code;
break;
default: System.out.println("you chose an invalid option);
System.exit(0);
}
the program only executes the code according to the case it represents, the break statement tells the program to skip all the other code in the block once it is executed. The default option is there in case a user inputs an option that doesn't match any of the values of the case status. In the above switch statement, the default ouputs that an invalid option has been chosen and terminates the program.
In Ruby you use the term "when" to set up the case like this:
when a=1
do this line of code
when a=2
dot this line of code
else
puts "invalid option selected"
end
In Ruby you use "else" instead of "default"
I also found perusing the googles that you can use multiple values in your when statement.
Here is a link to a very concise and well written explanation about Ruby switch statements.
(Actually quite a bit of good reading on that blog about several different programming subjects)
There we have it for Chapter 3.
Now on to the exercises.
Tuesday, August 16, 2011
Chapter 3 pt. 2
If statements.
We start with one way if statements, meaning if a statement is true we execute the code, and if the statement is false we skip the code all together.
In Java the syntax works like this.
if (boolean expression) {
do this code inside of the blocks
}
You can in Java skip the code blocks if its only one statement or line of code. (i always put them in to avoid any confusion on my part)
A two way if statement executes one set of instructions if it the boolean expression is true, and executes a different set of code if the expression is false.
the syntax looks like this
if (boolean expression){
do this code
}
else{
do this code
}
you can also nest an if statement inside of an if statement. Meaning that you can simply put an if or if else statement in the block containing the code to be executed in an if or else block.
Ruby works exactly the same you just leave out the method blocks and the parenthesis that surround the boolean expression.
if
do this
end
if
do this
else
do this
end
or you can even do
if
do this
elseif
do this
else
do this
end
The text then makes an improvement on the math quiz with our new found if statement knowledge. The new quiz is subtraction instead of addition and lets you know the answer if you are wrong.
here is the code in Java
import java.util.Scanner;
public class SubQuiz{
public static void main(String args [ ] ){
// below is a diff method for getting a random #
int number1 = (int) (Math.random( )*10);
int number2 = (int) (Math.random( )*10);
// our if statement assigns the highest # to the first integer
if (number1 < number2){
int temp = number1
number1 = number2
number2 = temp
}
System.out.print (" what is" + number1 + "-" + number2 + "?");
Scanner input = new Scanner(System.in);
int answer = input.NextInt( );
// above makes new scanner object and turns input from string to int
// if the number that was input matches the correct answer our boolean statement is true and
the system outputs you are correct.
if (number1 - number2 == answer)
System.out.println ("you are correct");
else
System.out.println ("you are wrong" + number 1 + "-" + number2 + "should be" +(number1 - number2 );
// else it prints out you are wrong and the correct anwer
}
}
and here is my ruby code for the same thing.
class SubQuiz
number1 = rand(10-1)
number1 = number1.to_i
number2 = rand(10-1)
number2= number2.to_i
if
number1 < number2
temp = number1
number2 = temp
end
puts ( "what is #{number1} - #{number2} ?")
answer1 = gets
answer1 = answer1.to_i
answer2 = number1 - number2
if
(answer1==answer2)
puts "You are correct!"
else
puts "wrong, correct answer should be #{answer2}"
end
end
pretty self explanatory, if else is the same without the brackets.
Next we will get into some Logical operators and switch statements, then to the chapter 3 exercises.
Also hopefully i will come up with a blog with details about some of the gui stuff i mentioned earlier as well as some other problem solving exercises i uncovered whilst surfing the web.
Next semester starts in less than a week, hopefully i can keep posting to this blog without too many lengthy breaks.
We start with one way if statements, meaning if a statement is true we execute the code, and if the statement is false we skip the code all together.
In Java the syntax works like this.
if (boolean expression) {
do this code inside of the blocks
}
You can in Java skip the code blocks if its only one statement or line of code. (i always put them in to avoid any confusion on my part)
A two way if statement executes one set of instructions if it the boolean expression is true, and executes a different set of code if the expression is false.
the syntax looks like this
if (boolean expression){
do this code
}
else{
do this code
}
you can also nest an if statement inside of an if statement. Meaning that you can simply put an if or if else statement in the block containing the code to be executed in an if or else block.
Ruby works exactly the same you just leave out the method blocks and the parenthesis that surround the boolean expression.
if
do this
end
if
do this
else
do this
end
or you can even do
if
do this
elseif
do this
else
do this
end
The text then makes an improvement on the math quiz with our new found if statement knowledge. The new quiz is subtraction instead of addition and lets you know the answer if you are wrong.
here is the code in Java
import java.util.Scanner;
public class SubQuiz{
public static void main(String args [ ] ){
// below is a diff method for getting a random #
int number1 = (int) (Math.random( )*10);
int number2 = (int) (Math.random( )*10);
// our if statement assigns the highest # to the first integer
if (number1 < number2){
int temp = number1
number1 = number2
number2 = temp
}
System.out.print (" what is" + number1 + "-" + number2 + "?");
Scanner input = new Scanner(System.in);
int answer = input.NextInt( );
// above makes new scanner object and turns input from string to int
// if the number that was input matches the correct answer our boolean statement is true and
the system outputs you are correct.
if (number1 - number2 == answer)
System.out.println ("you are correct");
else
System.out.println ("you are wrong" + number 1 + "-" + number2 + "should be" +(number1 - number2 );
// else it prints out you are wrong and the correct anwer
}
}
and here is my ruby code for the same thing.
class SubQuiz
number1 = rand(10-1)
number1 = number1.to_i
number2 = rand(10-1)
number2= number2.to_i
if
number1 < number2
temp = number1
number2 = temp
end
puts ( "what is #{number1} - #{number2} ?")
answer1 = gets
answer1 = answer1.to_i
answer2 = number1 - number2
if
(answer1==answer2)
puts "You are correct!"
else
puts "wrong, correct answer should be #{answer2}"
end
end
pretty self explanatory, if else is the same without the brackets.
Next we will get into some Logical operators and switch statements, then to the chapter 3 exercises.
Also hopefully i will come up with a blog with details about some of the gui stuff i mentioned earlier as well as some other problem solving exercises i uncovered whilst surfing the web.
Next semester starts in less than a week, hopefully i can keep posting to this blog without too many lengthy breaks.
Monday, August 15, 2011
Chapter 3
Chapter 3 is about selections!
We start with boolean data types and of course outline the comparison operators..after a quick glance it appears that all of the comparison operators in Ruby are the exact same as they are in Java.
A variable that holds a Boolean value is known as a Boolean variable. Java uses the reserved word boolean to denote a boolean variable. A boolean variable returns either true or false.
In Ruby there is no boolean variable, true and false belong to seperate classes, which means there is no boolean "type" for now this doesnt really get us off track, but i think is important to note.
In my textbook they throw a math quiz type program at you that is a good example of a way to use a boolean operator.
Here is the code:
import java.util.scanner;
public class MathQuiz{
public static void main (String args [ ]){
int number1 = (int)(System.currentTimeMillis( )% 1o);
int number2 = (int)(System.currentTimeMillis( ) *7 % 10);
// above is just a way to generate a random number
Scanner input = new Scanner (System.in);
// creates a scanner object called "input"
System.out.println("what is " + number1 + "+" + number2 + "?");
// outputs the addition question
int answer = input.nextInt( );
// gets the answer from the input
System.out.println( number1 + "+" + number2 + "=" + answer + "is" + (number1 + number2 ==answer));
// uses a boolean operator to see if answer is correct, if it is it returns true, if not false
}
}
I really like this example in my book, i think it is a very clear way to show how a boolean operator returns a value and its very clever.
Now lets see if I can do it in Ruby
First I will have to find a way to get ruby to list a random integer! go go gadget google search.
Turns out there is a method for that.
Here is a link to a site called stackoverflow where they explain it better than i could.
Great and informative site.
Here is the code in ruby:
class MathQuiz
number1 = rand(10-1) #generates the random number 1
number1 = number1.to_i #converts it to an integer
number2 = rand(10-1) #same as above
number2= number2.to_i
puts ( "what is #{number1} + #{number2} ?") #asks the question
answer1 = gets #gets the input (the guess)
answer1 = answer1.to_i #converts to an int
answer2 = number1 + number2 #computes the actual answer
puts (answer1==answer2) #boolean comparison returns value of true or false
end
And there you have it, some things that i did in this program that i havent done before are :
1. concatenate a string! In Java you put strings in quotes and use the + sign to run them together, in Ruby you use one set of quotes and #{ } to enclose a variable so you can add a value to the string.
2. Use the random number generator method (explained in link above)
3. a boolean comparison (thanks george!) it returns a value of either true or false just like in the good ol java
Thats all for today! Isnt learning fun?
We start with boolean data types and of course outline the comparison operators..after a quick glance it appears that all of the comparison operators in Ruby are the exact same as they are in Java.
A variable that holds a Boolean value is known as a Boolean variable. Java uses the reserved word boolean to denote a boolean variable. A boolean variable returns either true or false.
In Ruby there is no boolean variable, true and false belong to seperate classes, which means there is no boolean "type" for now this doesnt really get us off track, but i think is important to note.
In my textbook they throw a math quiz type program at you that is a good example of a way to use a boolean operator.
Here is the code:
import java.util.scanner;
public class MathQuiz{
public static void main (String args [ ]){
int number1 = (int)(System.currentTimeMillis( )% 1o);
int number2 = (int)(System.currentTimeMillis( ) *7 % 10);
// above is just a way to generate a random number
Scanner input = new Scanner (System.in);
// creates a scanner object called "input"
System.out.println("what is " + number1 + "+" + number2 + "?");
// outputs the addition question
int answer = input.nextInt( );
// gets the answer from the input
System.out.println( number1 + "+" + number2 + "=" + answer + "is" + (number1 + number2 ==answer));
// uses a boolean operator to see if answer is correct, if it is it returns true, if not false
}
}
I really like this example in my book, i think it is a very clear way to show how a boolean operator returns a value and its very clever.
Now lets see if I can do it in Ruby
First I will have to find a way to get ruby to list a random integer! go go gadget google search.
Turns out there is a method for that.
Here is a link to a site called stackoverflow where they explain it better than i could.
Great and informative site.
Here is the code in ruby:
class MathQuiz
number1 = rand(10-1) #generates the random number 1
number1 = number1.to_i #converts it to an integer
number2 = rand(10-1) #same as above
number2= number2.to_i
puts ( "what is #{number1} + #{number2} ?") #asks the question
answer1 = gets #gets the input (the guess)
answer1 = answer1.to_i #converts to an int
answer2 = number1 + number2 #computes the actual answer
puts (answer1==answer2) #boolean comparison returns value of true or false
end
And there you have it, some things that i did in this program that i havent done before are :
1. concatenate a string! In Java you put strings in quotes and use the + sign to run them together, in Ruby you use one set of quotes and #{ } to enclose a variable so you can add a value to the string.
2. Use the random number generator method (explained in link above)
3. a boolean comparison (thanks george!) it returns a value of either true or false just like in the good ol java
Thats all for today! Isnt learning fun?
Sunday, August 14, 2011
Chapter 2 exercises
I think from this point forward i will try and hit on the major points of the chapter and then go straight to the programming exercises at the end of the chapter. As i get a little deeper into the book I am hoping to do them in Java for review and then in Ruby. For now I am going to do them in Ruby only so I can make up some ground and mostly because the exercises are very basic and easy for anyone who already knows a specific language.
So here are the exercises in Ruby:
2.1 Write a program that reads a Celsius degree in double from the console, then converts it to Fahrenheit and displays the result.
The math is F = (9/5) * celsius + 32
Here is the code.. (very similar to our last example)
class ChapterTwoQuestionOne
puts "what is the temperature in Celsius?"
cels = gets
cels = cels.to_i #converts our string to an integer
puts 9.0/5 * cels + 32
end
problem 2.2 Write a program that reads in the radius and length of a cylinder and computes the volume using the following formula:
area = radius * radius * 3.14
volume = area * length
here is the code:
class ChapterTwoQuestionTwo
puts "what is the radius?"
radius = gets
radius = radius.to_f
puts "what is the length?"
leng = gets
leng = leng.to_f #convert to float ( because we need the decimals!)
area = radius * radius * 3.14159
puts area
volume = area * leng
puts volume
end
Since most of these are very similar i will skip ahead to problem 2.6
Problem 2.6 Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example if an integer is 932 the sum of the integers is 14.
A little bit more involved, the text implies that you should use the remainder operator, which is how i will do it.
here is the code:
class ChapterTwoQuestionSix
puts "enter an integer between 0 and 999"
number = gets
number = number.to_i
num1 = number%100
num1 = num1%10
num2 = number /10
num2 = num2%10
num3 = number/100
puts num1 +num2+ num3
end
Good refresher on the remainder operator.. but so far not a whole lot of challenge. I have found that Ruby is so far in the basic sense very very easy to pick up on the language elements.
Chapter 3 is ahead and contains selections, which means some boolean stuff as well as if statements and nested if statements and etc.
There is quite of bit in my textbook that uses JOptionPane for input and output, which is from the Java swing class. I haven't looked very hard yet, but i have noticed that there doesn't seem to be an obvious Ruby version of Swing.. which is probably mostly due to the nature of what Ruby is used for in programming in general as opposed to Java.
As i get some time this week hopefully i can find a solution or a way implement some i/0 version in ruby.
So here are the exercises in Ruby:
2.1 Write a program that reads a Celsius degree in double from the console, then converts it to Fahrenheit and displays the result.
The math is F = (9/5) * celsius + 32
Here is the code.. (very similar to our last example)
class ChapterTwoQuestionOne
puts "what is the temperature in Celsius?"
cels = gets
cels = cels.to_i #converts our string to an integer
puts 9.0/5 * cels + 32
end
problem 2.2 Write a program that reads in the radius and length of a cylinder and computes the volume using the following formula:
area = radius * radius * 3.14
volume = area * length
here is the code:
class ChapterTwoQuestionTwo
puts "what is the radius?"
radius = gets
radius = radius.to_f
puts "what is the length?"
leng = gets
leng = leng.to_f #convert to float ( because we need the decimals!)
area = radius * radius * 3.14159
puts area
volume = area * leng
puts volume
end
Since most of these are very similar i will skip ahead to problem 2.6
Problem 2.6 Write a program that reads an integer between 0 and 1000 and adds all the digits in the integer. For example if an integer is 932 the sum of the integers is 14.
A little bit more involved, the text implies that you should use the remainder operator, which is how i will do it.
here is the code:
class ChapterTwoQuestionSix
puts "enter an integer between 0 and 999"
number = gets
number = number.to_i
num1 = number%100
num1 = num1%10
num2 = number /10
num2 = num2%10
num3 = number/100
puts num1 +num2+ num3
end
Good refresher on the remainder operator.. but so far not a whole lot of challenge. I have found that Ruby is so far in the basic sense very very easy to pick up on the language elements.
Chapter 3 is ahead and contains selections, which means some boolean stuff as well as if statements and nested if statements and etc.
There is quite of bit in my textbook that uses JOptionPane for input and output, which is from the Java swing class. I haven't looked very hard yet, but i have noticed that there doesn't seem to be an obvious Ruby version of Swing.. which is probably mostly due to the nature of what Ruby is used for in programming in general as opposed to Java.
As i get some time this week hopefully i can find a solution or a way implement some i/0 version in ruby.
Chapter 2 pt. 2
Moving forward in chapter 2.
The chapter goes into Data types and numeric operators, as well as numeric literals and shorthand operators.
From what i have read from the web unlike java there are two different numeric data types in Ruby, Integer and Float.
The remainder operator works differently in Ruby as well.
the statement -7%5 = 2 and not -1 like in Java.
7%-5 will however return -2
In Ruby the statement returns the same sign as the second operand.
Shorthand operators are very similar as well with a few differences.
It is important to note that you cant increment variables like in Java.
For example i++ or j-- etc.
You have to use += or -=
Here is a list of Ruby shorthand operators:
For example i++ or j-- etc.
You have to use += or -=
Here is a list of Ruby shorthand operators:
x = 1 this is basically a literal that assigns a variable of 1 to x
x += x this increments by one (because x is one)
x -= x this decrements by one (because x is one)
x += anynumber this increments by whatever number
x *= x this multiplies by x
x **= x raises to the power of
x /= x divides by
Chapter 2
Chapter 2 starts at looking how some simple programs are constructed, as well as assigning variables and getting input from the console.
Primarily it looks at the assignment of variables and the Java Scanner class.
The Scanner class in Java is used to get input and used to display output. Obviously the default input is the keyboard and the default output is the monitor.
Console input is not directly supported in Java , but you can simply import the Scanner class to create a Scanner Object and then read the input calling System.in.
here is an example:
Scanner input = new Scanner (System.in);
(input being the name of the new Scanner object that you created with the new operator.
I will copy some sample code from my textbook, describe what the code is doing, and then try and recreate the program in Ruby.
............................................................................................................................................
import java.util.Scanner;
// imports a class from java to handle the scanner input/output
public class RadiusComputation{
public static void main(String args[ ]){
// above is the class and of course our main method
Scanner input = new Scanner (System.in);
// this makes a new scanner and names it input, allows you to input a number
System.out.print("enter a number for the radius");
// this prints to the console the request for the user to enter a radius
double radius = input.nextDouble( );
// this takes the number from the console and assigns it to the variable "radius"
double area = radius * radius * 3.14159;
// this does the math involved in calculating a circle's area, and then it assigns it to
// the variable "area"
System.out.println("the radius of the circle is " + radius + "and the area is " + area);
// this is our ouput which takes the statement, adds in the variables and outputs to the screen
}
}
Now lets see if we can do this in Ruby!
class ChapterTwo
puts "what is the radius of the circle?"
radius = gets
# gets is our "scanner in"
radius = radius.to_i
# radius.to_i is how we convert the string to an integer in Ruby
puts radius * radius * 3.14159
#this is how we output the answer
end
puts "what is the radius of the circle?"
radius = gets
# gets is our "scanner in"
radius = radius.to_i
# radius.to_i is how we convert the string to an integer in Ruby
puts radius * radius * 3.14159
#this is how we output the answer
end
So in the end pretty simple! I learned a way in Ruby to get input and a way to convert a string into a number. Very nice.
Moving forward in this chapter we will be looking at getting input of multiple numbers, identifiers, order of operations, variables, numeric data types and more!
Saturday, August 13, 2011
Chapter One Programming Exercises
These are very simple and very lame and honestly probably not worth tackling at this point. I am going to do them anyway in an effort to be thorough and completist.
My plan as of now is to do all 7 of them in an alternating Java then Ruby pattern. If the need and or desire strikes i will add some comments after each.
now in Ruby
class ChapterOneOneRuby
puts "welcome to Ruby"
puts "welcome to Computer Science"
puts "Programming is fun"
end
Problem 1.2 Write a program that displays "Welcome to Java" five times.
public class ProblemOneTwo {
public static void main(String args[]){
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
}
}
now in Ruby
class ChapterOneTwoRuby
puts "welcome to Ruby"
puts "welcome to Ruby"
puts "welcome to Ruby"
puts "welcome to Ruby"
puts "welcome to Ruby"
end
Simple enough.
(the next two questions are very similar so i will skip to problem 1.5)
Problem 1.5 Write a program that displays the result of 9.5 x 4.5 - 2.5 x 3/45.5 - 3.5
Ruby handles simple Math operators the same way as Java.
You can either assign the result of the equation to a variable and then print the value of the variable or you can just pass the entire equation and have Java print the result... I have done the latter in this example.
public class ProblemOneFive {
public static void main(String args[]){
System.out.println (9.5*4.5-2.5*3/45.5-3.5);
}
}
almost exactly the same in Ruby
class ChapterOneFiveRuby
puts 9.5*4.5-2.5*3/45.5-3.5
end
So basically to summarize what was learned...
1. output in Ruby is the command puts
2. math works the same way
3. we dont need to define a main method in our simple ruby programs
We gotta long way to go, Chapter 2 is much more in depth and will have us doing a few more things, which will make for more interesting blog posts!
My plan as of now is to do all 7 of them in an alternating Java then Ruby pattern. If the need and or desire strikes i will add some comments after each.
Problem 1.1 Write a program that displays "Welcome to Java" , "Welcome to Computer Science" and "Programming is fun"
public class ProblemOneOne {
public static void main(String args[]){
System.out.println("Welcome to Java");
System.out.println("Welcome to Computer Science");
System.out.println("programming is fun");
}
}
public static void main(String args[]){
System.out.println("Welcome to Java");
System.out.println("Welcome to Computer Science");
System.out.println("programming is fun");
}
}
now in Ruby
class ChapterOneOneRuby
puts "welcome to Ruby"
puts "welcome to Computer Science"
puts "Programming is fun"
end
Problem 1.2 Write a program that displays "Welcome to Java" five times.
public class ProblemOneTwo {
public static void main(String args[]){
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
System.out.println("Welcome to Java");
}
}
now in Ruby
class ChapterOneTwoRuby
puts "welcome to Ruby"
puts "welcome to Ruby"
puts "welcome to Ruby"
puts "welcome to Ruby"
puts "welcome to Ruby"
end
Simple enough.
(the next two questions are very similar so i will skip to problem 1.5)
Problem 1.5 Write a program that displays the result of 9.5 x 4.5 - 2.5 x 3/45.5 - 3.5
Ruby handles simple Math operators the same way as Java.
You can either assign the result of the equation to a variable and then print the value of the variable or you can just pass the entire equation and have Java print the result... I have done the latter in this example.
public class ProblemOneFive {
public static void main(String args[]){
System.out.println (9.5*4.5-2.5*3/45.5-3.5);
}
}
almost exactly the same in Ruby
class ChapterOneFiveRuby
puts 9.5*4.5-2.5*3/45.5-3.5
end
So basically to summarize what was learned...
1. output in Ruby is the command puts
2. math works the same way
3. we dont need to define a main method in our simple ruby programs
We gotta long way to go, Chapter 2 is much more in depth and will have us doing a few more things, which will make for more interesting blog posts!
Chapter 1
Chapter one in my Java textbook is basically ten pages of basic Java information about the virtual machine and how Java is compiled and how to locate the API and etc..
The first real get down to it moment is about half way through, where they talk about the main method and then they build a simple "hello world" type program.
The program goes something like this:
public class Welcome{
public static void main(String args[]){
// displays java is da bomb in the console
System.out.println("Java is da bomb");
}
}
The top line of course defines the class, and we all know that Java programs must contain at least one class. It also notes that by convention class names start with a capital letter.
The second line of course outlines the main method. In order to run a class a program must contain a main method. The main method is where programs in Java are executed.
The third line is a comment line. Two slashes makes the compiler skip that line. It is there to communicate what the program is doing and for leaving messages in plain old English.
The fourth line is a method that tells the program to output what is in the parenthesis to the console. It is called System.out.println. Notice that the line ends in a semicolon, which in Java is the statement terminator.
Other things to note in this simple program are the curly brackets! These are for forming what is known as a programming block. Every class has a block that is used to group data and methods together. Every method also has a block that groups the code in the method together.
Soooooooooooo... how would we do this in RUBY???
Please wait while I Google "what is terms in red in ruby?"
Here is what i found....
a far as a class in ruby goes, you can define one as simply as typing in "class"
for example,
class Boogers
or
class Hockey
the naming convention is still the same for classes, you should start a class name with a capital letter.
With regards to the main method, in Ruby there is no need to define it or declare it.. without getting too deep into it (meaning take my word for it) it is there but you dont need to see it or know about it. (at least not at this point)
Long story short...Ruby will execute the code as it sees it!
Comment Line in Ruby can be done with a (#).
like this:
# this is the method that computes the radius
# displays cust name
no different than Java other than the // is now a #.
In Java we use System.out.println to output to the console, in Ruby you can simply use "puts"
like this :
puts " Ruby is the best language ever"
puts " your mother is not very attractive"
You might notice that in the above lines that there is no statement terminator!
Ruby doesnt use em.
The class block is ended with "end"
How simple is that?
Very simple.
Here is our first Java program translated to ruby.
class Welcome
# displays Ruby is way more bomb than Java in the console
puts "Ruby is way more bomb than Java"
end
That will conclude chapter 1.
I like it so far.
Now onto the excercises.
The first real get down to it moment is about half way through, where they talk about the main method and then they build a simple "hello world" type program.
The program goes something like this:
public class Welcome{
public static void main(String args[]){
// displays java is da bomb in the console
System.out.println("Java is da bomb");
}
}
The top line of course defines the class, and we all know that Java programs must contain at least one class. It also notes that by convention class names start with a capital letter.
The second line of course outlines the main method. In order to run a class a program must contain a main method. The main method is where programs in Java are executed.
The third line is a comment line. Two slashes makes the compiler skip that line. It is there to communicate what the program is doing and for leaving messages in plain old English.
The fourth line is a method that tells the program to output what is in the parenthesis to the console. It is called System.out.println. Notice that the line ends in a semicolon, which in Java is the statement terminator.
Other things to note in this simple program are the curly brackets! These are for forming what is known as a programming block. Every class has a block that is used to group data and methods together. Every method also has a block that groups the code in the method together.
Soooooooooooo... how would we do this in RUBY???
Please wait while I Google "what is terms in red in ruby?"
Here is what i found....
a far as a class in ruby goes, you can define one as simply as typing in "class"
for example,
class Boogers
or
class Hockey
the naming convention is still the same for classes, you should start a class name with a capital letter.
With regards to the main method, in Ruby there is no need to define it or declare it.. without getting too deep into it (meaning take my word for it) it is there but you dont need to see it or know about it. (at least not at this point)
Long story short...Ruby will execute the code as it sees it!
Comment Line in Ruby can be done with a (#).
like this:
# this is the method that computes the radius
# displays cust name
no different than Java other than the // is now a #.
In Java we use System.out.println to output to the console, in Ruby you can simply use "puts"
like this :
puts " Ruby is the best language ever"
puts " your mother is not very attractive"
You might notice that in the above lines that there is no statement terminator!
Ruby doesnt use em.
The class block is ended with "end"
How simple is that?
Very simple.
Here is our first Java program translated to ruby.
class Welcome
# displays Ruby is way more bomb than Java in the console
puts "Ruby is way more bomb than Java"
end
That will conclude chapter 1.
I like it so far.
Now onto the excercises.
Friday, August 12, 2011
I will be using Eclipse.
Just a quick note,
Since we have been encouraged to use Eclipse at school, i have started using it. As an unseasoned programmer i have no idea how well it works or what might work better. I only know that it does work and i kind of slightly know how to work it.
Therefore i have decided to use Eclipse for Ruby as well.
You can download Eclipse for free : http://www.eclipse.org/
There is some set up required, but i got everything up and running quite quickly, especially thanks to a post i found in the Eclipse forums.
Read this post if you need help getting your Eclipse "Ruby ready" http://www.eclipse.org/forums/index.php/t/174998/
If you are in South Africa and you see the Michelle that posted the instructions, please do tell her i said, "thank you very much."
I am off to play some hockey, but i will be back tommorrow with my first real post! You can look forward to some real fireworks as i begin my quest to work through my Java textbook using the new sleek, hip, cool, and awesome Ruby. Until then, keep your stick on the ice.
Since we have been encouraged to use Eclipse at school, i have started using it. As an unseasoned programmer i have no idea how well it works or what might work better. I only know that it does work and i kind of slightly know how to work it.
Therefore i have decided to use Eclipse for Ruby as well.
You can download Eclipse for free : http://www.eclipse.org/
There is some set up required, but i got everything up and running quite quickly, especially thanks to a post i found in the Eclipse forums.
Read this post if you need help getting your Eclipse "Ruby ready" http://www.eclipse.org/forums/index.php/t/174998/
If you are in South Africa and you see the Michelle that posted the instructions, please do tell her i said, "thank you very much."
I am off to play some hockey, but i will be back tommorrow with my first real post! You can look forward to some real fireworks as i begin my quest to work through my Java textbook using the new sleek, hip, cool, and awesome Ruby. Until then, keep your stick on the ice.
Thursday, August 11, 2011
Boom Boom Boom
I am a handsome man who recently decided to go back to school to study Computer Science, and as of this exact point in time I have exactly two programming classes worth of knowledge under my belt.
The university where I attend requires that everyone takes Java for their first three programming classes and so I have decided to start learning a second language outside of my course work. There are two reasons for this:
1. I enjoy this sort of thing.
2. I hope it makes it easier for me to get a job when i finish school.
I wish I had a wonderful reason to have selected Ruby to study along side of Java, but i don't. It does however seem new and fun and exciting.
I am going to use the exercises in my Java textbook as a base for learning Ruby. Which means that since I have already completed a majority of them in Java as homework for my classes I hope I can use that knowledge to speed up my Ruby learning process. (Time will tell!)
As of now, i do not have a Ruby textbook or a Ruby book to learn from. If I acquire one at some point I will document that here in my blog. My intention is to use information found on the Internet and of course Google, Google, and Google.
THINGS YOU SHOULD KNOW ABOUT THIS BLOG:
1. I am not a writer and I have no desire to be one. I will at the very least try to use the spellchecker.
2. I have no idea what I am doing most of the time... Be prepared to read about the answers and
the resolutions to dumb/obvious/ ridiculous questions that any idiot in my shoes should already know about programming/software/hardware and etc.
Hopefully I can do a decent job of documenting what I am learning for my own reference and
enjoyment.
I must say that after two semesters worth of Computer Science courses I have already learned a lot and met some really nice people who seem to really enjoy Star Wars and wizards and dragons and stuff.
The university where I attend requires that everyone takes Java for their first three programming classes and so I have decided to start learning a second language outside of my course work. There are two reasons for this:
1. I enjoy this sort of thing.
2. I hope it makes it easier for me to get a job when i finish school.
I wish I had a wonderful reason to have selected Ruby to study along side of Java, but i don't. It does however seem new and fun and exciting.
I am going to use the exercises in my Java textbook as a base for learning Ruby. Which means that since I have already completed a majority of them in Java as homework for my classes I hope I can use that knowledge to speed up my Ruby learning process. (Time will tell!)
As of now, i do not have a Ruby textbook or a Ruby book to learn from. If I acquire one at some point I will document that here in my blog. My intention is to use information found on the Internet and of course Google, Google, and Google.
THINGS YOU SHOULD KNOW ABOUT THIS BLOG:
1. I am not a writer and I have no desire to be one. I will at the very least try to use the spellchecker.
2. I have no idea what I am doing most of the time... Be prepared to read about the answers and
the resolutions to dumb/obvious/ ridiculous questions that any idiot in my shoes should already know about programming/software/hardware and etc.
Hopefully I can do a decent job of documenting what I am learning for my own reference and
enjoyment.
I must say that after two semesters worth of Computer Science courses I have already learned a lot and met some really nice people who seem to really enjoy Star Wars and wizards and dragons and stuff.
Subscribe to:
Posts (Atom)