Class Notes (836,591)
Canada (509,862)
CSC207H1 (40)
Lecture

sept24c.docx

3 Pages
62 Views
Unlock Document

Department
Computer Science
Course
CSC207H1
Professor
Diane Horton
Semester
Fall

Description
SEPTEMBER 24  Wrapper classes – the class version of each of the primitive types. Used to represent primitive values when an Object is neede o Autoboxing - Java can automatically "box" a primitive into an instance of its wrapper class  Unbox a wrapper object to get the primitive value  Ex. Integers automatically box/unboxing o Autoboxing  ArrayList auto wraps int into Integer  Also auto unbox the ArrayList 1. package javabasics; 2. 3. public class Autobox { 4. 5. public static void main(String[] args) { 6. Integer i2 = new Integer(5); // these are wrapper classess 7. Boolean b2 = new Boolean(false); 8. System.out.println(i2); 9. System.out.println(b2); 10. 11. Integer x = 6; // automatically does Integer i = new Integer(6) 12. int y = x + 4; // auotmatically does int y = x.intValue() + 4; 13. System.out.println(y); 14. } 15. } o Output: >>> 5 >>> false >>> 10  Using abstraction w/ getters and setters, o toString method, similar to python’s _ _str_ _  Subclass  use extends o Numbered.java 1. public interface Numbered { 1. int getNumber(); // This will be public even though I didn't say so. 2. } o Student.java 1. import java.util.ArrayList; 2. import java.util.Arrays; 3. 4. public class Student implements Comparable, Numbered { // Comparable, Numbered interfaces 5. 6. private int studentNumber; // This Student's student number. 7. private String name; // This Student's name. 8. private ArrayList grades; 9. 10. /** A new Student named name with student number num and no grades yet. 11. * @param name the student's name 12. * @param num the student's student number 13. */ 14. public Student(String name, int num) { 15. this.name = name; // use "this" to distinguish btwn parameter & instance variables 16. this.studentNumber = num; 17. this.grades = new ArrayList(); // Must construct the (initially empty) ArrayList 18. } 19. 20. /** Record the mark when this student has completed a course. 21. * @param mark The mark to record. 22. */ 23. public void completeCourse(int mark){ // we don’t care about the return value 24. this.grades.add(mark); // note “autoboxing” 25. } 26. 27. /** Return the student number of this Student. 28. * @return the studentNumber 29. */ 30. public int getStudentNumber() { 31. return studentNumber; // 'this' is optional here. Be consistent. 32. } 33. SEPTEMBER 24 34. @Override 35. public int getNumber() { 36. return studentNumber; 37. } 38. 39. /** Return the name of this Stu
More Less

Related notes for CSC207H1

Log In


OR

Join OneClass

Access over 10 million pages of study
documents for 1.3 million courses.

Sign up

Join to view


OR

By registering, I agree to the Terms and Privacy Policies
Already have an account?
Just a few more details

So we can recommend you notes for your school.

Reset Password

Please enter below the email address you registered with and we will send you a link to reset your password.

Add your courses

Get notes from the top students in your class.


Submit