1-rasmda berilgan struktura asosida klasslarni yarating. display() methodini overriding usulida amalga oshiring
Rasm : https://eclass.ictschool.uz/pluginfile.php/79282/mod_assign/introattachment/0/task1.png?forcedownload=1
public class Main {
public static void main(String[] args) {
Duck duck = new Duck();
duck.quack();
duck.swim();
MallardDuck mduck = new MallardDuck();
mduck.display();
RedheadDuck rhduck = new RedheadDuck();
rhduck.display();
}
}
public class Duck {
void quack(){System.out.println("O'rdak kvak lay oladi");}
void swim(){System.out.println("O'rdak suza oladi");}
void display(){};
}
public class MallardDuck extends Duck{
@Override
void display() {System.out.println("I o'rdak turi : Mallard");}
}
public class RedheadDuck extends Duck{
@Override
void display() {System.out.println("II o'rdak turi : Red Head");}
}
2-rasmda berilgan struktura asosida klasslarni yarating. sound() methodini overriding usulida amalga oshiring. Barcha hayvonlarning o'ziga xos parametrlarini klass fieldi sifatida bering.
Rasm: https://eclass.ictschool.uz/pluginfile.php/79282/mod_assign/introattachment/0/task2.png?forcedownload=1
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.sound();
Cat cat = new Cat();
cat.sound();
Duck duck = new Duck();
duck.sound();
}
}
public class Animal {
void sound() {};
}
public class Dog extends Animal{
public String dogSound = "Bow Bow";
@Override
void sound() {System.out.println("Kuchuk "+dogSound+" qila oladi ");
}
}
public class Cat extends Animal{
String catSound = "Meow Meow";
@Override
void sound() {System.out.println("Mushuk "+catSound+" qila oladi");
}
}
String duckSound = "Quack Quack";
@Override
void sound() {System.out.println("O'rdak "+duckSound+" qila oladi");
}
}
3-rasmda berilgan struktura asosida klasslarni yarating. area() va perimeter() methodlarini overriding usulida amalga oshiring.
Rasm: https://eclass.ictschool.uz/pluginfile.php/79282/mod_assign/introattachment/0/task3.png?forcedownload=1
public class Main {
public static void main(String[] args) {
Circle circle = new Circle();
System.out.println("Aylana radiusi: "+ circle.radius);
circle.area();
circle.perimetr();
System.out.println("---------------------------------------");
Triangle tria = new Triangle();
System.out.println("To'g'ri uchburchak I tomoni: "+tria.sideA);
System.out.println("To'g'ri uchburchak II tomoni: "+tria.sideB);
System.out.println("To'g'ri uchburchak gipotenuzasi: "+tria.hipotenuze);
tria.area();
tria.perimetr();
System.out.println("---------------------------------------");
Rectangle rect = new Rectangle();
System.out.println("to'g'ri to'rtburchak I tomoni: "+rect.sideA);
System.out.println("to'g'ri to'rtburchak II tomoni: "+rect.sideB);
rect.area();
rect.perimetr();
}
}
public class Figure {
void area(){};
void perimetr(){};
}
public class Circle extends Figure{
double radius = 30.5;
@Override
void area() {System.out.println("Aylana yuzi: "+ Math.PI * radius * radius);}
@Override
void perimetr() {System.out.println("Aylana perimetri: "+ 2 * Math.PI * radius);}
}
public class Triangle extends Figure{
int sideA = 3;
int sideB = 4;
int hipotenuze = 5;
@Override
void area() {System.out.println("To'g'ri uchburchak yuzi: "+ 0.5 * sideA * sideB);}
@Override
void perimetr() {System.out.println("To'g'ri uchburchak yuzi: "+ sideA + sideB + hipotenuze);}
}
public class Rectangle extends Figure{
int sideA = 15;
int sideB = 14;
@Override
void area() {System.out.println("yuzi: "+sideA * sideB);}
@Override
void perimetr() {System.out.println("perimetri: "+(sideA + sideB)*2);}
}
Do'stlaringiz bilan baham: |