CS100A, 10 Sept. 1998 Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println,

Slides:



Advertisements
Similar presentations
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Advertisements

1 CS Sept. Customizing a class & testing Quiz 2 on Tuesday 15 Sept Purpose of a constructor (slide 5) Evaluating a new expression (slide 6) Fields;
1 CS100J Classes, stepwise refinement 25 February 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht.
Java boot camp1 Subclasses Concepts: The subclass and inheritance: subclass B of class A inherits fields and methods from A. A is a superclass of B. Keyword.
CS 1110 Prelim I: Review Session. Exam Info Prelim 1: 7:30–9:00PM, Thursday, 6 October, Statler Auditorium Look at the previous Prelims Arrive early!
Understanding class definitions Looking inside classes.
Java File Structure.  File class which is defined by java.io does not operate on streams  deals directly with files and the file system  File class.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
CS/ENGRD 2110 FALL 2013 Lecture 5: Local vars; Inside-out rule; constructors 1.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Classes CS 21a: Introduction to Computing I First Semester,
1 CS1110 Fall 2011 David Gries, Steve Marschner Reading for this lecture and previous lecture: Sections 1.1, 1.2, 1.3. Lab 1 will give you practice with.
Chapter 6—Objects and Classes The Art and Science of An Introduction to Computer Science ERIC S. ROBERTS Java Objects and Classes C H A P T E R 6 To beautify.
CS100A, Fall Lecture 4 1 CS100A, Fall 1997 Lecture, Thursday, 12 September. This lecture continues the discussion of classes. In addition, the following.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
1 CS1110 lecture 4 9 Sept. Customizing a class & testing Classes: fields; getter & setter methods. Secs (p. 45) & 3.1 (pp. 105–110 only) Constructors.
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Java - Classes JPatterson. What is a class? public class _Alpha { public static void main(String [] args) { } You have been using classes all year – you.
1 CS100J Classes, stepwise refinement 14 Feb 2007 Rsrecah on spleilng Aoccdrnig to a rscheearch at Cmabirgde Uinervtisy, it deosn't mttaer in waht oredr.
Fall 2002CS 150: Intro. to Computing1 Streams and File I/O (That is, Input/Output) OR How you read data from files and write data to files.
Chapter 4&5 Defining Classes Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CS100A, Fall 1998 Key Concepts 1 These notes contain short definitions of the basic entities that make up a Java program, along with a description of the.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
CS100A, Fall Lec. 1, Tue. 1 Sept. 1 CS100A, Fall 1998 After discussing the notion of an algorithm and (computer) program briefly, the first lecture.
1 CS1110 Classes, stepwise refinement 17 Feb 2009 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of.
CS100A, 15 Sept Lecture 5 1 CS100A, 5 Sept This lecture continues the discussion of classes. The important new concept of this lecture is.
CS100A, Fall Lecture 5 1 CS100A, Fall 1997 Lecture, Tuesday, 16 September. This lecture continues the discussion of classes. The important new concept.
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
CSE 501N Fall ’09 07: Iteration 17 September 2009 Nick Leidenfrost.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
1 CS Sep 2011 Miscellaneous points about classes. More on stepwise refinement. Next: wrapper classes. Section 5.1 of class text Need Help? Make.
CS100A, Lect. 7, 22 Sept. 98. Static Fields and Methods 1 CS100A, Lecture 7, 22 Sept Static fields and methods. Suppose that we would like to main-
CS100Lecture 61 Announcements Homework P1 due on Thursday Homework P2 handed out.
1. Understanding the execution of local variable declaration (in a method body) new expression ( 3 steps ) method call (method frames, call stack) examples.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
Yanal Alahmad Java Workshop Yanal Alahmad
CS100A, Lecture 7, 22 Sept Static fields and methods
CS1110 Classes, stepwise refinement 23 Sep 2009
Chapter 6 More Conditionals and Loops
CS100J Final Class on Classes 22 September 2005
Java Programming with BlueJ
CS/ENGRD 2110 Spring 2017 Lecture 5: Local vars; Inside-out rule; constructors
OPERATORS (2) CSC 111.
Inheritance I Class Reuse with Inheritance
Creating Objects in a Few Simple Steps
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
CMSC 202 Inheritance.
CS/ENGRD 2110 Fall 2017 Lecture 5: Local vars; Inside-out rule; constructors
Class Reuse with Inheritance
More on Classes and Objects
CS/ENGRD 2110 Fall 2018 Lecture 5: Local vars; Inside-out rule; constructors
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)
Recap Week 2 and 3.
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
Concepts for this lecture: Class Instance of a class, (an object)
Suggested self-checks: Section 7.11 #1-11
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Classes CS 21a: Introduction to Computing I
Introduction to Object-Oriented Programming
CS 1110 Prelim I: Review Session Spring 2011
CS/ENGRD 2110 Spring 2019 Lecture 2: Objects and classes in Java
‘do’ and ‘for’ loops October 1, 2007 ComS 207: Programming I (in Java)
(We’ll spend a few lectures on iteration)
CS100J Classes, stepwise refinement 21 September 2005
Review for Midterm 3.
‘do’ and ‘for’ loops October 2, 2006 ComS 207: Programming I (in Java)
CS100J Final Class on Classes 10 February 2005
CS100A, Fall 1998 This lecture covers the declaration of variables, assignment, and the if statement. While discussing the declaration of variables,
CS100A Lect. 10, 1 Oct Input/Output & Program Schema
Presentation transcript:

CS100A, 10 Sept. 1998 Lecture 4: Continue discussion of classes and introduces Class String, Printing output using System.out.print, System,out.println, and System.out.flush(). Accessing modifiers public and private Functions versus procedures, and the modifier void. Constructors Concepts demonstrated using class Employee, an instance of which records information about an employee. This class appears later in these slides. With each concept, CodeWarrior Java on the Macintosh is used to demonstrate the concept and its application. This is important. For example, after making a field private, we syntax-check the program and see that references to it (outside the class) are now illegal. CS100A, 10 Sept. 1998. Lecture 4

Concepts for this lecture: CS100A, Fall 1998, 10 September Concepts for this lecture: Sequences of characters:class String. Holmes 1.8 (p. 30-34). Output, using System.out Method of a class. Holmes, Sec. 6.2. Functions versus procedures. Some security using access modifiers public and private. Holmes p 229. Constructors: Holmes, Sec. 205 (not worth much) CS100A, 10 Sept. 1998. Lecture 4

// An instance of Employee contains a person's name, // salary, and year hired. It has a constructor and // methods for raising the salary, printing the data, and // retrieving the person's name and the year hired. public class Employee {public String name; // The person's name public double pay; // The person's yearly salary public int hireDate; // The year hired // Set the name to n public void setName(String n) {name= n;} // Set the salary to s public void setSalary(double s) {pay= s;} // Set the year to y public void setHireDate(int y) {hireDate= y;} CS100A, 10 Sept. 1998. Lecture 4

// Yield the year the person was hired public int hireYear() {return hireDate;} // Raise the pay by p percent public void raiseSalary(double p) {pay= pay * (1 + p/100.0);} // Yield the person's name public String getName() {return name;} // Yield a String containing person’s data public String toString() { String s= name; s= s + " " + pay + " " + hireDate; return s; } CS100A, 10 Sept. 1998. Lecture 4

Class String. An instance is a “string” of characters. Declaration String d; Assignment d= new String(“David Gries”); or d= “David Gries”; Catenation of strings: infix operator + d + “ ” + “xyz” evaluates to “David Gries xyz” d= “David” + “ ” + “Gries” Holmes: Sect. 1.8, p 30 CS100A, 10 Sept. 1998. Lecture 4

Checking string equality s1 s2 if (s1==s2) --condition if false! Use instead if (s1.equals(s2)) equals:a method of class String. gries gries CS100A, 10 Sept. 1998. Lecture 4

// “Flush” the output buffer, making To print string s in the output window, use System.out.print(s); or System.out.println(s); The latter one prints an “end of line” after printing s. Thus, the following are equivalent: (1) System.out.print(s1); System.out.println(s2); (2) System.out.println(s1 + s2); // “Flush” the output buffer, making // sure that everything is written out. System.out.flush(); CS100A, 10 Sept. 1998. Lecture 4

Changing a field like pay should be limited to methods of the class. Accessing a field of an Employee object. Employee c; c= new Employee(); c.name= “G”; c.pay= 1000000; c.hireDate= 1998; c.pay= 220000; Allowing access to field pay allows any program that can reference c to change pay. This may not be desired! Changing a field like pay should be limited to methods of the class. CS100A, 10 Sept. 1998. Lecture 4

// Anyone can reference field salary! public int salary; Alternative: // Anyone can reference field salary! public int salary; // Only methods within Employee can // reference pay! private int pay; So, the following is illegal: Employee c; c= new Employee( ); c.pay= 1000000; Now, to change c1.pay, you have to call method setSalary or raiseSalary. CS100A, 10 Sept. 1998. Lecture 4

Procedures versus functions Method raiseSalary has prefix void: public void raiseSalary(double p); void indicates that the method performs a task but does not “return” a result. Method getName has prefix String: public String getName ( ); The term String indicates that the method “returns” a result. CS100A, 10 Sept. 1998. Lecture 4

// Yield the person’s name public String getName (); {return name;} Execution of statement return <expression> terminates execution of the method (function) in which it appears and “returns” the value of <expression> to the place of call. Two examples of calls: String s= e1.getName(); System.out.println(e1.toString()); CS100A, 10 Sept. 1998. Lecture 4

A constructor: a normal method except Constructors A constructor: a normal method except (0) Its name is the same as the name of the class (1) It has no type-class prefix or void //Constructor: name n, year d hired, //salary s public Employee(String n, double s, int d) { name= n; pay= s; hireDate= d; } When the following is executed, after the instance of Employee is created, the constructor is executed, with “Gries” for n, 50000 for s, and 1969 for d: c= new Employee(“Gries”, 50000, 1969); Constructor is used to provide initialization of fields of the instance. CS100A, 10 Sept. 1998. Lecture 4