###### 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+"/");
```

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);
}
}
```