Java String Instance Practice Example #1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package edu.handong.csee.java.string; 
 
public class FamilyNamePrinter{
    private String[] stringNames; 
    private String[] familyNames;
     
    public void printFamilyNames(){
        for(String name: familyNames){
            if(name == null){
                System.out.print(""); 
            }else{
                System.out.println(name);
            }
        }
    }
    public FamilyNamePrinter(String names){
        
       stringNames = names.split(",");
       familyNames = new String[stringNames.length];
       int i=0
       for(String name: stringNames)
         familyNames[i++= name.trim().split(" ")[1]; 
    }
 
    public static void main (String[] args) {
        String myString = args[0]; 
        FamilyNamePrinter myFamily = new FamilyNamePrinter(myString); 
        myFamily.printFamilyNames();     
    }
}
cs

 

[Java Programming] Java return, 자바 리턴값 배우기


[Person.java]


class Person{

String name = "Lin";

String bloodType;

int age = 20;


void speak(){

System.out.println("저의 이름은"+name+"이고 혈액형은"+bloodType+"입니다.");

}

String getName(){

return name;

}


int getAge(){

return age;

}

}


[Application.java]


public class Application{

public static void main(String[] agrs){

Person person1 = new Person();

String myName = person1.getName();

int myAge = person1.getAge() + 6;

System.out.println(myAge);

}

}


[result]

26

[Java Programming] Java Method 자바 메소드 배우기


class Person{

String name;

String bloodType;


void speak(){

System.out.println("저의 이름은"+name+"이고 혈액형은"+bloodType+"입니다.");

}

}


class Cat{

String name;

}


public class Application{

public static void main(String[] args){

Person person1 = new Person();

person1.name = "Lin";

person1.bloodType = "A";

person1.speak();


Cat cat1 = new Cat();

cat1.name = "Kitty";

}

}


// 같은 package 안에 있으면 클래스 호출 가능.


[result]

저의 이름은 Lin이고 혈액형은 A입니다.

[Java Programming] 자바 2D Array 배우기


public class Array{

public static void main(String[] args){

int[][] numbers = {

{10, 30, 80},

{1, 3, 8}.

{100, 200, 300}

};

for (int x = 0 ; x < numbers.length ; x++){

for (int i = 0 ; i < numbers[x].length ; i ++){

System.out.print(numbers[x][i] + "\t");

}

System.out.println();

}

}

}


[result]

10    30    80

1    3    8    

100    200    300


[Java Programming] 배열 배우기 _ 2, Array_2


public class Application{

public static void main(String[] args){

String[] fruits = {'Apple', 'orange', 'banana'};

System.out.println(fruits[0]);


for(String text : fruits){

    System.out.println(text);

}

}

}


[result]

Apple

Apple

orange

banana

+ Recent posts