Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objects. Strings String x = “abc”; String is a class, not a primitive x.charAt(2) is ‘b’ use name and ‘.’ and name.

Similar presentations


Presentation on theme: "Objects. Strings String x = “abc”; String is a class, not a primitive x.charAt(2) is ‘b’ use name and ‘.’ and name."— Presentation transcript:

1 Objects

2 Strings String x = “abc”; String is a class, not a primitive http://java.sun.com/javase/6/docs/api/ x.charAt(2) is ‘b’ use name and ‘.’ and name of method

3 Practice Go to http://java.sun.com/javase/6/docs/api/. Write a program that creates a string with the value (“this is a string”), and prints out every other character (“ti sasrn”). print the length of the string check to see if they’re equal

4 More on Strings String a=“Abc”; String b = “Bcd”; if (a == b) System.out.println(“a is the same as b”); if (a.equals(b)) System.out.println(“a is the same as b”); use dot notation for everything for all classes

5 Objects A variable that has a class as a type is called an object A variable that has a primitive as a type is a primitive (double, char, int, boolean) Objects are references; primitives are not

6 Objects String s = new String(“abc”); int x = 123; s “abc” 123 x holds a pointer holds a value

7 A Different Class: Voter public class Voter { private char vote; public Voter( ) { vote = ' '; } public char getVote( ) { return vote; } public void setVote(char v) { vote = v; }

8 public class UsingMyVote { public static void main(String args[]) { DriverClass driver = new DriverClass(); driver.start(); } } class Vote { public void start( ) { Voter v1 = new Voter( ); Voter v2 = new Voter( ); Voter v3 = new Voter( ); v1.setVote(‘r’); v2.setVote(‘o’); v3.setVote(‘d’); if (v2.getVote( ) == ‘r’) System.out.println(“V2 is voting Republican”); }

9 You try it Type in the Voter class and start method. Print the value of each vote, and what they correspond to.

10 Objects are references char is primitive

11 Constructors To create an integer, type: int n=3; To create an object, type: Voter v1 = new Voter( ); Calls a constructor

12 Voter Constructor public class Voter { private char vote; public Voter( ) { vote = ' '; } public char getVote( ) { return vote; } public void setVote(char v) { vote = v; }

13 More on Constructors Can have multiple constructors Voter v1 = new Voter( ); Voter v2 = new Voter(‘r’);

14 Voter Constructor public class Voter { private char vote; public Voter( ) { vote = ' '; } public Voter(char value) { vote = value; } public char getVote( ) { return vote; } public void setVote(char v) { vote = v; }

15 public class Voter { private char vote; public Voter( ) { vote = ' '; } public Voter(char value) { vote = value; } Voter Constructor public char getVote( ) { return vote; } public void setVote(char v) { vote = v; } Instance variable Constructors } } } Accessor } Mutator

16 Definitions Instance Variables: variables at the top of a class that can be different for every object (diameter, color in Circle; vote in Voter) Object: an instantiation of a Class (v1, v2, v3 for Voter) Constructor: a special method that is called automatically when an object is new-ed

17 Circle Use the Shapes project in BlueJ to create and draw 3 circles in different places. Make a constructor that allows you to pass a color. Use that to create a blue, yellow and green circle. Then make each visible and move them so that each can be seen.

18 Exercise Create a class Building that has a String instance variable name. Make an accessor (get) method for name. Create a way to name buildings (through a mutator method or overriding the constructor). –Building is the server. –Name is a property of Building. Make a client method (a start or main method) that creates 3 buildings. Their names should be Gosnold, McMurran and DSU. Print the names of the buildings by calling the acessor method.

19 Exercise continued Assume that there is a method in Building dateBuilt that returns a String containing the year a Building was built. Call it for Gosnold. –This won’t compile because dateBuilt( ) doesn’t exist. Now, create an instance variable/property of Building that holds a date a building was built. The variable should be of type Date as defined in the Java 6 API. Write a dateBuilt( ) method that returns the date the building was built as a String. In the constructor, set the year built for all Buildings to be 12/1/60. –Now, compile and test your program.


Download ppt "Objects. Strings String x = “abc”; String is a class, not a primitive x.charAt(2) is ‘b’ use name and ‘.’ and name."

Similar presentations


Ads by Google