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.


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");

}
}



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!


































No comments:

Post a Comment