Posted By

joshnunn on 08/13/09


Tagged

basic class java learning getter setter IFELSE


Versions (?)

Robot.java


 / Published in: Java
 

  1. public class Robot {
  2. private String name;
  3. private double weight;
  4. private String badHabit;
  5. private boolean hasVision;
  6. private int aiq;
  7. private int mutations;
  8. private boolean decommissioned;
  9.  
  10. public Robot(String name, double weight, String badHabit, boolean hasVision, int aiq){
  11. this.name = name;
  12. if (weight < 0){
  13. this.weight = Math.abs(weight);
  14. }
  15. else {
  16. this.weight = weight;
  17. }
  18. this.badHabit = badHabit;
  19. this.hasVision = hasVision;
  20. this.aiq = aiq;
  21. if (this.aiq <= 0){
  22. this.aiq = 0;
  23. }
  24. if (this.aiq >= 3){
  25. this.aiq = 3;
  26. }
  27. mutations = 0;
  28. decommissioned = false;
  29. }
  30.  
  31. public void mutate(){
  32. mutations++;
  33. if (mutations >= 5){
  34. decommissioned = true;
  35. }
  36. }
  37.  
  38. public void setBadHabit(String badHabit){
  39. if (!decommissioned){
  40. this.badHabit = badHabit;
  41. mutate();
  42. }
  43. }
  44.  
  45. public void addModule(){
  46. if (!decommissioned){
  47. if (aiq < 3){
  48. aiq++;
  49. }
  50. else {
  51. aiq = 3;
  52. }
  53. mutate();
  54. }
  55. }
  56.  
  57. public String getName(){
  58. return name;
  59. }
  60.  
  61. public double getWeight(){
  62. return weight;
  63. }
  64.  
  65. public boolean getSight(){
  66. return hasVision;
  67. }
  68.  
  69. public String getAIQ(){
  70. if (aiq == 0){
  71. return "Non Existant";
  72. }
  73. if (aiq == 1){
  74. return "Low";
  75. }
  76. if (aiq == 2){
  77. return "Medium";
  78. }
  79. else {
  80. return "High";
  81. }
  82. }
  83.  
  84. public String getBadHabit(){
  85. return badHabit;
  86. }
  87.  
  88. public void displayDetails(){
  89. System.out.println("The robot's name is " + name);
  90. System.out.println("The robot's weight is " + weight);
  91. System.out.println("The robot has a bad habit of " + badHabit);
  92. System.out.println("The robot can see = " + hasVision);
  93. System.out.println("The robot's AIQ is " + getAIQ());
  94. System.out.println("The robot has undergone " + mutations + " mutations.");
  95. System.out.println();
  96. }
  97. }

Report this snippet  

You need to login to post a comment.