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.
Calculator
https://gist.github.com/Angelica190456/7160f3a2ca2f542a7a9b0ddf59c5a9a4
HelloWorld code
https://gist.github.com/Angelica190456/c3e442ce3b53e20bbc0df61eed21d463
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.
What I learned today
August 21, 2017
- We learned a lot of commands we can use in terminal. With those commands we wrote some code within the text editor of the terminal. Whenever you executed the code, it will ask you for an input and print out Hello __!
- cd=navigate somewhere
- mkdir=create folder
- nano=enter text editor within terminal
- less=see contents of file
- ls=list out contents within folder
- javac=compile code
- java=execute code
- We also learned about compiling and interpreting. To better understand the concept, we used an analogy about frozen and fresh pizzas. When you compile, you first have to develop and write the source code (getting a chef to follow a recipe and make a fresh pizza). Then you have to compile it (the chef has to now bake it). And you can now only execute that source code on that one type of platform (you can only eat the pizza at the restaurant, you can’t get take out). However, in interpreting, you have the developers make the source code (make a frozen pizza). Then anyone can come and buy the frozen pizza and bake it in the oven. In this case, we will be using Java Virtual Machine. This is so that we can use the code on another platform.
|