CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on.

Slides:



Advertisements
Similar presentations
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
Advertisements

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.
SUMMARY: abstract classes and interfaces 1 Make a class abstract so instances of it cannot be created. Make a method abstract so it must be overridden.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Methods. int month; int year class Month Defining Classes A class contains data declarations (static and instance variables) and method declarations (behaviors)
Java Software Solutions
CS 180 Problem Solving and Object Oriented Programming Fall 2011 Notes for Week 4: September 12-16, 2011 Aditya Mathur/Tim Korb Department of Computer.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Lecture 2 Classes and objects, Constructors, Arrays and vectors.
1 Lecture 06(Abstract Classes)Lecture 9 Abstract Classes Overview  Abstract Classes: A Definition.  Declaring Abstract Classes.  Abstract Methods: A.
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.
University of British Columbia CPSC 111, Intro to Computation 2009W2: Jan-Apr 2010 Tamara Munzner 1 More Class Design III, Parameter/Scope Review Lecture.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-4: Interfaces reading: self-check: exercises: #11.
CS 121 – Intro to Programming:Java - Lecture 3 Announcements Course home page: Owl due Friday;
Multiple Choice Solutions True/False a c b e d   T F.
CS 121 – Intro to Programming:Java - Lecture 6 Announcements Fourth programming assignment now up, due in Friday. OWL assignments due as indicated MidTerm:
CS 121 – Intro to Programming:Java - Lecture 10 Announcements Two Owl assignments up, due 17th, 22nd Another up today, due 11/30 Next programming assignment.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Object Oriented Programming: Java Edition By: Samuel Robinson.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
CS125 Exam Review Winter Some Exam Info Tuesday (22nd) at 4:00-6:30pm in the PAC CHECK YOUR SEAT!!! Read Final Exam Information on website –Practice.
Inheritance. Inheritance Early programmers often wrote code very similar to existing code Example: A human resources system might handle different types.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
Static Class Methods CSIS 3701: Advanced Object Oriented Programming.
1 final (the keyword, not the exam). 2 Motivation Suppose we’ve defined an Employee class, and we don’t want someone to come along and muck it up  E.g.,
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.
Inheritance. What Is Inheritance? Familiar examples: –A family tree (individuals inherit characteristics from other individuals) –A taxonomy (classes.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CS 121 – Intro to Programming:Java - Lecture 8 Announcements Current Owl assignment is due Wednesday. Next Owl assignment - arrays I - will be up by tomorrow.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Newport Robotics Group 1 Tuesdays, 6:30 – 8:30 PM Newport High School Week 4 10/23/2014.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
Object Oriented Programming with Java 03 - Introduction to Classes and Objects.
Chapter 3 Introduction to Classes and Objects Definitions Examples.
Lecture 71 CS110 Lecture 8 February 19, 2004 Announcements –hw3 due tonight –hw4 available, due Thursday February 26 –exam Tuesday March 2 Agenda –questions.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
EE 422C Interfaces Day 5. 2 Announcements SVN has Project 2. –Partly due next week. –SVN update to get it in your repository. See Piazza for Grocery List.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
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.
Inheritance CSI 1101 Nour El Kadri. OOP  We have seen that object-oriented programming (OOP) helps organizing and maintaining large software systems.
CS 2430 Day 24. Announcements Quiz this Friday Program 5 posted on Monday Program 4 due date: Friday at 10pm Program 4 grace date: Wednesday at 10pm (don’t.
Topic 8Classes, Objects and Methods 1 Topic 8 l Class and Method Definitions l Information Hiding and Encapsulation l Objects and Reference Classes, Objects,
1 Static Variable and Method Lecture 9 by Dr. Norazah Yusof.
Java – Methods Lecture Notes 5. Methods All objects, including software objects, have state and behavior. example, a student as an object has name, address,
CS 121 – Intro to Programming:Java - Lecture 12 Announcements New Owl assignment up soon (today?) For this week: read Ch 8, sections 0 -3 Programming assignment.
1 Class and Object Lecture 7. 2 Classes Classes are constructs that define objects of the same type. A Java class uses instance variables to define data.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
Utilities (Part 2) Implementing static features 1.
Structured Programming Dr. Atif Alhejali Lecture 4 Modifiers Parameters passing 1Structured Programming.
This In Java, the keyword this allows an object to refer to itself. Or, in other words, this refers to the current object – the object whose method or.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Java Programing Basics COMP.
CS 121 – Intro to Programming:Java - Lecture 4 Announcements Course home page: Owl due soon; another.
Advanced Programming Practice Questions Advanced Programming. All slides copyright: Chetan Arora.
Lecture 9: Object and Classes Michael Hsu CSULA. 2 OO Programming Concepts Object-oriented programming (OOP) involves programming using objects. An object.
Basic Class Structure. Class vs. Object class - a template for building an object –defines the instance data that the object will hold –defines instance.
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.
 The word static is used to declare either a ________ variable or method.  Why do we use statics?  What is Polymorphism? class In general, we use a.
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CS 121 – Intro to Programming:Java - Lecture 11 Announcements Inheritance Owl assignment due 11/30 at 11 am Programming assignment six due Friday 12/3.
Chapter 8 Arrays and the ArrayList Class Arrays of Objects.
April 13, 1998CS102-02Lecture 3-1 Data Types in Java CS Lecture 3-1 Java’s Central Casting.
Lecture 10: More on Methods and Scope
Wrapper Classes ints, doubles, and chars are known as primitive types, or built-in types. There are no methods associated with these types of variables.
Introduction to Object-oriented Program Design
Introduction to Java Programming
2009 Test Key.
Presentation transcript:

CS 121 – Intro to Programming:Java - Lecture 7 Announcements A new Owl assignment is available. Programming assignment 4 is due on Thursday - hand in on paper in class. Late rules in effect - late programs are reduced in value by half. Wednesday office hours 4->6 have been cancelled for this week, have been replaced with hours on Friday, 3-5.

Objects and Methods Methods can be passed objects: L.drawOn(d); Methods can return objects: public String doubleString(String s){return (s + s); } Constructors make objects The new keyword, + a constructor call, mfgs an object that can be copied to a variable (or returned from a method) Pt p = new Pt(3,45); Constructors give the rules for manufacture; “new” activates the construction work

Static variables and static methods Static variables don’t belong to an object -- they only belong to the class. Constants are sometimes designated static, since their values are independent of the characteristics of a calling object. Static methods are methods that aren’t tied to objects. main is one! also: Math.sqrt(), etc

Wrapper classes These classes shadow the primitive types, e.g. Integer for int, Character for char, etc They represent an awkward response to Java’s “currency” issue: the main currency is objects, but objects are a pain if you’re working with - say - ints. These classes come with some very useful static methods. String s = “236”; int n = Integer.parseInt(s); // n now holds num 236

Nested classes Classes are our mechanism for organizing data -- so it makes senses for us to have subsidiary, embedded or nested classes to capture subsidiary relationships. Such relationships can actually happen in a number of ways - we’ll look at one: nonstatic inner classes. Our example: We’ll make a wheel class. The wheel will roll across the screen. We’ll endow it with spokes. The spokes don’t make any sense except in the context of the Wheel class - so we’ll make it an inner class.

Interfaces Interfaces are a very big deal. The interface is Java’s specification mechanism. They support encapsulation. Think of copy and paste in a text editor. Think of the actions you use when you drive a car. You’re only interested in effects, not in the “how” of it. Interfaces represent a contract

An interface is a “class” with a list of constants and method headers public interface Drawable{ // in elements; 11 headers public int left(); public void center(); public void drawOn(DrawingWindow d);.. public class Rect implements Drawable{ Blah blah.. }

import element.*; public class WheelTester{ public static void main(String[] args){ int width = 500; int height = 200; DrawingWindow d = new DrawingWindow(width,height); Wheel w = new Wheel(100,20,width,height); w.roll(d); } }

import element.*; public class Wheel{ final int VERTICAL = 0; final int HORIZONTAL = 1; int slant = VERTICAL; int base; // floor of roll int size; // radius of wheel int xPos,yPos; // wheel ctr Circle c; Spoke s; int width, height;

// the Wheel constructor public Wheel(int base, int size,int width, int height){ this.base = base; this.size = size; c = new Circle(0,base,size); this.width = width; this.height = height; xPos = 0; yPos = base; s = new Spoke(slant); }

public void drawOn(DrawingWindow d){ c.drawOn(d); s.drawOn(d); } public void clearOn(DrawingWindow d){ c.clearOn(d); s.clearOn(d); } public void move(int dx, int dy){ xPos += dx; yPos += dy; c.move(dx,dy); s.move(dx,dy); }

public void roll(DrawingWindow d){ Waiting w = new Waiting(); for(int j = 0; j < width; j++){ move(1,0); drawOn(d); w.snooze(40); clearOn(d); } }

private class Spoke{ public int slant; public Pt top,bot; public Line L; Spoke(int slant){ this.slant = slant; if (slant == VERTICAL){ top = new Pt(xPos,yPos - size); bot = new Pt(xPos, yPos + size); } else { top = new Pt(xPos+size,yPos); bot = new Pt(xPos-size, yPos); } }

public void move(){ if(slant ==VERTICAL) slant = HORIZONTAL; // change the slant else slant = VERTICAL; if (slant == VERTICAL){ top = new Pt(xPos,yPos - size); bot = new Pt(xPos, yPos + size); } else { top = new Pt(xPos+size,yPos); bot = new Pt(xPos-size, yPos); } L = new Line(top,bot); }

public void drawOn(DrawingWindow d){ L.drawOn(d); } public void clearOn(DrawingWindow d){ L.clearOn(d); } }