David Prieto
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Start point with Java :::info This is a part of preparation for Paper 2 in IB Computer Science diploma. The lessons are 1) [Start point with Java](/S3oXtcWLQduOy_R5_03Z2g) 2) [The concept of Object in OOP](/6FsqJbw3SKq5m1NKk56U3Q) 3) [The POJO (Plain Old Java Object) and their common methods](/zJgmWQUbSLqf3JIiVehBNA) 4) [Relationship between objects and UML diagrams](/5rUt1ACuTQGF7nuLhvx7TA) ::: Java is a High level programming language. By high level we refer high level of abstraction. In this case the abstraction is that doesn't care about the machin is made to. It's a compiled then run programming language. It compiles into a "bitcode" program that is going to be run by a Virtual Machine (Java Virtual Machine, to be specific) :::info **Virtual machine:** Is software that emulates a type of machine. That machine that it's emulated can also have their own OS (or maybe not). Examples of Virtual Machines are JVM but also old consoles emulators. Also these VM can be accessed sometimes online. _example of when I used VM for the TSB_ ::: The purpose of the JVM The Java Virtual Machine is a software that interpret the bytecode compiled from a java IDE. This software is different for different OS and devices. But the idea is the same program would run exactly the same in both OS using this layer of the VM ![imagen](https://hackmd.io/_uploads/rJ07SM_61g.png) _Quick paint and mouse used here. Still better than some AIs_ Their motto is "compile once run everywhere" ## Characteristics of Java One of the main characteristics of Java is that is a highly typed programming language (unlike python or javascript) This means that every time that we declare a variable we need to define also its type. When we declare a variable the word that it's just before it is the type. ```java String name = "M"; int height = 145; boolean empty = true; Student jcr = null; ``` Also statements ends with a semicolon (";") ## The Main class, the strart point The entry point of any java app is the Public static void Main function (method). Whatever is inside of it will be executed. So the most basic program in java would be this one: ```java import java.util.*; public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); } } ``` :::danger :warning: For now you don't need to understand everything that is about the public static void Main, but in the end you would need to understand it. ::: So a simple program would be like this: ```java= import java.util.*; public class Main { public static void main(String[] args) { System.out.println("Hello, World!"); int x = 5; int y = 6; x = x +y ; System.out.println(x); } } ``` ### Comments In java comments are written with //. If you want to do several lines comment you can do `/*` and finish with `*/` ### Output We're not going to deal with all the details but for outputing we're going to use the console with `System.out.println(stringToBeOutput);` ### Workflow (if/else and derivates) In Java the structure of an if is similar to pseudocode and C++. You put the condition in parethesis and what happens is between curly braces. ```java if (condition) { //this happens only if the condition } //this happens all the time ``` We also have if/else and also if/ else if ```java if (condition1) { //this happens only if the condition1 applies } else if (condition 2) { //this happens if conditon 1 is not met but condition 2 is met } else{ //if both conditons 1 and 2 are not met this happens } ``` ### Iteration #### While loop While loop is while is similar to all the whiles in other programming languages and pseudocode. Evaluates a certain condition and while this condition is met it repeats the block of code inside of it. ```java while (condition) { //this is repeated while the condition is met (can be forever) } ``` Exercise: With this information _use a while loop_ to add all numbers from 1 to 100 and output it: :::spoiler ```java= import java.util.*; public class Main { public static void main(String[] args) { int a=1; int sum=0; while(a<100) { sum=sum+a; a=a+1; } System.out.println(sum); } } ``` Credit: C.R. The result should be 5050 ::: #### For loop In Java the for loop has 3 statements inside The first statement is the iteration variable declaration (usually int i = 0). This is a local variable whose scope is going to be just the for loop. The second statement is the staying condition. It's a condition that needs to be met in order to continue in the loop. (usually i < than certain threshold but varies) The third statement is the step. This is executed at the end of each iteration (each loop) and usually is dedicated to modify the iteration variable. The most common is add one to the variable or substract ```java for( int x = 0; x < 100; x++) { //statements that are going to be repeated } ``` :::success x++ is the same as writing x +=1 and it's the same as writing x = x +1 ::: Exercise: With this information _use a for loop_ to add all numbers from 1 to 100 and output it: solution in the spoiler: :::spoiler ```java= import java.util.*; public class Main { public static void main(String[] args) { int sum = 0; for (int i = 1; i <= 100; i++){ sum += i; } System.out.println(sum); } } ``` Credit P.H.F. The result should be (still) 5050 ::: ### Exercises #### Exercise 1 Reverse count I want a program whose output is a reverse count from 20 to 0. After that it's going to say "LIFT OFF!" Solutions in the spoiler: :::spoiler ```java= import java.util.*; public class Main { public static void main(String[] args) { for (int i = 20; i >= 0; i--) { System.out.println(i); } System.out.println("LIFT OFF!"); } } ``` Credit D.Z. ::: #### Exercise 2 I want a program that outputs all odd numbers that are multiples of 7 from 0 to 200. :::info **Modulus operator** Here you can use the modulus operator to specify if a number has a certain remainder. To do that you use %. For example if we write ```java int x = 5; int y = 2; System.out.println(5%2); //1 ``` ::: Solution in the spoiler :::spoiler ```java= import java.util.*; public class Main { public static void main(String[] args) { for (int i = 1; i <= 200; i++){ if(i%2 == 1 && i%7 == 0){ System.out.println(i); } } } } ``` ::: #### Exercise 3 Output the 20 first Fibonacci numbers. The first one is 1, the second is 1, then rest are the sum of the 2 previous. :::success HL you can use an iterative way or you may try to use a recursive function to do it. ::: Solution in the spoiler :::spoiler ```java= import java.util.*; public class Main { public static void main(String[] args) { System.out.println("Fibonacci numbers"); int n = 0; int secondLastFibonacci = 1; int lastFibonacci = 1; System.out.println(secondLastFibonacci); n++; System.out.println(lastFibonacci); n++; int newFibonacci = secondLastFibonacci + lastFibonacci; while (n < 20) { newFibonacci = secondLastFibonacci + lastFibonacci; n++; System.out.println(newFibonacci + " is the fibonacci number number "+ n); secondLastFibonacci = lastFibonacci; lastFibonacci = newFibonacci; } } } ``` ::: #### Exercise 4 (a bit complex) Print all numbers from 0 to 1000 whose figures add 12. :::info **Hint** Use modulus 10 for separate figures. ::: Solution in the spoiler :::spoiler ```java= import java.util.*; public class Main { public static void main(String[] args) { for(int i= 1 ; i <= 1000; i++){ if(i%10 + (i/10)%10 + (i/100)%10 + (i/1000)%10 == 12){ System.out.println(i); } } } } ``` Credit P.H.F. ::: ### Arrays :::info **Reference** https://www.w3schools.com/java/java_arrays.asp ::: We need to state the type of the variable. ```java= import java.util.*; public class Main { public static void main(String[] args) { int[] numbers = {1, 2,2, 3, 4,4,5,5,5,5, 7, 8,9,10, 12}; //Mean (average) int sum = 0; for (int x = 0; x < numbers.length; x++){ sum = sum + numbers[x]; } System.out.println("The mean is: "+ (sum/numbers.length)); //Median (previously the array has to be sorted) if (numbers.length %2 == 1) { System.out.println("The median is "+ numbers[numbers.length/2] ); } else{ int median =(numbers[numbers.length/2] + numbers[numbers.length/2-1] )/2; System.out.println("The median is " + median); } } } ``` ## Resources https://onecompiler.com/java/ https://www.w3schools.com/java/default.asp ## Next step Once you have grasped the concepts of java we can jump to [The concept of Object in OOP](/6FsqJbw3SKq5m1NKk56U3Q)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully