Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Notes Unit 2
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package unit2;
- /**
- *Name:
- *Date:
- * @author bassr8431
- */
- public class NotesUnit2 {
- public static void main(String[] args)
- {
- 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
- 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
- Student student2 = new Student("Joe", 16, 345345345);
- //??? Why would he throw the numbers on the end when we are just creating a new student?
- student1.setName("Bill"); // So this changes the name of student one instead of naming the student upon its creation
- System.out.println("");
- System.out.println(student1.getName()); // So this is setter and getter for name. You can also have them for Id and GPA
- System.out.println("using modifier");
- }
- }
- Student.Java
- *
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package unit2;
- /**
- *Name:
- *Date:
- * @author bassr8431
- */
- public class Student {
- private String name;
- private int age;
- private int idNum;
- //these up top that are accesible through all classes when starting brace
- //When using this however in Java they do have to be assigned later on in the code
- //Private means that they cannot be changed
- //default constructor
- public Student()
- {
- name = "default";
- age = 13;
- idNum = 0000;
- //double gpa = 0.0;
- // so this GPA double only works within this class. Why constructors are important
- //This would be used if the data does not need be saved like the others but just calculated once
- System.out.println(name + " " + age + " " + idNum);
- // you do not have to define a default but you just need one constructor. The other is just an example of my possiblity
- }
- //constructor that takes name, age , and ID number
- 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
- {
- name = n;
- age = a;
- idNum = (int)i; //So I would not use a double here since the i isnt a double. Integers however can be promoted to doubles
- // 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
- System.out.println(name + " " + age + " " + idNum);
- }
- // So any parameter set up has to have a corresponding variable or possibly has to be cast.
- //Constructor that takes names, age , and Idnumber; We dont need to use this however since
- //we defined what it is at the top of the curly brace and can reference it later
- // public Student(String n, int a, int i)
- // {
- // String name = n;
- // int age = a;
- // int idNum = i;
- // }
- public void setName(String n) // void means it wont return anything so it will set the name but not return it
- {
- name = n;
- }
- public String getName() //would get the name and return it instead of Void saving it
- {
- return name;
- }
- public void setAge(int a)
- {
- age = a;
- }
- public int getAge() // so like the name this will return the age except instead of string its an Int
- {
- return age;
- }
- }
- Unit 2
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package unit2;
- /**
- *
- * @author bassr8431
- */
- public class Unit2 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- }
- }
Add Comment
Please, Sign In to add comment