714-18 guruh talabasi
Madaminov Suxail
Amaliy mashg’ulot 1
Mavzu:Java dasturlash tilida klasslar, obyektlar va massivlar.
Reja:
1.Inson klassini yaratish, ob’ektlar bilan ishlash.
2.Massivlar yaratish
package mypacket;
public class Inson {
private String familya;
private String ism;
private int yosh;
public Inson(){
this.familya="";
this.ism="";
this.yosh=0;
}
public String getFamilya(){
return this.familya;
}
public String getIsm(){
return this.ism;
}
public int getYosh(){
return this.yosh;
}
public void setFamilya(String value){
this.familya = value;
}
public void setIsm(String value){
this.ism = value;
}
public void setYosh(int value){
if (value < 0){
System.err.println("Yosh chegarasi hato kiritilgan");
System.exit();
}
else this.yosh = value;
}
}
obyekt yaratish
import mypacket.Inson;
public class myclass {
public static void main(String[] args) {
Inson i = new Inson();
i.setFamilya("Madaminov");
i.setIsm("Suxail");
i.setYosh(23);
System.out.println(i.getFamilya()+ " " + i.getIsm() +" yoshi "+ i.getYosh());
}
}
Dinamik massivlar yaratish
public class Array1 {
public static void main (String[] args) {
int[] m = new int[7];
}
}
Statik massiv yaratish
public class Array1 {
public static void main (String[] args) {
int [] n={1,2,5,8,6};
}
}
Massiv elementlarini kiritish jarayoni
import java.util.Scanner;
public class Array1 {
public static void main (String[] args) {
int[] a;
Scanner input = new Scanner(System.in);
int n = input.nextInt();
a=new int[n];
for(int i=0; i< n; i++){
a[i]=input.nextInt();
}
for(int i=0; i< n; i++){
System.out.print(a[i]+", ");
}
}
}
Arrays klasidan foydalanish
import java.util.Arrays;
import java.util.Scanner;
public class Array1 {
public static void main (String[] args) {
int[] a;
Scanner input = new Scanner(System.in);
int n = input.nextInt();
a=new int[n];
for(int i=0; i< n; i++){
a[i]=input.nextInt();
}
Arrays.sort(a);
for(int i=0; i< n; i++){
System.out.print(a[i]+", ");
}
}
}
ArrayList
public static void main (String[] args) {
ArrayList mevaList = new ArrayList();
mevaList.add("banan");
mevaList.add("olma");
mevaList.add("mandarin");
mevaList.add("mango");
System.out.println(mevaList);
}
Quydagi holatda ArrayListdan istaganini olib tashlash mumkin
ArrayList mevaList = new ArrayList();
mevaList.add("banan");
mevaList.add("olma");
mevaList.add("mandarin");
mevaList.add("mango");
mevaList.remove("banan");
System.out.println(mevaList);
}
Hash Map
public static void main (String[] args) {
int a = 10;
int b = 8;
int c = 3;
HashMap hashmap1= new HashMap ();
hashmap1.put("a",10);
hashmap1.put("b",8);
hashmap1.put("c",3);
System.out.println(hashmap1);
}
{
HashMap hashmap1= new HashMap ();
hashmap1.put("a",10);
hashmap1.put("b",8);
hashmap1.put("Suxail",3);
hashmap1.remove("Suxail");
System.out.println(hashmap1);
}
Quydagi holatda Hashmapdagi 1ta elementni remove buyrug’i orqali o’chirib yubordik.
Xusolsa:
Men ushbu labartoriya mashg’ulotida Java dasturlash tilida klaslar yaratish va ulardan voris olish yaratilgan klasslar yordamida ob’ektlar hosilqilish ,massivlar va Arrays klassidan doydalanish ArrayList HashMap va Map kontekslaridan foydalanishni o’rgandik.Klasslar haqida umumiy tushunchaga ega bo’ldik.
Do'stlaringiz bilan baham: |