Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.

Slides:



Advertisements
Similar presentations
The Line Class Suppose you are involved in the development of a large mathematical application, and this application needs an object to represent a Line.
Advertisements

 2005 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
1 Classes Overview l Classes as Types l Declaring Instance Variables l Implementing Methods l Constructors l Accessor and Mutator Methods.
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)
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology- George Koutsogiannakis.
1 Creating Classes. 2 Writing Classes Thus far, we have mainly used existing classes in the Java library  (also main classes for executing) True object-oriented.
UML Basics & Access Modifier
More Object Concepts Chapter 4.  Our class is made up of several students and each student has a name and test grades  How do we assign the variables.
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.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
4.1 Instance Variables, Constructors, and Methods.
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!
ACO 101: Introduction to Computer Science Anatomy Part 2: Methods.
Introduction to Java Classes and Objects. What is a class A class is description of a structure that contains both data and methods – Describes a set.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
CSC 142 Computer Science II Zhen Jiang West Chester University
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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.
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.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
JAVA PROGRAMMING PART III. METHOD STATEMENT Form of method statement [ ] [static] ( [ ]) { } Example public static void main(String args[])
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Week 11 - Friday.  What did we talk about last time?  Object methods  Accessors  Mutators  Constructors  Defining classes.
Chapter 8: Designing Classes Accessors, Mutators, and Immutable Classes(8.3) Side Effects(8.4) Parameter Passing (Advanced Topic 8.1) Preconditions and.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 5 GEORGE KOUTSOGIANNAKIS Copyright: FALL 2015 Illinois Institute of Technology- George Koutsogiannakis 1.
 2005 Pearson Education, Inc. All rights reserved. 1 Introduction to Classes and Objects.
Written by: Dr. JJ Shepherd
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Classes - Intermediate
Object Oriented Programming I ( ) Dr. Adel hamdan Part 03 (Week 4) Dr. Adel Hamdan Date Created: 7/10/2011.
Class Everything in Java is in a class. The class has a constructor that creates the object. If you do not supply a constructor Java will create a default.
Exam 2 EXAM 2 Thursday!!! 25% of Final Grade Know: loops, switch/case Files Input failure (e.g. scan.hasNextInt())
Class Definitions and Writing Methods Chapter 3 10/12/15 & 10/13/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Class Definitions: The Fundamentals Chapter 6 3/30/15 & 4/2/15 Imagine! Java: Programming Concepts in Context by Frank M. Carrano, (c) Pearson Education.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Staples are our staple Building upon our solution.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Introduction to java (class and object). Programming languages: –Easier to understand than CPU instructions –Needs to be translated for the CPU to understand.
Andrew(amwallis) Classes!
3 Introduction to Classes and Objects.
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
CS116 OBJECT ORIENTED PROGRAMMING II LECTURE 1 Part II
CS 302 Week 11 Jim Williams, PhD.
Initializing Arrays char [] cArray3 = {'a', 'b', 'c'};
CS1316: Representing Structure and Behavior
An Introduction to Java – Part II
Introduction to Classes and Methods
Classes & Objects: Examples
CS1316: Representing Structure and Behavior
More on Classes and Objects
Implementing Classes Chapter 3.
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)
© A+ Computer Science - Classes And Objects © A+ Computer Science -
Building Java Programs
JAVA CLASSES.
Dr. R Z Khan Handout-3 Classes
CSG2H3 Object Oriented Programming
Day 11 The Last Week!.
Presentation transcript:

Java - Classes JPatterson

What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you just didn’t know

What is a class? public class _Alpha { public static void main(String [] args) { } When a class file has a main you can execute the file

What is a class? public class _AlphaClass { } We are going to make class files without the main method.

What is a class? public class Student { } You still need to save the file as the class name with the.java extension – in this case it would be Student.java

Attributes / Fields public class Student { } What do we need to know about a student? These will be the ATTRIBUTES of the class.

Attributes / Fields public class Student { private String name; private int idNumber; private double gpa; } All attributes are declared to be private. Attributes are the global variables of the class.

Attributes / Fields public class Student { private String name; private int idNumber; private double gpa; } NOTICE – you only DECLARE the attributes you do NOT initialize them here…we will do that later.

Attributes / Fields public class Student { private String name; private int idNumber; private double gpa; } So far we have DECLARED the global or instance variables of the class

Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) { } To initialize the variables we have to write a method called a CONSTRUCTOR

Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) { } CONSTRUCTORS are public and have the same name as the class – ALWAYS, NO EXCEPTIONS

Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } This constructor is called the DEFAULT constructor. We want to initialize the attributes to generic starting values.

Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } Classes can have more than 1 constructor. Next we are going to make a SECONDARY constructor

Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } In this case the secondary constructor will allow your client to pass over values to the class and then store them in the attributes.

Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } FLEXIBLITY – having more than 1 constructor makes your class more flexible. There are now 2 ways to initialize your attributes.

Constructors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } INITIALIZE ALL ATTRIBUTES IN EVERY CONSTRUCTOR

CLASS public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } So – let’s look at a client program that might use this class.

Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); }

Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); } s1 name“ idNumber0 Gpa0 s1 is a Student OBJECT that was declared with the default constructor so all the attributes are set to the default values.

Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0); } s1 name“ idNumber0 Gpa0 name“ Sookie “ idNumber10025 Gpa4.0 s2

Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0); } s1 name“ idNumber0 Gpa0 name“ Sookie “ idNumber10025 Gpa4.0 s2 “Sookie” is passed over to n

Declaring and Instantiating Classes public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public class Example { public static void main (String [] args) { Scanner scan = new Scanner (System.in); Student s1 = new Student (); Student s2 = new Student(“Sookie”, 10025, 4.0); } s1 name“ idNumber0 Gpa0 name“ Sookie “ idNumber10025 Gpa4.0 s2 The attribute name is assigned n

Accessors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } Now, let’s look at accessors. Accessors are used to give information about the class. What is your name? What is your idNumber? What is your gpa? If I ask you, “what is your name?” what data type are you going to tell me?

Accessors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } Now, let’s look at accessors. Accessors are used to give information about the class. What is your name? What is your idNumber? What is your gpa? If I ask you, “what is your name?” what data type are you going to tell me? STRING – that is the return type of the accessor. The return type is the data type of the value returned or given by the method

Accessors public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public String getName() { return name; } Now, let’s look at accessors. Accessors are used to give information about the class. What is your name? What is your idNumber? What is your gpa? If I ask you, “what is your name?” what data type are you going to tell me? STRING – that is the return type of the accessor. The return type is the data type of the value returned or given by the method

Mutators public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public void setName(String n) { name = n; } Now, let’s look at mutators. Mutators are used to change information about the class. What if on the first day of class I check my roll sheet and you are listed as Orvil but you go by your middle name? I would want to change your name to Sam. I could call a mutator method that would set your name to a new and improved value.

Mutators public class Student { private String name; private int idNumber; private double gpa; public Student( ) {name = “ “; idNumber = 0; gpa = 0; } public Student( String n, int id, double g ) {name = n; idNumber = id; gpa = g; } public void setName(String n) { name = n; } Now, let’s look at mutators. Mutators are used to change information about the class. What if on the first day of class I check my roll sheet and you are listed as Orvil but you go by your middle name? I would want to change your name to Sam. I could call a mutator method that would set your name to a new and improved value.