Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips 367 1.

Similar presentations


Presentation on theme: "COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips 367 1."— Presentation transcript:

1 COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips 367 1

2 Announcements Lab 4 Due Friday Office Hours - Today after Class Help with loops Help with Lab 4 Jason Jerald 2

3 Participants Needed for Virtual Reality Motion 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

4 Questions? 4

5 Today in COMP 110 Worksheet Review (quick) Debugger Classes 5

6 Debugger

7 What is the length? thisisastring 0123456789101112131415 7

8 Classes Class Object Methods 8

9 Classes Class Object Methods 9

10 Classes Class Object Methods 10

11 UML Diagram 11 Class Name: Student Class Year GPA Major Credits GPA sum + getMajor + printData + increaseYear How: increase year by 1 + calcGPA How: average grades

12 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

13 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

14 Create instance of Class in main public class StudentStats { public static void main(String[] args) { // instantiating object of type student Student s1 = new Student(); } Class ObjectClass Different Name From Class 14

15 Create Class in Separate File public class Student { //everything in your class goes here } Save as Student.java 15

16 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

17 Data (in your class file) 17 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

18 Public Variables Student s1 = new Student(); s1.classYear = 2; classYear is a variable NOT a method Don’t use () 18

19 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

20 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

21 Method getMajor /*getMajor Input: none Return: major*/ public String getMajor() { return major; } 21 Return type Type String

22 Calling method getMajor 22 public static void main(String[] args) { Student s1 = new Student(); s1.major = “English”; String m = s1.getMajor(); System.out.print(m); }

23 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

24 Method increaseYear /*increaseYear Input: none Description: increase student year by one Return: none*/ public void increaseYear() { if (classYear < 4) classYear++; } 24

25 Calling method increaseYear 25 public static void main(String[] args) { Student s1 = new Student(); s1.classYear = 2; s1.increaseYear(); // classYear = 3 s1.increaseYear(); // classYear = 4 }

26 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

27 Method calcGpa /*calcGpa Input: grade(double) Description: update credits and gpaSum calculate GPA Return: none*/ public void calcGpa(double grade) { credits++; //increase number of credits gpaSum = gpaSum + grade; gpa = gpaSum / credits; } 27

28 Calling method calcGpa 28 public static void main(String[] args) { Student s1 = new Student(); s1.calcGpa(3.5); //gpa = 3.5 s1.calcGpa(2.4); // gpa = 2.95 }

29 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

30 Method printData /*printData Input: noe Description: print instance variables Return: none*/ public void printData() { DecimalFormat df = new DecimalFormat("0.00"); System.out.print("Major: " + major + ”\nClass year: " + classYear + ”\nGPA: " + df.format(gpa)); } 30

31 Code on Website Open the code Save the code Understand the code Run the code 31

32 Friday Lab 5 Classes 32


Download ppt "COMP 110 Classes Tabitha Peck M.S. February 20, 2008 MWF 3-3:50 pm Philips 367 1."

Similar presentations


Ads by Google