Multidimensional Arrays

You can access the individual 1D arrays in a multi-dimensional array using the .length property of arrays.

arrayName.length is the number of rows

arrayName[rowNumber].length is the number of columns in the given row

To access each individual element in a multidemensional array, you need to use nested for loops.

In a basic formula, you would do so like this:

for(int row = 0; row < arrayName.length; row++) {

for(int column = 0; column < arrayName[row].length; column++) {

}

}

 

APCS(S)- (D)Encryption using String class

Assignment description:

Write a program with 3 classes in Java for encryption and decryption using the String class methods such as “length”, “charAt” and loops.

class ImitationGame (with main method)
class Cipher (takes in one String and store the encrypted text as a field variable)
class Decipher (takes in one String and store the encrpted String) as a field variable

Complete the program and upload the code to github. Write a blog post describing and explaining how you completed the program and all resources (e.g. stackoverflow) you used. Avoid copying directly from other sample code.

Sources that I used throughout this project:

https://stackoverflow.com/questions/16458564/convert-character-to-ascii-numeric-value-in-java

https://beginnersbook.com/2013/12/java-string-substring-method-example/

How I completed the program:

https://github.com/Angelica190456/ImmitationGame2-no-substring-

https://github.com/Angelica190456/ImitationGame

The first thing that I did was create the program that didn’t utilize the substring function. Here, a string is taken and encrypted into a “secret” (it can easily be figured out) code. All it does it shift the letters in the string by one letter in the alphabet. How it works is through a single method that I either called decipherCode() or convertCharacter(). A for loop is used to go through each char in the given string. The char are of course being read by the charAt() function. With each char, I had to then figure out how I could shift the letter by one in the alphabet. Initially, I thought arrays could be my solution. But that became very complicated and I didn’t really feel it would be efficient to teach myself a whole other topic. Instead, I turned to ASCII. Each char would be converted into an int through casting it, one would be added to value, and re-converted back into the next char in the ASCII chart. Finally, the method would print the result.

The second source code pretty much does the same thing, but the reason why I created it was so I could create a purpose of using substring in my code. This one became a lot more complicated than the first program I typed up. In this one, users are able to select what parts of the string they would like to encrypt or decrypt rather than automatically acting on the whole string. This is done by taking in two numbers (indexes). At first, I considered just making users have to put in those two numbers in the source code as a parameter to some kind of method. But I decided to go a little bit further and allow them to input it through scanner. I created two extra methods, where each of them have the purpose of enabling people to input a number (one to select when to start encrypting/decrypting the string and one to select when to end encrypting/decrypting the string). Then I used the substring function, where the two numbers inputed by the user become the parameters of the method. The way the actual method of encrypting and decrypting the message is very similar to the original program I created. The major difference lies in an extra if/else statement and change in the way I wrote my for loop.

 

 

post test reflection (Unit 2-Classes and Objects)

 

  • I forgot to include the main method (just a careless mistake)
  • I have to work on declaration of variables, get and set statements, private and public variables, the whole application of super classes, inheritance, and sub classes, use of super();
  • My biggest take aways:
    • What I didn’t understand specifically is why we use get and set statements. So to be honest, the reason why I didn’t do very well on those parts is because I didn’t think it was necessary as long as you declare all instances as public. But now it has been clarified that on the AP exam and in programming in general, instance variables should almost always be private. Thus, getters and setters are needed to set or get the value of these instance variables.
    • Now, I also better understand how to use super() (it is still not 100% clear though). Generally, it is used when you want to use methods (especially for constructors, which can’t be inherited with just the key word extends) of the superclass.

Matrix-Homework

  1. “Ignorance is Bliss” (https://www.youtube.com/watch?v=OLv6ycYcpGI)
    In this scene, try to explain what’s happening in Cypher’s mind when he “tasted” the beef. How is it similar to a class and an object?
  • Object=car, house, person, dog (structures with states/behaviors) 
    • Objects contain properties (state): colour, size, etc
    • Objects contain behaviours: what actions it can do
      • example: object=dog,
  • Objects in programming:
    • behaviours=represented by methods 
    • states=represented by variables 
    • objects are organized into classes
    • objects=instances (instances of a class) 
    • data in object=instance variables 
    • objects in same class have the same properties, the properties only have different values
  • Classes=blueprints, template to make the object
    • Specifies variables that go in the object, how an object of this class should be created
    • doesn’t actually define any data
  • The mind is like computer reading the code.
  • The beef is the object. It has many properties, for instance, the juiciness, the taste, and the colour. 
  • And the class in this situation could be food or meat. But overall, it has to be something so that a bunch other things in the matrix have many things in similar with the steak. 
  1. The Gun scene (https://www.youtube.com/watch?v=lPOXR4dXxDQ&start=1120&end=1142)
    Are the guns in this scene “real”? What was the operator doing in this scene for Neo and Trinity? Explain the processing using the OOP concept.
  • Object oriented programming: just a style of computer programming
    • Centered around using objects 
  • The operator was adding guns into the program, which are not real. But they are real enough so they behave like the object in real life. In the end though, it’s just code.
  • So the operator created a class. It was probably defined as something like guns, and the objects under the class were the types of guns. 
  1. “The Constrcut” (https://www.youtube.com/watch?v=Dm-nf1FZsWY)
    What is the “Construct”? In java there’s something with a similar name called  constructor”. Describe both the “Contract” (In the movie Matrix) and the “constructor” in Java language. Explain the function of a “constructor” in a class.
  • According to the Matrix, the construct is the loading program.
  • Constructors are used to initialise (assign values) variables of objects in java

ADVANCED QUESTIONS

There are other reference to a computer system in the Matrix Trilogy (The Matrix, The Matrix Reloaded and The Matrix Revolution). Try to explain how they correspond to modern computer system.

  • The Merovingian
  • Agent Smith-virus
  • The Keymaker
  • The Trainman
  • The Architect
  • The Oracle

What we learned-August 29, 2017

Calculator:

Today, we reviewed in class how to create an operator on BlueJ.

In order to make it, we had to learn a couple of things such as how to get user input, doing something with that input using operations, and we got a little taste of conditional statements towards the end.

Scan Function:

The Scan function is used to to get user input. In order to use it, you have to import the scan function library. A library is kind of like pre-made bits of codes that you can use easily. In this case, you will need this at the very  top of your code: import.util.Scanner;

And to actually use the code, it goes something like this:

Scanner name = new Scanner(System.in);
dataType variableName = name.nextDataType();

Example:

Scanner magic = new Scanner(System.in);

double a = magic.nextDouble();

Operations:

The four basic operations are +, -, *, /

Concatenation:

This means to link something together, usually a string and something else. And the symbol is represented by a + sign.

For example, if you have:

String yourName=Michelle;

System.out.println(“My name is ” + yourName);

It’s going to print out: My name is Michelle

Type Casting:

When you have multiple data types in one line, they are all going to be converted into one kind of data type by the computer. It depends on what data types you have and what comes first.

Here are a few of the situations we discussed in class:

double a=3.5
int b=2

System.out.println(a + b); will print out 5.5

System.out.println(“The sum of the two numbers is: ” + a+b) will print out The sum of the two numbers is 3.52
-What happened was a+b got evaluated as a string
-To print “the sum of the two numbers is 5.5″ you have to put a brackets around a+b

System.out.println(a+b ” is the sum of your two numbers”); will print out 5.5 is the sum of your two numbers.

To summarise, the few basic rules we discussed in class include:

  • If you have a string first, everything else will be converted into strings. If you want the things after the string to remain as their original data type, you must put brackets around them.
  • If you have just numbers with different data types, the most precise data type will be used.
  • If you have an operation first, it will calculate the result, then it will print the string.

 

What we learned-August 25, 2017

Data Types:

Today we learned about data types and their importance. If you were to enter one, the computer won’t know what kind of one it is. Is it 1, ‘1’, or “one”? Therefore, data types are used to indicate what kind of data that is. And some of those types we studied today include integers, doubles, floats, and booleans.

Integers store whole, positive or negative values. They range from around negative two million to positive two million.

Booleans store whether something is true or false.

Doubles store decimal point numbers. Floats do the same thing, however, doubles are more precise than floats. When you use floats, computers store those values in scientific notation, except the base is two. In doing so, we humans use are not represented in the same way through a computer. For example, 0.1 is actually 0.0001100011…and it actually goes on forever. But not only that, computers only store a certain amount of digits after the decimal. So some numbers may get cut off. They just don’t understand recursion in the same way people do.

 

What we learned-August 17, 2017

August 17, 2017

Methods

We learned about what a method is and the importance of a main method. A method is basically like a function, which means it’s like a group of statements that perform some kind of operation. But in order for you to run your program you have to ensure you have a main method. Because without it, the computer will not know where to start executing the code.

Arguments

Additionally, we learned about what an argument is. In the process, some of us may have bumped into the concept of a parameter since the two terms are very similar. However, they are not the same thing. While a parameter is basically the variable being passed, the argument is the value of the variable or simply a value if there was no variable given to it.