Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AccessExample {
- public static void main(String[] args) {
- // обращение к полям с разными режимами доступа
- Cube c = new Cube(5,4,3);
- c.print();
- System.out.println(c.width);
- System.out.println(c.depth);
- }
- }
- public class Chair extends Object {
- private String color;
- String material;
- double age;
- public Chair(String material, double age, String color) {
- this.material = material;
- this.age = age;
- this.color = color;
- }
- @Override
- public String toString() {
- System.out.println("color: "+this.color + ", material: " + this.material + ", age: " + this.age);
- return "";
- }
- }
- public class Cube {
- public double width;
- private double height;
- protected double depth;
- public Cube(double width, double height, double depth) {
- this.width = width;
- this.height = height;
- this.depth = depth;
- }
- public void print(){ System.out.println(this.width + " | " +
- this.height +" | "
- + this.depth);
- }
- }
- public class Furniture {
- static String material;
- double age;
- public Furniture() {}
- public Furniture(String material, double age) {
- this.material = material;
- this.age = age;
- }
- public void print() {
- System.out.println("Furniture: ");
- System.out.println("material: " +
- this.material + "; age: " + this.age);
- }
- }
- public class InheritanceExample {
- public static void main(String[] args){
- Furniture furniture = new Furniture("wood",2.5);
- furniture.print();
- Chair chair = new Chair("aluminum", 1.3, "beige");
- System.out.println(chair.material);
- chair.toString();
- }
- }
- public class OverloadExample {
- public static void main(String[] args) {
- Plant p1 = new Plant();
- Plant p2 = new Plant("tulip", "red");
- Plant p3 = new Plant("cactus");
- Plant p4 = new Plant("rose", "white", "Holland", false);
- p1.print();
- p2.print();
- p3.print();
- p4.print();
- }
- }
- public class Plant {
- private String type;
- private String color;
- private String existenceArea;
- private Boolean rare;
- public Plant() {}
- public Plant(String type, String color) {
- this.type = type;
- this.color = color;
- }
- public Plant(String type) {
- this.type = type;
- }
- public Plant(String type, String color, String existenceArea) {
- this.type = type;
- this.color = color;
- this.existenceArea = existenceArea;
- }
- public Plant(String type, String color, String existenceArea, Boolean rare) {
- this.type = type;
- this.color = color;
- this.existenceArea = existenceArea;
- this.rare = rare;
- }
- public void print(){
- System.out.println("type: " + this.type + "; color: " + this.color + "; existing area " + this.existenceArea + "; rare " + this.rare);
- }
- }
- public class Spy {
- public String name;
- private String realName;
- private Integer squad;
- private void getSpyInfo(){
- System.out.println(this.realName + " " + this.squad);
- }
- public void print(){
- System.out.println(this.name);
- }
- public void setName(String name){
- this.name = name;
- }
- public void setRealName(String realName){
- this.realName = realName;
- }
- public void setSquad(Integer squad){
- this.squad = squad;
- }
- public String getName(){
- return this.name;
- }
- public String getRealName(){
- return this.realName;
- }
- public Integer getSquad(){
- return this.squad;
- }
- }
- public class SpyAccess {
- public static void main(String[] args) {
- Spy spy = new Spy();
- spy.setName("James");
- spy.setRealName("James Bond");
- spy.setSquad(7);
- spy.print();
- // spy.getSpyInfo(); вызовет ошибку
- System.out.println(spy.getName());
- System.out.println(spy.getRealName());
- System.out.println(spy.getSquad());
- System.out.println(spy.name);
- //System.out.println(spy.squad); вызовет ошибку
- //System.out.println(spy.realName); вызовет ошибку
- }
- }
- public class StaticCheck {
- public static void main(String[] args) {
- while (true){
- StaticContainer.operation();
- if (StaticContainer.counter > 100){
- System.out.println(StaticContainer.counter);
- break;
- }
- }
- }
- }
- public class StaticContainer {
- static Integer counter = 0;
- public static void operation(){
- counter +=3;
- }
- }
- public class StaticExample {
- static int result;
- public static void main(String[] args){
- int[] array = {-3, 20, 5, 16, 27};
- for (int value: array) {
- result = (value % 4 == 0) ? 1 : 0;
- System.out.println(result);
- }
- Triangle t1 = new Triangle(3);
- Triangle t2 = new Triangle(4);
- Triangle.checkTriangles(t1,t2);
- }
- }
- public class Triangle {
- public double side; // сторона
- public Triangle(double side) {
- this.side = side;
- }
- public double area(){
- return(Math.sqrt(3)/4)*Math.pow(this.side,2);
- }
- public static void checkTriangles(Triangle triangle1, Triangle triangle2) {
- double area1 = triangle1.area();
- double area2 = triangle2.area();
- if (area1 == area2) {
- System.out.println("Треугольники равны");
- }
- else if (area1 > area2) {
- System.out.println("Первый треугольник больше второго");
- }
- else {System.out.println("Второй треугольник больше первого");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement