logo

Java'da deve vakası

Java, sınıfları, arayüzleri, yöntemleri ve değişkenleri adlandırmak için deve tipi sözdizimini izler. İsim iki kelimeyle birleştirilirse, ikinci kelime her zaman büyük harfle başlayacak, örneğin maxMarks(), lastName, ClassTest gibi tüm boşluklar kaldırılacaktır.

Camel kasasını kullanmanın iki yolu vardır:

  1. İlk kelimenin ilk karakterinin küçük harf olduğu küçük deve harfi. Yöntemleri ve değişkenleri adlandırırken genellikle bu kurala uyulur. Örnek, name, lastName, actionEvent, printArray(), vb.
  2. İlk kelimenin ilk karakterinin büyük harf olduğu, başlık durumu olarak da bilinen Üst deve durumu. Sınıfları ve arayüzleri adlandırırken genellikle bu kurala uyulur. Örneğin, Çalışan, Yazdırılabilir vb.

Normal bir dizeyi deve durumuna dönüştürme

Bir dize, yalnızca dizedeki boşlukların kaldırılmasıyla alt veya büyük deve şekline dönüştürülebilir.

Alt Deve Kasası Örneği:

Giriş: JavaTpoint programlama dilleri için en iyi eğitim sitesidir.

Çıktı: javaTpointIsTheBestTutorialSiteForProgrammingLanguages.

if else if else if java

Üst Deve Kasası Örneği:

Giriş: bu java eğitimi

Çıktı: ThisIsTheJavaTutorial

Algoritma:

  1. Karakter dizisini sonuna ulaşana kadar karakter karakter dolaşın.
  2. İndeks = 0'daki dizenin ilk harfi ya küçük harfe (küçük deve harfini takip ederken) ya da büyük harfe (büyük deve harfini takip ederken) dönüştürülür.
  3. Dizide boşluk olup olmadığı kontrol edilir ve boşluğun hemen ardından gelen harf büyük harfe dönüştürülür.
  4. Boşluk olmayan karakterle karşılaşılırsa, ortaya çıkan diziye kopyalanır.

Algoritmayı bir Java programında uygulayalım.

A. String'i Lower Camel Case'e Dönüştürme

LowerCamel.java

 public class LowerCamel { // function to convert the string into lower camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to lower case as we are following camel in this program if( i="=" converting using tolowercase( in-built function ch[ ]="Character.toLowerCase(" ; need remove all spaces between, check for empty if ( ' incrementing space counter by ctr++ immediately after upper + continue loop } is not encountered simply copy character else c++ size new string will reduced have been removed thus, returning with return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name a method str="Max Marks()" system.out.println( convertstring( variable str1="Last name" str2="JavaTpoint is the best tutorial site for programming languages." < pre> <p> <strong>Output:</strong> </p> <pre> maxMarks() lastName javaTpointIsTheBestTutorialSiteForProgrammingLanguages. </pre> <h3>B. Converting String to Upper Camel Case</h3> <p> <strong>UpperCamel.java</strong> </p> <pre> public class UpperCamel { // function to convert the string into upper camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to upper case. we checked this before second if statement as that is executed only when it encounters space and, there no a string. if( i="=" converting case using touppercase( in-built function ch[ ]="Character.toUpperCase(" ; need remove all spaces in between, check for empty ( ' incrementing counter by ctr++ immediately after + 1] continue loop } not encountered simply copy character else c++ new string will reduced have been removed thus returning with size return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name method str="class test" system.out.println( convertstring( variable str1="employee" str2="this is the java tutorial" system.out.println(convertstring( < pre> <p> <strong>Output:</strong> </p> <pre> ClassTest Employee ThisIsTheJavaTutorial </pre> <hr></n;></pre></n;>

B. String'i Upper Camel Case'e Dönüştürme

UpperCamel.java

polis komiseri yardımcısı
 public class UpperCamel { // function to convert the string into upper camel case static String convertString( String s ) { // to keep track of spaces int ctr = 0 ; // variable to hold the length of the string int n = s.length( ) ; // converting the string expression to character array char ch[ ] = s.toCharArray( ) ; // // keep track of indices of ch[ ] array int c = 0 ; // traversing through each character of the array for ( int i = 0; i <n; 0 1 i++ ) { the first position of array i.e., letter must be converted to upper case. we checked this before second if statement as that is executed only when it encounters space and, there no a string. if( i="=" converting case using touppercase( in-built function ch[ ]="Character.toUpperCase(" ; need remove all spaces in between, check for empty ( \' incrementing counter by ctr++ immediately after + 1] continue loop } not encountered simply copy character else c++ new string will reduced have been removed thus returning with size return string.valueof( ch, 0, n - ctr driver code public static void main( args[ passing name method str="class test" system.out.println( convertstring( variable str1="employee" str2="this is the java tutorial" system.out.println(convertstring( < pre> <p> <strong>Output:</strong> </p> <pre> ClassTest Employee ThisIsTheJavaTutorial </pre> <hr></n;>