Guest User

Untitled

a guest
Jun 19th, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. Notes Unit 2
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package unit2;
  7.  
  8.  
  9. /**
  10. *Name:
  11. *Date:
  12. * @author bassr8431
  13. */
  14. public class NotesUnit2 {
  15.  
  16. public static void main(String[] args)
  17. {
  18. Student defaultstu = new Student(); // So the three names are just number of them and the variables are placed in same order behind to define it
  19. Student student1 = new Student("Sue", 15, 234.989); // So this wont be a double since I casted it down to an int with (int)i
  20. Student student2 = new Student("Joe", 16, 345345345);
  21. //??? Why would he throw the numbers on the end when we are just creating a new student?
  22. student1.setName("Bill"); // So this changes the name of student one instead of naming the student upon its creation
  23. System.out.println("");
  24. System.out.println(student1.getName()); // So this is setter and getter for name. You can also have them for Id and GPA
  25. System.out.println("using modifier");
  26.  
  27. }
  28. }
  29.  
  30. Student.Java
  31. *
  32. * To change this template, choose Tools | Templates
  33. * and open the template in the editor.
  34. */
  35. package unit2;
  36.  
  37. /**
  38. *Name:
  39. *Date:
  40. * @author bassr8431
  41. */
  42. public class Student {
  43. private String name;
  44. private int age;
  45. private int idNum;
  46. //these up top that are accesible through all classes when starting brace
  47. //When using this however in Java they do have to be assigned later on in the code
  48. //Private means that they cannot be changed
  49. //default constructor
  50. public Student()
  51. {
  52. name = "default";
  53. age = 13;
  54. idNum = 0000;
  55. //double gpa = 0.0;
  56. // so this GPA double only works within this class. Why constructors are important
  57. //This would be used if the data does not need be saved like the others but just calculated once
  58. System.out.println(name + " " + age + " " + idNum);
  59. // you do not have to define a default but you just need one constructor. The other is just an example of my possiblity
  60. }
  61. //constructor that takes name, age , and ID number
  62. public Student(String n, int a, double i) // This defines the paramteters and has to match variable. So Name is a String and its defined as n
  63. {
  64. name = n;
  65. age = a;
  66. idNum = (int)i; //So I would not use a double here since the i isnt a double. Integers however can be promoted to doubles
  67. // So the Int demotes it down to a lower number. Cast will lowers the memory down to an int rather than memory for a double
  68. System.out.println(name + " " + age + " " + idNum);
  69. }
  70.  
  71. // So any parameter set up has to have a corresponding variable or possibly has to be cast.
  72.  
  73. //Constructor that takes names, age , and Idnumber; We dont need to use this however since
  74. //we defined what it is at the top of the curly brace and can reference it later
  75. // public Student(String n, int a, int i)
  76. // {
  77. // String name = n;
  78. // int age = a;
  79. // int idNum = i;
  80. // }
  81.  
  82. public void setName(String n) // void means it wont return anything so it will set the name but not return it
  83.  
  84. {
  85. name = n;
  86.  
  87. }
  88. public String getName() //would get the name and return it instead of Void saving it
  89. {
  90. return name;
  91. }
  92.  
  93. public void setAge(int a)
  94. {
  95. age = a;
  96. }
  97.  
  98. public int getAge() // so like the name this will return the age except instead of string its an Int
  99. {
  100. return age;
  101. }
  102. }
  103.  
  104.  
  105. Unit 2
  106. /*
  107. * To change this template, choose Tools | Templates
  108. * and open the template in the editor.
  109. */
  110. package unit2;
  111.  
  112. /**
  113. *
  114. * @author bassr8431
  115. */
  116. public class Unit2 {
  117.  
  118. /**
  119. * @param args the command line arguments
  120. */
  121. public static void main(String[] args) {
  122. // TODO code application logic here
  123. }
  124. }
Add Comment
Please, Sign In to add comment