Download presentation
Presentation is loading. Please wait.
Published byGeoffrey Long Modified over 9 years ago
1
Question of the Day What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side. A A B B 4 4 7 7
2
Question of the Day What card(s) must you flip to verify the following statement: Cards with a vowel on one side, have an even number on the other side. A A B B 7 7 4 4
4
Announcements If you need more review of Java… I have lots of good resources – talk to me Use “Additional Help” link on webpage
5
Classes vs. Objects Classes are blueprints describing data type On their own, classes (usually) cannot do anything Objects are instances of a class New objects created (instantiated) using new Fields describe state of an object Object’s behavior represented by methods
6
Instance Variables All of class's instances have same fields… … but values can differ between each instance In a class, each field must have unique name Different classes can duplicate names of fields Field declaration must include data type Will act like variables of that type Can be primitive, enum, or reference type
7
Class Example public class Car { /** What kind of car & who made it */ private String makeAndModel; /** Color of the car. */ private String color; /** Percent full the gas tank is */ private float tankLevel; /** Miles recorded on the odometer */ private int odometerReading; /* Definition continues from here */
8
Using Fields (1) Car profHertzCar = new Car(); profHertzCar.makeAndModel = “BMW Z4”; profHertzCar.color = “Deep Green Metallic”; profHertzCar.tankLevel = 1.0; profHertzCar.odometerReading = 10000; Car actualCar = new Car(); actualCar.makeAndModel = “Subaru Outback"; actualCar.color = “Brown”; actualCar.tankLevel = 0.0001; actualCar.odometerReading = 67634;
9
Using Fields (2) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; dreamCar.tankLevel = 1.0; dreamCar.odometerReading = 10000;
10
Using Fields (2) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; dreamCar.tankLevel = 1.0; dreamCar.odometerReading = 10000; Car realCar = dreamCar; realCar.makeAndModel = “Subaru Outback”; realCar.color = “Silver”; realCar.tankLevel = 0.0001; realCar.odometerReading = 67634;
11
Using Fields (2) Car dreamCar = new Car(); dreamCar.makeAndModel = “BMW Z4”; dreamCar.color = “Deep Green Metallic”; dreamCar.tankLevel = 1.0; dreamCar.odometerReading = 10000; Car realCar = dreamCar; realCar.makeAndModel = “Subaru Outback”; realCar.color = “Silver”; realCar.tankLevel = 0.0001; realCar.odometerReading = 67634;
12
static Fields Some data belongs to class, not instance Float.MAX_VALUE Citizen.nationalPopulation Instructor.bestProfessor static fields used for any class-based data All of the class’s instances see same value Do not need an instance to access this value Can be updated via any instance or via class
13
static Code Use Example public class Player { public static int num; public String name; public static void main(String[] args) { Player player1, player2, player3; player1 = new Player(); player1.name = "Homer"; player1.num += 1; player2 = new Player(); player2.name = “JJ"; Player.num += 1; player3 = player2; player3.name = "Hertz"; System.out.print(player2.name+" "+player1.num); } }
14
Tracing With Objects & Fields public static void main(String[] args) { Player player1, player2, player3; player1 = new Player(); player1.name = "Homer"; player1.num += 1; player2 = new Player(); player2.name = “JJ"; Player.num += 1; player3 = player2; player3.name = "Hertz"; System.out.print(player2.name+" "+player1.num); }
15
static v. non- static Review static fieldsnon- static fields Belong to class as a whole No instance needed to use Can set value at any time No limit on use in code Primitive or reference okay static Declare in class using: static type name; Values are instance-based Must specify instance to use Can set value at any time No limit on use in code Primitive or reference okay Declare in class using: type name;
16
Your Turn Get into your groups and complete activity
17
For Next Lecture Keep reviewing your Java lessons Will be discussing methods & constructors Friday Really need to be back up-to-speed at that point There is weekly assignment problem on Angel Due next Tuesday at 5PM but could start earlier With this finish removing rust from lazy summer
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.