2-IntelliJ IDEA o’rnatish
IntelliJ IDEA- bu kompyuter dasturlarini ishlab chiqish uchun Java tilida yozilgan integratsiyalashgan ishlab chiqish muhiti. U JetBrains tomonidan ishlab chiqilgan va Apache 2 Litsenziyalangan hamjamiyat nashri va xususiy tijorat nashrida mavjud. Ikkalasidan ham tijorat rivojlanishi uchun foydalanish mumkin.. Malumot: https://en.wikipedia.org/wiki/IntelliJ_IDEA
Saytdan intellij idea yuklab olinadi
.exe fayl ochiladi va “next” tugmasi bosiladi
O’rnatish joyi va menyusi tanlandi
.java fayllarini bog’lash tanlanadi.
O’rnatishni tugatish uchun checkbox ga belgi qo’yiladi
Import qilish uchun tanlangan standartlarni qoldiriladi
“Skip remaining and set default” tanlanadi
Agar xavfsizlik ga kirish haqida ogohlantirsangiz, ruhsat etishni tanlanadi
Terminalda java file dagi dasturni ishga tushiramiz:
Bu yerda javac HelloJava.java buyug’ini terminalga yozganimizda bizga java class ni yaratib berdi:
K od qisimlarini tushuntirishga o’tamiz:
Java Switch Statements:
switch ( expression) { case x : // kod qisimi break ; case y : // kod qisimi break ; default : // kod qisimi } int day = 4 ; switch ( day ) { case 1 : System . out . println ( "Duyshanba" ); break ; case 2 : System . out . println ( "Seshanba" ); break ; case 3 : System . out . println ( "Chorshanba" ); break ; case 4 : System . out . println ( "Payshanba" ); break ; case 5 : System . out . println ( "Juma" ); break ; case 6 : System . out . println ( "Shanba" ); break ; case 7 : System . out . println ( "Yakshanba" ); break ; }
Java While
Belgilangan shartga erishilganda, sikillar kod blokini bajarishi mumkin.
Sintaksisi:
while ( shart) { /kod qismi } Misol uchun int i = 0 ; while ( i < 5 ) { System . out . println ( i ); i ++ ; }
do/while sikli while siklining bir variantidir. Ushbu sikl kod blokini shartning to'g'ri yoki yo'qligini tekshirishdan oldin bir marta bajaradi, so'ngra shart to'g'ri bo'lsa, u siklni takrorlaydi.
do { k od qismi } while ( shart); Misol uchun public class Main { public static void main(String[] args) { int i = 0; do { System.out.println(i); i++; } while (i < 5); } } K od blokini necha marta aylantirmoqchi ekanligingizni aniq bilsangiz, while tsikli o'rniga for siklidan foydalana olamiz: for ( holat 1 ; holat 2 ; holat 3 ) { // code block to be executed } Misol uchun public class Main { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i); } } } P rotsessual dasturlash – bu ma'lumotlar ustida operatsiyalarni bajaradigan protseduralar yoki usullarni yozish, ob'ektga yo'naltirilgan dasturlash esa ma'lumotlar va usullarni o'z ichiga olgan ob'ektlarni yaratishdir. Ob'ektga yo'naltirilgan dasturlash protsessual dasturlashdan bir qancha afzalliklarga ega: OOP tezroq va bajarilishi osonroq OOP dasturlar uchun aniq tuzilmani taqdim etadi OOP Java kodini DRY "O'zingizni takrorlamang" saqlashga yordam beradi va kodni saqlash, o'zgartirish va disk raskadrovka qilishni osonlashtiradi. OOP kamroq kod va qisqaroq ishlab chiqish vaqti bilan to'liq qayta foydalanish mumkin bo'lgan ilovalarni yaratish imkonini beradi
Misol uchun
public class Main { int x = 5 ; } public class Main { int x = 5 ; public static void main ( String [] args ) { Main myClass = new Main (); System . out . println ( myClass . x ); } }
biz bu holatda klassdan object olib uni ichidagi parametrlardan foydalanishimiz mumkin.
public class Main { int x = 5 ; public static void main ( String [] args ) { Main myObj1 = new Main (); // Object 1 Main myObj2 = new Main (); // Object 2 System . out . println ( myObj1 . x ); System . out . println ( myObj2 . x ); } }
Bu holatda ham natijalar bir xil chiqaveradi.
Java class atributlariga misola-lar:
public class Main { int x ; public static void main ( String [] args ) { Main myObj = new Main (); myObj . x = 40 ; System . out . println ( myObj . x ); } } Bu holatda classimizdagi ozgaruvchiga qiymat ham berish imkoniyatiga egamiz.
Hozir method yaratishni ko’rib chiqamiz:
public class Main { static void myMethod () { System . out . println ( "Hello World!" ); } }
myMethod() chaqirilganda matnni (harakatni) chop etadi. Usulni chaqirish uchun usul nomini keyin ikkita qavs () va nuqta-vergul qo’yamiz;
public class Main { static void myMethod () { System . out . println ( "Hello World!" ); } public static void main ( String [] args ) { myMethod (); } }
Java polymorphism:
class Animal { public void animalSound () { System . out . println ( "Bu hayvon ovoz chiqaradi" ); } } class Pig extends Animal { public void animalSound () { System . out . println ( "Cho’chqa ovoz chiqaradi: wee wee" ); } } class Dog extends Animal { public void animalSound () { System . out . println ( "It ovoz chiqaradix: bow wow" ); } } class Main { public static void main ( String [] args ) { Animal myAnimal = new Animal (); // Create a Animal object Animal myPig = new Pig (); // Create a Pig object Animal myDog = new Dog (); // Create a Dog object myAnimal . animalSound (); myPig . animalSound (); myDog . animalSound (); } }
public class Main {
public static void main(String[] args) {
Pig myPig = new Pig();
myPig.animalSleep();
myPig.animaSound();
}
}
interface Animal {
public void animalSleep();
public void animaSound();
}
class Pig implements Animal {
@Override
public void animalSleep() {
System.out.println("Cho'chqa uhlaydi zzz");
}
@Override
public void animaSound() {
System.out.println("Cho'chqani ovozi:wee -wee");
}
}
Java ArrayList
ArrayList klassi java.util paketida topilishi mumkin bo'lgan o'lchami o'zgartiriladigan massivdir.
Java-da o'rnatilgan massiv va ArrayList o'rtasidagi farq shundaki, massiv hajmini o'zgartirib bo'lmaydi (agar siz massivga/massivdan elementlar qo'shish yoki olib tashlashni istasangiz, yangisini yaratishingiz kerak). Elementlarni istalgan vaqtda ArrayList ga qo'shish va olib tashlash mumkin. Sintaksis ham biroz boshqacha:
Misol uchun
import java . util . ArrayList ; // import the ArrayList class ArrayList < String > cars = new ArrayList < String >(); // Create an ArrayList object
Misol uchun:
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList cars = new ArrayList();
cars.add("Lacetti");
cars.add("Tiko");
cars.add("Damas");
cars.add("Nexia");
System.out.println(cars);
}
}
Arraylistning biror qiymatini uni indexiga qarab o’qib olishimiz mumkin:
cars.get(2) holatida
Java HashMap
ArrayList bo'limida siz Arrays elementlarni tartiblangan to'plam sifatida saqlashini bilib oldingiz va siz ularga indeks raqami (int turi) bilan kirishingiz kerak. Biroq, HashMap elementlarni "kalit/qiymat" juftlarida saqlang va siz ularga boshqa turdagi indeks (masalan, String) orqali kirishingiz mumkin.
Bitta ob'ekt boshqa ob'ekt (qiymat) uchun kalit (indeks) sifatida ishlatiladi. U har xil turlarni saqlashi mumkin: String kalitlari va Integer qiymatlari yoki bir xil turdagi, masalan: String keys va String qiymatlari:
import java . util . HashMap ; // import the HashMap class HashMap < String , String > capitalCities = new HashMap < String , String >();
Misol uchun:
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap capitalCities = new HashMap();
capitalCities.put("Uzbekistan", "Toshkent");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities);
}
}
Bu arraylistni kalit so’ziga qarab o’qib olishimiz mumkin masalan:
System.out.println(capitalCities.get("Uzbekistan"));
Va bu arraylarni kalit so’z yoramida o’zgartirishimiz o’chirishimiz va boshqa amallarni bajarishimiz mumkin
public class Main {
public static void main(String[] args) {
HashMap capitalCities = new HashMap();
capitalCities.put("Uzbekistan", "Toshkent");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
capitalCities.remove("Germany");
capitalCities.replace("USA","Washington DC","Qashqadaryo");
System.out.println(capitalCities);
}
}
HashSet - bu har bir element noyob bo'lgan va java.util paketida joylashgan elementlar to'plami:
import java . util . HashSet ; // Import the HashSet class HashSet < String > cars = new HashSet < String >();
HashSet sinfida juda ko'p foydali usullar mavjud. Masalan, unga elementlar qo'shish uchun add() usulidan foydalaning:
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet cars = new HashSet();
cars.add("Nexia");
cars.add("Tiko");
cars.add("Malibu");
cars.add("Damas");
cars.add("Maskivuch");
System.out.println(cars);
}
}
HashSet-da element mavjudligini tekshirish uchun contain() usulidan foydalaning:
System.out.println(cars.contains("Damas"));
public class Main {
public static void main(String[] args) {
HashSet numbers = new HashSet();
numbers.add(4);
numbers.add(7);
numbers.add(8);
for(int i = 1; i <= 10; i++) {
if(numbers.contains(i)) {
System.out.println(i + " son bu listda bor");
} else {
System.out.println(i + " son bu listda yo'q");
}
}
}
}
Do'stlaringiz bilan baham: |