Wednesday, January 28, 2015

A Simple Factorial Program

      For this assignment, we needed to make a simple program to show what we have learned so far in our knowledge of Java. For this, my partner and I created a program that will calculate the factorials of a number. The factorial command is represented by a ! mark, and is a mathematical function that multiplies the factors of a number together.

For example Factorial two, or 2! would be 2*1, which equals 2. Factorial 4 (4!), is 4*3*2*1, which is 24. As the numbers get higher, it becomes quite the ordeal to multiply these out, and a factorial calculator becomes very useful.

The code is displayed below.


Because of the limitations of the standard int (integer) variable type in Java, the maximum our calculator can calculate without errors is factorial 13, as any number larger than that creates a value too large for the standard Java variable to handle.

To make this program work we used For loops to run the logic of the actual factorial maths. We learned about for loops many times through the different languages we have learned so far and they proved to be invaluable in making the code more efficient. At the end of the code, the result is returned to the user as all of the factorial values from 1 to 13.

Head First Java Chapter 3

In Chapter 3, we learned all about variables, how they worked and how they are useful.

There are three basic operations we can perform on variables.

We can declare variables, which is stating what a variable "is", its type and name.

We can assign values to them, by using an equal sign.These values can be three different things: They can be a value ( X = 10), you can assign another variable to a variable (X = Y), and  you can assign an expression to a variable (X = X + 5).

We can also use the variables in our code. The dog code above helped me with understanding variables, as I had to actually use variables in my code, and actually writing some code really helps me learn and understand the concepts that the chapter is teaching me.

When naming variables, You must follow certain criteria.
A variable:
  1.  Must Start with a letter, underscore, or dollar sign only.
  2. Can contain numbers, just not start with a letter.
  3. can be anything other than reserved java words

Head First Java Chapter 2

The second chapter of this book is all about objects, and how objects are beneficial to to the programmer. Objects make the program much more organized as opposed to procedural programming. With objects, you can reuse your code many times and greatly reduce the time it takes to write the code and the amount of code you have to write. The object is a completed version of a class, like a recipe to a Chocolate tart.

Head First Java Chapter 1

In the first chapter of Head first, I learned a few things.


  • I learned how java is a language that is very nice because it is portable, meaning that you can write it anywhere and run it everywhere.
  • java has an easy syntax that is similar to python, which is a a language that we have previously worked with.
  • All java applications are in terms of classes and objects
  • classes hold all of the methods of your program, and methods are organized by the curly brackets
  •  
 In the chapter we made a program that sang the 99 bottles of beer song, with the code below
This code creates one class, and the variable beer, with a value of 99. The string word changes to bottle if the value of beer is 1, and then prints the song and subtracts the value of beer by one. when the value of beer is 0, it prints that there are no more bottles of beer on the wall.

This program helped me understand the syntax of the java language, how classes and methods work, and how the compiler of java works, as java creates its own virtual machine and runs the code within the machine. We also looked at the phase o matic program, that showed how lists and arrays work, and i learned that they are very similar to python.

Thursday, January 22, 2015

1.3.7: For Loops

1.3.7:  For Loops

Hangman Game:


Lottery Ticket Game:



1. Repeating the code in this way would slow down the program significantly. Having all of those extra lines of code would take a long time to go through, an although the code would function the same, it would slow down the processes, wasting resources. The extra lines would also cause the program to be bigger, which isn't a big deal with small programs, but with a larger piece of software, the size difference would be immense.

2. If you were processing large amounts of data from a single source, such as sales from a store in a chain, loops like this would be much easier to update sales and maintain live updates from stores.


3.      Since iterations do the same actions on every piece of data they receive, processing a large amount of data that needs the same operations performed on each piece, iterative loops are the most efficient way to do this

1.3.6: Lists and Tuples

1.3.6: Lists and Tuples

1: Below are examples of a string, tuple, and list of characters, respectively: 

a = 'acbde'
                 b = ('a', 'b', 'c', 'd', 'e')
                 c = ['a', 'b', 'c', 'd', 'e']
  a[2] is a part of a string and thus cannot be indexed like a list or tuple, while b[2] is in a tuple, so it is not able to be changed. c[2] is in a list, so it may function like a tuple, but it can also be changed.



2: The reason for having multiple variable types is to be able to have the most efficient way of storing data within a program, so you can find the most efficient way to solve the problem you have. Having multiple variable types allows you to solve problems more quickly, and having more options in your code is better to become a better programmer

1.3.5: Strings


1.3.5: Strings




For this assignment, we were required to create a program that would check if a tweet satisfied all the requirements that were previously set out. The tweet needed to to be less than 140 characters and contain a question mark, comma, double quote, and exclamation mark. 


Conclusion:
    1: The sentence has 41 characters (including spaces), and it makes no difference whether the characters are stored

    2: The outcome of the code is "d an" because of the assigned variables and the way in which they are switched around,as you can see in the code below.


In []: a = 'one string'
In []: b = 'another'
In []: c = a[:3] + ' and ' + b
In []: print(c[6:10])