COSC 111 Lecture 19: Classes and Objects

54 views3 pages
Verified Note
[CLASSES AND OBJECTS]
1. Blueprint
Attributes = what the object is
Behavior (Actions) = what the object can do
2. Construction
------------------------------------------------------- The Farmer -------------------------------------------------------
(Refer to the game FARMVILLE)
//phase 1: Blueprint
public class Farmer {
//attributes (variables)
String name;
double weight;
int x,y;
}
Accessing objects
//phase 2: production
public class FarmerTest {
public static void main(String[] args) {
Farmer f1 = new Farmer();
Farmer f2 = new Farmer();
}
default values for attributes…
F1
null
f1.name
0.0
f1.weight
0
f1.x
0
f1.y
public class FarmerTest {
public static void main(String[] args) {
Farmer f1 = new Farmer();
f1.name = "Mark";
f1.weight = 60.5;
f1.x = 5;
f1.y = 9;
System.out.println(f1.x);
what happens in the memory
F1
“Mark”
f1.name
60.5
f1.weight
5
f1.x
9
f1.y
output = 5
Unlock document

This preview shows page 1 of the document.
Unlock all 3 pages and 3 million more documents.

Already have an account? Log in

Document Summary

[classes and objects: blueprint, attributes = what the object is, behavior (actions) = what the object can do, construction. ------------------------------------------------------ the farmer ------------------------------------------------------- (refer to the game farmville) //phase 2: production public class farmertest { public static void main(string[] args) { Farmer f2 = new farmer(); default (cid:448)alues for attributes . Accessing objects public class farmertest { public static void main(string[] args) { 9 f1. name f1. weight f1. x f1. y output = 5. //actions (behaviour) public void moveright() public void moveleft() public void moveup() public void movedown() public void moveto(int a, int b) { x = 6; y = b; {y--;} public class farmertest { public static void main(string[] args) { Farmer f1 = new farmer(); f1. name = mark; f1. weight = 60. 5; f1. x = 5; f1. y = 9; Farmer f2 = new farmer(); f2. name = lili; f2. weight = 50. 5; f2. x = 1; f2. y = 5; ------------------------------------------------------ the cow ------------------------------------------------------- public class cow {

Get access

Grade+20% off
$8 USD/m$10 USD/m
Billed $96 USD annually
Grade+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
40 Verified Answers
Class+
$8 USD/m
Billed $96 USD annually
Class+
Homework Help
Study Guides
Textbook Solutions
Class Notes
Textbook Notes
Booster Class
30 Verified Answers

Related Documents

Related Questions