Austin Wu
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `Java` `Note` # Hello Java ## 一般用語 1. \n 換行 2. \t 空Tab格 3. // 註解 4. \' 輸入單引號 5. \\ 單個反斜線 6. void 不需reture sth 7. public class HelloWorld { public static void main(String[] args) 8. static : that it is a class method not an object method. eg. ```java String a = ("郭台銘"); String b = ("ㄍㄊㄇ"); System.out.println("\\"+a+"/"+"\t"+"\\"+a+"/"+"\t"+"\\"+a+"/"+"\n"+"\t"+"\\"+b+"/"+"\t"+"\\"+b+"/"+"\t"+"\\"+b+"/"+"\n"+"\\"+a+"/"+"\t"+"\\"+a+"/"+"\t"+"\\"+a+"/"); ``` ![](https://i.imgur.com/gg9ejfh.png) 6. ++ int a = 96; int b = 20; System.out.println(a++); System.out.println(++b); ++a 先加1 # Note ## a 和 b 交換 ```java int a =5; int b =3; int c =a; a=b; b=c; boolean t =true; System.out.println(a); System.out.print(b); ``` ## 條件式 等於 == 不等於 != And && or || not ! Xor ^ ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean sister = true; if (!sister) System.out.println ("You have no sister"); else System.out.println ("Wake up, you have no sister"); }} ``` ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int numberRange = sc.nextInt(); if ((numberRange>0 && numberRange<20) || numberRange>50){ System.out.print("Bullshit"); }}} ``` ## 三角形成立? 直角/銳角/鈍角? ```java import javax.swing.*; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int sideLength1 = sc.nextInt(); int sideLength2 = sc.nextInt(); int sideLength3 = sc.nextInt(); if ((sideLength1+sideLength2>sideLength3) &&(sideLength2+sideLength3>sideLength1) &&(sideLength1+sideLength3>sideLength2)) { if (((sideLength1) * (sideLength1)) + ((sideLength2) * (sideLength2)) == ((sideLength3) * (sideLength3)) || ((sideLength1) * (sideLength1)) == ((sideLength2) * (sideLength2)) + ((sideLength3) * (sideLength3)) || ((sideLength1) * (sideLength1)) + ((sideLength3) * (sideLength3)) == ((sideLength2) * (sideLength2)) ) System.out.print("right angle"); else if (((sideLength1) * (sideLength1)) + ((sideLength2) * (sideLength2)) < ((sideLength3) * (sideLength3)) || ((sideLength1) * (sideLength1)) < ((sideLength2) * (sideLength2)) + ((sideLength3) * (sideLength3)) || ((sideLength1) * (sideLength1)) + ((sideLength3) * (sideLength3)) < ((sideLength2) * (sideLength2))) System.out.print("obtute triangle"); else System.out.print("acute angle"); } else System.out.print("Can not be a triangle"); } } ``` ## 網路流量 ```java import javax.swing.*; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double GB = sc.nextDouble(); if (GB <= 5) { System.out.print("$599"); } else { if (GB > 5 && GB < 7) { double b = (599 + 12 * 10 * (GB - 5)); System.out.print("$" + b); } else if (GB > 7 ) { double c = (599 + 12 * 0.1 * (GB - 5) + 17 * 10 * (GB - 7)); if (c>1500){ System.out.print("$" + 1500); } else { System.out.print("$" + c); } } } }} ``` ## 1到100 奇數相加 ```java public class HelloWorld { public static void main(String[] args){ int a = 0; int sum = 0; while(a<=100){ sum += a; a=a+2; } System.out.print(sum); } } ``` ## 輸入n 求sum (1/n) ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); double n = sc.nextDouble(); double sum = 0.0; while(n>0){ sum +=1/n; n--; } System.out.print(sum); }} ``` ## 判斷質數 ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int count = 2; boolean isPrime = true; while(count*count <= a) { if (a% count ==0) { isPrime = false; break; } count++; } if(isPrime) { System.out.print("prime"); } else { System.out.print("Not Prime"); } } } ``` ## 1到100 整除2不整除3的數值全部加起來 ###### tachinical problem in code?? ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int a = 100; int count = 1; while (count <= a) { if (count % 3 != 0 && count % 2 == 0) { System.out.print(count + "\t"); } count++; } } } ``` ## 2進位轉十進位 ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); int deci = 0; int exp = 1; for (; i != 0; i /= 10) { if (i % 10 == 1) { deci = exp; } exp *= 2; } System.out.print(deci); } } ``` ## 比i小的所有平方數和 ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); int n = 1; int e = 0; while (i!=0 && n<=i ){ if (n*n<=i){ e+=n*n; } n++; } System.out.println(e); } } ``` ## input n and output the value of n! ###### method 1 ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); int n = 1; int e = 1; while (i!=0 && n<=i ){ e*=n; n++; } System.out.println(e); } } ``` ###### method 2 ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int a ; Scanner sc = new Scanner(System.in); a= sc.nextInt(); int total = 1; for(int i= 1 ; i<=a ; i++){ total = total * i; } System.out.print(total); } } ``` ## input n and ouput summantion n! ```java import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int m ; Scanner sc = new Scanner(System.in); m= sc.nextInt(); int total = 1; int sum = 0; for(int i= 1 ; i<=m ; i++){ total = total * i; sum = sum+total; } System.out.print(sum); } } ``` ## input m n and output C(m,n) ```java import java.util.Scanner; public class CmTaken { private static int fact(int n){ int total = 1; for(int i=1 ;i <= n ; i++ ){ total = total*i; } return total; } public static void main(String[] args){ int m ,n; Scanner sc = new Scanner(System.in); m = sc.nextInt(); n = sc.nextInt(); int totalM = fact(m); int totalN = fact(n); int totalM_N = fact(m-n); System.out.print(totalM/totalN/totalM_N); } } ``` ## AP Practice 20201115 (1) - Read the following context and complete the question. An APLine is a line defined by the equation \begin{gather*} ax + by + c = 0 , a ≠ 0 , b ≠ 0\end{gather*} a, b, and c are all integers. The slope of an APLine is defined to be the double value $-a/b$ . A point represented by x and y is on the line if ax + by + c is equal to 0. Assume that the following code segment shows an example of using the APLine class. Write the APLine class. Your implementation must include a constructor that has three integer parameters that represent a, b, and c, in that order. You may assume that the values of the parameters representing a and b are not zero. It must also include a method getSlope that calculates and returns the slope of the line, and a method isOnLine that returns true if the point represented by its two parameters (x and y, in that order) is on the APLine and returns false otherwise. Your class must produce the indicated results when invoked by the code segment given above. You may ignore any issues related to integer overflow. ```java APLine line1 = new APLine(5,4,-17); double slope1 = line1.getSlope(); boolean onLine1 = line1.isOnLine(5,-2); APLine line2 = new APLine(-25,40,30); double slope2 = line2.getSlope(); boolean onLine2 = line2.isOnLine(5,-2); **Solution** ```java public class HelloWorld{ private int a; private int b; private int c; public HelloWorld(int A, int b, int c){ this.a = a; b = b; c = c; } public double slope1(){ return -(double)a/b; } public boolean OnLine(int x,int y){ if (a*x + b*y + c ==0) { return true; } else{ return false; } } } ``` ## AP Practice 20201115 (2) - Read the following context and complete the question. A mathematical sequence is an ordered list of numbers. This question involves a sequence called a hailstone sequence. If **n** is the value of a term in the sequence, then the following rules are used to find the next term, if one exists. If **n** is 1, the sequence terminates. If **n** is even, then the next term is n/2 . If **n** is odd, then the next term is 3n+1 . For this question, assume that when the rules are applied, the sequence will eventually terminate with the term n=1 . The following are examples of hailstone sequences. Example 1: 5, 16, 8, 4, 2, 1 The first term is **5**, so the second term is $5*3+1=16$ . The second term is **16**, so the third term is $16/2=8$ . The third term is **8**, so the fourth term is $8/2=4$ . The fourth term is **4**, so the fifth term is $4/2=2$ . The fifth term is **2**, so the sixth term is $2/2=1$ . Since the sixth term is **1**, the sequence terminates. Example 2: 8, 4, 2, 1 The first term is **8**, so the second term is $8/2=4$ . The second term is **4**, so the third term is $4/2=2$ . The third term is **2**, so the fourth term is $2/2=1$ . Since the fourth term is **1**, the sequence terminates. The Hailstone class, shown below, is used to represent a hailstone sequence. You will write three methods in the Hailstone class. ```java public class Hailstone{ /** Returns the length of a hailstone sequence that starts with n, * as described in part (a). * Precondition: n > 0 */ public static int hailstoneLength(int n){ /* to be implemented in part (a) */ } /** Returns true if the hailstone sequence that starts with n is considered long * and false otherwise, as described in part (b). * Precondition: n > 0 */ public static boolean isLongSeq(int n){ /* to be implemented in part (b) */ } /** Returns the proportion of the first n hailstone sequences that are considered long, * as described in part (c). * Precondition: n > 0 */ public static double propLong(int n){ /* to be implemented in part (c) */ } // There may be instance variables, constructors, and methods not shown. } ``` - (a) The length of a hailstone sequence is the number of terms it contains. For example, the hailstone sequence in example 1 (5, 16, 8, 4, 2, 1) has a length of 6 and the hailstone sequence in example 2 (8, 4, 2, 1) has a length of 4. Write the method hailstoneLength(int n), which returns the length of the hailstone sequence that starts with n. ```java public static int hailstoneLength(int n) ``` - (b)A hailstone sequence is considered long if its length is greater than its starting value. For example, the hailstone sequence in example 1 (5, 16, 8, 4, 2, 1) is considered long because its length (6) is greater than its starting value (5). The hailstone sequence in example 2 (8, 4, 2, 1) is not considered long because its length (4) is less than or equal to its starting value (8). Write the method isLongSeq(int n), which returns true if the hailstone sequence starting with n is considered long and returns false otherwise. Assume that hailstoneLength works as intended, regardless of what you wrote in part (a). You must use hailstoneLength appropriately to receive full credit. ```java public static boolean isLongSeq(int n) ``` - (c\)The method propLong(int n) returns the proportion of long hailstone sequences with starting values between 1 and n.It calls Hailstone.propLong(10) returns 0.5, since 5 of the 10 hailstone sequences shown in the table are considered long. Write the propLong method. Assume that hailstoneLength and isLongSeq work as intended, regardless of what you wrote in parts (a) and (b). You must use isLongSeq appropriately to receive full credit. **Solution** ```java public class Hailstone{ public static int hailstoneLength(int n){ int count =1; while (n!=1) { if (n % 2 == 0) { n /= 2; count += 1; } else { n = 3 *n + 1; count += 1; } } return count; } public static boolean isLongSeq(int n){ int count = hailstoneLength(n); if (count>n) { return true; } else { return false; } } public static double propLong(int n){ int countFalse=0; int countTrue=0; while (n>0) { if(isLongSeq(n)){ countTrue++; } else { countFalse++; } n-=1; } return (double)countTrue/(countFalse+countTrue); } } ``` ## String [老師hackMD-String](https://hackmd.io/pCpWOs-FSbq8KkYluCFHKQ?view) ### Practice - Delete method 1. Write a static method named `delete` with two String parameters . The first String , named original, is the target String which will be delete something. And the second String , named keyword, is the text which to delete. The datatype of return is String . The String is the original after being deleted all the keyword. ```java public static String delete(String original,String keyword) ``` **Solution** ```java public class HelloWorld { public static void main(String[] args) { String original = "original"; String keyword = "or"; } public static String delete (String original, String keyword){ return original.replace("or",""); } } ``` public class HelloWorld { public static void main(String[] args) { String original = "original"; String keyword = "or"; String newWord; int n=5; } public static String replacement(String original, String keyeord , String newWord , int n){ return original.replace((original.indexOf(keyeord,n)),newWord); } } [2015 APCS #2](https://secure-media.collegeboard.org/digitalServices/pdf/ap/ap15_frq_computer_science_a.pdf) ```java public class HiddenWord { private String word; public HiddenWord(String word) { this.word = word; } public String getHint(String guess) { String hint = ""; for(int i = 0; i < word.length(); i++) { String guessLetter = guess.substring(i, i + 1); if(word.substring(i, i + 1).equals(guessLetter)) hint += guessLetter; else if(word.indexOf(guessLetter) != -1) hint += "+"; else hint += "*"; } return hint; } } ``` ## Array ```java import java.util.Arrays; ``` ### Declaring an array 宣告Array ```java int[] numberArray= null; ``` 宣告Array及其長度 ```java numberArray = new in[5]; ``` 複製相同的Array ```java int[] num2 = Arrays.copyOf(numberArray,numberArray.length); ``` 輸出Array ```java System.out.println(Arrays.toString(numberArray)); ``` 製作已成array ```java int[ ] numberArray = {99,98,98,88,68}; ``` For-Each Loop ```java for(int k;numberArray){ System.out.print(k+" ") } ``` ### 列出99乘法表 index*n 之值 ```java import java.util.Arrays; import java.util.Scanner; public class HelloWorld { public static void main(String[] args) { int n; Scanner sc = new Scanner(System.in); n = sc.nextInt(); int i=0; int[] numberArray= null; numberArray = new int[10]; for (;i<=9; i++ ) { numberArray[i]=i*n; } System.out.println(Arrays.toString(numberArray)); } } ``` ### 輸出Array中最大質數 ```java public class Array { public static int theMaxPrime(int[] arr){ int max = 0; for(int k:arr){ if(k > max && isPrime(k)){ max = k; } } return max; } public static boolean isPrime(int a){ for(int i=2 ; i*i<a;i++){ if(a%i==0){ return false; } } return true; } public static void main(String[] args){ int[] numberArray ={47,87,83,91,95}; int max = theMaxPrime(numberArray); System.out.print(max); } } ```

    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