realizatsiya qiluvchi sinf:
public class Real implements Matrix {
@Override
public int min(int[] a) {
int min = a[0];
for (int i = 0; i < a.length; i++) {
if ( a[i] min=a[i];
}
return min;
}
@Override
public int max(int[] a) {
int max = a[0];
for (int i = 0; i < a.length; i++) {
if ( a[i]>max )
max=a[i];
}
return max;
}
@Override
public int juftlar_soni(int[] a) {
int j_soni = 0;
for (int i = 0; i < a.length; i++) {
if ( a[i]%2==0 )
j_soni++;
}
return j_soni;
}
@Override
public int toqlar_soni(int[] a) {
int t_soni = 0;
for (int i = 0; i < a.length; i++) {
if ( a[i]%2==1 )
t_soni++;
}
return t_soni;
}
@Override
public int musbat_soni(int[] a) {
int m_soni = 0;
for (int i = 0; i < a.length; i++) {
if ( a[i]>0 )
m_soni++;
}
return m_soni;
}
@Override
public int manfiy_soni(int[] a) {
int man_soni = 0;
for (int i = 0; i < a.length; i++) {
if ( a[i]<0 )
man_soni++;
}
return man_soni;
}
@Override
public int sum(int[] a) {
int s = 0;
for (int i = 0; i < a.length; i++) {
s+=a[i];
}
return s;
}
@Override
public int kop(int[] a) {
int p = 1;
for (int i = 0; i < a.length; i++) {
p*=a[i];
}
return p;
}
}
Asosiy sinfi:
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
Real m=new Real();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] a=new int[n];
for (int i = 0; i < n; i++) {
a[i]=sc.nextInt();
}
System.out.println( "juftlar soni="+ m.juftlar_soni(a));
System.out.println("min="+m.min(a));
System.out.println("max="+m.max(a));
System.out.println("sum+"+m.sum(a));
System.out.println("kop"+m.kop(a));
System.out.println("toqlar soni"+m.toqlar_soni(a));
System.out.println("musbat soni"+m.musbat_soni(a));
System.out.println("manfiy soni"+ m.manfiy_soni(a));
}}
Interfeyslarda turli sinflarga birgalikda foydalanuvchi constantalarni import qilish uchun ham foydalanish mumkin. Bu hol biror sinfda interfeys realizatsiya qilinsa shu sinfda constanta sifatida ko’rsatiladi.
Biz oldingi mulohazalarimizda sinflar metod va maydonlardan iborat deb qarardik, lekin sinflarda interfeyslar tashkil qilsa ham mumkin.
A sinfda interface yaratamiz
public class A {
public interface Test{
public int qoshish(int x, int y);
}
}
//Realizatsiya qiluvchi sinf B
public class B implements A.Test{
@Override
public int qoshish(int x, int y){
return x+y;
}
}
//Asosiy sinf:
package lesson28;
public class MainKlass {
public static void main(String[] Args){
B b=new B();
System.out.println(b.qoshish(5,45));
}
}
0>
Do'stlaringiz bilan baham: |