Advertisement
kostya_voronovich

laba2

Dec 6th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.00 KB | None | 0 0
  1. public class AccessExample {
  2. public static void main(String[] args) {
  3. // обращение к полям с разными режимами доступа
  4. Cube c = new Cube(5,4,3);
  5. c.print();
  6. System.out.println(c.width);
  7. System.out.println(c.depth);
  8. }
  9. }
  10.  
  11. public class Chair extends Object {
  12. private String color;
  13. String material;
  14. double age;
  15. public Chair(String material, double age, String color) {
  16. this.material = material;
  17. this.age = age;
  18. this.color = color;
  19. }
  20. @Override
  21. public String toString() {
  22. System.out.println("color: "+this.color + ", material: " + this.material + ", age: " + this.age);
  23. return "";
  24. }
  25. }
  26.  
  27. public class Cube {
  28. public double width;
  29. private double height;
  30. protected double depth;
  31. public Cube(double width, double height, double depth) {
  32. this.width = width;
  33. this.height = height;
  34. this.depth = depth;
  35. }
  36. public void print(){ System.out.println(this.width + " | " +
  37. this.height +" | "
  38. + this.depth);
  39. }
  40. }
  41.  
  42. public class Furniture {
  43. static String material;
  44. double age;
  45. public Furniture() {}
  46. public Furniture(String material, double age) {
  47. this.material = material;
  48. this.age = age;
  49. }
  50. public void print() {
  51. System.out.println("Furniture: ");
  52. System.out.println("material: " +
  53. this.material + "; age: " + this.age);
  54. }
  55. }
  56.  
  57. public class InheritanceExample {
  58. public static void main(String[] args){
  59. Furniture furniture = new Furniture("wood",2.5);
  60. furniture.print();
  61. Chair chair = new Chair("aluminum", 1.3, "beige");
  62. System.out.println(chair.material);
  63. chair.toString();
  64. }
  65. }
  66.  
  67. public class OverloadExample {
  68. public static void main(String[] args) {
  69. Plant p1 = new Plant();
  70. Plant p2 = new Plant("tulip", "red");
  71. Plant p3 = new Plant("cactus");
  72. Plant p4 = new Plant("rose", "white", "Holland", false);
  73. p1.print();
  74. p2.print();
  75. p3.print();
  76. p4.print();
  77. }
  78. }
  79.  
  80. public class Plant {
  81. private String type;
  82. private String color;
  83. private String existenceArea;
  84. private Boolean rare;
  85. public Plant() {}
  86. public Plant(String type, String color) {
  87. this.type = type;
  88. this.color = color;
  89. }
  90. public Plant(String type) {
  91. this.type = type;
  92. }
  93. public Plant(String type, String color, String existenceArea) {
  94. this.type = type;
  95. this.color = color;
  96. this.existenceArea = existenceArea;
  97. }
  98. public Plant(String type, String color, String existenceArea, Boolean rare) {
  99. this.type = type;
  100. this.color = color;
  101. this.existenceArea = existenceArea;
  102. this.rare = rare;
  103. }
  104. public void print(){
  105. System.out.println("type: " + this.type + "; color: " + this.color + "; existing area " + this.existenceArea + "; rare " + this.rare);
  106. }
  107. }
  108.  
  109. public class Spy {
  110. public String name;
  111. private String realName;
  112. private Integer squad;
  113. private void getSpyInfo(){
  114. System.out.println(this.realName + " " + this.squad);
  115. }
  116. public void print(){
  117. System.out.println(this.name);
  118. }
  119. public void setName(String name){
  120. this.name = name;
  121. }
  122. public void setRealName(String realName){
  123. this.realName = realName;
  124. }
  125. public void setSquad(Integer squad){
  126. this.squad = squad;
  127. }
  128. public String getName(){
  129. return this.name;
  130. }
  131. public String getRealName(){
  132. return this.realName;
  133. }
  134. public Integer getSquad(){
  135. return this.squad;
  136. }
  137. }
  138. public class SpyAccess {
  139. public static void main(String[] args) {
  140. Spy spy = new Spy();
  141. spy.setName("James");
  142. spy.setRealName("James Bond");
  143. spy.setSquad(7);
  144. spy.print();
  145. // spy.getSpyInfo(); вызовет ошибку
  146. System.out.println(spy.getName());
  147. System.out.println(spy.getRealName());
  148. System.out.println(spy.getSquad());
  149. System.out.println(spy.name);
  150. //System.out.println(spy.squad); вызовет ошибку
  151. //System.out.println(spy.realName); вызовет ошибку
  152. }
  153. }
  154.  
  155. public class StaticCheck {
  156. public static void main(String[] args) {
  157. while (true){
  158. StaticContainer.operation();
  159. if (StaticContainer.counter > 100){
  160. System.out.println(StaticContainer.counter);
  161. break;
  162. }
  163. }
  164. }
  165. }
  166.  
  167. public class StaticContainer {
  168. static Integer counter = 0;
  169. public static void operation(){
  170. counter +=3;
  171. }
  172. }
  173.  
  174. public class StaticExample {
  175. static int result;
  176. public static void main(String[] args){
  177. int[] array = {-3, 20, 5, 16, 27};
  178. for (int value: array) {
  179. result = (value % 4 == 0) ? 1 : 0;
  180. System.out.println(result);
  181. }
  182. Triangle t1 = new Triangle(3);
  183. Triangle t2 = new Triangle(4);
  184. Triangle.checkTriangles(t1,t2);
  185. }
  186. }
  187.  
  188. public class Triangle {
  189. public double side; // сторона
  190. public Triangle(double side) {
  191. this.side = side;
  192. }
  193.  
  194. public double area(){
  195. return(Math.sqrt(3)/4)*Math.pow(this.side,2);
  196. }
  197. public static void checkTriangles(Triangle triangle1, Triangle triangle2) {
  198. double area1 = triangle1.area();
  199. double area2 = triangle2.area();
  200. if (area1 == area2) {
  201. System.out.println("Треугольники равны");
  202. }
  203. else if (area1 > area2) {
  204. System.out.println("Первый треугольник больше второго");
  205. }
  206. else {System.out.println("Второй треугольник больше первого");
  207. }
  208. }
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement