Pseudocode, Trace Table, and Intro to Java

In this week, we further developed our understanding about pseudocode and flowchart, which we have learned a bit in grade 10. Also, we finally start learning the high-level programming language, java.

Here are some definitions of key terms that we learned this week.

  • Algorithm: It is a set of step-by-step instructions that is used to solve problem.
  • Properties of algorithm:
    • Finiteness: The algorithm should be able to be completed in a reasonable number of steps.
    • Definiteness: Each step of the algorithm should be unambiguously and rigorously defined
    • Input: Before the algorithm begins, there should be quantities given initially
    • Output: After certain steps of algorithm finish their implementation, there should be quantities that have special relationship with inputs
    • Effectiveness: the algorithm should be able to be done with basic principles in a finite range of time
  • Expressions for algorithm:
    • Natural language: Expressed in simple english. It can be easily understood by human beings.
    • Flow chart: Use graphical elements to show the logic of code. For example, rectangles, parallelogram, etc.
    • Pseudocode: Generic artificial language. It’s useful for users to pre-design the code before they actually write it.
    • Programming language: It is a type of artificial language that is used to communicate the computing system, such as Java, Python or C++.

Decision making process:
In order to generate algorithm, the decision-making process has always played an important role. The decision-making process includes four steps:

  1. Identification: To recognize, understand, and formulate the problem
  2. Development: During the process of seeking solutions to the problem, we can explore different approaches to the problem
  3. Selection: In order to address the problem in the most efficient way, we need to choose the most suitable alternative as final solution
  4. Implementation: After we choose the solution, the only thing we need to do is to carry it out.

There are some exercises of pseudocode on the slides, but because we already did them in grade 10, we skipped it.

Exercise

Linear&Binary search

We did some activities on battleship in this class to explore search methods.
As the picture shown, each student has 26 ships, labeled with specific letters and numbers. By having two students in a group, student A will first give student B a number, and student B will do the same thing. Then, two students start to guess which letter the ship has, corresponding to the number given by the other initially. If the guessed letter is wrong, the other student will tell the guesser what number the ship with guessed letter has. If the letter is correct, then the guesser wins the game.

Activity

When numbers are arranged in ascending orders, we are able to compare the inquiry number with original number, and then narrow down the potential range of ships. By halving intervals, binary search greatly helps us to enhance our efficiency for sorting.

Trace table:
In order to test whether the algorithm is valid or not, we use “trace table”, a technique that includes all conditions and variables of the algorithm and records outcomes of the trial test process.

Trace table

So the steps to create a trace table is to first write down all the variables in the code, then put the initial value in the first raw. Follow the logic of the statement and write the result after each statement, then output the final value.

Flowchart:
This is also a concept that we have learned in grade 10. But we add two new concepts here.

  1. Method Call: We use a name to represent a function of a group of instructions, instead of more detailed content (abstraction).
Screen Shot 2018-10-27 at 11.12.05 PM.png

2. Connector: If there is no enough space when we are drawing flowchart, we can use connector by drawing two dots at two different places to connect separate parts and continue the flowchart.

Screen Shot 2018-10-27 at 11.12.09 PM.png

The picture above includes all the representations for flowchart.

Flowchart Homework

Intro to Java:

What is Java?

  • It is a high-level programming language
  • Source codes are written on a plain text with java. extension
  • It has Java Virtual Machine (platform dependent), including compiler and interpreter, to run source codes on computers:
    • Compiler: converts source into bytecodes (.class file)
    • Interpreter: converts bytecodes into machine language
    • Has API (application programming interface): contains a collection of packages, providing useful capabilities (arithmetic calculations…)
    • Case sensitive & strictly typed: For example, a and A is different in Java

In-class Quick Search
Java SE vs. Java EE:

  • Java SE stands for Java Standard edition, mainly used for designing desktop applications
  • Java EE is built based on Java SE platform, which provides API and runtime environment for developing and running applications

JDK vs. JRE:

  • JDK stands for Java Development Kit, which includes everything of JRE, compiler, and other tools, capable of creating programs.
  • JRE stands for Java Runtime Environment, it is a package including various things: JVM, Java Class Library, Java command, and other infrastructures.

Integrated Development Environment (IDE)

  • It is a software application that consist of compiler, program compiler, and a run-time environment. All these components are combined by Graphical User Interface.
  • With all these facilities, IDE is used to develop programs.

I tried Eclipse during summer holiday. In our class, we use BlueJ.

Helloworld
As usual, our first program is called HelloWorld.

HelloWorld
  1. The first 7 lines are basic descriptions of this program, which are convenient for programmers to know the main purpose, author, and also version.
  2. The section between {} is called code block. For example, section between line 10 and line 15.
  3. In line 10, we have “public static void main(String [] args)”. This is necessary for all Java programs.
  4. In line 12, we have a statement ends with a semicolon: system.out.println(); to print out the result.

Three types of error:

  1. Compiler errors:

Usually caused by syntax error. For example, for the “HelloWorld” program, if I delete the semicolon after the “System.out.print()” statement, it will appear an error message.

Screen Shot 2018-10-30 at 9.04.28 PM
  1. Run-time errors:

This type or error occurs during the process of running the program, this error is caused by invalid data or incorrect use of prewritten classes.

  1. Logic errors:

This type of error causes the program to be operated incorrectly.

Typical Program Structure

  • In a program, we have input, processing, and output.

General steps of writing and implementing a program:

  1. Identify data we know
  2. Identify data we need
  3. Declare variables’ names
  4. Initialize the value of any possible identifiers
  5. Processing the data
  6. Output the final result

Good Programming Conventions
Similar with previous parts, in order to have an efficient programming process, we need good practices:

  1. Beginning of every program should include description of program’s general purpose, author’s name, and the latest modification date.
  2. // for single-line comment and /* for multiple-line comment
  3. Use white space (space, tab, newline) to increase readability
  4. First letters of all words in class names should be capitalized, and there should be no space between words.

Identifier

Identifiers are names given to things like variables, methods, and classes.

  • Declaration of a variable: In order to declare a variable, we need to include name, type (int, double, boolean…), and specific value that is assigned to this variable.
  • Name: It must begin with a lower-cased letter, a dollar sign, or an underscore (_). In addition, except the first word, all words’ first letter should be capitalized. Numbers are permitted, but no space and other special characters.

Java Keyword

In order to carry out different functions to attain different purposes, we have different keywords in Java, which cannot be used as identifiers. Here is a chart of frequently used keywords:

See the source image
Java keywords

Primitive Data Type
Primitive data includes following 8 types:

  • byte: shortest integer number we can declare (8 bits)
  • short: (16 bits) medium integer
  • int: (32 bits) longer integer
  • long: (64 bits) longest integer
  • float: (32 bits) for decimal numbers
  • double: (64 bits) compatible with all primitive data type except Boolean
  • boolean: true & false
  • char: stands for character (16-bit Unicode)

Java Operators:
Java operators are special symbols that are used to perform special operations. There are four types of operators:

  1. Arithmetic operators: carries out calculations
    1. Add, subtract, multiply, divide
    2. Operation precedence (same with precedence in math)
  2. Concatenation (+): used to connect Strings
  3. Logical operators: operating on Boolean values
    1. &&(AND): if both conditions are true, the result is true; otherwise it is false.
    2. ||(OR): The result is true if both or one of conditions is true.
    3. !(NOT): if the condition is false, then the result is true
  4. Assignment operator (=): we use this operator to assign value on the right side of the operator to variable on the left side.

Conclusion
Computer science continues to make more sense as we can connect all different knowledge together, like logic gates in Java. It’s so excited to get in touch with Java, I hope I could continue improving my programming skills so that I can be better prepared for IA.

Leave a comment