fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Problem3_1 {
  9.  
  10. // complete this class, called Problem3_1, with the following items:
  11.  
  12. // 1. Declare four attributes, name, age, height, and weight of types String
  13. // and int-s.
  14. // Write a constructor for this class that initializes ONLY the name, age,
  15. // and height to three incoming arguments,
  16. // and sets the weight to always be -1 (the latter is not an incoming
  17. // argument).
  18. String name;
  19. int age;
  20. @Override
  21. public String toString() {
  22. return "Problem3_1 [name=" + name + ", age=" + age + ", height=" + height + ", weight=" + weight + ", address="
  23. + address + "]";
  24. }
  25.  
  26. int height;
  27. int weight;
  28.  
  29. Address address;
  30.  
  31. public Problem3_1(String name1, int age1, int height1) {
  32. name = name1;
  33. age = age1;
  34. height = height1;
  35. weight = -1;
  36. }
  37.  
  38. void setAddress(Address s) {
  39. //address = s;
  40.  
  41. this.address = s;
  42. }
  43.  
  44. // 2. Imagine there is a class called Address that you have access to (it's
  45. // below).
  46. // Its constructor takes an integer street number and a String street. Add
  47. // an attribute called address to
  48. // the Problem3_1 class, and create a **method called setAddress that sets
  49. // the attribute to the incoming argument.**
  50.  
  51. public static class Address {
  52.  
  53. int number;
  54. String street;
  55.  
  56. public Address(int n, String s) {
  57. number = n;
  58. street = s;
  59. }
  60.  
  61. @Override
  62. public String toString() {
  63. return "Address [number=" + number + ", street=" + street + "]";
  64. }
  65.  
  66. }
  67.  
  68. public static void main(String[] args) {
  69. // TODO Auto-generated method stub
  70.  
  71. /*Problem3_1 ppp = new Problem3_1("name1",20,5);
  72. Problem3_1.Address a_address= new Problem3_1.Address(20,"1st street");
  73. a.setAddress(a_address);
  74.  
  75. System.out.println(a.address);*/
  76.  
  77. int failed = 0;
  78.  
  79. Problem3_1 p = new Problem3_1("Jane", 22, 65);
  80.  
  81. if (p.name.compareTo("Jane") == 0 && p.age == 22 && p.height == 65 && p.weight == -1)
  82. System.out.println("Test 1 passed!");
  83. else{
  84. failed ++;
  85. System.out.println("Please check your code for question 1! ");
  86. }
  87.  
  88. Address a = new Address(12, "Fairfax Dr");
  89. p.setAddress(a);
  90.  
  91. if (p.address == a)
  92. System.out.println("Test 2 passed!");
  93. else{
  94. failed ++;
  95. System.out.println("Please check your code for question 2! ");
  96. }
  97.  
  98.  
  99. if (failed > 0)
  100. System.out.println("Failed");
  101. else{
  102. System.out.println("GREAT WORK! EVERYTHING PASSED!");
  103. System.out.println("Nice");
  104. }
  105. }
  106.  
  107. }
Success #stdin #stdout 0.04s 711168KB
stdin
Standard input is empty
stdout
Test 1 passed!
Test 2 passed!
GREAT WORK! EVERYTHING PASSED!
Nice