COMP 110 More about classes
description
Transcript of COMP 110 More about classes
![Page 1: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/1.jpg)
Catie WelshFebruary 23, 2011
![Page 2: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/2.jpg)
Lab 4 due on Friday
Lab 5 will be assigned on Friday
2
![Page 3: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/3.jpg)
3
![Page 4: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/4.jpg)
Class: a definition of a kind of object Object: an instance of a class
◦ Contains instance variables (data) and methods Methods
◦ Methods that return a value◦ Methods that return nothing
4
![Page 5: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/5.jpg)
Local variables and instance variables
Brief introduction to methods with parameters
In-class exercise
5
![Page 6: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/6.jpg)
Instance variables◦ Declared in a class◦ Confined to the class
Can be used anywhere in the class that declares the variable, including inside the class’ methods
Local variables◦ Declared in a method◦ Confined to the method
Can only be used inside the method that declares the variable
6
![Page 7: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/7.jpg)
public class Student{ public String name; public int classYear; // ...
public void printInfo() { String info = name + “: ” +
classYear; System.out.println(info); }
public void increaseYear() { classYear++; }
public void decreaseYear() { classYear--; }}
7
• classYear and name are instance variables• can be used in any method in this class
• info is a local variable declared inside method printInfo()• can only be used inside method printInfo()
![Page 8: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/8.jpg)
public class Student{ public String name; public int classYear; // ...
public void printInfo() { String info = name + “: ” +
classYear; System.out.println(info); }
public void increaseYear() { classYear++; info = “My info string”; //
ERROR!!! }
public void decreaseYear() { classYear--; }}
8
The compiler will not recognize the variable info inside of method increaseYear()
![Page 9: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/9.jpg)
public static void main(String[] args){ Student jack = new Student(); jack.name = “Jack Smith”; jack.major = “Computer Science”;
String info = “Hello there!”; System.out.println(info);
System.out.println(jack.name + “ is majoring in ” + jack.major);
Student apu = new Student(); apu.name = “Apu Nahasapeemapetilon”; apu.major = “Biology”;
System.out.println(apu.name + “ is majoring in ” + apu.major);}
9
Variable info in main method not affected by variable info in printInfo method in class Student
![Page 10: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/10.jpg)
Compute the square of this number◦ 5◦ 10◦ 7
I could give you any number, and you could tell me the square of it
We can do the same thing with methods
10
![Page 11: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/11.jpg)
Parameters are used to hold the value that you pass to the method
Parameters can be used as (local) variables inside the method
public int square(int number){ return number * number;}
11
Parameters go inside parentheses of method header
![Page 12: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/12.jpg)
public class Student{ public String name; public int classYear; // ...
public void setName(String studentName) { name = studentName; }
public void setClassYear(int year) { classYear = year; }}
12
![Page 13: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/13.jpg)
public static void main(String[] args){ Student jack = new Student(); jack.setName(“Jack Smith”); jack.setClassYear(3);}
13
Arguments
![Page 14: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/14.jpg)
Parameters are used to hold the value that you pass to the method
Parameters can be used as (local) variables inside the method
public int square(int number){ return number * number;}
14
Parameters go inside parentheses of method header
![Page 15: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/15.jpg)
Multiple parameters separated by commas
public double getTotal(double price, double tax){ return price + price * tax;}
15
![Page 16: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/16.jpg)
Order, type, and number of arguments must match parameters specified in method heading
Add these two numbers
16
+ = ???
![Page 17: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/17.jpg)
public class SalesComputer{ public double getTotal(double price, double tax) { return price + price * tax; }// ...
SalesComputer sc = new SalesComputer();double total = sc.getTotal(“19.99”, Color.RED);double total = sc.getTotal(19.99);double total = sc.getTotal(19.99, 0.065);int price = 50;total = sc.getTotal(price, 0.065);
17
Automatic typecasting
![Page 18: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/18.jpg)
18
![Page 19: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/19.jpg)
Recitation
Lab 5 will be assigned
19
![Page 20: COMP 110 More about classes](https://reader036.fdocuments.net/reader036/viewer/2022062321/56813d50550346895da70c52/html5/thumbnails/20.jpg)
Even more about classes
Information Hiding and Encapsulation
20