14. Boshiga qo’yish usulidan foydalanib ro’yhatda qo’shni elementlar ayrimasi juft bo’lgan elementni toping. Agar bunday elementlar ko„p bo’lsa



Yüklə 366,28 Kb.
tarix24.03.2023
ölçüsü366,28 Kb.
#89531

1-labaratoriya ishi;


14. Avtomobil nomli klass yarating va unda turli o„zgaruvchi va funksiyalar yarating. Ushbu klassdan foydalanib, turli markali avtomobillar uchun o„zgaruvchilarni yaratib, ular haqida ma‟lumotlar kiritib, ekranga chiqaring.
class Car{
int year;
int usage;
String color;
String model;
String manufacturer;
public Car(int year,int usage,String color,String model,String manufacturer){
this.year = year;
this.usage = usage;
this.color = color;
this.model = model;
this.manufacturer = manufacturer;
}
public Car(int year,int usage,String color,String model){
this(year,usage,color,model,"");
}
public Car(int year,int usage,String color){
this(year,usage,color,"");
}
public Car(int year,int usage){
this(year,usage,"");
}
public Car(int year){
this(year,0);
}
public Car(){
this(0);
}
public String toString(){
return "year : " + year + "/ usage : " + usage + "/ color : " + color + "/ model : " + model + "/ manufacturer : " + manufacturer;
}
}
class Task1{
public static void main(String[] args) {
Car car1 = new Car();
car1.manufacturer = "mersedez";
car1.model = "s class";
car1.year = 2019;
car1.usage = 2;
car1.color = "black";
System.out.println(car1);
}
}


2-labaratoriya ishi
14. Boshiga qo’yish usulidan foydalanib ro’yhatda qo’shni
elementlar ayrimasi juft bo’lgan elementni toping.
Agar bunday elementlar ko„p bo’lsa, u holda ularning eng
kattasi yoki eng kichigini toping;
agar bunday element mavjud bo’lmasa – shunga mos ma
lumot chiqaring.
Kodi:
package com.company;
import java.util.*;
public class Coding {
private static int max_dif_neighbours(int[]
nums){
int result = 0;
for(int i = 1; i < nums.length - 1; i++){
if((nums[i - 1] - nums[i + 1]) % 2 ==
0){ // qo'shni elementlar ayirmasi
result =
Math.max(result,(Math.abs(nums[i - 1] - nums[i +
1])));
}
}
return result;
}
public static void main(String[] args) {
int[] nums = new int[]
{1,3,2,4,45,5,7,8,9,654,23,5,6,64,32342,12};
System.out.println(max_dif_neighbours(nums));
}
}

3-labaratoriya ishi;


To„g„ri tanlov usulidan foydalanib, saralashni amalga oshirish dasturini ishlab chiqing (variantga mos ravishda):
14. Talabalarni birinchi imtihoni natijasi bo„yicha o„sish tartibida.
import java.util.Arrays;
class Quicksort {
// method to find the partition position
static int partition(int array[], int low, int high) {
// choose the rightmost element as pivot
int pivot = array[high];
// pointer for greater element
int i = (low - 1);
// traverse through all elements
// compare each element with pivot
for (int j = low; j < high; j++) {
if (array[j] <= pivot) {
// if element smaller than pivot is found
// swap it with the greater element pointed by i
i++;
// swapping element at i with element at j
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
// swapt the pivot element with the greater element specified by i
int temp = array[i + 1];
array[i + 1] = array[high];
array[high] = temp;
// return the position from where partition is done
return (i + 1);
}
static void quickSort(int array[], int low, int high) {
if (low < high) {
// find pivot element such that
// elements smaller than pivot are on the left
// elements greater than pivot are on the right
int pi = partition(array, low, high);
// recursive call on the left of pivot
quickSort(array, low, pi - 1);
// recursive call on the right of pivot
quickSort(array, pi + 1, high);
}
}
}
// Main class
class Main {
public static void main(String args[]) {
int[] data = { 8, 7, 2, 1, 0, 9, 6 };
System.out.println("Unsorted Array");
System.out.println(Arrays.toString(data));
int size = data.length;
// call quicksort() on array data
Quicksort.quickSort(data, 0, size - 1);
System.out.println("Sorted Array in Ascending Order ");
System.out.println(Arrays.toString(data));
}
}

Yüklə 366,28 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©azkurs.org 2024
rəhbərliyinə müraciət

gir | qeydiyyatdan keç
    Ana səhifə


yükləyin