Forum Posts

cloudiiiday
Feb 17, 2022
In Introduction to Java
Today, we learned about functions/methods. When you have a long program, it can help to break it down into smaller groups of functionality. A function/method groups a sequence of statements and provides a well defined, easy to understand functionality which can be reused. A method takes input, performs an action or a series of actions, and produces an output. In Java, each method is defined within a class. An example of a standard method is System.out.println(). To define your own method, you must declare it with a header. class MyClass { static int methodName (int parameter1, int parameter2){ //This is the header //The method body goes here } } You close the body of a method with a return statement. The return type of a method indicates the type of value that the method sends back to the calling location. A method that does not return a value has a void return type in the heading. The return statement specifies the value that will be returned. At a given point, the variables that a statement can access are determined by the scoping rule. The scope of a variable is the section of a program in which the variable can be accessed (also called visible or in scope). There are 2 types of scopes in Java, class scope - a variable defined in a class but not in any method, and block scope (also called a local variable) - a variable defined in a block of a method. Method overloading is when a class defines multiple methods with the same name. It is usually used to perform the same task on different data types. However, the complier must be able to determine which version of the method is being invoked. This is done by analyzing the parameters, which form the signature of a method which includes the type and order of the parameters. In object oriented design, methods are grouped together according to objects on which they operate. An object has state, or descriptive characteristics, and behaviors, what it can do (or be done to it). A class contains data declarations and method declarations. Homework: https://docs.google.com/forms/d/e/1FAIpQLScni2F2ENq4glTeg7PGb-ZFSqRyp5lA-Ajz-6raeK18Dkyotg/viewform?usp=sf_link
0
0
2
cloudiiiday
Feb 03, 2022
In Introduction to Java
A 2D array is an array of arrays, It can be thought of as having rows and columns such as a matrix with both row and column starting from 0. To declare a 2D array, you need 2 sets of brackets and 2 size declarators. double[][] heights = new double[4][300]; //This 2D array would contain 4 rows and 300 columns. To initialize a 2D array using a list of values, you need to enclose each row's initialization list in its own set of braces. int[][] numbers = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; Nested loops are used in programs involving 2D arrays. The outer loop is used to traverse through rows while the inner loop is used to traverse through columns. double[][] heights = new double[4][300]; Scanner in = new Scanner(new FileReader("inputData.txt")); for (int row = 0; row < 4; row++){ for (int col = 0; col < 300; col++){ heights[row][col] = in.nextDouble(); } } Because 2D arrays are arrays of 1D arrays, the length of a 2D array is the number of rows. Because each row is a 1D array, the length of each row tells how many columns there are. Each row can also have a different number of columns which is called a ragged array.
0
2
4
cloudiiiday
Jan 06, 2022
In Introduction to Java
Homework: https://docs.google.com/forms/d/e/1FAIpQLScH8YbvVv1uYnJsa4XtecECADk0RZFmA4tWWxLQ67R_nrB9pw/viewform?usp=sf_link Do loop: do loop example: int count = 0; do { count++; System.out.println(count); } while (count <5); Nested loops: Similar to nested for statements, while loops and do loops can be nested as well "Nested" means the inner loop must complete reside in the outer loops - no overlapping For each iteration of the outer loop, the inner loop iterates completely How to hand trace a program for debugging: Your code will go wrong so debugging and testing are always needed You can debug (troubleshoot) by: Hand tracing Adding print statements to your code Adding testing methods to the class Hand tracing (tracing by hand) is a simulation of code execution in which you step through statements and tack the values of certain variables. It is effective with a short program or part of a method. When you hand trace code or pseudocode, you write the names of the variables on a sheet of paper, mentally execute each step of the code, and update the variable values accordingly. When you use print statements for debugging, you insert print statements at key locations to show values of significant variables and how far your code got before there was a problem. In the print statement, it is a good idea to specify the location of the trace (what method) and the variable name and its value. All Integrated Development Environments (IDE) such as Eclipse have an interactive debugger feature. You can single-step step through your code (one statement at a time), see what is stored in variables, set breakpoints, and watch a variable or expression during execution. Phases of Programming A typical programming task can be divided into multiple phases Analyzing and solution design phase: Analyze the problem to understand data and constraints, produce an ordered sequence of steps that describe the solution of a problem This sequence of steps is called an algorithm Implementation phase: implement the program in some programming language Debugging and testing phase: test the program thoroughly, debug and resolve defects Steps in Solution design phase Produce a general algorithm You can use pseudocode (pseudocode is an artificial and informal language that helps programmers develop algorithms, it is very similar to everyday English) Refine the algorithm successively to get a step by step detailed algorithm that is very close to a computer language Turn pseudocode into flowchart to verify the designed solution works correctly in all cases Flowchart A schematic representation of the algorithm or problem-solving process A graphical representation of the sequence of operations in a program (it can have different levels of granularity) Shows logic of an algorithm Emphasizes individual steps and their interconnections Different symbols are used to draw each type of flowchart
0
1
5
cloudiiiday
Dec 09, 2021
In Introduction to Java
In today's class, we learned about for loops. Iteration Statements: Allows us to execute a statement multiple times Often referred to as loops Like conditional statements (if-else statements), they are controlled by boolean expressions Java has 3 kinds of repetition statements for loop while loop do loop Programmers should choose the right kind for the situation The for statement for(initialization; condition; increment){ statement; } The initiation is executed once before the loop begins The statement is executed until the condition becomes false The increment portion is executed at the end of each iteration Best for executing statements a specific number of times that can be determined in advance Example: for(int count = 1; count <= 5; count++){ System.out.println(count); } Homework: https://docs.google.com/forms/d/e/1FAIpQLScHJ_8ym3qeoHIhRtyHRN35yJIJIxKJ3uEkgS6ThiEmD8GahA/viewform?usp=sf_link
0
1
10
cloudiiiday
Nov 18, 2021
In Introduction to Java
Often we have to compare different expressions or variables. Boolean variables only have 2 values, true and false or 1 and 0 (1 corresponds to true, 0 corresponds to false). Bitwise operations: AND, * (logic product) 0 * 0 = 0 1 * 0 = 0 0 * 1 = 0 1 * 1 = 1 OR, + (logic sum) 0 + 0 = 0 1 + 0 = 1 0 + 1 = 1 1 + 1 = 1 NOT, ! (logic negation) !0 = 1 !1 = 0 XOR, ⊕ or ^ 0 ^ 0 = 0 1 ^ 0 = 1 0 ^ 1 = 1 1 ^ 1 = 0 XNOR, ⊙ (opposite of ⊕) 0 ⊙ 0 = 1 1 ⊙ 0 = 0 0 ⊙ 1 = 0 1 ⊙ 1 = 1 Order of Precedence: () > ! > * > ⊕ = ⊙ > + Boolean Expressions: == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to Logical Operators: ! Logical NOT && Logical AND a&&b is true only if both a and b are true || Logical OR a||b is true if a or b or both are true Comparison: char types are compared by ACSII values Strings are compared by ACSII values of each character from left to right When paring two double or float values, we often compare the difference with a small enough value because of round off errors A boolean variable x == true is the same as x Selected Operators and their Precedence: [] Array element access ++, --, ! Increment, decrement, Boolean not *, /, % Multiplication, division, remainder +, - Addition, subtraction <, <=, >, >= Comparisons ==, != Equal, not equal && Boolean and || Boolean or = Assignment
0
2
6
cloudiiiday
Nov 04, 2021
In Introduction to Java
Today in class, we started off by talking about the difference between strings and characters. Characters just store one character at a time but strings are made up of multiple characters. Each character has an integer it corresponds to. You can see which character corresponds to what integer here: https://www.asciitable.com/ Concatenation means combining different things into one string. The operation we use for this is a plus sign (+). For example, String a = "Hello"; char b = 'a'; int c = 1; System.out.println(a + b + c); would print out "Helloa1". We also learned about other string operations. String length and index: length() A character of a string: charAt(): Substrings: substring() Search for a substring: indexOf(), lastIndexOf(), contains() Comparing strings: compareTo() For example, String a = "Hello World"; System.out.println(a.length()); //This will print out "11" System.out.println(a.charAt(2)); //This will print out "l" System.out.println(a.substring(1, 4); //This will print out "ell" System.out.println(a.indexOf("Hello")); //This will print out "0" System.out.println(a.lastIndexOf("l")); //This will print out "9" System.out.println(a.contains("World")): //This will print out "true" Here are some methods to compare strings: boolean equals() boolean equalsIgnoreCase() int compareTo() int compareToIgnoreCase() trim() toUpperCase() toLowerCase() Here's the homework for this week: https://docs.google.com/forms/d/e/1FAIpQLSf6_GW5MSvlQv6rjZsBrOsT2P7xHRcDE_V6Z69J7jWWqmEYiw/viewform?usp=sf_link
0
1
6
cloudiiiday
Oct 14, 2021
In Introduction to Java
Today in class, we started by learning how to get user input: First, we need to import java.util.Scanner; Then, we name the Scanner: Scanner in = new Scanner(System.in); System.out.println(in.next()); This will print the next string you input. Ex: If you input "Hello World", only "Hello" will be printed. System.out.println(in.nextLine()); This will print the next line you input. Ex: If you input "Hello World", it would print out "Hello World". You can also do in.nextInt(), in.nextDouble(), etc. We can also learned how to get input from a data file: A data file is a plain text file (a common suffix is .txt or .data). These files don't have any font or size formatting information. Data files often have multiple data columns, separated by commas, white spaces, or other characters. These separators are called "delimiters". Reading data files: import java.util.*; import java.io.*; class Main{ public static void main(String[] args) throws Exception { File theInput = new File("input.txt"); BufferedReader br = new BufferedReader(new FileReader(theInput)); String a = br.readLine(); //this will read the first line in input.txt System.out.println(a); } } Homework for this week: https://docs.google.com/forms/d/e/1FAIpQLSe1hdcr7ZYGrf7N7bEJD17EXkpqo5ckWecXsY-QhmDl1CcIPQ/viewform?usp=sf_link
0
0
9
cloudiiiday
Sep 30, 2021
In Introduction to Java
Here is a recap of what we learned in class today: -Literals and data types Literals are numeric values that are used to compute. Arithmetic equations include variables, literals, and operators. If you mix different types of data, all "lower" types will be automatically promoted to the "higher" type in the operations. For example, if you divide an integer by a double, the result would be a double. -Casting If you want to specify the types of a variable or expression, you can use explicit type casting. For example, (double)1729/10 = 172.0, but ((double)1729)/10 = 172.9. -Assignment In coding, the equal sign "=" has a different meaning. Instead of meaning "equals", it means assigning the right side of the equation to the left or "assignment." For example, x=x+1 would mean adding 1 to x then assigning the value back to x. There are also shortcut operators for this purpose like "++" and "--."
2
2
13
c
cloudiiiday
More actions