320 likes | 414 Views
COMP 110 Classes. Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips 367. 1. Announcements. Lab 4 Due Friday Office Hours - Today after Class Help with loops Help with Lab 4 Jason Jerald. 2.
E N D
COMP 110Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips 367 1
Announcements • Lab 4 Due Friday • Office Hours - Today after Class • Help with loops • Help with Lab 4 • Jason Jerald 2
Participants Needed for Virtual RealityMotion Perception in Virtual Environments During Head Turns • What you get • Learn about programming in a research environment • Learn what grad students are doing in computer science • Help contribute to improving virtual reality technology • Earn real money • The task • select which of two presentations you believe contains scene motion • 600-1000 trials • Earn $.05 for each correct selection - Do the math! • Study takes 3-5 Hours • For more info • Filling out form is not committing • Name • Preferred contact (email or phone) • Preferred hours 3
Today in COMP 110 • Worksheet Review (quick) • Debugger • Classes 5
Classes Class Object Methods 8
Classes Class Object Methods 9
Classes Class Object Methods 10
UML Diagram Class Name: Student Class Year GPA Major Credits GPA sum + getMajor + printData + increaseYear How: increase year by 1 + calcGPA How: average grades 11
UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 12
Class File • Each class in SEPARATE file • Create instance of Class in main • You will have TWO (or more) files!! • Call methods in main • You must run your file from main!!!!!!! 13
Create instance of Class in main publicclass StudentStats { publicstaticvoid main(String[] args) { // instantiating object of type student Student s1 = new Student(); } } Different Name From Class Object Class Class 14
Create Class in Separate File publicclass Student{ //everything in your class goes here } • Save as Student.java 15
UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 16
Data (in your class file) public int classYear; private double gpa; public String major; private int credits = 0; private double gpaSum; • private - only access variables INSIDE your class • public - access to variables OUTSIDE your class 17
Public Variables Student s1 = new Student(); s1.classYear = 2; • classYear is a variable NOT a method • Don’t use () 18
Private Variables • Variables you do not want a user to manipulate • GPA - • calculate GPA in your class • Don’t want user to reset the value 19
UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 20
Method getMajor /*getMajor Input: none Return: major*/ public String getMajor() {return major; } Return type Type String 21
Calling method getMajor publicstaticvoid main(String[] args) { Student s1 = new Student(); s1.major = “English”; String m = s1.getMajor(); System.out.print(m); } 22
UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 23
Method increaseYear /*increaseYear Input: none Description: increase student year by one Return: none*/ publicvoid increaseYear() {if (classYear < 4) classYear++; } 24
Calling method increaseYear publicstaticvoid main(String[] args) { Student s1 = new Student(); s1.classYear = 2; s1.increaseYear(); // classYear = 3 s1.increaseYear(); // classYear = 4 s1.increaseYear(); // classYear = 4 } 25
UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade): void + printData(): void 26
Method calcGpa /*calcGpa Input: grade(double) Description: update credits and gpaSum calculate GPA Return: none*/ publicvoid calcGpa(double grade){ credits++;//increase number of credits gpaSum = gpaSum + grade; gpa = gpaSum / credits;} 27
Calling method calcGpa publicstaticvoid main(String[] args) { Student s1 = new Student(); s1.calcGpa(3.5); //gpa = 3.5 s1.calcGpa(2.4); // gpa = 2.95 } 28
UML Diagram Class Name: Student Class Year (int) GPA (double) Major (String) Credits (int) GPA sum (double) + getMajor(): String + increaseYear(): void + calcGPA(double grade1, double grade2): void + printData(): void 29
Method printData /*printData Input: noe Description: print instance variables Return: none*/ publicvoid printData() {DecimalFormat df = new DecimalFormat("0.00");System.out.print("Major: " + major +”\nClass year: " + classYear +”\nGPA: " + df.format(gpa)); } 30
Code on Website • Open the code • Save the code • Understand the code • Run the code 31
Friday • Lab 5 • Classes 32