2014-04-16 3 views
0

일부 작업을 수행하는 중, 개체 지향 프로그래밍을 사용하여 메서드를 작성하고 개체 인스턴스화없이 메서드를 작성해야합니다. 나는 이것을하는 방법을 모른다. 다음 의견이 반품되었습니다.개체 인스턴스화없이 정적 메서드 호출을 작성하는 방법

Your main() method should go in the tester class, while all of the implementation methods and   
constructor should go in the object class. 

다음은 몇 가지 코드입니다. 감사합니다.

public static void main(String[] args) throws IOException 
{  
    // variables 
    Scanner in = new Scanner(System.in); 
    File fileRead = new File("/Users/jerome/Desktop/MorseCode.txt"); 
    Scanner withinFile = new Scanner (fileRead); 
    String userInput = ""; 
    String toMorse = ""; 
    String[] file = new String[25]; 
    String[] alp = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "o", "m", "n", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; 
     for(int i = 0; i < 25; i ++) 
    { 
     file[i] = withinFile.nextLine(); 

    } 
    System.out.print("Enter a word to translate: "); 
    userInput = in.nextLine(); 
    // static methods 
    MorseCode2 morse = new MorseCode2(); 
    morse.tooMorse(userInput, alp, file, toMorse); 
    // output 
    System.out.print("'"+ userInput+"'"+" translated into Morse Code is: " + tooMorse(userInput, alp, file, toMorse)); 
} 

Annnd some more!

public static String tooMorse(String userInput, String[] alp, String[] file, String toMorse) 
    { 
for(int i = 0; i < userInput.length(); i ++) 
    { 
    if(userInput.substring(i, i+1).equals(alp[i])) // a 
    { 
     toMorse += " " + file[i]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+1])) // b 
    { 
     toMorse += " " + file[i+1]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+2])) // c 
    { 
     toMorse += " " + file[i+2]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+3])) // d 
    { 
     toMorse += " " + file[i+3]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+4])) // e 
    { 
     toMorse += " " + file[i+4]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+5])) // f 
    { 
     toMorse += " " + file[i+5]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+6])) // g 
    { 
     toMorse += " " + file[i+6]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+7])) //h 
    { 
     toMorse += " " + file[i+7]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+8])) // i 
    { 
     toMorse += " " + file[i+8]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+9])) // j 
    { 
     toMorse += " " + file[i+9]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+10])) // k 
    { 
     toMorse += " " + file[i+10]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+11])) // l 
    { 
     toMorse += " " + file[i+11]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+12])) // m 
    { 
     toMorse += " " + file[i+12]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+13])) // n 
    { 
     toMorse += " " + file[i+13]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+14])) //o 
    { 
     toMorse += " " + file[i+14]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+15])) // p 
    { 
     toMorse += " " + file[i+15]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+16])) // q 
    { 
     toMorse += " " + file[i+16]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+17])) // r 
    { 
     toMorse += " " + file[i+17]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+18])) // s 
    { 
     toMorse += " " + file[i+18]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+19])) // t 
    { 
     toMorse += " " + file[i+19]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+20])) // u 
    { 
     toMorse += " " + file[i+20]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+21])) // v 
    { 
     toMorse += " " + file[i+21]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+22])) // w 
    { 
     toMorse += " " + file[i+22]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+23])) // x 
    { 
     toMorse += " " + file[i+23]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+24])) // y 
    { 
     toMorse += " " + file[i+24]; 
    } 
    else if(userInput.substring(i, i+1).equals(alp[i+25])) // z 
    { 
     toMorse += " " + file[i+25]; 
    } 
    else 
    { 
     return "no"; 
    } 
} 
    return toMorse; 
+0

대신에 'MorseCode2 morse = new MorseCode2(); morse.tooMorse (userInput, alp, file, toMorse);'그냥 MorseCode2.tooMorse (userInput, alp, file, toMorse)를 사용하십시오. – csmckelvey

답변

0
MorseCode2 morse = new MorseCode2(); 
morse.tooMorse(userInput, alp, file, toMorse); 

변경이로 : 당신이 그와 같은 다른 정적 메서드가있는 경우

MorseCode2.tooMorse(userInput, alp, file, toMorse); 

은 같은 일을한다.

일반적인 방법과 달리 클래스 인스턴스에서만 정적 메서드를 호출하지 않습니다. 일 경우 morse.tooMorse(...)을 작성하면 컴파일러에서이를 MorseCode2.tooMorse(...)으로 간단하게 변환하고 경고를 내 보냅니다. 그래서 원래의 코드는 다음과 동일하다 : 임의의 장소에서 사용하지 않는 morse 때문에 약간 비효율적이다

MorseCode2 morse = new MorseCode2(); 
MorseCode2.tooMorse(userInput, alp, file, toMorse); 

.

관련 문제