rushdie

Preaent 7 Adult & Triangle + Present 6 Last

Dec 6th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.87 KB | None | 0 0
  1. package Rectangletester;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class rectangleProgram {
  6.  
  7. public static void main(String[] args) {
  8. Scanner s = new Scanner(System.in);
  9. Rectangle rec1 = new Rectangle();
  10. Rectangle rec2 = new Rectangle();
  11.  
  12. rec1.setLength(4.5);
  13. rec1.setHeight(5.5);
  14. System.out.println("The Area Surface is: " + rec1.surface(rec1.getLength(), rec1.getHeight()));
  15. System.out.println("The Scope is: " + rec1.scope(rec1.getLength(), rec1.getHeight()));
  16.  
  17. rec1.starPrint(4.5, 5.5);
  18. System.out.println("Enter the character to print with");
  19. rec1.setCH(s.nextLine().charAt(0));
  20. rec1.charPrint(rec1.getLength(), rec1.getHeight(), rec1.getCH());
  21.  
  22. //Check rec2
  23.  
  24. rec2.setLength(-5);
  25. rec2.setHeight(-5);
  26. rec2.starPrint(rec2.getLength(), rec2.getHeight()); // it should print the default 10 BY 10
  27. //================================================================================================================
  28. package Rectangletester;
  29.  
  30. public class Rectangle {
  31. private double _length, _height;
  32. private char _CH;
  33.  
  34. /**
  35. * @param _length
  36. * @param _hieght
  37. * @param _CH
  38. * Constructor with the default of 10 BY 10 and * as the printing
  39. * character
  40. */
  41. public Rectangle() {
  42. super();
  43. this._length = 10;
  44. this._height = 10;
  45. this._CH = '*';
  46. }
  47.  
  48. public Rectangle(double length, double hieght, char CH) {
  49. this._length = length;
  50. this._height = hieght;
  51. this._CH = CH;
  52. }
  53.  
  54. // Duplicate constructor of the ORIGIONAL
  55. public Rectangle(Rectangle other) {
  56. _length = other._length;
  57. _height = other._height;
  58. _CH = other._CH;
  59. }
  60.  
  61. // Builder without the Character
  62. public Rectangle(double length, double height) {
  63. this(length, height, ' ');
  64.  
  65. }
  66.  
  67. // _length getter
  68. public double getLength() {
  69. return _length;
  70. }
  71.  
  72. // length Setter with checking if user enter a negative length or hieght
  73. public void setLength(double length) {
  74. if (_length <= 1 || _height <= 1) {
  75. System.out.println(" Length and Height of a RectAngle has to be greator than 0");
  76. System.out.println(" Default Length of RectAngle will Equal 10.0 X 10.0 cm. ");
  77. this._length = 10; // set length and height to 10 and printing with
  78. this._height = 10;
  79. this._CH = '*';
  80.  
  81. } else {
  82. this._length = length;
  83. }
  84. }
  85.  
  86. // height getter
  87. public double getHeight() {
  88. return _height;
  89. }
  90.  
  91. // Height setter
  92. public void setHeight(double height) {
  93. if (_length <= 1 || _height <= 1) {
  94. System.out.println(" Length and Hieght of a RectAngle has to be greator than 0");
  95. System.out.println(" Default Length of RectAngle will Equal 10.0X10.0 cm. ");
  96. this._length = 10; // set length and height to 10 and printing with
  97. this._height = 10;
  98. this._CH = '*';
  99.  
  100. } else {
  101. this._height = height;
  102. }
  103. }
  104.  
  105. // CH Getter from the user
  106. public char getCH() {
  107. return _CH;
  108. }
  109.  
  110. // CH Setter
  111. public char setCH(char CH) {
  112. return this._CH = CH;
  113.  
  114. }
  115.  
  116. // Printing Rectangle using the Math.floor method to round Length and Height
  117. public void starPrint(double length, double height) {
  118. for (int i = 1; i <= Math.floor(height); i += 1) {
  119. for (int j = 1; j <= Math.floor(length); j += 1)
  120. System.out.print("*");
  121. System.out.println();
  122. }
  123. System.out.println();
  124. }
  125.  
  126. // Printing Rectangle using the Math.floor method to round Length and Height
  127. // to print w user Input
  128. public void charPrint(double length, double height, char CH) {
  129. for (int i = 1; i <= Math.floor(height); i += 1) {
  130. for (int j = 1; j <= Math.floor(length); j += 1)
  131. System.out.print(this._CH);
  132. System.out.println();
  133. }
  134. System.out.println();
  135.  
  136. }
  137.  
  138. // method calculate Surface or Area
  139. public double surface(double length, double height) {
  140. double surface = 0;
  141. surface = length * height;
  142. return surface;
  143. }
  144.  
  145. // Method to calculate Scope the outer length and Height of Rectangle
  146. public double scope(double length, double height) {
  147. double scope = 0;
  148. return scope = 2 * length + 2 * height;
  149.  
  150. }
  151.  
  152. }
  153. //================================================================================
  154. package PersonTester;
  155.  
  156. public class Person {
  157.  
  158. private String _name;
  159. private float _height;
  160. private float _weight;
  161.  
  162. public Person() {
  163.  
  164. }
  165.  
  166. public Person(String name, float height, float weight) {
  167. setName(name);
  168. setHeight(height);
  169. setWeight(weight);
  170. }
  171.  
  172. private Person(Person person) {
  173. this(person.getName(), person.getHeight(), person.getWeight());
  174. }
  175.  
  176. public String getName() {
  177. return _name;
  178. }
  179.  
  180. public float getHeight() {
  181. return _height;
  182. }
  183.  
  184. public float getWeight() {
  185. return _weight;
  186. }
  187.  
  188. public void setName(String name) {
  189. _name = name;
  190.  
  191. }
  192.  
  193. public void setHeight(float height) {
  194. _height = height;
  195.  
  196. }
  197.  
  198. public void setWeight(float weight) {
  199. _weight = weight;
  200.  
  201. }
  202.  
  203. public String show() {
  204. return toString();
  205. }
  206.  
  207. public String toString() {
  208. return getName() + ": Height: " + getHeight() + " and Weight: "
  209. + getWeight();
  210. }
  211.  
  212. public Person clone() {
  213. return new Person(this);
  214. }
  215.  
  216. public boolean equals(Object person) {
  217. if (!(person instanceof Person)) {
  218. return false;
  219. }
  220. Person p = (Person) person;
  221. if (p.getName().equals(getName()) && p.getHeight() == getHeight()
  222. && p.getWeight() == getWeight()) {
  223. return true;
  224. }
  225. return false;
  226. }
  227.  
  228. public static void orderArrayByHeight(Person[] people) {
  229. // Short to Tall
  230. Person temp;
  231.  
  232. for (int i = 1; i < people.length; i += 1) {
  233. if (people[i].getHeight() > people[i - 1].getHeight()) {
  234. temp = people[i];
  235. people[i] = people[i - 1];
  236. people[i - 1] = temp;
  237. i = 0;
  238.  
  239. }
  240.  
  241. }
  242.  
  243. }
  244.  
  245. public static void orderArrayByWeight(Person[] people) {
  246. // Light Weight to Heavy
  247. Person temp;
  248.  
  249. for (int i = 1; i < people.length; i += 1) {
  250. if (people[i].getWeight() < people[i - 1].getWeight()) {
  251. temp = people[i];
  252. people[i] = people[i - 1];
  253. people[i - 1] = temp;
  254.  
  255. i = 0;
  256. }
  257.  
  258. }
  259. } // end of array
  260.  
  261. } // End of Person Class
  262.  
  263. //======================================================================================================
  264. package PersonTester;
  265.  
  266. public class PersonProgram {
  267.  
  268. public static void main(String[] args) {
  269. //Scanner s = new Scanner(System.in);
  270. Person[] people = new Person[10];
  271. fillRandomPersonArray(people);
  272. System.out.println(" People's Array generated Randomly");
  273. System.out.println("=================================================");
  274.  
  275.  
  276. for(Person p: people){
  277. System.out.println(p.show());
  278. }
  279.  
  280. Person.orderArrayByHeight(people);
  281. System.out.println("\nAfter arranging by height (tallest to shortest)");
  282. System.out.println("================================================");
  283.  
  284. for(Person p: people){
  285. System.out.println(p);
  286. }
  287.  
  288. Person.orderArrayByWeight(people);
  289. System.out.println("\nAfter arranging by weight (lightest to heaviest)");
  290. System.out.println("=================================================");
  291.  
  292. for(Person p: people){
  293. System.out.println(p);
  294. }
  295.  
  296.  
  297. }
  298.  
  299. //name: fillRandomPersonArray
  300. //args: Person[] people - an array of type Person
  301. //description: fills an array of type Person with random entries. the name will be "name"+indexNumber
  302. //returns: N/A (void) but changes the given array
  303. public static void fillRandomPersonArray(Person[] people){
  304. for(int i=0; i<people.length; i+=1){
  305. people[i]= new Person("Person"+i,(int)(Math.random()*85)+115,(int)(Math.random()*50)+50);
  306. //there is implicit casting from int to float
  307.  
  308.  
  309. /*
  310. * Person p = new Person("name", height, weight);
  311. * for our purposes is the same as:
  312. * p.setName("name");
  313. * p.setHeight(height);
  314. * p.serWeight(weight);
  315. */
  316.  
  317. }
  318. }
  319.  
  320.  
  321.  
  322. }
  323. //=================================================================================================================
  324. /**
  325. *
  326. */
  327.  
  328. /**
  329. * @author Rushdy
  330. *
  331. */
  332. public class Adult {
  333. private String name;
  334. private float height;
  335. private String job;
  336.  
  337. /**
  338. *
  339. */
  340. public Adult() {
  341. super();
  342. // TODO Auto-generated constructor stub
  343. }
  344.  
  345. /**
  346. * @param name
  347. * @param height
  348. * @param job
  349. */
  350. public Adult(String name, float height, String job) {
  351. super();
  352. if (this.name == null) {
  353. this.name = " Person";
  354. }
  355. this.height = height;
  356. this.job = job;
  357. }
  358.  
  359. public Adult(String name, float height) {
  360. this(name, height, null);
  361. }
  362.  
  363. // Copy Constructor
  364.  
  365. public Adult(Adult other) {
  366. name = other.name;
  367. height = other.height;
  368. job = other.job;
  369.  
  370. }
  371.  
  372. public Adult(String job) {
  373.  
  374. this(" ", 0, job);
  375. }
  376.  
  377. public String getName() {
  378. return name;
  379. }
  380.  
  381. public void setName(String name) {
  382. this.name = name;
  383. }
  384.  
  385. public float getHeight() {
  386. return height;
  387. }
  388.  
  389. public void setHeight(float height) {
  390. this.height = height;
  391. }
  392.  
  393. public String getJob() {
  394. return job;
  395. }
  396.  
  397. public void setJob(String job) {
  398. this.job = job;
  399. }
  400.  
  401. public void print(String name, float height, String job) {
  402. if (job == null)
  403. {
  404. System.out.println(" Person name: " + name + " Person Height: " + height);
  405. }
  406. else
  407. {
  408. System.out.println(" Person name: " + name + " Person Height: " + height + " Person Job :" + job);
  409. }
  410. }
  411.  
  412.  
  413.  
  414. }
  415. //=========================================================================================================================
  416.  
  417. public class adultProgram {
  418.  
  419. public static void main(String[] args) {
  420.  
  421.  
  422. // TODO Auto-generated method stub
  423. Adult person1 = new Adult();
  424. person1.setName("geege");
  425. person1.setHeight(190);
  426. person1.setJob("Teacher");
  427.  
  428. person1.print(person1.getName(), person1.getHeight(), person1.getJob());
  429.  
  430. Adult person2 = new Adult();
  431. person2.setName("feefe");
  432. person2.setHeight(180);
  433.  
  434. person2.print(person2.getName(),person2.getHeight(),person2.getJob()
  435. );
  436. Adult person3 = new Adult();
  437. person3.setName("Meeme");
  438. person3.setHeight(190);
  439. person3.setJob("Teacher");
  440. person3.print(person3.getName(), person3.getHeight(), person3.getJob());
  441.  
  442. person3.setJob("Programmer");
  443. person3.print(person3.getName(), person3.getHeight(), person3.getJob());
  444.  
  445. }
  446.  
  447. }
Add Comment
Please, Sign In to add comment