Review Creating Objects Pepper many references from rial/java/objects/object.html

Slides:



Advertisements
Similar presentations
JAVA Revision Lecture Electronic Voting System Marina De Vos.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
CSCI 160 Midterm Review Rasanjalee DM.
Using Classes to Store Data Computer Science 2 Gerb.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Scanner Pepper With credits to Dr. Siegfried. The Scanner Class Most programs will need some form of input. At the beginning, all of our input will come.
Random (1) Random class contains a method to generate random numbers of integer and double type Note: before using Random class, you should add following.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
1 More on Classes Overview l Overloading l The this keyword l The toString method l The equals method.
Understanding class definitions Looking inside classes.
OOPDA Review. Terminology class-A template defining state and behavior class-A template defining state and behavior object-An instance of a class object-An.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Introduction to Java Java SE 8 for Programmers Paul Deitel Harvey Deitel Deitel Developer Series 2014.
COMP More About Classes Yi Hong May 22, 2015.
Writing Classes (Chapter 4)
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
CSC172 Intro Pepper. Goals Reminder of what a class is How to create and use classes How to design classes How to think in terms of objects How to comment.
Inheritance, Abstract & Interface Pepper With help from and
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
F27SA1 Software Development 1 3. Java Programming 2 Greg Michaelson.
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
Questions? Suggestions?. References References Revisited What happens when we say: int x; double y; char c; ???
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Copyright by Scott GrissomCh 2 Class Definition Slide 1 Class Structure public class BankAccount{ fields constructor(s) methods } Sample Code Ticket Machine.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Classes and Objects - Part I. What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
Object Oriented Programming and Data Abstraction Rowan University Earl Huff.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
CS0007: Introduction to Computer Programming The for Loop, Accumulator Variables, Seninel Values, and The Random Class.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Classes and Objects Introduced
Yanal Alahmad Java Workshop Yanal Alahmad
Yanal Alahmad Java Workshop Yanal Alahmad
Agenda Warmup AP Exam Review: Litvin A2
Methods Attributes Method Modifiers ‘static’
public class BankAccount{
CLASS DEFINITION (> 1 CONSTRUCTOR)
CS 106A, Lecture 22 More Classes
Classes and Objects, State and Behavior
Building Java Programs
Topic 28 classes and objects, part 2
Topic 29 classes and objects, part 3
Building Java Programs
Classes and objects Readings: 8.1.
Recap Week 2 and 3.
Building Java Programs
Handout-4b More on Classes
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Special instance methods: toString
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
CS 1054: Lecture 2, Chapter 1 Objects and Classes.
Topic 29 classes and objects, part 3
Building Java Programs
Topic 29 classes and objects, part 3
Presentation transcript:

Review Creating Objects Pepper many references from rial/java/objects/object.html rial/java/objects/object.html

Objects you use Scanner Random ABC System.in nextInt nextDouble next

Class blueprint  Instance Scanner x = new Scanner(System.in) int y = x.nextInt; nextInt nextDouble next Scanner Class System.in 1223 nextInt nextDouble next x Input &Type

People class say moveArm blink People Class mary hair color eye color arm length say moveArm blink brown green 11 carrie say moveArm blink blond blue 5

Creating a blueprint Add instance variables –  inside class and outside methods public class Student { private String hairColor; private String eyeColor; private int armLength; /** }

Creating a blueprint remove static from methods public void printPerson () { System.out.println ("The eye color is "+ eyeColor + " and the hair color is " + hairColor); }

Use the blueprint Student mary = new Student(); Student carrie = new Student(); mary.setEyeColor("green"); mary.setHairColor("brown"); carrie.setEyeColor("blue"); carrie.setHairColor("blonde");

Look at the class again - this public void setEyeColor(String colorin) { this.eyeColor = colorin; } mary say moveArm blink brown green 11 carrie say moveArm blink blond blue 5

Exercise – make a point class setxy – take in 2 int to set x and y getx – return x gety – return y getDistance – take in 1 point and return distance to it Point gety getx setxy xyxy getDistance

Make a point public class Point { int x; int y; public void setxy (int xIn, int yIn) { this.x = xIn; this.y = yIn;} public int getx() { return this.x;} public int gety() { return this.y;} public double getDistance(Point another) { return Math.sqrt((this.x-another.getx()) * (this.x-another.getx()) + (this.y-another.gety()) * (this.y-another.gety())); }}

PointUse public class PointUse { public static void main() { Point one = new Point(); Point two = new Point(); one.setxy(1,2); two.setxy(4,6); System.out.println(one.getDistance(two)); }}

Player class setName moveMan getLoc Player Class location name score setLoc reportWin setName moveMan getLoc mary 30 mary 100 setLoc reportWin setName moveMan getLoc carrie 40 carrie 30 setLoc reportWin

Teach that player how to do things setName – take a string to change name setLoc – take an int to change location getLoc – return its loc reportWin – print win if > 30 moveMan – take in int - amount to move; int - current loc ; return new loc setName moveMan getLoc Player Class location name score setLoc reportWin

Player class public class Player { int location; String name; int score; public void setName(String namein) { this.name = namein;} public void setLoc(int locin) { this.location = locin;} public int setLoc() { return this.location; } public boolean reportWin() {if (this.location > 30) { System.out.println(name + " won."); return true;} else {return false;}} public int moveMan(int move) { this.location = this.location+move; return this.location;} }

Game class public class Game1 { public static void main() { Player player1 = new Player(); Player player2 = new Player(); player1.setLoc(0); player2.setLoc(0); player1.setName("mary"); player2.setName("carrie"); player1.moveMan(15); player2.moveMan(13); player1.moveMan(12); player2.moveMan(20); player1.reportWin(); player2.reportWin(); }}

Summary So Far Classes as Blueprints vs Instances –Variable of a class type – hold instance –Create instances with new Class () Behavior – methods –placement: same as other methods, not static –access: instance.method State – instance variables –placement: in class; outside method; –access: this. –scope: throughout class

Constructors Creates a new instance (a new object) Can take in parameters so the new object has information set inside it right away Special method –No return –Same name as the class Call it with "new" keyword

Point class constructor public class Point { int x; int y; //============ Constructor public Point(int x, int y) { this.x = x; this.y = y; }

Use the Point Constructor Point myPoint = new Point(1,3); Point yourPoint = new Point(4,5); myPoint gety getx setxy 1313 getDistance yourPoint gety getx setxy 4545 getDistance

Constructors vs Methods Differences between methods and constructors. –There is no return type given in a constructor signature (header). The value is this object itself so there is no need to indicate a return value. –There is no return statement in the body of the constructor.

What is wrong with this code public class usePoint { public static void main(){ Point myPoint1; Point myPoint2; System.out.println(myPoint1.getX()); System.out.println(myPoint1.getDistance(my Point2)); }}

Summary So Far2 Classes as Blueprints vs Instances –Variable of a class type – hold instance –Create instances with new Class () – default –Create instances with new Class (cons parms) Behavior – methods –placement: same as other methods, not static –access: instance.method –Special method: constructor State – instance variables –placement: in class; outside method; –access: this. –scope: throughout class