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

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Written by: Dr. JJ Shepherd
Road Map Introduction to object oriented programming. Classes
References and Objects Weiss ch. 2. Objects Are structured regions of memory –Reside in heap Each object is an instance of a type –e.g. String, File,
Java Syntax Primitive data types Operators Control statements.
Writing methods and Java Statements. Java program import package; // comments and /* … */ and /** javadoc here */ public class Name { // instance variables.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
Objects & Object-Oriented Programming (OOP) CSC 1401: Introduction to Programming with Java Week 15 – Lecture 1 Wanda M. Kunkle.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
MIT AITI 2003 Lecture 7 Class and Object - Part I.
UML Basics & Access Modifier
Data Objects (revisited) Recall that values are stored in data objects, and that each data object holds one value of a particular type. Data objects may.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
2: Everything is an Object You Manipulate Objects Using References Primitives Arrays in Java Scoping You Never Destroy Objects Creating New Data Types:
Objects, Classes and Syntax Dr. Andrew Wallace PhD BEng(hons) EurIng
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
10-Nov-15 Java Object Oriented Programming What is it?
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Defining Classes II. Today’s topics  Static methods  Static variables  Wrapper classes  References  Class parameters  Copy constructor.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Classes. Student class We are tasked with creating a class for objects that store data about students. We first want to consider what is needed for the.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
Chapter 5 Introduction to Defining Classes
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
SEEM Java – Basic Introduction, Classes and Objects.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 3 A First Look at Classes and Objects.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
Array Size Arrays use static allocation of space. That is, when the array is created, we must specify the size of the array, e.g., int[] grades = new int[100];
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Methods.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
CSH Intro. to Java. The Big Ideas in Computer Science Beyond programming Solving tough problems Creating extensible solutions Teams of “Computational.
Chapter 5: Arrays in Java. The objectives of this chapter are:  1. To discuss the creation and use of Arrays.   2. To continue to use the String class.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CSC 205 Programming II Lecture 4 Abstract Class. The abstract keyword indicate that a class is not instantiable Defining a type which will be specialized.
Object Oriented Programming Lecture 2: BallWorld.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Introduction to Programming using Java Day 3 and 4 Java Language Basics Review The “For” loop Subroutines The “String” class.
Java: Base Types All information has a type or class designation
Sixth Lecture ArrayList Abstract Class and Interface
Java: Base Types All information has a type or class designation
Intro To Classes Review
HKCT Java OOP Unit 02 Object Oriented Programming in Java Unit 02 Methods, Classes, and Objects 1.
CS 302 Week 11 Jim Williams, PhD.
Escape Sequences What if we wanted to print the quote character?
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
Classes Variables That Are Not of a Built-in Type Are Objects
Class Everything if Java is in a class. The class has a constructor that creates the object. public class ClassName private Field data (instance variables)
Recap Week 2 and 3.
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Tonga Institute of Higher Education
An Example of Inheritance
Outline Creating Objects The String Class The Random and Math Classes
Subtype Substitution Principle
Chapter 7 Objects and Classes
Day 11 The Last Week!.
Presentation transcript:

Objects

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

Practice Go to 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

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

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

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

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; }

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”); }

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

Objects are references char is primitive

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

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

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

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; }

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

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

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.

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.

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.