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.

Slides:



Advertisements
Similar presentations
Lecture 17 Abstract classes Interfaces The Comparable interface Event listeners All in chapter 10: please read it.
Advertisements

Road Map Introduction to object oriented programming. Classes
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
Introduction to Java Programming, 4E Y. Daniel Liang.
Advanced Java and Android Day 1 Object-Oriented Programming in Java Advanced Java and Android -- Day 11.
1 Chapter 8 Objects and Classes. 2 Motivations After learning the preceding chapters, you are capable of solving many programming problems using selections,
Java Programming, Second Edition Chapter Four Advanced Object Concepts.
Writing Classes (Chapter 4)
1 Lecture 2: Object Oriented Programming in Java.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Java Classes Appendix C © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
1 Objects and Classes. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object represents an entity.
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!
1 Java Programming Using Methods, Classes, and Objects.
Methods in Java. Program Modules in Java  Java programs are written by combining new methods and classes with predefined methods in the Java Application.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
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.
More About Objects and Methods Chapter 5. Outline Programming with Methods Static Methods and Static Variables Designing Methods Overloading Constructors.
1.  At the end of this slide, student able to:  Object-Oriented Programming  Research on OOP features.  Do a code walkthrough to examine the implementation.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Chapter 8 Objects and Classes Object Oriented programming Instructor: Dr. Essam H. Houssein.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part B Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Objects and Classes Mostafa Abdallah
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 9 Inheritance and.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
The Math class Java provides certain math functions for us. The Math class contains methods and constants that can be very useful. The Math class is like.
More Object Concepts— Farrell, Chapter 4 Dr. Burns.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 7 Objects and Classes.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Objects and Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 6 Objects and Classes.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 10 More on Objects and Classes.
1 Chapter 6 Programming with Objects and Classes F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive.
Introduction To Objects Oriented Programming Instructor: Mohammed Faisal.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Chapter 4: More Object Concepts. Objectives Understand blocks and scope Overload a method Avoid ambiguity Create and call constructors with parameters.
Topics Instance variables, set and get methods Encapsulation
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Lecture 3: Introduction to Object and Classes Michael Hsu CSULA.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Chapter VII: Arrays.
Lecture 3: Introduction to Object and Classes
Static data members Constructors and Destructors
Yanal Alahmad Java Workshop Yanal Alahmad
Intro To Classes Review
Chapter 3: Using Methods, Classes, and Objects
CMSC 202 Static Methods.
Chapter 3 Introduction to Classes, Objects Methods and Strings
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Object Oriented Programming in java
Applying OO Concepts Using Java
Classes and Objects Static Methods
Using java libraries CGS3416 spring 2019.
OO Programming Concepts
Classes 5/5 May 14, 2019 ICS102: Classes 5/5.
Chapter 7 Objects and Classes
Presentation transcript:

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 for each student? Student 1 Name:Anne ID#: Test1:92 Test 2:85 Student 2 Name:Barry ID#: Test1:88 Test 2:81 Student 3 Name:Carl ID#: Test1:90 Test 2:95

 The differences between one student and another is simply the variable assignments  Instead of creating multiple variables for each student, we could create one class with multiple objects  This produces a concept of an object within an object

 What does it mean to be a student? class Student { String name; String idnum; int test1; int test2; }

 How do we fill in the information?  Let’s create a new student within the class student1 = new Student( ); student2 = new Student( );  Now we can fill in the desired variable assignment for each unique student

class Student { String name; String idnum; int test1; int test2; } class StudentInfo { public static void main(String[ ] args) { Student student1; student1 = new Student( ); student1.name = "Anne"; student1.idnum = "900001"; student1.test1 = 92; student1.test2 = 85; System.out.println(student1.name); System.out.println(student1.test1); } Create and compile the Student class Create and compile the StudentInfo class

 Declare a variable within the object Student student1;  This indicates that if and or when the variable student1 is assigned, that assignment will be an instance of the Student class  Reserves the variable name student1  The actual creation of the object occurs in the statement: student1 = new Student( );

 Our Student class works well, but has one serious flaw  Once the data is assigned, anyone can view the data for any student  Student1 should not have access to student2 or student3  We can create an accessor method to hide the data not assigned to that particular user

class Student { private String name; public void setName(String n) { name = n; } public String getName( ) { return name; } Note: private variable can only be referenced within the class

 The accessor method for the Student class now limits access to each instance (in this case for each student along with their individual information)  In the main method we will replace the assignment student1.name = "Anne"; with the reference student1.setName( "Anne" ); and student1.getName( );

class Student { private String name; private String idnum; private int test1; private int test2; public void setName(String n) { name = n; } public String getName( ) { return name; } public void setIdNum(String id) { idnum = id; } public String getIdNum( ) { return idnum; } public void setTest1(int t) { test1 = t; } public int getTest1( ) { return test1; } public void setTest2(int t) { test2 = t; } public int getTest2( ) { return test2; } class StudentInfo { public static void main(String[ ] args) { Student student1 = new Student( ); student1.setName ( "Anne" ); student1.setIdNum ( "900001" ); student1.setTest1 ( 92 ); student1.setTest2 ( 85 ); System.out.println(student1.getName( )); System.out.println(student1.getIdNum( )); System.out.println(student1.getTest1( )); System.out.println(student1.getTest2( )); }

 The Student class we created is called a constructor  Constructors are used to initialize the instance variables of an object  Constructors are similar to methods, but with some important differences  Constructor name is the class name  If a constructor does not exists, a default constructor initializes the variables (zero, null, false)

 Each constructor in a class may have the same name as the class  Which constructor to use depends on the parameter data type. Class Student { private String name; private int test1; public Student( ) { name = " " ; test1 = 0; } public Student(String student) { name = student; } public Student( int test ) { test1 = test; } } No parameter uses this method String parameter uses this method Integer parameter uses this method

 Now we do not need to worry about multiple method names  setName ( )  setTest1 ( )  Unfortunately, we still have multiple variable names. We can fix this using the this command  this(...) - Calls another constructor in same class  If a constructor uses this, it must be in the constructor's first line; ignoring this rule will cause a compile error

Class Student { private String name; private int test1; public Student( ) { name = " " ; test1 = 0; } public Student(String student) { name = student; } public Student( int test ) { test1 = test; } Class Student { private String name; private int test1; public Student( ) { this ( " ", 0 ); } public Student(String name) { this ( name, 0 ); } public Student( int test1 ) { this ( " ", test1 ); } }

 this must be used if a parameter or other local variable with the same name is used in the method  Otherwise, all instances of the variable name will be interpreted as local int someVariable = this.someVariable localinstance

public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public Circle() { this(1.0); } public double getArea() { return this.radius * this.radius * ; } this must be explicitly used to reference the data field radius of the object being constructed this is used to invoke another constructor Every instance variable belongs to an instance represented by this, which is normally omitted

 Many classes commonly used by wide variety of programmers  Java contains nearly 500 classes  Package or library of classes  Folder provides convenient grouping for classes  Many contain classes available only if explicitly named within program  Some classes available automatically

 Fundamental or basic classes  Implicitly imported into every Java program  java.lang package  Only automatically imported, named package  Optional classes  Must be explicitly named

 java.lang.Math class  Contains constants and methods used to perform common mathematical functions  No need to create instance  Imported automatically  Cannot instantiate objects of type Math  Constructor for Math class private

public class MainClass { public static void main(String[ ] args) { System.out.println(Math.E); System.out.println(Math.PI); System.out.println(Math.abs(-1234)); System.out.println(Math.cos(Math.PI/4)); System.out.println(Math.sqrt(25)); System.out.println(Math.min(20, -2)); System.out.println(Math.round(1.573)); System.out.println(Math.pow(2, 4)); System.out.print(Math.random( ));} }

 Use prewritten classes  Use entire path with class name  Import class  Import package that contains class  To use import statements  Place before any executing statement in Java file  Prior to import statement  Use blank line or comment line but nothing else

 Wildcard symbol  Alternative to importing class  Import entire package of classes  Use asterisk  Can be replaced by any set of characters  Represents all classes in package  No disadvantage to importing extra classes  Importing each class by name can be form of documentation

 GregorianCalendar class  Seven constructors  Default constructor creates calendar object containing current date and time in default locale (time zone)  Can also specify date, time, and time zone  get() method  Access data fields