Advertisement
Guest User

Untitled

a guest
May 11th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.86 KB | None | 0 0
  1. LAB 4
  2.  
  3. ex1
  4.  
  5.  
  6. public class Circle {
  7. private double radius;
  8. private String color;
  9.  
  10. public Circle() {
  11. this.radius = 1.0;
  12. this.color = "red";
  13. }
  14.  
  15. Circle(double r) {
  16. radius = r;
  17. color = "pink";
  18. }
  19.  
  20. Circle(double r, String c) {
  21. radius = r;
  22. color = c;
  23. }
  24.  
  25. public double getRadius() {
  26.  
  27. return radius;
  28. }
  29.  
  30. public double getArea() {
  31. return 2 * Math.PI * radius;
  32. }
  33. }
  34.  
  35.  
  36. public class TestCircle {
  37. public static void main(String[] args) {
  38. Circle c1 = new Circle();
  39. System.out.println("The first circle has the length of the radius: " + c1.getRadius());
  40. Circle c2 = new Circle();
  41. System.out.println("The second circle has the area: " + c2.getArea());
  42. }
  43. }
  44.  
  45.  
  46. ex3
  47.  
  48. public class Author {
  49. private String name;
  50. private String email;
  51. private char gender;
  52.  
  53. public Author(String name, String email, char gender) {
  54. this.name = name;
  55. this.email = email;
  56. this.gender = gender;
  57. }
  58.  
  59. public String getName() {
  60.  
  61. return name;
  62. }
  63.  
  64. public String getEmail() {
  65.  
  66. return email;
  67. }
  68.  
  69. public char getGender() {
  70.  
  71. return gender;
  72. }
  73.  
  74. public void setEmail(String email)
  75. {
  76. this.email = email;
  77. }
  78.  
  79. public String toString() {
  80. String v = this.name + "(" + this.gender + ") at " + this.email;
  81. return v;
  82. }
  83. }
  84.  
  85.  
  86. public class TestAuthor {
  87. public static void main(String[] args) {
  88. Author a1 = new Author("Ana Popescu", "anapopescu@yahoo.com", 'f');
  89. Author a2 = new Author("Alexandru Rus", "alexandru.rus@yahoo.com",'m');
  90. Author a3 = new Author("Maria Campean", "mariacampean12@yahoo.com", 'f');
  91. System.out.println("The name of the first author is "+ a1.getName());
  92. System.out.println("The gender of the second author is "+ a2.getGender());
  93. System.out.println("The email of the third author is "+ a3.getEmail());
  94. String s = a1.toString();
  95. System.out.println("First author: "+s);
  96. a2.setEmail("alexandru_rus@gmail.com");
  97. System.out.println("The new email of the second author is "+a2.getEmail());
  98. }
  99. }
  100.  
  101.  
  102. ex3
  103.  
  104. public class Book {
  105. private String name;
  106. private Author author;
  107. private double price;
  108. private int qtyInStock;
  109.  
  110. public Book(String name, Author author, double price) {
  111. this.name = name;
  112. this.author = author;
  113. this.price = price;
  114. }
  115.  
  116. public Book(String name, Author author, double price, int qtyInStock) {
  117. this.name = name;
  118. this.author = author;
  119. this.price = price;
  120. this.qtyInStock = qtyInStock;
  121. }
  122.  
  123. public String getName() {
  124. return name;
  125. }
  126.  
  127. public Author getAuthor() {
  128. return author;
  129. }
  130.  
  131. public double getPrice() {
  132. return price;
  133. }
  134.  
  135. public void setPrice(double price) {
  136. this.price = price;
  137. }
  138.  
  139. public int getQtyInStock() {
  140. return qtyInStock;
  141. }
  142.  
  143. public void setQtyInStock(int qtyInStock) {
  144. this.qtyInStock = qtyInStock;
  145. }
  146.  
  147. @Override
  148. public String toString() {
  149. String v = this.getName() + " by " + this.author.toString();
  150. return v;
  151. }
  152. }
  153.  
  154.  
  155. public class TestBook {
  156. public static void main(String[] args) {
  157. Author a1 = new Author("Anna Todd", "annatodd@yahoo.com", 'f');
  158. Author a2 = new Author("Suzzane Collins", "suzzanecollins@yahoo.com", 'f');
  159. Author a3 = new Author("Veronica Roth", "veronicaroth@yahoo.com", 'f');
  160. Book b1 = new Book("After", a1, 25, 150);
  161. Book b2 = new Book("Hunger Games", a2, 15, 110);
  162. Book b3 = new Book("Divergent", a3, 20, 200);
  163. System.out.println("The author of the book After is " + b1.getAuthor());
  164. System.out.println("The second book is " + b2.getName());
  165. System.out.println("The price of the third book is " + b3.getPrice() + " dollars");
  166. System.out.println("There are " + b1.getQtyInStock() + " After books in stock");
  167. b2.setPrice(13);
  168. System.out.println("The new price of the Hunger Games book is " + b2.getPrice() + " dollars");
  169. b3.setQtyInStock(90);
  170. System.out.println("There are only " + b3.getQtyInStock() + " Divergent books in stock! Don't miss your chance to buy one!");
  171. System.out.println("The information about the third book is " + b3.toString());
  172. }
  173. }
  174.  
  175.  
  176. ex4
  177.  
  178. public class Book {
  179. private String name;
  180. private Author[] authors;
  181. private double price;
  182. private int qtyInStock = 0;
  183.  
  184. public Book(String name, Author[] authors, double price) {
  185. this.name = name;
  186. this.authors = authors;
  187. this.price = price;
  188. }
  189.  
  190. public Book(String name, Author[] authors, double price, int qtyInStock) {
  191. this.name = name;
  192. this.authors = authors;
  193. this.price = price;
  194. this.qtyInStock = qtyInStock;
  195. }
  196.  
  197. public String getName() {
  198.  
  199. return name;
  200. }
  201.  
  202. public Author[] getAuthors() {
  203. return authors;
  204. }
  205.  
  206. public double getPrice() {
  207.  
  208. return price;
  209. }
  210.  
  211. public void setPrice(double price)
  212. {
  213. this.price = price;
  214. }
  215.  
  216. public int getQtyInStock() {
  217.  
  218. return qtyInStock;
  219. }
  220.  
  221. public void setQtyInStock(int qtyInStock) {
  222. this.qtyInStock = qtyInStock;
  223. }
  224.  
  225. @Override
  226. public String toString() {
  227. String v = this.getName() + " by " + authors.length + " authors ";
  228. return v;
  229. }
  230.  
  231. public void printAuthors() {
  232. int i;
  233. for (i = 0; i<=authors.length; i++){
  234. System.out.println(this.authors[i].toString());
  235. }
  236. }
  237. }
  238.  
  239.  
  240. public class TestBook {
  241. public static void main(String[] args) {
  242. Author a1 = new Author("John Green", "johngreen@gmail.com", 'm');
  243. Author a2 = new Author("Maureen Johnson", "maureenjohnson@gmail.com", 'f');
  244. Author a3 = new Author("Lauren Myracle", "laurenmyracle@gmail.com", 'f');
  245. Author a4 = new Author("Stephanie Dray", "stephaniedray@yahoo.com", 'f');
  246. Author a5 = new Author("Vicky Alvear", "vickyalvear@yahoo.com", 'm');
  247. Author[] authors = new Author[3];
  248. authors[0] = a1;
  249. authors[1] = a2;
  250. authors[2] = a3;
  251. Book b1 = new Book("Let it snow", authors, 30);
  252. authors = new Author[2];
  253. authors[0] = a4;
  254. authors[1] = a5;
  255. Book b2 = new Book("A day of fire", authors, 26, 100);
  256. System.out.println("The second book is " + b2.getName());
  257. System.out.println("Authors: ");
  258. b2.printAuthors();
  259. System.out.println("The price of the first book is " + b1.getPrice() + " dollars");
  260. System.out.println("There are " + b1.getQtyInStock() + " Let it snow books in stock");
  261. b2.setPrice(13);
  262. System.out.println("The new price of the second book is " + b2.getPrice() + " dollars");
  263. b2.setQtyInStock(90);
  264. System.out.println("There are only " + b2.getQtyInStock() + " A day of fire books in stock!");
  265. System.out.println("The information about the first book:", b1.toString());
  266. }
  267. }
  268.  
  269.  
  270.  
  271. ex5
  272.  
  273.  
  274. public class Cylinder extends Circle {
  275. private double height;
  276. private double volume;
  277.  
  278. public Cylinder() {
  279. this.height = 1.0;
  280. }
  281.  
  282. public Cylinder(double radius) {
  283. super(radius);
  284. }
  285.  
  286. public Cylinder(double radius, double height) {
  287. super(radius);
  288. this.height = height;
  289. }
  290.  
  291. public double getHeight() {
  292. return this.height;
  293. }
  294.  
  295. @Override
  296. public double getArea() {
  297. double a=this.getRadius() * this.getRadius() * 3.14 * this.height;
  298. return a;
  299. }
  300. }
  301.  
  302.  
  303. public class TestCylinder {
  304. public static void main(String args[]) {
  305. Cylinder c1 = new Cylinder(2, 8);
  306. Cylinder c2 = new Cylinder(1, 5);
  307. Cylinder c3 = new Cylinder(4, 2);
  308. System.out.println("Area of c1 = " + c1.getArea());
  309. System.out.println("Area of c2 = " + c2.getArea());
  310. System.out.println("Area of c3 = " + c3.getArea());
  311. }
  312. }
  313. }
  314.  
  315.  
  316.  
  317. ex 6
  318.  
  319. public class Circle extends Shape {
  320. private double radius;
  321.  
  322. public Circle() {
  323. this.radius = 1.0;
  324. }
  325. public Circle(double radius){
  326. this.radius = radius;
  327. }
  328. public Circle(double radius, String color, boolean filled){
  329. super(color,filled);
  330. this.radius = radius;
  331. }
  332.  
  333. public double getRadius() {
  334. return radius;
  335. }
  336.  
  337. public void setRadius(double radius) {
  338. this.radius = radius;
  339. }
  340. public double getArea(){
  341. return this.radius*this.radius*3.14;
  342. }
  343. public double getPerimeter(){
  344. return 2*3.14*this.radius;
  345. }
  346. @Override
  347. public String toString(){
  348. return "A Circle with radius "+this.radius+" which is a subclass of "+ super.toString();
  349. }
  350. }
  351.  
  352.  
  353.  
  354.  
  355. public class Rectangle extends Shape {
  356. private double width;
  357. private double length;
  358.  
  359. public Rectangle() {
  360. this.width = 1.0;
  361. this.length = 1.0;
  362. }
  363.  
  364. public Rectangle(double width, double length) {
  365. this.width = width;
  366. this.length = length;
  367. }
  368.  
  369. public Rectangle(double width, double lenght, String color, boolean filled) {
  370. super(color, filled);
  371. this.width = width;
  372. this.length = lenght;
  373. }
  374.  
  375. public double getWidth() {
  376. return width;
  377. }
  378.  
  379. public void setWidth(double width) {
  380. this.width = width;
  381. }
  382.  
  383. public double getLength() {
  384. return length;
  385. }
  386.  
  387. public void setLength(double length) {
  388. this.length = length;
  389. }
  390.  
  391. public double getArea() {
  392. return this.width * this.length;
  393. }
  394.  
  395. public double getPerimeter() {
  396. return 2 * (this.width + this.length);
  397. }
  398.  
  399. @Override
  400. public String toString() {
  401. return "Rectangle with width= " + this.width + " and length= " + this.length + " which is a subclass of " + super.toString();
  402. }
  403. }
  404.  
  405.  
  406.  
  407. public class Shape {
  408. private String color;
  409. private boolean filled;
  410.  
  411. public Shape() {
  412. this.color = "red";
  413. this.filled = true;
  414. }
  415.  
  416. public Shape(String color, boolean filled) {
  417. this.color = color;
  418. this.filled = filled;
  419. }
  420.  
  421. public String getColor() {
  422. return color;
  423. }
  424.  
  425. public void setColor(String color) {
  426. this.color = color;
  427. }
  428.  
  429. public boolean isFilled() {
  430. return this.filled;
  431. }
  432.  
  433. public void setFilled(boolean filled) {
  434. this.filled = filled;
  435. }
  436.  
  437. @Override
  438. public String toString() {
  439. String s;
  440. if (filled = true)
  441. s = "filled";
  442. else s = "not filled";
  443. return "Shape{" + "color:" + color + ", is " + s + "}";
  444. }
  445. }
  446.  
  447.  
  448.  
  449.  
  450. public class Square extends Rectangle {
  451.  
  452. public Square() {
  453. }
  454.  
  455. public Square(double side) {
  456. super(side, side);
  457. side = side;
  458. }
  459.  
  460. public Square(double side, String color, boolean filled) {
  461. super(side, side, color, filled);
  462. side = side;
  463. }
  464.  
  465. public double getSide() {
  466. return super.getLength();
  467. }
  468.  
  469. public void setSide(double side) {
  470. this.setLenght(side);
  471. this.setWidth(side);
  472. }
  473.  
  474. public void setWidth(double side) {
  475. super.setWidth(side);
  476. }
  477.  
  478. public void setLenght(double side) {
  479. super.setLength(side);
  480. }
  481.  
  482. public String toString() {
  483. return "A square with side = " + this.getSide() + "which is a subclass of " + super.toString();
  484. }
  485. }
  486.  
  487.  
  488.  
  489. public class TestShape {
  490. public static void main(String[] args) {
  491. Shape s = new Shape("green",true);
  492. System.out.println(s.toString());
  493.  
  494. Circle c = new Circle(3,"yellow",true);
  495. System.out.println(c.toString());
  496. System.out.println("Area = "+c.getArea());
  497. System.out.println("Perimeter = "+c.getPerimeter());
  498. System.out.println("Is filled = "+c.isFilled());
  499.  
  500. Rectangle r = new Rectangle(4,6,"pink",false);
  501. System.out.println(r.toString());
  502. System.out.println("Width = "+r.getWidth());
  503. r.setWidth(5);
  504. System.out.println("New width = "+r.getWidth());
  505. System.out.println("Area = "+r.getArea());
  506. System.out.println("Is filled = "+r.isFilled());
  507.  
  508. Square sq = new Square(10,"turquoise",false);
  509. sq.setSide(3);
  510. System.out.println(sq.toString());
  511. System.out.println("Is filled = "+sq.isFilled());
  512. System.out.println("Perimeter = "+sq.getPerimeter());
  513.  
  514.  
  515. }
  516. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement