Set & Get Methods ICS 111: Introduction to Computer Science I

Slides:



Advertisements
Similar presentations
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 4 : Polymorphism King Fahd University of Petroleum & Minerals College of Computer.
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Programming With Java ICS201 University Of Hail1 Chapter 13 Inner Classes.
Chapter 4: Writing Classes
1 CS100J 1 February Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson,
CS100J 11 September 2003 Course Management System for CS100J is now populated with students who were pre-registered. Look at course web page to see how.
Class template Describing a generic class Instantiating classes that are type- specific version of this generic class Also are called parameterized types.
Slides prepared by Rose Williams, Binghamton University Chapter 8 Polymorphism Part I.
1 CS100J 05 February 2005 Today’s topic: Customizing a class (continued) Quiz 1 is today Quiz 2 is next Tuesday Quote for the day: There is no reason anyone.
Fall 2005CSE 115/503 Introduction to Computer Science I1 Composition details Recall, composition involves 3 things: –Declaration of instance variable of.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Programming With Java ICS201 University Of Ha’il1 Chapter 8 Polymorphism and Abstract Classes.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
Programming With Java ICS Chapter 8 Polymorphism.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
ECE122 Feb. 22, Any question on Vehicle sample code?
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
JAVA Classes Review. Definitions Class – a description of the attributes and behavior of a set of computational objects Constructor – a method that is.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
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.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
1 CS100J 08 September 2005 Today’s topic: Customizing a class (continued) Quote for the day: There is no reason anyone would want a computer in their home.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
Non-Static Classes What is the Object of this lecture?
1 Introduction to Computer Science for Majors II CPSC 233, Winter 2013 CPSC 233, winter 2013 Tutorial 7, Feb 6/7, 2013.
Java Classes Introduction. Contents Introduction Objects and Classes Using the Methods in a Java Class – References and Aliases Defining a Java Class.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
 An object is basically anything in the world that can be created and manipulated by a program.  Examples: dog, school, person, password, bank account,
1 CS Sept Customizing a class & testing Quote for the day: There is no reason anyone would want a computer in their home. - -Ken Olson, founder.
Re-Intro to Object Oriented Programming
Intro To Classes Review
FIGURE 4-10 Function Return Statements
Chapter 3: Using Methods, Classes, and Objects
Classes In C#.
Static Variables ICS 111: Introduction to Computer Science I
Java Programming with BlueJ
Arrays ICS 111: Introduction to Computer Science I
FIGURE 4-10 Function Return Statements
String Output ICS 111: Introduction to Computer Science I
Class Inheritance (Cont.)
Defining Classes and Methods
An Introduction to Java – Part II
4-2 Functions in C In C, the idea of top–down design is done using functions. A C program is made of one or more functions, one and only one of which.
CSC 113: Computer programming II
String Methods: length substring
Command Line Arguments
File I/O ICS 111: Introduction to Computer Science I
Classes, Encapsulation, Methods and Constructors (Continued)
CSCI 3328 Object Oriented Programming in C# Chapter 9: Classes and Objects: A Deeper Look – Exercises UTPA – Fall 2012 This set of slides is revised from.
Implementing Non-Static Features
More on Classes and Objects
© A+ Computer Science - OOP © A+ Computer Science -
CS150 Introduction to Computer Science 1
Defining Classes and Methods
FIGURE 4-10 Function Return Statements
© A+ Computer Science - Classes And Objects © A+ Computer Science -
© A+ Computer Science - OOP Pieces © A+ Computer Science -
Instance Method – CSC142 Computer Science II
Notes from Week 5 CSE 115 Spring 2006 February 13, 15 & 17, 2006.
Kinds of methods accessor: A method that lets clients examine object state. Examples: distance, distanceFromOrigin often has a non-void return type mutator: A.
Defining Classes and Methods
Classes CS 21a: Introduction to Computing I
Defining Classes and Methods
Building Java Programs
Building Java Programs
FIGURE 4-10 Function Return Statements
Day 11 The Last Week!.
Presentation transcript:

Set & Get Methods ICS 111: Introduction to Computer Science I William Albritton Information and Computer Sciences Department at the University of Hawai‘i at Mānoa "There is no reason anyone would want a computer in their home." Ken Olson, president, chairman and founder of Digital Equipment Corp., 1977 11/20/2018 © 2007 William Albritton

Last Assignment Go over last assignment General steps for writing a class definition Class name at top Write the data fields Write the constructor Write the methods General steps for writing main() method of client program, which uses class definition Instantiate (create) an object of the class definition Call the methods of the class definition Display the results of the methods 11/20/2018 © 2007 William Albritton

Accessor Methods Simple methods that get a data field Usually start with “get” Have return values but no parameters Used to return data to the calling method Name name = new Name("Nami","Suzuki"); String firstName = name.getFirst(); //firstName is "Nami" public String getFirst(){ return first; } 11/20/2018 © 2007 William Albritton

Mutator Methods Simple methods that set a data field Usually start with “set” Have parameters but no return value Used to change the value of a data field Name name = new Name("Nami","Suzuki"); name.setFirst("Nancy"); String herName = name.toString(); //herName is "Nancy Suzuki" public void setFirst(String firstP){ first = firstP; } 11/20/2018 © 2007 William Albritton

Class Name Class Name has several methods that are typical methods of other classes Constructor to initialize data fields of object toString() method to output the data fields Access (get) method to access each data field individually Mutator (set) method to change each data field individually See Name.java & NameClient.java 11/20/2018 © 2007 William Albritton

Class Exercise 1 Write class Point & a client program that uses objects of class Point The Point class definition should have Two integer data fields (for x & y) A constructor A toString() method A mutator (set) & accessor (get) method for both x & y (so 4 methods) The client program’s main() method should Instantiate (create) an object of class Point Call the methods of class Point Output the results of the methods 11/20/2018 © 2007 William Albritton

Class Fraction Class Fraction has several methods that are typical methods of other classes Constructor to initialize data fields of object toString() method to output the data fields Access (get) method to access each data field individually Mutator (set) method to change each data field individually See Fraction.java & FractionClient.java 11/20/2018 © 2007 William Albritton

Constructor & Parameters Your formal & actual parameters have to have the same type, and the same number of parameters For example, these statements do not compile Fraction fraction1 = new Fraction("5", "3"); Fraction fraction2 = new Fraction(12.3, 3.98); Fraction fraction3 = new Fraction(5, 6, 7); 11/20/2018 © 2007 William Albritton

Class Exercise 2 List all the variables in Exercise2.java (class Car & client program) For each variable, write the following: The kind of variable (data field, parameter, or local variable) The scope (specific line numbers) 11/20/2018 © 2007 William Albritton

Class Kurochan Class Kurochan has several methods that are typical methods of other classes Constructor to initialize data fields of object draw() method to display the drawing Access (get) method to access each data field individually Mutator (set) method to change each data field individually See Kurochan.java & KurochanClientApplet.java & kurochan.html The client program uses get() & set() to draw several Kurochans diagonally 11/20/2018 © 2007 William Albritton

Class Exercise 3 What is the output of the following program? Exercise3.java 11/20/2018 © 2007 William Albritton