Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)

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

1 CSCE 1030 Computer Science 1 Introduction to Object Oriented Programming.
Programmer-defined classes Part 2. Topics Returning objects from methods The this keyword Overloading methods Class methods Packaging classes Javadoc.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall
Web Application Development Slides Credit Umair Javed LUMS.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Written by: Dr. JJ Shepherd
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
Classes and Objects: Recap A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 3rd Edition.
Classes and Objects  A typical Java program creates many objects which interact with one another by sending messages. Through the objects interactions,
CS 106 Introduction to Computer Science I 03 / 19 / 2008 Instructor: Michael Eckmann.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
CSC 142 C 1 CSC 142 Object based programming in Java [Reading: chapter 4]
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
BPJ444: Business Programming Using Java Classes and Objects Tim McKenna
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 8. About the Midterm Exam.. Exam on March 12 Monday (Tentatively) Review on March 7 Wednesday Cover from Chapter 6 Grades will be out before spring.
Lecture # 8 Constructors Overloading. Topics We will discuss the following main topics: – Static Class Members – Overloaded Methods – Overloaded Constructors.
© A+ Computer Science - public Triangle() { setSides(0,0,0); } Constructors are similar to methods. Constructors set the properties.
Java development environment and Review of Java. Eclipse TM Intergrated Development Environment (IDE) Running Eclipse: Warning: Never check the “Use this.
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.
David Streader & Peter Andreae Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 26 - Java Object-Based Programming Outline 26.1Introduction.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 5 Classes and Methods II Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas, E.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
CS 139 Objects Based on a lecture by Dr. Farzana Rahman Assistant Professor Department of Computer Science.
Objects and Classes Start on Slide 30 for day 2 Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Much of.
Written by: Dr. JJ Shepherd
Classes and Objects - Part I. What is an Object? An Object has two primary components: state – properties of the object behavior – operations the object.
CS 106 Introduction to Computer Science I 03 / 22 / 2010 Instructor: Michael Eckmann.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Powerpoint slides from A+ Computer Science Modified by Mr. Smith for his course.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
C# Programming: From Problem Analysis to Program Design1 Creating Your Own Classes C# Programming: From Problem Analysis to Program Design 4th Edition.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
The need for Programming Languages
OOP Powerpoint slides from A+ Computer Science
Chapter 7- Inheritance.
Software Development Java Classes and Methods
suggested reading: Java Ch. 6
CS 106A, Lecture 22 More Classes
CSC 113: Computer programming II
Creating Objects in a Few Simple Steps
Building Java Programs
Today’s topics UML Diagramming review of terms
Building Java Programs
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)
Assessment – Java Basics: Part 1
Building Java Programs
Building Java Programs
Introduction to Object-Oriented Programming
Building Java Programs
Building Java Programs
Objects with ArrayLists as Attributes
Methods/Functions.
References Revisted (Ch 5)
Presentation transcript:

Writing a Class (defining a data-type)

Create a new project : Project (uncheck the “Create Main Class”)

Name the class For example: Add a new file - > Java Class

Add 4 private instance variables These instance variables are the object’s properties (attributes) For example: private String color; private double size; private String type; private boolean feather;

Add a constructor The constructor is a special method of the class that always has the same name as the class and has NO return type. The constructor is used to set the values of the object at construction time. The only way an object can be constructed is the way it is defined by the constructor.

Constructor example: public OrthHat ( String c, double s) { color = c; size = s; type = “Fedora”; //default type for all hats feather = false; //default for all hats }

To test the class, we must create a test class. Add a new file to your project – Java Main Class For example: OrthTestHat

Add code to create 2 objects of the class that you just created For example: OrthHat hat = new OrthHat (“BLUE”, 6.5); OrthHat hat2 = new OrthHat (“RED”, 4.8); SOPL (hat); SOPL (hat2);

Run your main class What does the ouput look like? An object variable points to the memory location of where the object can be found To print the values of the private instance variables (properties) we must write a method to do so.

Add a toString method to your class definition For example: public String toString ( ) { return “\nColor: “ + color + “\nSize: “ + size + “\nType: “ + type + “\nFeather: “ + feather; }

Now run your test class again The output should show the values of the instance variables (the state of the object). For example: Color: BLUE Size: 6.5 Type: Fedora Feather: false

We can do better though… Change your toString method so that it doesn’t print true or false, but rather prints a statement based on the value of the feather variable (see next slide). public String toString( ) {

Modified toString( ) public String toString ( ) { String str = “”; str += “\nColor: “ + color + “\nSize: “ + size + “\nType: “ + type; if (feather == true) str += “ with feather “; return str;

Add a method to you class definition that lets you change an instance variable For example: public void changeType (String t) { type = t; } Or public void changeFeather ( ) { feather = ! feather; }

Use this method in you test class For example: hat.changeType (“Baseball cap”); hat2.changeFeather( ); (NOTE: the values (arguments) passed into a method must match the class method definition)

Check your output Add another SOPL to your main code that displays the object property values again. SOPL (hat); SOPL (hat2); Did the output show the new information?